Compare commits

...

7 Commits

Author SHA1 Message Date
dickreckard
1013473d6b keyS!!! 2014-05-27 22:53:19 +02:00
jngrt
0e587e93d7 main.py fixed, working with sending messages for users, in subfoldes 2014-05-23 19:47:04 +02:00
jngrt
60d77071c5 get_messages check if uid folder exists 2014-05-23 19:21:11 +02:00
jngrt
d6549ea19d changing path for received msgs 2014-05-23 18:56:45 +02:00
jngrt
fe28173fa7 trying to fix wget bug 2014-05-23 18:45:45 +02:00
jngrt
2653eb5bb2 trying to get new index to work 2014-05-23 18:11:31 +02:00
jngrt
4732e448b2 changed main.py buildindex for targeted msgs 2014-05-23 17:29:31 +02:00
3 changed files with 91 additions and 23 deletions

88
main.py
View File

@ -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):
"""

View File

@ -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)

View File

@ -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