From e91905d63b583f711510724b9f4ffafb2683a3e9 Mon Sep 17 00:00:00 2001 From: jngrt Date: Thu, 6 Nov 2014 21:34:56 +0100 Subject: [PATCH] build index successfully untangled --- main.py | 8 +++++--- meshenger_clientserve.py | 26 +++++++++++--------------- webapp.html | 2 +- 3 files changed, 17 insertions(+), 19 deletions(-) diff --git a/main.py b/main.py index 6499067..2627e0b 100755 --- a/main.py +++ b/main.py @@ -162,8 +162,10 @@ Initialize the clientserver """ logger.info('Serving to client') import meshenger_clientserve + # set a reference to this object + meshenger_clientserve.meshenger = self meshenger_clientserve.main() - meshenger_clientserve.build_index_callback = self.build_index + # meshenger_clientserve.build_index_callback = self.build_index def init_index(self): logger.info('Building own index for the first time\n') @@ -174,14 +176,14 @@ Initialize the clientserver self.previous_index = [] else: self.previous_index = open('index').readlines() - + self.build_index() def build_index(self): """ Make an index file of all the messages present on the node. Save the time of the last update. """ - + logger.debug('build_index') current_index = os.listdir(self.msg_dir) if current_index != self.previous_index: with open('index', 'wb') as index: diff --git a/meshenger_clientserve.py b/meshenger_clientserve.py index 75839cd..c867476 100644 --- a/meshenger_clientserve.py +++ b/meshenger_clientserve.py @@ -14,7 +14,7 @@ logging.config.fileConfig('pylog.conf') logger = logging.getLogger('meshenger'+'.clientserve') # this variable is set from main, to be called when new messages available -build_index_callback = None +# build_index_callback = None class ClientServeHandler(SimpleHTTPServer.SimpleHTTPRequestHandler): @@ -26,16 +26,7 @@ class ClientServeHandler(SimpleHTTPServer.SimpleHTTPRequestHandler): """ def do_GET(self): - if self.path == '/': - 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 ): + if self.path == '/index' or self.path.startswith( "/"+self.messageDir ): return SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self) elif self.path == '/log': self.send_response(200) @@ -48,7 +39,7 @@ class ClientServeHandler(SimpleHTTPServer.SimpleHTTPRequestHandler): self.send_response(200) #serve the webapp on every url requested self.send_header('Content-type', 'text/html') self.end_headers() - f = os.path.join( "webapp.html") + f = os.path.relpath( 'webapp.html') with open( f, 'r') as the_file: self.wfile.write(the_file.read()) @@ -58,7 +49,7 @@ class ClientServeHandler(SimpleHTTPServer.SimpleHTTPRequestHandler): """ def do_POST(self): if self.path == '/send': - + logger.info('incoming message from client!') length = int(self.headers['Content-Length']) post_data = urlparse.parse_qs(self.rfile.read(length).decode('utf-8')) @@ -70,8 +61,12 @@ class ClientServeHandler(SimpleHTTPServer.SimpleHTTPRequestHandler): self.wfile.write('message created') #let main rebuild message index - if build_index_callback: - build_index_callback() + 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): @@ -84,6 +79,7 @@ class ClientServeHandler(SimpleHTTPServer.SimpleHTTPRequestHandler): class ClientServe(): def __init__(self, port): + logger.info('ClientServe.__init__') server = HTTPServer( ('', port), ClientServeHandler) server.serve_forever() diff --git a/webapp.html b/webapp.html index 6a4dbd8..1e267b9 100644 --- a/webapp.html +++ b/webapp.html @@ -355,7 +355,7 @@ updateOutboxView(); window.setInterval( function(){ checkInbox(); checkOutbox(); -}, 3000 ); +}, 7000 );