-
-
Notifications
You must be signed in to change notification settings - Fork 66
/
misc.go
63 lines (55 loc) · 1.25 KB
/
misc.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
package dht
import (
"fmt"
"net"
"github.com/anacrolix/dht/v2/krpc"
)
func mustListen(addr string) net.PacketConn {
ret, err := net.ListenPacket("udp", addr)
if err != nil {
panic(err)
}
return ret
}
func addrResolver(addr string) func() ([]Addr, error) {
return func() ([]Addr, error) {
ua, err := net.ResolveUDPAddr("udp", addr)
return []Addr{NewAddr(ua)}, err
}
}
type addrMaybeId struct {
Addr krpc.NodeAddr
Id *int160
}
func (me addrMaybeId) String() string {
if me.Id == nil {
return fmt.Sprintf("unknown id at %s", me.Addr)
} else {
return fmt.Sprintf("%x at %v", *me.Id, me.Addr)
}
}
type nodesByDistance struct {
nis []addrMaybeId
target int160
}
func (me nodesByDistance) Len() int { return len(me.nis) }
func (me nodesByDistance) Less(i, j int) bool {
if me.nis[i].Id == nil {
return false
}
if me.nis[j].Id == nil {
return true
}
return distance(*me.nis[i].Id, me.target).Cmp(distance(*me.nis[j].Id, me.target)) < 0
}
func (me *nodesByDistance) Pop() interface{} {
ret := me.nis[len(me.nis)-1]
me.nis = me.nis[:len(me.nis)-1]
return ret
}
func (me *nodesByDistance) Push(x interface{}) {
me.nis = append(me.nis, x.(addrMaybeId))
}
func (me nodesByDistance) Swap(i, j int) {
me.nis[i], me.nis[j] = me.nis[j], me.nis[i]
}