Compare commits
7 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
1013473d6b | ||
|
0e587e93d7 | ||
|
60d77071c5 | ||
|
d6549ea19d | ||
|
fe28173fa7 | ||
|
2653eb5bb2 | ||
|
4732e448b2 |
88
main.py
88
main.py
@ -1,6 +1,6 @@
|
||||
#!/usr/bin/python
|
||||
|
||||
import socket, os, time, select, urllib, sys, threading
|
||||
import socket, os, time, select, urllib, sys, threading, subprocess
|
||||
|
||||
class Meshenger:
|
||||
devices = {} #the dictionary of all the nodes this this node has seen
|
||||
@ -9,7 +9,7 @@ class Meshenger:
|
||||
#own_ip = "0.0.0.0"
|
||||
msg_dir = os.path.relpath('msg/')
|
||||
exitapp = False #to kill all threads on
|
||||
index_last_update = str(int(time.time()))
|
||||
index_last_update = "0" #str(int(time.time()))
|
||||
|
||||
def __init__(self):
|
||||
|
||||
@ -59,7 +59,7 @@ class Meshenger:
|
||||
if len(self.devices) > 0:
|
||||
print 'found', len(self.devices),'device(s)'
|
||||
|
||||
for device in self.devices:
|
||||
for device in self.devices.keys():
|
||||
nodepath = self.ip_to_hash_path(device) #make a folder for the node (nodes/'hash'/)
|
||||
nodeupdatepath = os.path.join(self.ip_to_hash_path(device), 'lastupdate')
|
||||
|
||||
@ -76,9 +76,16 @@ class Meshenger:
|
||||
|
||||
if self.devices[device] > foreign_node_update:
|
||||
print 'Foreign node"s index is newer, proceed to download index'
|
||||
self.get_index(device, nodepath)
|
||||
if not self.get_index(device, nodepath):
|
||||
print 'index wget failed'
|
||||
continue
|
||||
|
||||
print 'downloading messages'
|
||||
self.get_messages(device, nodepath)
|
||||
if not self.get_messages(device, nodepath):
|
||||
print 'getting messages failed'
|
||||
continue
|
||||
|
||||
print 'updating node timestamp'
|
||||
self.node_timestamp(device)
|
||||
|
||||
time.sleep(5) #free process or ctrl+c
|
||||
@ -121,7 +128,7 @@ Discover other devices by listening to the Meshenger announce port
|
||||
node_path = os.path.join(os.path.abspath('nodes'), self.hasj(ip))
|
||||
|
||||
if not os.path.exists(node_path) and ip != self.own_ip:
|
||||
#loop for first time
|
||||
#loop for first timef
|
||||
self.ip_to_hash_path(ip) #make a folder /nodes/hash
|
||||
self.devices[ip] = result[0]
|
||||
#self.node_timestamp(ip) #make a local copy of the timestamp in /nodes/hash/updatetimestamp
|
||||
@ -157,6 +164,44 @@ Make an index file of all the messages present on the node.
|
||||
Save the time of the last update.
|
||||
"""
|
||||
|
||||
|
||||
index_file = os.path.relpath( 'index' )
|
||||
previous_index = []
|
||||
if not os.path.exists( index_file ):
|
||||
with open('index','wb') as index:
|
||||
index.write('')
|
||||
else:
|
||||
previous_index = open( index_file ).read().split()
|
||||
|
||||
index_last_update_file = os.path.relpath( 'index_last_update' )
|
||||
if os.path.exists( index_last_update_file ):
|
||||
self.index_last_update = open( index_last_update_file ).read()
|
||||
|
||||
while not self.exitapp:
|
||||
|
||||
current_index = []
|
||||
for root, folders, files in os.walk( self.msg_dir ):
|
||||
if root == 'msg':
|
||||
folders.sort()
|
||||
else:
|
||||
files.sort()
|
||||
current_index += [root + '/' + f for f in files]
|
||||
|
||||
if current_index != previous_index:
|
||||
with open( index_file, 'w' ) as f:
|
||||
f.write( '\n'.join( current_index ))
|
||||
|
||||
self.index_last_update = str( int( time.time()))
|
||||
print 'Index updated:', current_index
|
||||
|
||||
with open( os.path.relpath('index_last_update'), 'w') as f:
|
||||
f.write( self.index_last_update )
|
||||
|
||||
previous_index = current_index
|
||||
|
||||
time.sleep( 5 )
|
||||
|
||||
"""
|
||||
print 'Building own index for the first time\n'
|
||||
|
||||
if not os.path.exists('index'):
|
||||
@ -176,36 +221,51 @@ Save the time of the last update.
|
||||
self.index_last_update = str(int(time.time()))
|
||||
|
||||
print 'Index updated:', current_index
|
||||
|
||||
with open('index_last_update', 'wb') as indexupdate:
|
||||
indexupdate.write(self.index_last_update) ### misschien moet dit index_last_update zijn
|
||||
|
||||
previous_index = current_index
|
||||
time.sleep(5)
|
||||
"""
|
||||
|
||||
def get_index(self,ip, path):
|
||||
"""
|
||||
Download the indices from other nodes.
|
||||
"""
|
||||
# time.sleep(0) # hack to prevent wget bug
|
||||
# os.system('wget http://['+ip+'%adhoc0]:'+self.serve_port+'/index -O '+os.path.join(path,'index'))
|
||||
command = 'wget http://['+ip+'%adhoc0]:'+self.serve_port+'/index -O '+os.path.join(path,'index')
|
||||
print 'get_index: ', command
|
||||
status = subprocess.call( command, shell=True )
|
||||
return status == 0
|
||||
|
||||
os.system('wget http://['+ip+'%adhoc0]:'+self.serve_port+'/index -O '+os.path.join(path,'index'))
|
||||
|
||||
|
||||
def get_messages(self, ip, path):
|
||||
def get_messages(self, ip, path ):
|
||||
"""
|
||||
Get new messages from other node based on it's index file
|
||||
"""
|
||||
|
||||
|
||||
try:
|
||||
with open(os.path.join(path,'index')) as index:
|
||||
index = index.read().split('\n')
|
||||
for message in index:
|
||||
messagepath = os.path.join(os.path.abspath(self.msg_dir), message)
|
||||
parts = message.split('/')
|
||||
dirpath = os.path.join( parts[0], parts[1])
|
||||
if not os.path.isdir( dirpath ):
|
||||
os.mkdir( dirpath )
|
||||
|
||||
messagepath = os.path.relpath( message )
|
||||
if not os.path.exists(messagepath):
|
||||
print 'downloading', message, 'to', messagepath
|
||||
os.system('wget http://['+ip+'%adhoc0]:'+self.serve_port+'/msg/'+message+' -O '+messagepath)
|
||||
command = 'wget http://['+ip+'%adhoc0]:' + self.serve_port + '/' + message+' -O ' + messagepath
|
||||
status = subprocess.call( command, shell=True)
|
||||
if status != 0:
|
||||
return False
|
||||
# succesfuly downloaded all messages, return true
|
||||
return True
|
||||
except:
|
||||
print 'Failed to download messages'
|
||||
pass
|
||||
return False
|
||||
|
||||
def ip_to_hash_path(self, ip):
|
||||
"""
|
||||
|
@ -19,15 +19,13 @@ class ClientServeHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
|
||||
"""
|
||||
def do_GET(self):
|
||||
|
||||
if self.path == '/':
|
||||
self.send_response(200)
|
||||
self.send_header('Content-type', 'text/html')
|
||||
self.end_headers()
|
||||
|
||||
f = os.path.join( "webapp.html")
|
||||
with open( f, 'r') as the_file:
|
||||
self.wfile.write(the_file.read())
|
||||
|
||||
if self.path.startswith( '/'+self.messageDir ):
|
||||
parts = self.path.split('/');
|
||||
if len( parts ) == 2:
|
||||
|
||||
self.wfile.write('give index')
|
||||
elif len( parts ) == 3:
|
||||
self.wfile.write('give msg')
|
||||
elif self.path == '/index' or self.path.startswith( "/"+self.messageDir ):
|
||||
return SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)
|
||||
|
||||
|
@ -7,6 +7,14 @@
|
||||
|
||||
-perhaps replace the functionality of self.devices with something based on crawling nodes/hash/
|
||||
|
||||
-sometimes node tries to connect to itself (something wrong with receiving broadcast maybe??)
|
||||
|
||||
- HTTP calls the client needs:
|
||||
- /msg/<uid> -> give list of all messages for this uid
|
||||
- /msg/<uid>/<timestamp> -> get message for this timestamp
|
||||
- /clients -> return list of all known clients (uid's + usernames)
|
||||
- /getUID -> return uid for client ( unique id based on what? mac address? )
|
||||
|
||||
# Cryptoshit
|
||||
|
||||
encrypt / decrypt:
|
||||
@ -14,3 +22,5 @@ http://www.davedoesdev.com/a-simple-and-consistent-wrapper-for-javascript-crypto
|
||||
|
||||
generating keys:
|
||||
https://bitbucket.org/adrianpasternak/js-rsa-pem/wiki/Home
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user