-
Notifications
You must be signed in to change notification settings - Fork 0
/
galaxy.go
86 lines (73 loc) · 1.73 KB
/
galaxy.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
package main
import (
"io/ioutil"
"github.com/bkolad/gTorrent/db"
i "github.com/bkolad/gTorrent/init"
"github.com/bkolad/gTorrent/peer"
"github.com/bkolad/gTorrent/piece"
"github.com/bkolad/gTorrent/torrent"
"github.com/bkolad/gTorrent/tracker"
"github.com/gchaincl/dotsql"
)
type galaxy struct {
db *db.DB
tracker tracker.Tracker
peerManager peer.Manager
info *torrent.Info
peerInfos chan torrent.PeerInfo
}
func newGalaxy(
db *db.DB,
initState i.State,
conf i.Configuration) (*galaxy, error) {
data, err := ioutil.ReadFile(conf.TorrentPath)
if err != nil {
return nil, err
}
dec := torrent.NewTorrentDecoder(string(data))
info, err := dec.Decode()
if err != nil {
return nil, err
}
handshake := peer.NewHandshake(conf, info)
tracker, _ := tracker.NewTracker(info, initState, conf)
pieceRepo, err := piece.NewRepoDb(db, info)
if err != nil {
return nil, err
}
pieceManager := piece.NewManager(*info, pieceRepo)
peerInfos := make(chan torrent.PeerInfo, 100)
peerManager := peer.NewManager(peerInfos, handshake, pieceManager)
return &galaxy{
db: db,
tracker: tracker,
peerManager: peerManager,
info: info,
peerInfos: peerInfos,
}, nil
}
func (g *galaxy) saveTorrent() error {
dot, err := dotsql.LoadFromFile("queries.sql")
if err != nil {
return err
}
_, err = dot.Exec(g.db, "create-torrent", g.info.Name, "pending", g.info.InfoHash)
if err != nil {
return err
}
return nil
}
func (g *galaxy) retrievePeersFromTracker() error {
peers, err := g.tracker.Peers()
if err != nil {
return err
}
for _, peer := range peers {
g.peerInfos <- peer
}
close(g.peerInfos)
return nil
}
func (g *galaxy) connectToPeers() {
g.peerManager.ConnectToPeers()
}