Merge pull request #2 from rscmbbng/unthread-buildindex

Unthread buildindex
This commit is contained in:
jngrt 2014-11-06 22:02:15 +01:00
commit df281b3fa9
4 changed files with 62 additions and 53 deletions

49
main.py
View File

@ -27,6 +27,8 @@ class Meshenger:
os.mkdir(self.msg_dir) os.mkdir(self.msg_dir)
logger.info('Making message directory') logger.info('Making message directory')
self.init_index()
try: try:
d = threading.Thread(target=self.discover) d = threading.Thread(target=self.discover)
d.daemon = True d.daemon = True
@ -44,12 +46,6 @@ class Meshenger:
c.daemon = True c.daemon = True
c.start() c.start()
b = threading.Thread(target=self.build_index)
b.daemon = True
b.start()
#os.system("python meshenger_clientserve.py")
except (KeyboardInterrupt, SystemExit): except (KeyboardInterrupt, SystemExit):
logger.info('exiting discovery thread') logger.info('exiting discovery thread')
d.join() d.join()
@ -58,6 +54,10 @@ class Meshenger:
n.join() n.join()
c.join() c.join()
sys.exit() sys.exit()
except Exception as e:
logger.warning( 'Main __init__ thread exception: %s', repr(e) )
except:
logger.warning( 'Main __init__ unknown thread exception')
while True: while True:
logger.debug('Entering main loop') logger.debug('Entering main loop')
@ -85,6 +85,7 @@ class Meshenger:
self.get_index(device, nodepath) self.get_index(device, nodepath)
logger.info('downloading messages') logger.info('downloading messages')
self.get_messages(device, nodepath, nodehash) self.get_messages(device, nodepath, nodehash)
self.build_index()
self.node_timestamp(device) self.node_timestamp(device)
time.sleep(5) #free process or ctrl+c time.sleep(5) #free process or ctrl+c
@ -114,7 +115,7 @@ Announce the node's existance to other nodes
Discover other devices by listening to the Meshenger announce port Discover other devices by listening to the Meshenger announce port
""" """
print 'Discovering' logger.info('Discovering')
bufferSize = 1024 # whatever you need? bufferSize = 1024 # whatever you need?
s = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM, socket.IPPROTO_UDP) s = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
@ -154,27 +155,33 @@ Initialize the clientserver
""" """
logger.info('Serving to client') logger.info('Serving to client')
import meshenger_clientserve import meshenger_clientserve
# set a reference to this object
meshenger_clientserve.meshenger = self
meshenger_clientserve.main() meshenger_clientserve.main()
# meshenger_clientserve.build_index_callback = self.build_index
def init_index(self):
"""
Initialize the index. Read from disk or create new.
"""
logger.info('Checking own index for the first time\n')
if not os.path.exists('index'):
with open('index','wb') as index:
index.write('')
self.previous_index = []
else:
self.previous_index = open('index').readlines()
self.build_index()
def build_index(self): def build_index(self):
""" """
Make an index file of all the messages present on the node. 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.
""" """
logger.debug('build_index')
logger.info('Building own index for the first time\n')
if not os.path.exists('index'):
with open('index','wb') as index:
index.write('')
previous_index = []
else:
previous_index = open('index').readlines()
while not self.exitapp:
current_index = os.listdir(self.msg_dir) current_index = os.listdir(self.msg_dir)
if current_index != previous_index: if current_index != self.previous_index:
with open('index', 'wb') as index: with open('index', 'wb') as index:
for message in os.listdir(self.msg_dir): for message in os.listdir(self.msg_dir):
index.write(message) index.write(message)
@ -186,8 +193,8 @@ Save the time of the last update.
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 self.previous_index = current_index
time.sleep(5)
def get_index(self,ip, path): def get_index(self,ip, path):
""" """

View File

@ -13,26 +13,16 @@ import logging, logging.config
logging.config.fileConfig('pylog.conf') logging.config.fileConfig('pylog.conf')
logger = logging.getLogger('meshenger'+'.clientserve') logger = logging.getLogger('meshenger'+'.clientserve')
class ClientServeHandler(SimpleHTTPServer.SimpleHTTPRequestHandler): class ClientServeHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
messageDir = "msg" messageDir = "msg"
def do_GET(self):
""" """
Serve index and messages Serve index and messages
""" """
def do_GET(self):
if self.path == '/': if self.path == '/index' or self.path.startswith( "/"+self.messageDir ):
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
f = os.path.relpath('webapp.html')
# f = os.path.join('/root/meshenger/',"webapp.html")
with open( f, 'r') as the_file:
self.wfile.write(the_file.read())
elif self.path == '/index' or self.path.startswith( "/"+self.messageDir ):
return SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self) return SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)
elif self.path == '/log': elif self.path == '/log':
self.send_response(200) self.send_response(200)
@ -45,17 +35,17 @@ class ClientServeHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
self.send_response(200) #serve the webapp on every url requested self.send_response(200) #serve the webapp on every url requested
self.send_header('Content-type', 'text/html') self.send_header('Content-type', 'text/html')
self.end_headers() self.end_headers()
f = os.path.join( "webapp.html") f = os.path.relpath( 'webapp.html')
with open( f, 'r') as the_file: with open( f, 'r') as the_file:
self.wfile.write(the_file.read()) self.wfile.write(the_file.read())
def do_POST(self):
""" """
Allow clients to post messages Allow clients to post messages
""" """
def do_POST(self):
if self.path == '/send': if self.path == '/send':
logger.info('incoming message from client!')
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'))
@ -66,8 +56,18 @@ class ClientServeHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
self.end_headers() self.end_headers()
self.wfile.write('message created') self.wfile.write('message created')
def writeMessage(self, time, message): #let main rebuild message index
try:
logger.debug('try to call meshenger.build_index: %s', repr(meshenger))
meshenger.build_index()
except:
logger.error('failed to call meshenger.build_index')
pass
def writeMessage(self, time, message):
"""
Write message to disk
"""
f = os.path.join( self.messageDir, time) f = os.path.join( self.messageDir, time)
if os.path.isfile( f ): if os.path.isfile( f ):
return return
@ -77,6 +77,7 @@ class ClientServeHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
class ClientServe(): class ClientServe():
def __init__(self, port): def __init__(self, port):
logger.info('ClientServe.__init__')
server = HTTPServer( ('', port), ClientServeHandler) server = HTTPServer( ('', port), ClientServeHandler)
server.serve_forever() server.serve_forever()

View File

@ -28,10 +28,10 @@ class NodeServeHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
messageDir = "/msg" messageDir = "/msg"
def do_GET(self):
""" """
Serve index and messages Serve index and messages
""" """
def do_GET(self):
if self.path == '/': if self.path == '/':
self.path = "/index" self.path = "/index"
@ -49,6 +49,7 @@ class HTTPServerV6(BaseHTTPServer.HTTPServer):
class NodeServe(): class NodeServe():
def __init__(self, port): def __init__(self, port):
logger.info('NodeServe.__init__')
server = HTTPServerV6(('::', port), NodeServeHandler) server = HTTPServerV6(('::', port), NodeServeHandler)
server.serve_forever() server.serve_forever()

View File

@ -355,7 +355,7 @@ updateOutboxView();
window.setInterval( function(){ window.setInterval( function(){
checkInbox(); checkInbox();
checkOutbox(); checkOutbox();
}, 3000 ); }, 7000 );
</script> </script>