Skip to content

Commit

Permalink
reorganize packages
Browse files Browse the repository at this point in the history
  • Loading branch information
burmudar committed Oct 28, 2023
1 parent a10d127 commit 48ff319
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 64 deletions.
Original file line number Diff line number Diff line change
@@ -1,35 +1,19 @@
package btencoding
package encoding

import (
"bytes"
"crypto/sha1"
"errors"
"fmt"
"io"
"os"
"strings"
"unicode"

bt "github.com/codecrafters-io/bittorrent-starter-go/cmd/mybittorrent/bt/types"
//"github.com/jackpal/bencode-go"
)

type FileInfo struct {
Length int
Paths []string
}

type FileMeta struct {
Announce string
Name string
PieceLength int
Pieces []string
Length int
Files []*FileInfo
Hash []byte
RawInfo map[string]interface{}
}

func newFileInfo(value map[string]interface{}) *FileInfo {
var f FileInfo
func newFileInfo(value map[string]interface{}) *bt.FileInfo {
var f bt.FileInfo

f.Length = value["length"].(int)
paths := []string{}
Expand All @@ -41,7 +25,7 @@ func newFileInfo(value map[string]interface{}) *FileInfo {
return &f
}

func DecodeTorrent(filename string) (*FileMeta, error) {
func DecodeTorrent(filename string) (*bt.FileMeta, error) {
raw, err := os.ReadFile(filename)
if err != nil {
return nil, err
Expand All @@ -62,7 +46,7 @@ func DecodeTorrent(filename string) (*FileMeta, error) {
return nil, fmt.Errorf("info dict not found")
}

var m FileMeta
var m bt.FileMeta
m.RawInfo = info
m.Announce = dict["announce"].(string)
if v, ok := info["name"]; ok {
Expand Down Expand Up @@ -92,7 +76,7 @@ func DecodeTorrent(filename string) (*FileMeta, error) {
if v, ok := info["length"]; ok {
m.Length = v.(int)
} else {
m.Files = make([]*FileInfo, 0)
m.Files = make([]*bt.FileInfo, 0)
fileList := info["files"].([]interface{})

for _, item := range fileList {
Expand All @@ -104,32 +88,6 @@ func DecodeTorrent(filename string) (*FileMeta, error) {
return &m, nil
}

func (m *FileMeta) InfoHash() ([20]byte, error) {
var info map[string]interface{}
if len(m.Files) == 0 {
info = map[string]interface{}{
"name": m.Name,
"length": m.Length,
"piece length": m.PieceLength,
"pieces": strings.Join(m.Pieces, ""),
}
} else {
info = map[string]interface{}{
"name": m.Name,
"piece length": m.PieceLength,
"pieces": strings.Join(m.Pieces, ""),
"files": m.Files,
}
}

w := NewBenEncoder()
data, err := w.encode(info)
if err != nil {
return [20]byte{}, err
}
return sha1.Sum(data), nil
}

func DecodeDict(r *BencodeReader) (interface{}, error) {
dict := make(map[string]interface{}, 0)
r.ReadChar() // move past 'd'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package btencoding
package encoding

import (
"reflect"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package btencoding
package encoding

import (
"bytes"
Expand All @@ -16,7 +16,7 @@ func NewBenEncoder() *BenEncoder {
}
}

func (b *BenEncoder) encode(value interface{}) ([]byte, error) {
func (b *BenEncoder) Encode(value interface{}) ([]byte, error) {

switch v := value.(type) {
case string:
Expand All @@ -37,7 +37,7 @@ func (b *BenEncoder) encode(value interface{}) ([]byte, error) {
func (b *BenEncoder) encodeList(list []interface{}) {
fmt.Fprintf(b.buf, "l")
for _, i := range list {
b.encode(i)
b.Encode(i)
}
fmt.Fprintf(b.buf, "e")
}
Expand All @@ -54,12 +54,12 @@ func (b *BenEncoder) encodeDict(dict map[string]interface{}) {

for _, k := range keys {
if k != "" {
b.encode(k)
b.Encode(k)
} else {
continue
}
if v, ok := dict[k]; ok {
b.encode(v)
b.Encode(v)
}
}
fmt.Fprintf(b.buf, "e")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package btencoding
package encoding

import (
"bytes"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package btencoding
package encoding

import (
"fmt"
Expand Down
17 changes: 17 additions & 0 deletions cmd/mybittorrent/bt/hash.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package bt

import (
"crypto/sha1"

"github.com/codecrafters-io/bittorrent-starter-go/cmd/mybittorrent/bt/encoding"
"github.com/codecrafters-io/bittorrent-starter-go/cmd/mybittorrent/bt/types"
)

func InfoHash(m *types.FileMeta) ([20]byte, error) {
w := encoding.NewBenEncoder()
data, err := w.Encode(m.InfoDict())
if err != nil {
return [20]byte{}, err
}
return sha1.Sum(data), nil
}
12 changes: 12 additions & 0 deletions cmd/mybittorrent/bt/tracker/tracker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package tracker

import "github.com/codecrafters-io/bittorrent-starter-go/cmd/mybittorrent/bt/types"

type TrackerClient struct{}

type TrackerRequest struct{}
type TrackerResponse struct{}

func (t *TrackerClient) PeerRequest(m *types.FileInfo) (*TrackerResponse, error) {
return nil, nil
}
40 changes: 40 additions & 0 deletions cmd/mybittorrent/bt/types/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package types

import "strings"

type FileInfo struct {
Length int
Paths []string
}

type FileMeta struct {
Announce string
Name string
PieceLength int
Pieces []string
Length int
Files []*FileInfo
Hash []byte
RawInfo map[string]interface{}
}

func (m *FileMeta) InfoDict() map[string]interface{} {
var info map[string]interface{}
if len(m.Files) == 0 {
info = map[string]interface{}{
"name": m.Name,
"length": m.Length,
"piece length": m.PieceLength,
"pieces": strings.Join(m.Pieces, ""),
}
} else {
info = map[string]interface{}{
"name": m.Name,
"piece length": m.PieceLength,
"pieces": strings.Join(m.Pieces, ""),
"files": m.Files,
}
}

return info
}
14 changes: 8 additions & 6 deletions cmd/mybittorrent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ import (
//bencode "github.com/jackpal/bencode-go" // Available if you need it!
"os"

"github.com/codecrafters-io/bittorrent-starter-go/cmd/mybittorrent/btencoding"
"github.com/codecrafters-io/bittorrent-starter-go/cmd/mybittorrent/bt"
"github.com/codecrafters-io/bittorrent-starter-go/cmd/mybittorrent/bt/encoding"
"github.com/codecrafters-io/bittorrent-starter-go/cmd/mybittorrent/bt/types"
)

func printMetaInfo(m *btencoding.FileMeta) {
func printMetaInfo(m *types.FileMeta) {
fmt.Printf("Tracker URL: %s\n", m.Announce)
if len(m.Files) == 0 {
fmt.Printf("Length: %d\n", m.Length)
Expand All @@ -22,7 +24,7 @@ func printMetaInfo(m *btencoding.FileMeta) {
}
}

hash, err := m.InfoHash()
hash, err := bt.InfoHash(m)
if err != nil {
fmt.Fprintf(os.Stderr, "calculating info hash error: %v", err)
}
Expand All @@ -42,8 +44,8 @@ func main() {
{
value := os.Args[2]

r := btencoding.NewBencodeReader(value)
if result, err := btencoding.DecodeBencode(r); err == nil {
r := encoding.NewBencodeReader(value)
if result, err := encoding.DecodeBencode(r); err == nil {
r, err := json.Marshal(result)
if err != nil {
fmt.Printf("marshalling faliure: %v\n", err)
Expand All @@ -58,7 +60,7 @@ func main() {
case "info":
{
filename := os.Args[2]
t, err := btencoding.DecodeTorrent(filename)
t, err := encoding.DecodeTorrent(filename)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to read torrent %q: %v", os.Args[2], err)
}
Expand Down

0 comments on commit 48ff319

Please sign in to comment.