turned ip-to-hash into 2 functions, known devices are determined on the basis of reading the nodes/hash/ now, fixed bug where index for other nodes was always newer even if it wasnt

This commit is contained in:
Roel 2014-05-15 21:53:13 +02:00
parent 03f3fef866
commit 21011546dc

45
main.py
View File

@ -60,8 +60,8 @@ class Meshenger:
print 'found', len(self.devices),'device(s)' print 'found', len(self.devices),'device(s)'
for device in self.devices: for device in self.devices:
nodepath = self.ip_to_hash(device) #make a folder for the node (nodes/'hash'/) nodepath = self.ip_to_hash_path(device) #make a folder for the node (nodes/'hash'/)
nodeupdatepath = os.path.join(self.ip_to_hash(device), 'lastupdate') nodeupdatepath = os.path.join(self.ip_to_hash_path(device), 'lastupdate')
print 'Checking age of foreign node index' print 'Checking age of foreign node index'
@ -81,7 +81,7 @@ class Meshenger:
def node_timestamp(self, ip): def node_timestamp(self, ip):
updatepath = os.path.join(self.ip_to_hash(ip), 'lastupdate') updatepath = os.path.join(self.ip_to_hash_path(ip), 'lastupdate')
with open(updatepath, 'wb') as lastupdate: with open(updatepath, 'wb') as lastupdate:
lastupdate.write(self.devices[ip]) lastupdate.write(self.devices[ip])
#return updatepath #return updatepath
@ -112,17 +112,20 @@ Discover other devices by listening to the Meshenger announce port
s.setblocking(0) s.setblocking(0)
while not self.exitapp: while not self.exitapp:
result = select.select([s],[],[])[0][0].recvfrom(bufferSize) result = select.select([s],[],[])[0][0].recvfrom(bufferSize)
ip = result[1][0]
if result[1][0] in self.devices and result[1][0] != self.own_ip: if os.path.exists(self.ip_to_hash_path(ip)) and ip != self.own_ip:
print 'Known node', result[1][0] print 'Known node', ip
self.devices[result[1][0]] = result[0] self.devices[ip] = result[0]
#self.devices.append(result[1][0]) self.node_timestamp(ip) #klopt dit?
#self.devices.append(ip)
elif result[1][0] not in self.devices and result[1][0] != self.own_ip: elif not os.path.exists(self.ip_to_hash_path(ip)) and ip != self.own_ip:
#loop for first time #loop for first time
self.devices[result[1][0]] = result[0] self.ip_to_hash_path(ip)
self.node_timestamp(result[1][0]) self.devices[ip] = result[0]
print 'New node', result[1][0] self.node_timestamp(ip)
print 'New node', ip
@ -156,8 +159,9 @@ Save the time of the last update.
if not os.path.exists('index'): if not os.path.exists('index'):
with open('index','wb') as index: with open('index','wb') as index:
index.write('') index.write('')
previous_index = [] previous_index = []
else:
previous_index = open('index').readlines()
while not self.exitapp: while not self.exitapp:
current_index = os.listdir(self.msg_dir) current_index = os.listdir(self.msg_dir)
@ -170,8 +174,8 @@ Save the time of the last update.
print 'Index updated:', current_index print 'Index updated:', current_index
with open('index_last_update', 'wb') as indexupdate: ### misschien is dit overbodig with open('index_last_update', 'wb') as indexupdate:
indexupdate.write(str(int(time.time()))) indexupdate.write(self.index_last_update) ### misschien moet dit index_last_update zijn
previous_index = current_index previous_index = current_index
time.sleep(5) time.sleep(5)
@ -199,21 +203,26 @@ Get new messages from other node based on it's index file
except: except:
pass pass
def ip_to_hash(self, ip): def ip_to_hash_path(self, ip):
""" """
Convert a node's ip into a hash and make a directory to store it's files Convert a node's ip into a hash and make a directory to store it's files
""" """
if not os.path.exists('nodes'): if not os.path.exists('nodes'):
os.mkdir('nodes') os.mkdir('nodes')
import hashlib nodepath = os.path.join(os.path.abspath('nodes'), self.hasj(ip))
hasj = hashlib.md5(ip).hexdigest()
nodepath = os.path.join(os.path.abspath('nodes'), hasj)
if not os.path.exists(nodepath): if not os.path.exists(nodepath):
os.mkdir(nodepath) os.mkdir(nodepath)
return nodepath return nodepath
def hasj(self, ip):
"""
Convert a node's ip into a hash
"""
import hashlib
hasj = hashlib.md5(ip).hexdigest()
return hasj
def clientsite(self): def clientsite(self):
a = '' a = ''