split node and client serve
This commit is contained in:
parent
73ddf0f806
commit
b2893eaf3c
46
main.py
46
main.py
@ -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:
|
||||||
@ -57,7 +63,7 @@ class Meshenger:
|
|||||||
nodepath = self.ip_to_hash(device) #make a folder for the node (nodes/'hash'/)
|
nodepath = self.ip_to_hash(device) #make a folder for the node (nodes/'hash'/)
|
||||||
nodeupdatepath = os.path.join(self.ip_to_hash(device), 'lastupdate')
|
nodeupdatepath = os.path.join(self.ip_to_hash(device), 'lastupdate')
|
||||||
|
|
||||||
|
|
||||||
print 'Checking age of foreign node index'
|
print 'Checking age of foreign node index'
|
||||||
print self.devices[device], 'Foreign announce timestamp'
|
print self.devices[device], 'Foreign announce timestamp'
|
||||||
foreign_node_update = open(nodeupdatepath).read()
|
foreign_node_update = open(nodeupdatepath).read()
|
||||||
@ -70,9 +76,9 @@ class Meshenger:
|
|||||||
self.node_timestamp(device)
|
self.node_timestamp(device)
|
||||||
print 'downloading messages'
|
print 'downloading messages'
|
||||||
self.get_messages(device, nodepath)
|
self.get_messages(device, nodepath)
|
||||||
|
|
||||||
time.sleep(5) #free process or ctrl+c
|
time.sleep(5) #free process or ctrl+c
|
||||||
|
|
||||||
def node_timestamp(self, ip):
|
def node_timestamp(self, ip):
|
||||||
|
|
||||||
updatepath = os.path.join(self.ip_to_hash(ip), 'lastupdate')
|
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]
|
print 'Known node', result[1][0]
|
||||||
self.devices[result[1][0]] = result[0]
|
self.devices[result[1][0]] = result[0]
|
||||||
#self.devices.append(result[1][0])
|
#self.devices.append(result[1][0])
|
||||||
|
|
||||||
elif result[1][0] not in self.devices and result[1][0] != self.own_ip:
|
elif result[1][0] not in self.devices and result[1][0] != self.own_ip:
|
||||||
#loop for first time
|
#loop for first time
|
||||||
self.devices[result[1][0]] = result[0]
|
self.devices[result[1][0]] = result[0]
|
||||||
@ -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):
|
||||||
"""
|
"""
|
||||||
@ -152,7 +167,7 @@ Save the time of the last update.
|
|||||||
index.write(message)
|
index.write(message)
|
||||||
index.write('\n')
|
index.write('\n')
|
||||||
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: ### misschien is dit overbodig
|
with open('index_last_update', 'wb') as indexupdate: ### misschien is dit overbodig
|
||||||
@ -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
20
meshenger_serve.py → meshenger_clientserve.py
Executable file → Normal 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
45
meshenger_nodeserve.py
Executable 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()
|
Loading…
Reference in New Issue
Block a user