Merge pull request #2 from rscmbbng/unthread-buildindex
Unthread buildindex
This commit is contained in:
commit
df281b3fa9
49
main.py
49
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,12 +46,6 @@ class Meshenger:
|
||||
c.daemon = True
|
||||
c.start()
|
||||
|
||||
b = threading.Thread(target=self.build_index)
|
||||
b.daemon = True
|
||||
b.start()
|
||||
|
||||
#os.system("python meshenger_clientserve.py")
|
||||
|
||||
except (KeyboardInterrupt, SystemExit):
|
||||
logger.info('exiting discovery thread')
|
||||
d.join()
|
||||
@ -58,6 +54,10 @@ class Meshenger:
|
||||
n.join()
|
||||
c.join()
|
||||
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:
|
||||
logger.debug('Entering main loop')
|
||||
@ -85,6 +85,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 +115,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)
|
||||
@ -154,27 +155,33 @@ 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
|
||||
|
||||
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):
|
||||
"""
|
||||
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()
|
||||
|
||||
while not self.exitapp:
|
||||
logger.debug('build_index')
|
||||
current_index = os.listdir(self.msg_dir)
|
||||
if current_index != previous_index:
|
||||
if current_index != self.previous_index:
|
||||
with open('index', 'wb') as index:
|
||||
for message in os.listdir(self.msg_dir):
|
||||
index.write(message)
|
||||
@ -186,8 +193,8 @@ Save the time of the last update.
|
||||
with open('index_last_update', 'wb') as indexupdate:
|
||||
indexupdate.write(self.index_last_update) ### misschien moet dit index_last_update zijn
|
||||
|
||||
previous_index = current_index
|
||||
time.sleep(5)
|
||||
self.previous_index = current_index
|
||||
|
||||
|
||||
def get_index(self,ip, path):
|
||||
"""
|
||||
|
@ -13,26 +13,16 @@ import logging, logging.config
|
||||
logging.config.fileConfig('pylog.conf')
|
||||
logger = logging.getLogger('meshenger'+'.clientserve')
|
||||
|
||||
|
||||
class ClientServeHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
|
||||
|
||||
messageDir = "msg"
|
||||
|
||||
"""
|
||||
Serve index and messages
|
||||
"""
|
||||
def do_GET(self):
|
||||
"""
|
||||
Serve index and messages
|
||||
"""
|
||||
|
||||
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)
|
||||
@ -45,17 +35,17 @@ 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())
|
||||
|
||||
|
||||
"""
|
||||
Allow clients to post messages
|
||||
"""
|
||||
def do_POST(self):
|
||||
"""
|
||||
Allow clients to post messages
|
||||
"""
|
||||
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'))
|
||||
|
||||
@ -66,8 +56,18 @@ class ClientServeHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
|
||||
self.end_headers()
|
||||
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)
|
||||
if os.path.isfile( f ):
|
||||
return
|
||||
@ -77,6 +77,7 @@ class ClientServeHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
|
||||
|
||||
class ClientServe():
|
||||
def __init__(self, port):
|
||||
logger.info('ClientServe.__init__')
|
||||
server = HTTPServer( ('', port), ClientServeHandler)
|
||||
server.serve_forever()
|
||||
|
||||
|
@ -28,10 +28,10 @@ class NodeServeHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
|
||||
|
||||
messageDir = "/msg"
|
||||
|
||||
"""
|
||||
Serve index and messages
|
||||
"""
|
||||
def do_GET(self):
|
||||
"""
|
||||
Serve index and messages
|
||||
"""
|
||||
if self.path == '/':
|
||||
self.path = "/index"
|
||||
|
||||
@ -49,6 +49,7 @@ class HTTPServerV6(BaseHTTPServer.HTTPServer):
|
||||
|
||||
class NodeServe():
|
||||
def __init__(self, port):
|
||||
logger.info('NodeServe.__init__')
|
||||
server = HTTPServerV6(('::', port), NodeServeHandler)
|
||||
server.serve_forever()
|
||||
|
||||
|
@ -355,7 +355,7 @@ updateOutboxView();
|
||||
window.setInterval( function(){
|
||||
checkInbox();
|
||||
checkOutbox();
|
||||
}, 3000 );
|
||||
}, 7000 );
|
||||
|
||||
</script>
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user