-
-
Notifications
You must be signed in to change notification settings - Fork 66
/
bucket.go
50 lines (42 loc) · 827 Bytes
/
bucket.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
package dht
import (
"time"
"github.com/anacrolix/chansync"
"github.com/anacrolix/dht/v2/int160"
)
type bucket struct {
// Per the "Routing Table" section of BEP 5.
changed chansync.BroadcastCond
lastChanged time.Time
nodes map[*node]struct{}
}
func (b *bucket) Len() int {
return len(b.nodes)
}
func (b *bucket) EachNode(f func(*node) bool) bool {
for n := range b.nodes {
if !f(n) {
return false
}
}
return true
}
func (b *bucket) AddNode(n *node, k int) {
if _, ok := b.nodes[n]; ok {
return
}
if b.nodes == nil {
b.nodes = make(map[*node]struct{}, k)
}
b.nodes[n] = struct{}{}
b.lastChanged = time.Now()
b.changed.Broadcast()
}
func (b *bucket) GetNode(addr Addr, id int160.T) *node {
for n := range b.nodes {
if n.hasAddrAndID(addr, id) {
return n
}
}
return nil
}