-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsearch.go
155 lines (134 loc) · 2.26 KB
/
search.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
package dht
import (
"math"
"net"
"time"
)
// CallBack function
type CallBack func(tor *ID, peer []byte)
type node struct {
id *ID
addr *net.UDPAddr
time time.Time
acked bool
}
type search struct {
tor *ID
cb CallBack
nodes map[ID]*node
}
func newSearch(tor *ID, cb CallBack) *search {
return &search{
tor: tor,
cb: cb,
nodes: make(map[ID]*node),
}
}
func (s *search) Count() int {
return len(s.nodes)
}
func (s *search) Get(id *ID) *node {
if n, ok := s.nodes[*id]; ok {
return n
}
return nil
}
func (s *search) Insert(id *ID, addr *net.UDPAddr) (n *node) {
n, ok := s.nodes[*id]
if !ok {
n = &node{
id: id,
addr: addr,
time: time.Now(),
}
s.nodes[*id] = n
}
return
}
func (s *search) Remove(id *ID) {
delete(s.nodes, *id)
}
func (s *search) Notify(tor *ID, peer []byte) {
if s.cb != nil {
s.cb(tor, peer)
}
}
func (s *search) Done(d time.Duration) (done bool) {
s.Map(func(n *node) bool {
if n.acked || (d != 0 && time.Since(n.time) > d) {
done = true
}
return done
})
return
}
func (s *search) Map(f func(*node) bool) {
for _, n := range s.nodes {
if f(n) == false {
return
}
}
}
type searches struct {
tid int16
ss map[int16]*search
}
func newSearches() *searches {
return &searches{
ss: make(map[int16]*search),
}
}
func (s *searches) Count() int {
return len(s.ss)
}
func (s *searches) Get(tid int16) *search {
if sr, ok := s.ss[tid]; ok {
return sr
}
return nil
}
func (s *searches) Find(tor *ID) (tid int16, sr *search) {
tid = -1
s.Map(func(t int16, s *search) bool {
if s.tor.Compare(tor) == 0 {
tid = t
sr = s
return false
}
return false
})
return
}
func (s *searches) nextTID() int16 {
if n := s.Count(); n < math.MaxInt16+1 {
for i := 0; i <= n; i++ {
tid := s.tid
if s.tid == math.MaxInt16 {
s.tid = 0
} else {
s.tid++
}
if s.Get(tid) == nil {
return tid
}
}
}
return -1
}
func (s *searches) Insert(tor *ID, cb CallBack) (tid int16, sr *search) {
if tid = s.nextTID(); tid != -1 {
sr = newSearch(tor, cb)
s.ss[tid] = sr
}
return
}
func (s *searches) Remove(tid int16) {
delete(s.ss, tid)
}
func (s *searches) Map(f func(int16, *search) bool) {
for tid, sr := range s.ss {
if f(tid, sr) == false {
return
}
}
}