6 # listen on it for incoming messages and forward them
7 # manage the forwarding routing table
8 # the peudo-code can be found here http://en.wikipedia.org/wiki/Chord_%28peer-to-peer%29
12 def __init__(self
, id, ip
, port
):
18 return str(self
.id) + ' ' + self
.ip
+ ' ' + str(self
.port
)
22 def __init__(self
, entryPoint
):
23 # initialize the connection
24 self
.sock
= socket
.socket( socket
.AF_INET6
, socket
.SOCK_DGRAM
)
25 self
.sock
.bind(('', 0))
26 self
.me
= RingMember(uuid
.uuid1().int ,'', self
.sock
.getsockname()[1]) # TODO : get the address
28 self
.predecessor
= None
29 if entryPoint
== None:
30 self
.successor
= self
.me
32 self
.send('FIND_SUCCESSOR ' + str(self
.me
.id) + ' ' + self
.me
.toString(), entryPoint
)
33 log
.log('Init the ring with me = ' + self
.me
.toString(), 3)
36 def handleMessages(self
):
37 # TODO : switch to log
38 log
.log('Handling messages ...', 3)
41 def send(self
, message
, target
):
42 # TODO : switch to log
43 log
.log('Sending : ' + message
+ ' to ' + target
.toString(), 5)
44 self
.sock
.sendto(message
, (target
.ip
, target
.port
))
46 def findSuccessor(self
, id, sender
):
47 if self
.id < id and id <= self
.successor
:
48 self
.send('SUCCESSOR_IS ' + self
.successor
.toString(), sender
)
50 self
.send('FIND_SUCCESSOR ' + str(id) + ' ' + sender
.toString(), successor
) # TODO : use the fingers
52 # Just copying the pseudocode from wikipedia, I will make it work later
53 # Possible messages (just for the creation of the ring) :
55 # find_successor $id $sender : $sender whants the IP of the successor of $id
56 # successor_is $ip $successor
58 # notify $sender_ip $sender_id
62 # pb : how to fix successor
63 # def stabilize(self):
64 # x = SEND get_predecessor TO self.successor
65 # if n < x && x < self.successor:
67 # SEND notify self.ip, self.id TO self.successor
69 # def notify(self, n2)
70 # if self.predecessor == None || (predecessor < n2 && n2 < n):
71 # self.predecessor = n2
73 # to be called periodically
74 # def fixFingers(self)
75 # next = (next + 1) mod (nFingers) # Or Random, cf google
76 # finger[next] = find_successor(n+2^{next-1});
78 # to be called periodically
79 # def checkPredecessor(self)
80 # if NO PING from self.predecessor:
81 # self.predecessor = None