Vifib works again
[re6stnet.git] / propagation.py
1 #!/usr/bin/env python
2 import socket, uuid
3 import log
4
5 # create an upd socket
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
9
10 class RingMember:
11
12 def __init__(self, id, ip, port):
13 self.port = port
14 self.ip = ip
15 self.id = id
16
17 def toString(self):
18 return str(self.id) + ' ' + self.ip + ' ' + str(self.port)
19
20 class Ring:
21
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
27 # to enter the ring
28 self.predecessor = None
29 if entryPoint == None:
30 self.successor = self.me
31 else:
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)
34
35 # TODO :
36 def handleMessages(self):
37 # TODO : switch to log
38 log.log('Handling messages ...', 3)
39 pass
40
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))
45
46 def findSuccessor(self, id, sender):
47 if self.id < id and id <= self.successor:
48 self.send('SUCCESSOR_IS ' + self.successor.toString(), sender)
49 else:
50 self.send('FIND_SUCCESSOR ' + str(id) + ' ' + sender.toString(), successor) # TODO : use the fingers
51
52 # Just copying the pseudocode from wikipedia, I will make it work later
53 # Possible messages (just for the creation of the ring) :
54 #
55 # find_successor $id $sender : $sender whants the IP of the successor of $id
56 # successor_is $ip $successor
57 # get_predecessor
58 # notify $sender_ip $sender_id
59 # PING
60
61 # called periodically
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:
66 # self.successor = x
67 # SEND notify self.ip, self.id TO self.successor
68
69 # def notify(self, n2)
70 # if self.predecessor == None || (predecessor < n2 && n2 < n):
71 # self.predecessor = n2
72
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});
77
78 # to be called periodically
79 # def checkPredecessor(self)
80 # if NO PING from self.predecessor:
81 # self.predecessor = None