-
Notifications
You must be signed in to change notification settings - Fork 2
/
cacheTracker.go
52 lines (41 loc) · 1.24 KB
/
cacheTracker.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
package bias_torrent
import (
. "github.com/danalex97/Speer/interfaces"
"github.com/danalex97/nfsTorrent/cache_torrent"
"github.com/danalex97/nfsTorrent/torrent"
)
// The CacheTracker is component which allows usage of bias neighbour election
// together with the CacheTorrent protocol.
type CacheTracker struct {
election *cache_torrent.Election
*Tracker
}
func (t *CacheTracker) New(util TorrentNodeUtil) TorrentNode {
tracker := new(CacheTracker)
tracker.Tracker = (tracker.Tracker.New(util)).(*Tracker)
tracker.election = cache_torrent.NewElection(tracker.Limit, tracker.Transport)
return tracker
}
func (t *CacheTracker) OnJoin() {
go t.election.Run()
go t.CheckMessages(t.Recv)
}
func (t *CacheTracker) Recv(m interface {}) {
switch msg := m.(type) {
case torrent.Join:
// Override the Neighbours method so we can send the notify the election.
t.Join(msg, t.Neighbours)
default:
t.Tracker.Recv(m)
}
t.election.Recv(m)
}
func (t *CacheTracker) Neighbours(id string) interface {} {
ids := (t.Tracker.Neighbours(id)).(torrent.Neighbours).Ids
// Notify the election.
t.election.NewJoin(id)
// Keep message type compatible with cache_torrent protocol
return cache_torrent.Neighbours{
Ids : ids,
}
}