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')
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:

View File

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

View File

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