-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode.go
148 lines (124 loc) · 2.74 KB
/
node.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
package main
import (
"github.com/apex/log"
"fmt"
"math"
)
type Node struct {
id uint64
m int
dist map[uint64]Record
channel chan Message
predecessor *Node
successor *Node
fingerTable []*Node
}
func NewNode(id uint64, dist map[uint64]Record, m int) *Node {
node := Node{
id: id,
m: m,
dist: dist,
channel: make(chan Message, 1000),
}
return &node
}
func (node *Node) start() {
log.WithFields(log.Fields{
"id": node.id,
"m": node.m,
"fingerTable": node.dumpFingerTable(),
}).Info("Node started:")
go node.receive()
}
func (node *Node) receive() {
for msg := range node.channel {
switch msg.tag {
case "find_successor":
node.recvFindSuccessor(msg)
case "get":
node.recvGet(msg)
}
}
}
func (node *Node) recvFindSuccessor(msg Message) {
msg.log("Node received find_successor message.")
s := node.id
e := node.successor.id
if node.isKeyInRange(s, e, msg.query.id, false, true) {
successorMsg := Message{
tag: "successor",
from: node.id,
query: msg.query,
to: msg.query.client.id,
custom: node.successor,
}
successorMsg.log("Node sent successor message.")
msg.query.client.chResponse <- successorMsg
} else {
cpNode := node.findClosestPrecedingNode(msg.query.id)
findSuccessorMsg := Message{
tag: "find_successor",
from: node.id,
query: msg.query,
to: cpNode.id,
}
findSuccessorMsg.log("Node sent find_successor message.")
cpNode.channel <- findSuccessorMsg
}
}
func (node *Node) recvGet(msg Message) {
msg.log("Node received get message.")
resultMsg := Message{
tag: "result",
from: node.id,
query: msg.query,
to: msg.query.client.id,
custom: node.dist[msg.query.id],
}
resultMsg.log("Node sent result.")
msg.query.client.chResponse <- resultMsg
}
/*
* Is id in (s, e)?
* Note : We also have to keep in mind that it is a ring!
*/
func (node *Node) isKeyInRange(s, e, id uint64, sinc bool, einc bool) bool {
if id < s {
id += (1 << uint(node.m)) - 1
}
if e < s {
e += (1 << uint(node.m)) - 1
}
var sCond, eCond bool
if sinc {
sCond = id >= s
} else {
sCond = id > s
}
if einc {
eCond = id <= e
} else {
eCond = id < e
}
return sCond && eCond
}
func (node *Node) findClosestPrecedingNode(id uint64) (cpNode *Node) {
for i := node.m - 1; i >= 0; i-- {
if node.isKeyInRange(node.id, id, node.fingerTable[i].id, false, false) {
cpNode = node.fingerTable[i]
break
}
}
if cpNode == nil {
cpNode = node.fingerTable[len(node.fingerTable) - 1]
}
return cpNode
}
func (node *Node) dumpFingerTable() string {
dump := ""
for i, entry := range node.fingerTable {
dump += fmt.Sprintf("(%d, %d); ",
int(float64(node.id) + math.Pow(2, float64(i))), entry.id)
}
return dump
}