Fix in the refresh dead peers in registry + added info about hello interval
[re6stnet.git] / propagation.py
1 import socket, uuid
2 import log
3
4 # create an upd socket
5 # listen on it for incoming messages and forward them
6 # manage the forwarding routing table
7 # the peudo-code can be found here http://en.wikipedia.org/wiki/Chord_%28peer-to-peer%29
8
9 class RingMember:
10
11 def __init__(self, id, ip, port):
12 self.port = port
13 self.ip = ip
14 self.id = id
15
16 def toString(self):
17 return str(self.id) + ' ' + self.ip + ' ' + str(self.port)
18
19 class Ring:
20
21 def __init__(self, entry_point):
22 # initialize the connection
23 self.sock = socket.socket( socket.AF_INET6, socket.SOCK_DGRAM )
24 self.sock.bind(('', 0))
25 self.me = RingMember(uuid.uuid1().int ,'', self.sock.getsockname()[1]) # TODO : get the address
26 # to enter the ring
27 self.predecessor = None
28 if entry_point == None:
29 self.successor = self.me
30 else:
31 self.send('FIND_SUCCESSOR ' + str(self.me.id) + ' ' + self.me.toString(), entry_point)
32 log.log('Init the ring with me = ' + self.me.toString(), 3)
33
34 # TODO :
35 def handleMessages(self):
36 # TODO : switch to log
37 log.log('Handling messages ...', 3)
38 pass
39
40 def send(self, message, target):
41 # TODO : switch to log
42 log.log('Sending : ' + message + ' to ' + target.toString(), 5)
43 self.sock.sendto(message, (target.ip, target.port))
44
45 def findSuccessor(self, id, sender):
46 if self.id < id and id <= self.successor:
47 self.send('SUCCESSOR_IS ' + self.successor.toString(), sender)
48 else:
49 self.send('FIND_SUCCESSOR ' + str(id) + ' ' + sender.toString(), successor) # TODO : use the fingers
50
51 # Just copying the pseudocode from wikipedia, I will make it work later
52 # Possible messages (just for the creation of the ring) :
53 #
54 # find_successor $id $sender : $sender whants the IP of the successor of $id
55 # successor_is $ip $successor
56 # get_predecessor
57 # notify $sender_ip $sender_id
58 # PING
59
60 # called periodically
61 # pb : how to fix successor
62 # def stabilize(self):
63 # x = SEND get_predecessor TO self.successor
64 # if n < x && x < self.successor:
65 # self.successor = x
66 # SEND notify self.ip, self.id TO self.successor
67
68 # def notify(self, n2)
69 # if self.predecessor == None || (predecessor < n2 && n2 < n):
70 # self.predecessor = n2
71
72 # to be called periodically
73 # def fixFingers(self)
74 # # XXX: naming - should be finger_count
75 # next = (next + 1) mod (nFingers) # Or Random, cf google
76 # finger[next] = find_successor(n+2^{next-1});
77
78 # to be called periodically
79 # def checkPredecessor(self)
80 # if NO PING from self.predecessor:
81 # self.predecessor = None