/
contacttimelist.go
69 lines (56 loc) · 1.51 KB
/
contacttimelist.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
package kad
import (
"container/list"
)
// ContactListNode is list node of contact.
type ContactListNode struct {
e *list.Element
pContact *Contact
}
// ContactTimeListElementValue is element value of list.
type ContactTimeListElementValue struct {
t int64
nodeMap map[uint32]*ContactListNode // key is IP, node with same time will be in this map.
}
// ContactTimeList is a list with time. Note that time is always growing.
type ContactTimeList struct {
pList *list.List
nodeMap map[uint32]*ContactListNode // key is IP
}
func (ctl *ContactTimeList) start() {
ctl.pList = list.New()
ctl.nodeMap = make(map[uint32]*ContactListNode)
}
func (ctl *ContactTimeList) remove(pContact *Contact) {
node := ctl.nodeMap[pContact.ip]
if node == nil {
return
}
// remove from list
pv := node.e.Value.(*ContactTimeListElementValue)
delete(pv.nodeMap, pContact.ip)
if len(pv.nodeMap) == 0 {
ctl.pList.Remove(node.e)
}
}
func (ctl *ContactTimeList) add(t int64, pContact *Contact) {
// remove first
ctl.remove(pContact)
// add as new one
node := ContactListNode{pContact: pContact}
// add into map
ctl.nodeMap[pContact.ip] = &node
// add into list
e := ctl.pList.Back()
if e == nil || t > e.Value.(*ContactTimeListElementValue).t {
v := ContactTimeListElementValue{t: t}
e = ctl.pList.PushBack(&v)
}
// add into map with same time
pv := e.Value.(*ContactTimeListElementValue)
if pv.nodeMap == nil {
pv.nodeMap = make(map[uint32]*ContactListNode)
}
pv.nodeMap[pContact.ip] = &node
node.e = e
}