Browse Source

made changes in discover() to assign assing a local time to node/hash/lastupdate

develop
Roel 11 years ago
parent
commit
b2e787bf32
  1. 31
      main.py

31
main.py

@ -9,7 +9,7 @@ class Meshenger:
#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 = str(time.time()) index_last_update = str(int(time.time()))
def __init__(self): def __init__(self):
@ -53,6 +53,7 @@ class Meshenger:
nodeupdatepath = self.node_timestamp(device, nodepath) #contains the path to the update timestamp of the node (nodes/'hash'/lastupdate) 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' print 'Checking age of foreign node index'
if self.devices[device] > nodeupdatepath: if self.devices[device] > nodeupdatepath:
print 'Foreign node"s index is newer, proceed to download index' print 'Foreign node"s index is newer, proceed to download index'
self.get_index(device, nodepath) self.get_index(device, nodepath)
@ -67,7 +68,7 @@ class Meshenger:
updatepath = os.path.join(path, 'lastupdate') updatepath = os.path.join(path, 'lastupdate')
with open(updatepath, 'wb') as lastupdate: with open(updatepath, 'wb') as lastupdate:
lastupdate.write(self.devices[ip]) lastupdate.write(self.devices[ip][2])
return updatepath return updatepath
@ -94,11 +95,30 @@ class Meshenger:
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)
print select.select([s],[],[]).recvfrom(bufferSize)
while not self.exitapp: while not self.exitapp:
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: foreign_node_ip = result[1][0]
self.devices[result[1][0]] = result[0][0]
if foreign_node_ip not in self.devices and foreign_node_ip != self.own_ip:
print 'voor het eerst toegevoegd'
timestamp = result[0]
oldtimestamp = timestamp
localtimestamp = str(int(time.time()))
self.devices[foreign_node_ip] = (timestamp, oldtimestamp, localtimestamp)
if foreign_node_ip in self.devices and foreign_node_ip != self.own_ip:
timestamp = result[0]
oldtimestamp = self.devices[foreign_node_ip][1]
self.devices[foreign_node_ip] = (timestamp, oldtimestamp, localtimestamp)
print 'timestamp update'
print self.devices[foreign_node_ip]
if timestamp != oldtimestamp:
localtimestamp = str(int(time.time()))
self.devices[foreign_node_ip] = (timestamp, timestamp, localtimestamp)
print 'timestamp check'
print self.devices[foreign_node_ip]
#self.devices.append(result[1][0]) #self.devices.append(result[1][0])
time.sleep(1) time.sleep(1)
@ -129,6 +149,7 @@ 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')
self.index_last_update = str(int(time.time())) self.index_last_update = str(int(time.time()))
with open('index_last_update', 'wb') as indexupdate: ### misschien is dit overbodig with open('index_last_update', 'wb') as indexupdate: ### misschien is dit overbodig

Loading…
Cancel
Save