Browse Source

removed build_index thread, only called when update

pull/2/head
jngrt 10 years ago
parent
commit
e336ba209e
  1. 59
      main.py
  2. 13
      meshenger_clientserve.py

59
main.py

@ -27,6 +27,8 @@ class Meshenger:
os.mkdir(self.msg_dir)
logger.info('Making message directory')
self.init_index()
try:
d = threading.Thread(target=self.discover)
d.daemon = True
@ -44,9 +46,9 @@ class Meshenger:
c.daemon = True
c.start()
b = threading.Thread(target=self.build_index)
b.daemon = True
b.start()
#b = threading.Thread(target=self.build_index)
#b.daemon = True
#b.start()
#os.system("python meshenger_clientserve.py")
@ -58,6 +60,11 @@ class Meshenger:
n.join()
c.join()
sys.exit()
except Exception as e:
#catch all other exceptions
logger.warning( 'Main __init__ thread exception: %s', e.message )
except:
logger.warning( 'Main __init__ unknown thread exception')
while True:
logger.debug('Entering main loop')
@ -85,6 +92,7 @@ class Meshenger:
self.get_index(device, nodepath)
logger.info('downloading messages')
self.get_messages(device, nodepath, nodehash)
self.build_index()
self.node_timestamp(device)
time.sleep(5) #free process or ctrl+c
@ -114,7 +122,7 @@ Announce the node's existance to other nodes
Discover other devices by listening to the Meshenger announce port
"""
print 'Discovering'
logger.info('Discovering')
bufferSize = 1024 # whatever you need?
s = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
@ -155,6 +163,17 @@ Initialize the clientserver
logger.info('Serving to client')
import meshenger_clientserve
meshenger_clientserve.main()
meshenger_clientserve.build_index_callback = self.build_index
def init_index(self):
logger.info('Building 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()
def build_index(self):
@ -163,31 +182,21 @@ Make an index file of all the messages present on the node.
Save the time of the last update.
"""
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()
current_index = os.listdir(self.msg_dir)
if current_index != self.previous_index:
with open('index', 'wb') as index:
for message in os.listdir(self.msg_dir):
index.write(message)
index.write('\n')
self.index_last_update = str(int(time.time()))
while not self.exitapp:
current_index = os.listdir(self.msg_dir)
if current_index != previous_index:
with open('index', 'wb') as index:
for message in os.listdir(self.msg_dir):
index.write(message)
index.write('\n')
self.index_last_update = str(int(time.time()))
logger.info('Index updated: %s', current_index)
logger.info('Index updated: %s', current_index)
with open('index_last_update', 'wb') as indexupdate:
indexupdate.write(self.index_last_update) ### misschien moet dit index_last_update zijn
with open('index_last_update', 'wb') as indexupdate:
indexupdate.write(self.index_last_update) ### misschien moet dit index_last_update zijn
self.previous_index = current_index
previous_index = current_index
time.sleep(5)
def get_index(self,ip, path):
"""

13
meshenger_clientserve.py

@ -13,6 +13,9 @@ import logging, logging.config
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
class ClientServeHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
@ -66,6 +69,10 @@ class ClientServeHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
self.end_headers()
self.wfile.write('message created')
#let main rebuild message index
if build_index_callback:
build_index_callback()
def writeMessage(self, time, message):
f = os.path.join( self.messageDir, time)
@ -76,13 +83,13 @@ class ClientServeHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
class ClientServe():
def __init__(self, port):
def __init__(self, port, build_index_callback):
server = HTTPServer( ('', port), ClientServeHandler)
server.serve_forever()
def main():
clientServe = ClientServe(80)
def main( build_index_callback ):
clientServe = ClientServe(80, build_index_callback)
if __name__ == '__main__':
main()

Loading…
Cancel
Save