-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtable.go
221 lines (195 loc) · 3.97 KB
/
table.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package dht
import (
"container/list"
"errors"
"fmt"
"net"
"sort"
)
// Table store all nodes
type Table struct {
id *ID
ksize int
buckets *list.List
}
// NewTable returns a table
func NewTable(id *ID, ksize int) *Table {
t := &Table{
id: id,
ksize: ksize,
buckets: list.New(),
}
b := NewBucket(ZeroID, ksize)
t.buckets.PushBack(b)
return t
}
// KSize returns bucket capaticy
func (t *Table) KSize() int {
return t.ksize
}
// NumNodes returns all node count
func (t *Table) NumNodes() (n int) {
t.Map(func(b *Bucket) bool {
n += b.Count()
return true
})
return
}
// Insert a node
func (t *Table) Insert(id *ID, addr *net.UDPAddr) (*Node, error) {
if id.Compare(t.id) == 0 {
return nil, errors.New("id equal to table's id")
}
return t.insert(id, addr)
}
func (t *Table) insert(id *ID, addr *net.UDPAddr) (n *Node, err error) {
if e := t.find(id); e != nil {
if n = e.Value.(*Bucket).Insert(id, addr); n != nil {
return
}
if inBucket(t.id, e) && t.split(e) {
return t.insert(id, addr)
}
}
err = errors.New("drop this node")
return
}
func (t *Table) split(e *list.Element) bool {
bit := e.Value.(*Bucket).first.LowBit()
if next := e.Next(); next != nil {
bit2 := next.Value.(*Bucket).first.LowBit()
if bit < bit2 {
bit = bit2
}
}
if bit++; bit >= 160 {
return false
}
b := e.Value.(*Bucket)
first, _ := NewID(b.first.Bytes())
first.SetBit(bit, true)
b2 := NewBucket(first, b.cap)
t.buckets.InsertAfter(b2, e)
var eles []*list.Element
b.handle(func(be *list.Element) bool {
if inBucket(be.Value.(*Node).id, e) == false {
eles = append(eles, be)
}
return true
})
for _, ele := range eles {
b2.nodes.PushBack(b.nodes.Remove(ele))
}
return true
}
// Find returns bucket
func (t *Table) Find(id *ID) *Bucket {
if e := t.find(id); e != nil {
return e.Value.(*Bucket)
}
return nil
}
func (t *Table) find(id *ID) (ele *list.Element) {
t.handle(func(e *list.Element) bool {
if inBucket(id, e) {
ele = e
return false
}
return true
})
return
}
// Lookup returns the K(8) closest good nodes
func (t *Table) Lookup(id *ID) []*Node {
e := t.find(id)
if e == nil {
return nil
}
ln := newLookupNodes(id, t.ksize)
if ln.CopyFrom(e); ln.Len() < t.ksize {
prev, next := e.Prev(), e.Next()
for ln.Len() < t.ksize && (prev != nil || next != nil) {
if prev != nil {
ln.CopyFrom(prev)
prev = prev.Prev()
}
if next != nil {
ln.CopyFrom(next)
next = next.Next()
}
}
}
sort.Sort(ln)
if ln.Len() > t.ksize {
return ln.nodes[:t.ksize]
}
return ln.nodes
}
func inBucket(id *ID, e *list.Element) bool {
if b := e.Value.(*Bucket); b.first.Compare(id) > 0 {
return false
}
if n := e.Next(); n != nil {
if b := n.Value.(*Bucket); b.first.Compare(id) <= 0 {
return false
}
}
return true
}
type lookupNodes struct {
id *ID
nodes []*Node
}
func newLookupNodes(id *ID, cap int) *lookupNodes {
return &lookupNodes{
id: id,
nodes: make([]*Node, 0, cap),
}
}
func (ln *lookupNodes) CopyFrom(e *list.Element) {
e.Value.(*Bucket).Map(func(n *Node) bool {
ln.nodes = append(ln.nodes, n)
return true
})
}
func (ln *lookupNodes) Len() int {
return len(ln.nodes)
}
func (ln *lookupNodes) Less(i, j int) bool {
for k := 0; k < IDLen; k++ {
n1 := ln.nodes[i].id[k] ^ ln.id[k]
n2 := ln.nodes[j].id[k] ^ ln.id[k]
if n1 < n2 {
return true
} else if n1 > n2 {
return false
}
}
return true
}
func (ln *lookupNodes) Swap(i, j int) {
ln.nodes[i], ln.nodes[j] = ln.nodes[j], ln.nodes[i]
}
// Map all buckets
func (t *Table) Map(f func(b *Bucket) bool) {
t.handle(func(e *list.Element) bool {
return f(e.Value.(*Bucket))
})
}
func (t *Table) handle(f func(e *list.Element) bool) {
for e := t.buckets.Front(); e != nil; e = e.Next() {
if f(e) == false {
return
}
}
}
func (t *Table) String() (s string) {
var n int
t.Map(func(b *Bucket) bool {
n += b.Count()
s += fmt.Sprintf("%v\n", b)
return true
})
s = fmt.Sprintf("%v %d\n\n", t.id, n) + s
return
}