build index successfully untangled

This commit is contained in:
jngrt 2014-11-06 21:34:56 +01:00
parent 0de4a6b878
commit e91905d63b
3 changed files with 17 additions and 19 deletions

View File

@ -162,8 +162,10 @@ 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 # meshenger_clientserve.build_index_callback = self.build_index
def init_index(self): def init_index(self):
logger.info('Building own index for the first time\n') logger.info('Building own index for the first time\n')
@ -174,14 +176,14 @@ Initialize the clientserver
self.previous_index = [] self.previous_index = []
else: else:
self.previous_index = open('index').readlines() 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')
current_index = os.listdir(self.msg_dir) current_index = os.listdir(self.msg_dir)
if current_index != self.previous_index: if current_index != self.previous_index:
with open('index', 'wb') as index: with open('index', 'wb') as index:

View File

@ -14,7 +14,7 @@ logging.config.fileConfig('pylog.conf')
logger = logging.getLogger('meshenger'+'.clientserve') logger = logging.getLogger('meshenger'+'.clientserve')
# this variable is set from main, to be called when new messages available # 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): class ClientServeHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
@ -26,16 +26,7 @@ class ClientServeHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
""" """
def do_GET(self): 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)
@ -48,7 +39,7 @@ 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())
@ -58,7 +49,7 @@ class ClientServeHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
""" """
def do_POST(self): 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'))
@ -70,8 +61,12 @@ class ClientServeHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
self.wfile.write('message created') self.wfile.write('message created')
#let main rebuild message index #let main rebuild message index
if build_index_callback: try:
build_index_callback() 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): def writeMessage(self, time, message):
@ -84,6 +79,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

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