From b2893eaf3cdc925472ed66aed98911d50963a104 Mon Sep 17 00:00:00 2001 From: jngrt Date: Mon, 28 Apr 2014 22:44:59 +0200 Subject: [PATCH] split node and client serve --- main.py | 46 ++++++++++++------- ...enger_serve.py => meshenger_clientserve.py | 20 ++++---- meshenger_nodeserve.py | 45 ++++++++++++++++++ 3 files changed, 84 insertions(+), 27 deletions(-) rename meshenger_serve.py => meshenger_clientserve.py (73%) mode change 100755 => 100644 create mode 100755 meshenger_nodeserve.py diff --git a/main.py b/main.py index 9633da3..6394253 100755 --- a/main.py +++ b/main.py @@ -11,7 +11,6 @@ class Meshenger: exitapp = False #to kill all threads on index_last_update = str(time.time()) - def __init__(self): os.system("echo 1 >> /proc/sys/net/ipv6/conf/br-lan/disable_ipv6") @@ -31,20 +30,27 @@ class Meshenger: a.daemon = True a.start() - s = threading.Thread(target=self.serve) - s.daemon = True - s.start() + n = threading.Thread(target=self.nodeserve) + n.daemon = True + n.start() + + c = threading.Thread(target=self.clientserve) + c.daemon = True + c.start() b = threading.Thread(target=self.build_index) b.daemon = True b.start() + #os.system("python meshenger_clientserve.py") + except (KeyboardInterrupt, SystemExit): print 'exiting discovery thread' d.join() a.join() b.join() - s.join() + n.join() + c.join() sys.exit() while True: @@ -57,7 +63,7 @@ class Meshenger: nodepath = self.ip_to_hash(device) #make a folder for the node (nodes/'hash'/) nodeupdatepath = os.path.join(self.ip_to_hash(device), 'lastupdate') - + print 'Checking age of foreign node index' print self.devices[device], 'Foreign announce timestamp' foreign_node_update = open(nodeupdatepath).read() @@ -70,9 +76,9 @@ class Meshenger: self.node_timestamp(device) print 'downloading messages' self.get_messages(device, nodepath) - + time.sleep(5) #free process or ctrl+c - + def node_timestamp(self, ip): updatepath = os.path.join(self.ip_to_hash(ip), 'lastupdate') @@ -111,7 +117,7 @@ Discover other devices by listening to the Meshenger announce port print 'Known node', result[1][0] self.devices[result[1][0]] = result[0] #self.devices.append(result[1][0]) - + elif result[1][0] not in self.devices and result[1][0] != self.own_ip: #loop for first time self.devices[result[1][0]] = result[0] @@ -122,13 +128,22 @@ Discover other devices by listening to the Meshenger announce port time.sleep(1) - def serve(self): + def nodeserve(self): """ -Initialize the server +Initialize the nodeserver """ - print 'Serving' - import meshenger_serve - meshenger_serve.main() + print 'Serving to nodes' + import meshenger_nodeserve + meshenger_nodeserve.main() + + def clientserve(self): + """ +Initialize the clientserver +""" + print 'Serving to client' + import meshenger_clientserve + meshenger_clientserve.main() + def build_index(self): """ @@ -152,7 +167,7 @@ Save the time of the last update. index.write(message) index.write('\n') self.index_last_update = str(int(time.time())) - + print 'Index updated:', current_index with open('index_last_update', 'wb') as indexupdate: ### misschien is dit overbodig @@ -223,4 +238,3 @@ if __name__ == "__main__": except (KeyboardInterrupt, SystemExit): exitapp = True raise - diff --git a/meshenger_serve.py b/meshenger_clientserve.py old mode 100755 new mode 100644 similarity index 73% rename from meshenger_serve.py rename to meshenger_clientserve.py index 52ed0f1..21190e7 --- a/meshenger_serve.py +++ b/meshenger_clientserve.py @@ -7,11 +7,12 @@ import socket from BaseHTTPServer import HTTPServer import SimpleHTTPServer import urlparse +import unicodedata -class MeshengerHandler(SimpleHTTPServer.SimpleHTTPRequestHandler): +class ClientServeHandler(SimpleHTTPServer.SimpleHTTPRequestHandler): - messageDir = "/msg" + messageDir = "msg" """ Serve index and messages @@ -38,7 +39,8 @@ class MeshengerHandler(SimpleHTTPServer.SimpleHTTPRequestHandler): length = int(self.headers['Content-Length']) post_data = urlparse.parse_qs(self.rfile.read(length).decode('utf-8')) - self.writeMessage( post_data["time"][0], post_data["message"][0]) + + self.writeMessage( post_data["time"][0].encode('ascii','ignore'), post_data["message"][0]) self.send_response(200) self.send_header('Content-type', 'text/html') @@ -47,25 +49,21 @@ class MeshengerHandler(SimpleHTTPServer.SimpleHTTPRequestHandler): def writeMessage(self, time, message): - f = os.path.join( self.message, time) + f = os.path.join( self.messageDir, time) if os.path.isfile( f ): return with open( f, 'a') as the_file: the_file.write(message) -class HTTPServerV6(HTTPServer): - address_family = socket.AF_INET6 - -class MeshengerServe(): +class ClientServe(): def __init__(self, port): - server = HTTPServerV6(('::', port), MeshengerHandler) + server = HTTPServer( ('', port), ClientServeHandler) server.serve_forever() def main(): - #clientServe = MeshengerServe(80) - nodeServe = MeshengerServe(13338) + clientServe = ClientServe(80) if __name__ == '__main__': main() diff --git a/meshenger_nodeserve.py b/meshenger_nodeserve.py new file mode 100755 index 0000000..4e78c20 --- /dev/null +++ b/meshenger_nodeserve.py @@ -0,0 +1,45 @@ +#!/usr/bin/python + +import logging +import cgi +import os +import socket +from BaseHTTPServer import HTTPServer +import SimpleHTTPServer +import urlparse + + +class NodeServeHandler(SimpleHTTPServer.SimpleHTTPRequestHandler): + + messageDir = "/msg" + + """ + Serve index and messages + """ + def do_GET(self): + if self.path == '/': + self.path = "/index" + + if self.path == '/index' or self.path.startswith( self.messageDir ): + return SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self) + + else: + self.send_response(404) + self.send_header('Content-type', 'text/html') + self.end_headers() + self.wfile.write('404 - Not Found') + +class HTTPServerV6(HTTPServer): + address_family = socket.AF_INET6 + +class NodeServe(): + def __init__(self, port): + server = HTTPServerV6(('::', port), NodeServeHandler) + server.serve_forever() + + +def main(): + nodeServe = NodeServe(13338) + +if __name__ == '__main__': + main()