-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrouting.lua
400 lines (310 loc) · 9.66 KB
/
routing.lua
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
-- routing.lua
-- (C) 2009, Michael Meier
-- maximum length of eventpipe (when written to by seenode)
local maxeventpipelen = 100
-- how soon a bucketwatchdog starts a probe again
local probebackoff = 5.0
-- maximum age of entries in routing table to go unchecked if bucket
-- is full
local maxage = 120
RoutingTable = {k=20}
function RoutingTable:new(id, node)
local o = {id=id,
node=node,
nodes={},
maxbucket=1
}
setmetatable(o,self)
self.__index = self
o.buckets= {o:newbucket()}
if o.node == nil then error("RoutingTable needs a node") end
return o
end
function RoutingTable.strcomp(a, b)
--print("COMP: in")
if #a ~= #b then error("strings have to be of the same length", 2) end
local i = 1
local len = #a
while i <= len do
--print("COMP: " .. ec.tohex(string.sub(a,i,i)) .. " <? " .. ec.tohex(string.sub(b,i,i)))
local ba = string.byte(a, i)
local bb = string.byte(b, i)
--print("COMP: ", ba, bb)
if ba < bb then return true end
if ba > bb then return false end
i = i + 1
end
return false
end
function RoutingTable:newbucket()
local eventpipe = Channel:new()
local bucket = {byunique={},
inorder={},
count=0,
eventpipe=eventpipe,
nodequeue={}
}
srun(RoutingTable.bucketwatchdog, self, bucket, eventpipe)
return bucket
end
function RoutingTable:numnodes()
local nodesum = 0
for i, bucket in ipairs(self.buckets) do
nodesum = nodesum + bucket.count
end
return nodesum
end
function RoutingTable.copynode(node)
return {addr=node.addr,
port=node.port,
id=node.id,
unique=node.unique,
distance=node.distance
}
end
function RoutingTable:bucketwatchdog(bucket, eventpipe)
local nodequeue = bucket.nodequeue
local k = RoutingTable.k
local maxage = maxage or 10
local lastprobetime = 0
while true do
local cmd, node = eventpipe:receive()
print("ROUTING: bucketwatchdog: " .. cmd)
if cmd == "free" then
print("ROUTING: bucketwatchdog: bucket.count", bucket.count)
end
if cmd == "new" or cmd == "free" then
-- get some node
local func, tab, par = pairs(nodequeue)
local unique, node = func(tab, par)
if node then
-- delete the node from the table
nodequeue[unique] = nil
-- if we have some node to insert
if bucket.count < k then
print("ROUTING: bucketwatchdog: fits in: " .. node.addr .. "|" .. tostring(node.port))
-- and there is free space in the bucket
local bucketno = self:getpos(node)
if not bucketno then
-- and the node does not yet exist in the routing
-- table... insert it
self:newnode(node)
nodequeue[node.unique] = nil
end
else
-- and there is no free space in the routing table
local livelinessmanager = self.node.callmanager.livelinessmanager
local now = ec.time()
-- get the least recently seen node
local lrsnode = bucket.inorder[1]
-- check if it has been active in the last maxage
-- seconds
if (now - lastprobetime) > probebackoff and
not livelinessmanager:isweaklyalive(lrsnode, maxage) then
-- if not: check if it responds
livelinessmanager:isstronglyalive(lrsnode)
lastprobetime = now
-- when the node is not alive we will be called back
-- via our eventpipe
-- at the moment incoming "new" nodes are not
-- cached, thus i reinsert the new node after the
-- "free" message of the livelinessmanager
nodequeue[node.unique] = node
scall(function()
eventpipe:sendasync("new", node)
end)
end
end
end
end
end
end
function RoutingTable:getpos(node)
local bucketno = math.min(ec.getbucketno(node.distance), self.maxbucket)
local bucket = self.buckets[bucketno]
if bucket.byunique[node.unique] ~= nil then
for i, nodei in ipairs(bucket.inorder) do
if node.unique == nodei.unique then
return bucketno, i
end
end
error("inconsistent routing table")
else
return false
end
end
function RoutingTable:removenodeatpos(bucketno, i)
local bucket = self.buckets[bucketno]
local node = table.remove(bucket.inorder, i)
bucket.byunique[node.unique] = nil
bucket.count = bucket.count - 1
return node
end
function RoutingTable:insertnodeinbucket(node, bucketno)
local bucket = self.buckets[bucketno]
bucket.byunique[node.unique] = node
table.insert(bucket.inorder, node)
bucket.count = bucket.count + 1
end
function RoutingTable:newnode(node)
local bucketno = math.min(ec.getbucketno(node.distance), self.maxbucket)
local bucket = self.buckets[bucketno]
if bucket.count < self.k then
self:insertnodeinbucket(node, bucketno)
else
if bucketno == self.maxbucket then
-- split the bucket
self.maxbucket = self.maxbucket + 1
local oldbucket = self.buckets[self.maxbucket - 1]
local newbucket = self:newbucket()
self.buckets[self.maxbucket] = newbucket
local nodecache={}
for i, nodei in ipairs(oldbucket.inorder) do table.insert(nodecache,nodei) end
oldbucket.inorder = {}
oldbucket.byunique = {}
oldbucket.count = 0
for i, nodei in ipairs(nodecache) do
self:newnode(nodei)
end
self:newnode(node)
else
-- maybe replace an old node
--print("PROBING LEAST RECENTLY SEEN NODE IN BUCKET " .. tostring(bucketno))
print("ROUTING: new? bucketno " .. bucketno .. " on pipe " .. tostring(bucket.eventpipe))
local eventpipe = bucket.eventpipe
if eventpipe.balance < maxeventpipelen then
print("ROUTING: new to watchdog for " .. node.addr .. "|" .. tostring(node.port))
bucket.nodequeue[node.unique] = node
eventpipe:sendasync("new", node)
end
end
end
end
function RoutingTable:nodedown(node)
local node = RoutingTable.copynode(node)
if not node.id then
-- contact is not complete so we don't know what to remove
return
end
print("XOR")
local distance = ec.xor(self.id, node.id)
print("done")
node.distance = distance
local bucketno, i = self:getpos(node)
if bucketno then
-- if the node exists in our table, remove it
self:removenodeatpos(bucketno, i)
-- notify the watchdog
self.buckets[bucketno].eventpipe:sendasync("free")
end
end
function RoutingTable:seenode(node)
local unique = node.unique
local nodeid = node.id
-- loopback
if nodeid == self.id then return end
local distance = ec.xor(self.id, nodeid)
-- copy node info
local node = {addr=node.addr,
port=node.port,
id=node.id,
unique=node.unique,
distance=distance}
local bucketno, i = self:getpos(node)
if bucketno then
-- move to tail of LRS queue
-- node = self:removenodeatpos(node, bucketno, i)
local node = self:removenodeatpos(bucketno, i)
self:insertnodeinbucket(node, bucketno)
else
-- insert a new node
self:newnode(node)
end
--self:print()
end
function RoutingTable:getclosest(id, n)
-- this function does not return contacts directly, but instead
-- shallow copies them so as to prevent accidentally tampering with
-- them by, say, changing the distance
local n = n or 20
local distance = ec.xor(self.id, id)
local bucketno = math.min(ec.getbucketno(distance), self.maxbucket)
local copynode = RoutingTable.copynode
local ret = {}
local basebucket = self.buckets[bucketno]
local inorder = basebucket.inorder
local bound = math.min(n, basebucket.count)
for i=1,bound do table.insert(ret, copynode(inorder[i])) end
local retn = bound
local i = 1
--table.foreach(ret,print)
local cangoup = (bucketno - i) >= 1
local cangodown = (bucketno + i) <= self.maxbucket
while (cangoup or cangodown) do
cangoup = (bucketno - i) >= 1
cangodown = (bucketno + i) <= self.maxbucket
if retn == n then return ret end
if cangoup then
local bucket = self.buckets[bucketno - i]
local inorder = bucket.inorder
local bound = math.min(bucket.count, n - retn)
for i=1,bound do table.insert(ret, copynode(inorder[i])) end
retn = retn + bound
end
if retn == n then return ret end
if cangodown then
local bucket = self.buckets[bucketno + i]
local inorder = bucket.inorder
local bound = math.min(bucket.count, n - retn)
for i=1,bound do table.insert(ret, copynode(inorder[i])) end
retn = retn + bound
end
i = i + 1
end
return ret
end
function RoutingTable:print()
for i=1,self.maxbucket do
local bucket = self.buckets[i]
print("bucket " .. tostring(i) .. ":")
for j,node in ipairs(bucket.inorder) do
print(ec.tohex(node.id) ..
" | " ..
string.sub(ec.tohex(node.distance), 1, 10) ..
" @ " ..
node.addr ..
":" ..
node.port)
end
end
end
function RoutingTable:dryrun()
--rtid = string.rep("\0", 20)
--rt = RoutingTable:new(rtid)
local rt = RoutingTable:new("das esch de rap shit")
local rt = RoutingTable:new(string.rep("\0", 20))
local myaddr = "192.168.1.5"
math.randomseed(42)
local nodes = {}
for i = 1,500 do
table.insert(nodes,
{addr=myaddr, port=4200, id=ec.sha1(tostring(math.random()))})
end
for i, node in ipairs(nodes) do
node.unique=node.addr .. "|" .. tostring(node.port) .. "|" .. node.id
rt:seenode(node)
end
rt:print()
local searchid = string.rep("\8",20)
local t1 = ec.time()
local cl = rt:getclosest(searchid, 20)
local t2 = ec.time()
print("took: " .. ((t2 - t1)*1000))
for i=1,#cl do
print(i .. " => " .. ec.tohex(cl[i].id))
end
print("---")
os.exit(0)
end
--rt = RoutingTable:new(string.rep("\0", 20))
--rt:dryrun()