-
Notifications
You must be signed in to change notification settings - Fork 2
/
tracker.go
61 lines (50 loc) · 1.29 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
package cache_torrent
import (
. "github.com/danalex97/Speer/interfaces"
"github.com/danalex97/nfsTorrent/torrent"
)
// The CacheTorrent Tracker is identical to the BitTorrent tracker, with the
// addition that it also handles Candidate messages by sending messages towards
// the Election.
type Tracker struct {
election *Election
*torrent.Tracker
}
func (t *Tracker) New(util TorrentNodeUtil) TorrentNode {
tracker := new(Tracker)
tracker.Tracker = (tracker.Tracker.New(util)).(*torrent.Tracker)
tracker.election = NewElection(tracker.Limit, tracker.Transport)
return tracker
}
func (t *Tracker) OnJoin() {
go t.election.Run()
go t.CheckMessages(t.Recv)
}
func (t *Tracker) Recv(m interface {}) {
switch msg := m.(type) {
// New Protocol.
case torrent.Join:
t.Join(msg, t.Neighbours)
default:
// Backward compatibility.
t.Tracker.Recv(m)
}
// New Protocol.
t.election.Recv(m)
}
func (t *Tracker) Neighbours(id string) interface {} {
ids := (t.Tracker.Neighbours(id)).(torrent.Neighbours).Ids
t.election.NewJoin(id)
return Neighbours{
Ids : ids,
}
}
func (t *Tracker) Local(id string) []string {
local := []string{}
for _, nid := range t.Ids {
if getAS(id) == getAS(nid) && id != nid {
local = append(local, nid)
}
}
return local
}