-
Notifications
You must be signed in to change notification settings - Fork 2
/
tracker.go
65 lines (51 loc) · 1.46 KB
/
tracker.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
package multi_torrent
import (
. "github.com/danalex97/Speer/interfaces"
"github.com/danalex97/nfsTorrent/cache_torrent"
"github.com/danalex97/nfsTorrent/torrent"
)
// The MultiTracker is a usual CacheTorrent Tracker which uses a StripProxy
// for sending messages towards the correct Peer and runs a MultiElection
// instead of a usual Election.
type MultiTracker struct {
election *MultiElection
*torrent.Tracker
}
func (t *MultiTracker) New(util TorrentNodeUtil) TorrentNode {
tracker := new(MultiTracker)
tracker.Tracker = (tracker.Tracker.New(util)).(*torrent.Tracker)
tracker.Transport = NewStripProxy(tracker.Transport)
tracker.election = NewMultiElection(
multi.Int(),
tracker.Limit,
tracker.Transport)
return tracker
}
func (t *MultiTracker) OnJoin() {
go t.election.Run()
go t.CheckMessages(t.Recv)
}
func (t *MultiTracker) Recv(m interface {}) {
switch msg := m.(type) {
case torrent.Join:
// We ignore torrent.Join messages
return
case Join:
t.Join(torrent.Join{msg.Id}, t.Neighbours)
case torrent.SeedReq:
t.Tracker.Recv(torrent.SeedReq{
From : ExternId(msg.From),
})
default:
t.Tracker.Recv(m)
}
t.election.Recv(m)
}
func (t *MultiTracker) Neighbours(id string) interface {} {
ids := (t.Tracker.Neighbours(id)).(torrent.Neighbours).Ids
t.election.NewJoin(id)
// Keep message type compatible with cache_torrent protocol
return cache_torrent.Neighbours{
Ids : ids,
}
}