reverted changes
This commit is contained in:
parent
fa67aba96e
commit
7a6d139280
53
main.py
53
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(int(time.time()))
|
index_last_update = str(time.time())
|
||||||
|
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
@ -50,12 +50,14 @@ class Meshenger:
|
|||||||
|
|
||||||
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(device) #make a folder for the node (nodes/'hash'/)
|
||||||
nodeupdatepath = os.path.join(nodepath, 'lastupdate') #contains the path to the update timestamp of the node (nodes/'hash'/lastupdate)
|
nodeupdatepath = os.path.join(self.ip_to_hash(device), 'lastupdate')
|
||||||
|
|
||||||
print 'Checking age of foreign node index'
|
print 'Checking age of foreign node index'
|
||||||
|
print self.devices[device]
|
||||||
|
foreign_node_update = open(nodeupdatepath).read()
|
||||||
|
print foreign_node_update
|
||||||
|
|
||||||
if self.devices[device][2] > open(nodeupdatepath).read():
|
if self.devices[device] > foreign_node_update:
|
||||||
|
|
||||||
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)
|
||||||
print 'downloading messages'
|
print 'downloading messages'
|
||||||
@ -65,12 +67,12 @@ class Meshenger:
|
|||||||
|
|
||||||
time.sleep(5) #free process or ctrl+c
|
time.sleep(5) #free process or ctrl+c
|
||||||
|
|
||||||
def node_timestamp(self):
|
def node_timestamp(self, ip):
|
||||||
|
|
||||||
updatepath = os.path.join(path, 'lastupdate')
|
updatepath = os.path.join(self.ip_to_hash(ip), 'lastupdate')
|
||||||
with open(updatepath, 'wb') as lastupdate:
|
with open(updatepath, 'wb') as lastupdate:
|
||||||
lastupdate.write(self.devices[ip][2])
|
lastupdate.write(self.devices[ip])
|
||||||
return updatepath
|
#return updatepath
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -98,35 +100,14 @@ class Meshenger:
|
|||||||
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)
|
||||||
foreign_node_ip = result[1][0]
|
|
||||||
|
|
||||||
if foreign_node_ip not in self.devices and foreign_node_ip != self.own_ip:
|
if result[1][0] not in self.devices and result[1][0] != self.own_ip:
|
||||||
print 'voor het eerst toegevoegd'
|
#loop for first time
|
||||||
timestamp = result[0] #get the timestamp that foreign node announced
|
self.devices[result[1][0]] = result[0]
|
||||||
oldtimestamp = timestamp
|
node_timestamp(result[1][0])
|
||||||
localtimestamp = str(int(time.time()))
|
|
||||||
self.devices[foreign_node_ip] = (timestamp, oldtimestamp, localtimestamp) #add everything to dictionary
|
|
||||||
|
|
||||||
with open(os.path.join(self.ip_to_hash(foreign_node_ip), 'lastupdate'),'wb') as lastupdate:
|
|
||||||
lastupdate.write(localtimestamp) #convert what node announced to local time and save for node in nodes/hash/lastupdate
|
|
||||||
|
|
||||||
if foreign_node_ip in self.devices and foreign_node_ip != self.own_ip:
|
|
||||||
timestamp = result[0] # we already know the node, so we update timestamp (might now give difference between old and new timestamp)
|
|
||||||
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)
|
|
||||||
|
|
||||||
with open(os.path.join(self.ip_to_hash(foreign_node_ip), 'lastupdate'),'wb') as lastupdate:
|
|
||||||
lastupdate.write(localtimestamp)
|
|
||||||
print 'local tijd aangepast'
|
|
||||||
|
|
||||||
#print self.devices[foreign_node_ip]
|
|
||||||
|
|
||||||
|
if result[1][0] in self.devices and result[1][0] != self.own_ip:
|
||||||
|
self.devices[result[1][0]] = result[0]
|
||||||
#self.devices.append(result[1][0])
|
#self.devices.append(result[1][0])
|
||||||
|
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
@ -157,7 +138,6 @@ 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
|
||||||
@ -225,3 +205,4 @@ if __name__ == "__main__":
|
|||||||
except (KeyboardInterrupt, SystemExit):
|
except (KeyboardInterrupt, SystemExit):
|
||||||
exitapp = True
|
exitapp = True
|
||||||
raise
|
raise
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user