split node and client serve

This commit is contained in:
jngrt 2014-04-28 22:44:59 +02:00
parent 73ddf0f806
commit b2893eaf3c
3 changed files with 84 additions and 27 deletions

36
main.py
View File

@ -11,7 +11,6 @@ class Meshenger:
exitapp = False #to kill all threads on exitapp = False #to kill all threads on
index_last_update = str(time.time()) index_last_update = str(time.time())
def __init__(self): def __init__(self):
os.system("echo 1 >> /proc/sys/net/ipv6/conf/br-lan/disable_ipv6") os.system("echo 1 >> /proc/sys/net/ipv6/conf/br-lan/disable_ipv6")
@ -31,20 +30,27 @@ class Meshenger:
a.daemon = True a.daemon = True
a.start() a.start()
s = threading.Thread(target=self.serve) n = threading.Thread(target=self.nodeserve)
s.daemon = True n.daemon = True
s.start() n.start()
c = threading.Thread(target=self.clientserve)
c.daemon = True
c.start()
b = threading.Thread(target=self.build_index) b = threading.Thread(target=self.build_index)
b.daemon = True b.daemon = True
b.start() b.start()
#os.system("python meshenger_clientserve.py")
except (KeyboardInterrupt, SystemExit): except (KeyboardInterrupt, SystemExit):
print 'exiting discovery thread' print 'exiting discovery thread'
d.join() d.join()
a.join() a.join()
b.join() b.join()
s.join() n.join()
c.join()
sys.exit() sys.exit()
while True: while True:
@ -122,13 +128,22 @@ Discover other devices by listening to the Meshenger announce port
time.sleep(1) time.sleep(1)
def serve(self): def nodeserve(self):
""" """
Initialize the server Initialize the nodeserver
""" """
print 'Serving' print 'Serving to nodes'
import meshenger_serve import meshenger_nodeserve
meshenger_serve.main() meshenger_nodeserve.main()
def clientserve(self):
"""
Initialize the clientserver
"""
print 'Serving to client'
import meshenger_clientserve
meshenger_clientserve.main()
def build_index(self): def build_index(self):
""" """
@ -223,4 +238,3 @@ if __name__ == "__main__":
except (KeyboardInterrupt, SystemExit): except (KeyboardInterrupt, SystemExit):
exitapp = True exitapp = True
raise raise

20
meshenger_serve.py → meshenger_clientserve.py Executable file → Normal file
View File

@ -7,11 +7,12 @@ import socket
from BaseHTTPServer import HTTPServer from BaseHTTPServer import HTTPServer
import SimpleHTTPServer import SimpleHTTPServer
import urlparse import urlparse
import unicodedata
class MeshengerHandler(SimpleHTTPServer.SimpleHTTPRequestHandler): class ClientServeHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
messageDir = "/msg" messageDir = "msg"
""" """
Serve index and messages Serve index and messages
@ -38,7 +39,8 @@ class MeshengerHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
length = int(self.headers['Content-Length']) length = int(self.headers['Content-Length'])
post_data = urlparse.parse_qs(self.rfile.read(length).decode('utf-8')) 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_response(200)
self.send_header('Content-type', 'text/html') self.send_header('Content-type', 'text/html')
@ -47,25 +49,21 @@ class MeshengerHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
def writeMessage(self, time, message): def writeMessage(self, time, message):
f = os.path.join( self.message, time) f = os.path.join( self.messageDir, time)
if os.path.isfile( f ): if os.path.isfile( f ):
return return
with open( f, 'a') as the_file: with open( f, 'a') as the_file:
the_file.write(message) the_file.write(message)
class HTTPServerV6(HTTPServer): class ClientServe():
address_family = socket.AF_INET6
class MeshengerServe():
def __init__(self, port): def __init__(self, port):
server = HTTPServerV6(('::', port), MeshengerHandler) server = HTTPServer( ('', port), ClientServeHandler)
server.serve_forever() server.serve_forever()
def main(): def main():
#clientServe = MeshengerServe(80) clientServe = ClientServe(80)
nodeServe = MeshengerServe(13338)
if __name__ == '__main__': if __name__ == '__main__':
main() main()

45
meshenger_nodeserve.py Executable file
View File

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