-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
91 lines (71 loc) · 1.87 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package main
import (
"fmt"
"os"
"sync"
log "github.com/sirupsen/logrus"
"byo_bittorrent/cli"
"byo_bittorrent/torrent/metadata/file"
"byo_bittorrent/torrent/p2p"
"byo_bittorrent/torrent/storage"
)
func generateDownloadChannel(torrent *file.TorrentFile) chan p2p.Block {
blocks := make(chan p2p.Block, len(torrent.PieceHashes))
for hashIndex, hash := range torrent.PieceHashes {
blocks <- p2p.Block{
Index: hashIndex,
Length: torrent.CalculatePieceSize(hashIndex),
Hash: hash,
}
}
return blocks
}
func generateSaveChannel(torrent *file.TorrentFile) chan p2p.Block {
return make(chan p2p.Block, len(torrent.PieceHashes))
}
func main() {
logFile, err := os.OpenFile("logs/main.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
fmt.Println(err)
}
defer logFile.Close()
log.SetOutput(logFile)
log.SetFormatter(&log.TextFormatter{})
log.SetLevel(log.InfoLevel)
args, err := cli.ParseArgs()
if err != nil {
log.Error(err)
return
}
content := &file.TorrentFile{}
if err := content.ReadFile(args.TorrentFile); err != nil {
log.Error("Failed to parse file:", err)
return
}
url, err := content.BuildTrackerUrl()
if err != nil {
log.Error("Failed to build tracker ulr:", err)
return
}
tracker := &p2p.Tracker{Url: url}
peers, err := tracker.RequestPeers()
if err != nil {
log.Error("Failed to request peers list:", err)
return
}
toDownload := generateDownloadChannel(content)
toSave := generateSaveChannel(content)
var wg sync.WaitGroup
for _, peer := range peers {
wg.Add(1)
client := &p2p.Client{Torrent: content}
go client.Start(&peer, toDownload, toSave, &wg)
}
writer := &storage.Writer{Torrent: content, SaveDir: args.SaveDir}
writer.CreateBitfield()
progressBar := &cli.ProgressBar{Bitfield: &writer.Bitfield}
go progressBar.Start()
writer.Write(toSave)
wg.Wait()
fmt.Println("All workers done")
}