nodes now broadcast a timestamp of their last change

This commit is contained in:
Generic Username 2014-04-04 20:53:22 +02:00
parent 8f61d977ce
commit 9cd2251db5

46
main.py
View File

@ -3,23 +3,23 @@
import socket, os, time, select, urllib, sys, threading import socket, os, time, select, urllib, sys, threading
class Meshenger: class Meshenger:
devices = [] #the list of all the nodes this this node has seen devices = {} #the dictionary of all the nodes this this node has seen
serve_port = 13338 serve_port = 13338
announce_port = 13337 announce_port = 13337
#own_ip = "0.0.0.0" #own_ip = "0.0.0.0"
msg_dir = os.path.relpath('msg/') msg_dir = os.path.relpath('msg/')
exitapp = False #to kill all threads on exitapp = False #to kill all threads on
index_last_update = ''
def __init__(self): def __init__(self):
os.system("echo 1 >> /proc/sys/net/ipv6/conf/br-lan/disable_ipv6") #os.system("echo 1 >> /proc/sys/net/ipv6/conf/br-lan/disable_ipv6")
self.own_ip = self.get_ip_adress() self.own_ip = self.get_ip_adress()
#self.own_ip = "192.168.2.196" print 'Building own index for the first time\n'
self.build_index()
try: try:
#print 'discovering devices'
d = threading.Thread(target=self.discover) d = threading.Thread(target=self.discover)
d.daemon = True d.daemon = True
d.start() d.start()
@ -38,26 +38,39 @@ class Meshenger:
if len(self.devices) > 0: if len(self.devices) > 0:
print 'found', len(self.devices),'device(s) retreiving indices' print 'found', len(self.devices),'device(s) retreiving indices'
for device in self.devices: for device in self.devices:
nodepath = self.ip_to_hash(device) nodepath = self.ip_to_hash(device) #make a folder for the node (nodes/'hash'/)
nodeupdatepath = self.node_timestamp(device, nodepath) #contains the path to the update timestamp of the node (nodes/'hash'/lastupdate)
print 'Checking age of foreign node index'
if self.devices[device] > nodeupdatepath:
print 'Foreign node"s index is newer, proceed to download index'
self.get_index(device, nodepath) self.get_index(device, nodepath)
print 'downloading messages'
self.get_messages(device, nodepath) self.get_messages(device, nodepath)
print 'updating own index' print 'updating own index'
self.build_index() self.build_index()
time.sleep(5) #free process or ctrl+c time.sleep(5) #free process or ctrl+c
def node_timestamp(self, ip, path):
updatepath = os.path.join(path, 'lastupdate')
with open(updatepath, 'wb') as lastupdate:
lastupdate.write(self.devices[ip])
return updatepath
def announce(self): def announce(self):
""" """
Announce the node's existance to other nodes Announce the node's existance to other nodes
""" """
print 'Announcing'
while not self.exitapp: while not self.exitapp:
print 'announcing'
sock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM, socket.IPPROTO_UDP) sock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
sock.sendto('bericht', ("ff02::1", self.announce_port)) sock.sendto(self.index_last_update, ("ff02::1", self.announce_port))
sock.close() sock.close()
time.sleep(5) time.sleep(5)
@ -66,20 +79,19 @@ class Meshenger:
Discover other devices by listening to the Meshenger announce port Discover other devices by listening to the Meshenger announce port
""" """
print 'Discovering'
bufferSize = 1024 # whatever you need bufferSize = 1024 # whatever you need
s = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM, socket.IPPROTO_UDP) s = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
s.bind(('::', self.announce_port)) s.bind(('::', self.announce_port))
s.setblocking(0) s.setblocking(0)
#global devices
while not self.exitapp: while not self.exitapp:
print 'discovering'
result = select.select([s],[],[])[0][0].recvfrom(bufferSize) result = select.select([s],[],[])[0][0].recvfrom(bufferSize)
if result[1][0] not in self.devices and result[1][0] != self.own_ip: if result[1][0] not in self.devices and result[1][0] != self.own_ip:
self.devices.append(result[1][0]) self.devices[result[1][0]] = result[0][0]
return #self.devices.append(result[1][0])
time.sleep(1) time.sleep(1)
def serve(self): def serve(self):
@ -111,8 +123,11 @@ class Meshenger:
for message in os.listdir(self.msg_dir): for message in os.listdir(self.msg_dir):
index.write(message) index.write(message)
index.write('\n') index.write('\n')
with open('index_last_update', 'wb') as indexupdate: self.index_last_update = str(int(time.time()))
with open('index_last_update', 'wb') as indexupdate: ### misschien is dit overbodig
indexupdate.write(str(int(time.time()))) indexupdate.write(str(int(time.time())))
current_index = previous_index current_index = previous_index
def get_index(self,ip, path): def get_index(self,ip, path):
@ -161,7 +176,6 @@ class Meshenger:
""" """
if not os.path.isfile('interfaceip6adress'): if not os.path.isfile('interfaceip6adress'):
os.system('ifconfig -a adhoc0 | grep inet6 > interfaceip6adress') os.system('ifconfig -a adhoc0 | grep inet6 > interfaceip6adress')
with open('interfaceip6adress', 'r') as a: with open('interfaceip6adress', 'r') as a:
return a.read().split()[2].split('/')[0] return a.read().split()[2].split('/')[0]