2 between, call, thread, ping = misc.between_c, rpc.call, events.thread, rpc.ping
3 n, predecessor, finger, timeout, m = {}, nil, {}, 5, 24
4 function join(n0) -- n0: some node in the ring
7 finger[1] = call(n0, {'find_successor', n.id})
8 simgrid.info("8Here..")
9 call(finger[1], {'notify', n})
12 function closest_preceding_node(id)
14 if finger[i] and between(finger[i].id, n.id, id) then
21 function find_successor(id)
22 if finger[1].id == n.id or between(id, n.id, (finger[1].id + 1) % 2^m) then
25 local n0 = closest_preceding_node(id)
26 return call(n0, {'find_successor', id})
30 local x = call(finger[1], 'predecessor')
31 if x and between(x.id, n.id, finger[1].id) then
32 finger[1] = x -- new successor
33 call(finger[1], {'notify', n})
38 (not predecessor or between(n0.id, predecessor.id, n.id)) then
42 function fix_fingers()
43 refresh = (refresh and (refresh % m) + 1) or 1 -- 1 <= next <= m
44 finger[refresh] = find_successor((n.id + 2^(refresh - 1)) % 2^m)
46 function check_predecessor()
47 if predecessor and not rpc.ping(predecessor) then
52 n.id = math.random(1, 2^m)
55 n.ip, n.port = job.me.ip, job.me.port
56 join({ip = "192.42.43.42", port = 20000})
58 simgrid.info("bizzaaaaaar...")
59 n.ip, n.port = "127.0.0.1", 20000
60 if arg[1] then n.ip = arg[1] end
61 if arg[2] then n.port = tonumber(arg[2]) end
66 thread(function() join({ip = arg[3], port = tonumber(arg[4])}) end)
70 events.periodic(stabilize, timeout)
71 events.periodic(check_predecessor, timeout)
72 events.periodic(fix_fingers, timeout)