changed main.py buildindex for targeted msgs

This commit is contained in:
jngrt 2014-05-23 17:29:31 +02:00
parent ddbd5842aa
commit 4732e448b2
3 changed files with 53 additions and 13 deletions

40
main.py
View File

@ -121,7 +121,7 @@ Discover other devices by listening to the Meshenger announce port
node_path = os.path.join(os.path.abspath('nodes'), self.hasj(ip)) node_path = os.path.join(os.path.abspath('nodes'), self.hasj(ip))
if not os.path.exists(node_path) and ip != self.own_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.ip_to_hash_path(ip) #make a folder /nodes/hash
self.devices[ip] = result[0] self.devices[ip] = result[0]
#self.node_timestamp(ip) #make a local copy of the timestamp in /nodes/hash/updatetimestamp #self.node_timestamp(ip) #make a local copy of the timestamp in /nodes/hash/updatetimestamp
@ -157,6 +157,37 @@ Make an index file of all the messages present on the node.
Save the time of the last update. Save the time of the last update.
""" """
index_file = os.path.relpath( 'index' )
previous_index = []
if os.path.exists( index_file ):
previous_index = open( index_file ).read().split()
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' print 'Building own index for the first time\n'
if not os.path.exists('index'): if not os.path.exists('index'):
@ -176,12 +207,12 @@ Save the time of the last update.
self.index_last_update = str(int(time.time())) self.index_last_update = str(int(time.time()))
print 'Index updated:', current_index print 'Index updated:', current_index
with open('index_last_update', 'wb') as indexupdate: with open('index_last_update', 'wb') as indexupdate:
indexupdate.write(self.index_last_update) ### misschien moet dit index_last_update zijn 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)
"""
def get_index(self,ip, path): def get_index(self,ip, path):
""" """
@ -195,11 +226,14 @@ Download the indices from other nodes.
""" """
Get new messages from other node based on it's index file Get new messages from other node based on it's index file
""" """
try: try:
with open(os.path.join(path,'index')) as index: with open(os.path.join(path,'index')) as index:
index = index.read().split('\n') index = index.read().split('\n')
for message in index: for message in index:
messagepath = os.path.join(os.path.abspath(self.msg_dir), message) # messagepath = os.path.join(os.path.abspath(self.msg_dir), message)
messagepath = os.path.join( self.msg_dir, message )
if not os.path.exists(messagepath): if not os.path.exists(messagepath):
print 'downloading', message, 'to', messagepath print 'downloading', message, 'to', messagepath
os.system('wget http://['+ip+'%adhoc0]:'+self.serve_port+'/msg/'+message+' -O '+messagepath) os.system('wget http://['+ip+'%adhoc0]:'+self.serve_port+'/msg/'+message+' -O '+messagepath)

View File

@ -19,15 +19,13 @@ class ClientServeHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
""" """
def do_GET(self): def do_GET(self):
if self.path == '/': if self.path.startswith( '/'+self.messageDir ):
self.send_response(200) parts = self.path.split('/');
self.send_header('Content-type', 'text/html') if len( parts ) == 2:
self.end_headers()
f = os.path.join( "webapp.html")
with open( f, 'r') as the_file:
self.wfile.write(the_file.read())
self.wfile.write('give index')
elif len( parts ) == 3:
self.wfile.write('give msg')
elif self.path == '/index' or self.path.startswith( "/"+self.messageDir ): elif self.path == '/index' or self.path.startswith( "/"+self.messageDir ):
return SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self) return SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)

View File

@ -7,6 +7,12 @@
-perhaps replace the functionality of self.devices with something based on crawling nodes/hash/ -perhaps replace the functionality of self.devices with something based on crawling nodes/hash/
- HTTP calls the client needs:
- /msg/<uid> -> give list of all messages for this uid
- /msg/<uid>/<timestamp>.json -> get message for this timestamp
- /clients -> return list of all known clients (uid's + usernames)
- /getUID -> return uid for client ( based on what? mac? )
# Cryptoshit # Cryptoshit
encrypt / decrypt: encrypt / decrypt:
@ -14,3 +20,5 @@ http://www.davedoesdev.com/a-simple-and-consistent-wrapper-for-javascript-crypto
generating keys: generating keys:
https://bitbucket.org/adrianpasternak/js-rsa-pem/wiki/Home https://bitbucket.org/adrianpasternak/js-rsa-pem/wiki/Home