Merge pull request #2 from rscmbbng/logging

Logging
This commit is contained in:
jngrt 2014-11-06 16:03:50 +01:00
commit 70cbbcdcf0
8 changed files with 138 additions and 63 deletions

1
.gitignore vendored
View File

@ -4,3 +4,4 @@ msg/*
index index
interfaceip6adress interfaceip6adress
nodes/* nodes/*
*.log

View File

@ -32,11 +32,12 @@ sed -i -e "s/option 'interfaces' 'mesh'/option 'interfaces' 'adhoc0'/g" /etc/con
opkg install python git opkg install python git
sleep 1 sleep 1
git clone git://github.com/jngrt/meshenger.git /meshenger git clone ://github.com/rscmbbng/meshenger /root/meshenger
mv uhttpd /etc/config/uhttpd mv uhttpd /etc/config/uhttpd
mv meshenger /etc/init.d/meshenger mv meshenger /etc/init.d/meshenger
/etc/init.d/meshenger enable
echo 'my ip address is:' #klopt nog niet echo 'my ip address is:' #klopt nog niet
ifconfig br-lan | grep 'inet addr' ifconfig br-lan | grep 'inet addr'

4
log/.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
# Ignore everything in this directory
*
# Except this file
!.gitignore

62
main.py
View File

@ -1,6 +1,9 @@
#!/usr/bin/python #!/usr/bin/python
import socket, os, time, select, urllib, sys, threading import socket, os, time, select, urllib, sys, threading, json, logging, logging.config
logging.config.fileConfig('pylog.conf')
logger = logging.getLogger('meshenger'+'.main')
class Meshenger: class Meshenger:
devices = {} #the dictionary of all the nodes this this node has seen devices = {} #the dictionary of all the nodes this this node has seen
@ -22,7 +25,7 @@ class Meshenger:
if not os.path.exists(self.msg_dir): if not os.path.exists(self.msg_dir):
os.mkdir(self.msg_dir) os.mkdir(self.msg_dir)
print 'Making message directory' logger.info('Making message directory')
try: try:
d = threading.Thread(target=self.discover) d = threading.Thread(target=self.discover)
@ -48,7 +51,7 @@ class Meshenger:
#os.system("python meshenger_clientserve.py") #os.system("python meshenger_clientserve.py")
except (KeyboardInterrupt, SystemExit): except (KeyboardInterrupt, SystemExit):
print 'exiting discovery thread' logger.info('exiting discovery thread')
d.join() d.join()
a.join() a.join()
b.join() b.join()
@ -57,31 +60,31 @@ class Meshenger:
sys.exit() sys.exit()
while True: while True:
print 'Entering main loop' logger.debug('Entering main loop')
# #
if len(self.devices) > 0: if len(self.devices) > 0:
print 'found', len(self.devices),'device(s)' logger.info('found %s device(s)', len(self.devices))
for device in self.devices.keys(): for device in self.devices.keys():
nodepath = self.ip_to_hash_path(device) #make a folder for the node (nodes/'hash'/) nodehash = self.hasj(device)
nodeupdatepath = os.path.join(self.ip_to_hash_path(device), 'lastupdate') nodepath = os.path.join(os.path.abspath('nodes'), nodehash)
nodeupdatepath = os.path.join(nodepath, 'lastupdate')
logger.info('Checking age of foreign node index')
print 'Checking age of foreign node index' logger.info('%s Foreign announce timestamp', self.devices[device])
print self.devices[device], 'Foreign announce timestamp'
try: try:
foreign_node_update = open(nodeupdatepath).read() foreign_node_update = open(nodeupdatepath).read()
except: except:
foreign_node_update = 0 #means it was never seen before foreign_node_update = 0 #means it was never seen before
print foreign_node_update, 'Locally stored timestamp for device' logger.info('%s Locally stored timestamp for device', foreign_node_update)
if self.devices[device] > foreign_node_update: if self.devices[device] > foreign_node_update:
print 'Foreign node"s index is newer, proceed to download index' logger.info('Foreign node"s index is newer, proceed to download index')
self.get_index(device, nodepath) self.get_index(device, nodepath)
print 'downloading messages' logger.info('downloading messages')
self.get_messages(device, nodepath) self.get_messages(device, nodepath, nodehash)
self.node_timestamp(device) self.node_timestamp(device)
time.sleep(5) #free process or ctrl+c time.sleep(5) #free process or ctrl+c
@ -99,7 +102,7 @@ class Meshenger:
""" """
Announce the node's existance to other nodes Announce the node's existance to other nodes
""" """
print 'Announcing' logger.info('Announcing')
while not self.exitapp: while not self.exitapp:
sock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM, socket.IPPROTO_UDP) sock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
sock.sendto(self.index_last_update, ("ff02::1", self.announce_port)) sock.sendto(self.index_last_update, ("ff02::1", self.announce_port))
@ -120,7 +123,7 @@ Discover other devices by listening to the Meshenger announce port
while not self.exitapp: while not self.exitapp:
result = select.select([s],[],[])[0][0].recvfrom(bufferSize) result = select.select([s],[],[])[0][0].recvfrom(bufferSize)
ip = result[1][0] ip = result[1][0]
print ip, "*"*45 logger.info('%s %s', ip, "*"*45)
node_path = os.path.join(os.path.abspath('nodes'), self.hasj(ip)) node_path = os.path.join(os.path.abspath('nodes'), self.hasj(ip))
if not os.path.exists(node_path) and ip != self.own_ip: if not os.path.exists(node_path) and ip != self.own_ip:
@ -128,10 +131,10 @@ Discover other devices by listening to the Meshenger announce port
self.ip_to_hash_path(ip) #make a folder /nodes/hash self.ip_to_hash_path(ip) #make a folder /nodes/hash
self.devices[ip] = result[0] self.devices[ip] = result[0]
#self.node_timestamp(ip) #make a local copy of the timestamp in /nodes/hash/updatetimestamp #self.node_timestamp(ip) #make a local copy of the timestamp in /nodes/hash/updatetimestamp
print 'New node', ip logger.info('New node %s', ip)
elif os.path.exists(node_path) and ip != self.own_ip: elif os.path.exists(node_path) and ip != self.own_ip:
print 'Known node', ip logger.info('Known node %s', ip)
self.devices[ip] = result[0] self.devices[ip] = result[0]
@ -141,7 +144,7 @@ Discover other devices by listening to the Meshenger announce port
""" """
Initialize the nodeserver Initialize the nodeserver
""" """
print 'Serving to nodes' logger.info('Serving to nodes')
import meshenger_nodeserve import meshenger_nodeserve
meshenger_nodeserve.main() meshenger_nodeserve.main()
@ -149,7 +152,7 @@ Initialize the nodeserver
""" """
Initialize the clientserver Initialize the clientserver
""" """
print 'Serving to client' logger.info('Serving to client')
import meshenger_clientserve import meshenger_clientserve
meshenger_clientserve.main() meshenger_clientserve.main()
@ -160,7 +163,7 @@ 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.
""" """
print 'Building own index for the first time\n' logger.info('Building own index for the first time\n')
if not os.path.exists('index'): if not os.path.exists('index'):
with open('index','wb') as index: with open('index','wb') as index:
@ -178,7 +181,7 @@ Save the time of the last update.
index.write('\n') index.write('\n')
self.index_last_update = str(int(time.time())) self.index_last_update = str(int(time.time()))
print 'Index updated:', current_index logger.info('Index updated: %s', current_index)
with open('index_last_update', 'wb') as indexupdate: with open('index_last_update', 'wb') as indexupdate:
indexupdate.write(self.index_last_update) ### misschien moet dit index_last_update zijn indexupdate.write(self.index_last_update) ### misschien moet dit index_last_update zijn
@ -194,7 +197,7 @@ Download the indices from other nodes.
os.system('wget http://['+ip+'%adhoc0]:'+self.serve_port+'/index -O '+os.path.join(path,'index')) os.system('wget http://['+ip+'%adhoc0]:'+self.serve_port+'/index -O '+os.path.join(path,'index'))
def get_messages(self, ip, path): def get_messages(self, ip, path, hash):
""" """
Get new messages from other node based on it's index file Get new messages from other node based on it's index file
""" """
@ -204,10 +207,16 @@ Get new messages from other node based on it's index file
for message in index: for message in index:
messagepath = os.path.join(os.path.abspath(self.msg_dir), message) messagepath = os.path.join(os.path.abspath(self.msg_dir), message)
if not os.path.exists(messagepath): if not os.path.exists(messagepath):
print 'downloading', message, 'to', messagepath logger.info('downloading %s to %s', message, messagepath)
os.system('wget http://['+ip+'%adhoc0]:'+self.serve_port+'/msg/'+message+' -O '+messagepath) os.system('wget http://['+ip+'%adhoc0]:'+self.serve_port+'/msg/'+message+' -O '+messagepath)
with open(messagepath, 'r+') as f:
data=json.load(f)
data['hops']=str(int(data['hops'])+1)
data['node']=hash
f.seek(0)
json.dump(data, f)
except: except:
print 'Failed to download messages' logger.info('Failed to download messages')
pass pass
def ip_to_hash_path(self, ip): def ip_to_hash_path(self, ip):
@ -223,6 +232,7 @@ Convert a node's ip into a hash and make a directory to store it's files
return nodepath return nodepath
def hasj(self, ip): def hasj(self, ip):
""" """
Convert a node's ip into a hash Convert a node's ip into a hash
@ -243,7 +253,7 @@ Hack to adhoc0's inet6 adress
if __name__ == "__main__": if __name__ == "__main__":
print "test" logger.info("starting main...")
try: try:
meshenger = Meshenger() meshenger = Meshenger()
except (KeyboardInterrupt, SystemExit): except (KeyboardInterrupt, SystemExit):

View File

@ -8,6 +8,10 @@ from BaseHTTPServer import HTTPServer
import SimpleHTTPServer import SimpleHTTPServer
import urlparse import urlparse
import unicodedata import unicodedata
import logging, logging.config
logging.config.fileConfig('pylog.conf')
logger = logging.getLogger('meshenger'+'.clientserve')
class ClientServeHandler(SimpleHTTPServer.SimpleHTTPRequestHandler): class ClientServeHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
@ -23,14 +27,20 @@ class ClientServeHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
self.send_response(200) self.send_response(200)
self.send_header('Content-type', 'text/html') self.send_header('Content-type', 'text/html')
self.end_headers() self.end_headers()
f = os.path.relpath('webapp.html')
f = os.path.join('/root/meshenger/',"webapp.html") # f = os.path.join('/root/meshenger/',"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())
elif self.path == '/index' or self.path.startswith( "/"+self.messageDir ): 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':
self.send_response(200)
self.send_header('Content-type', 'text-html')
self.end_headers()
f = os.path.relpath('log/meshenger.log')
with open( f, 'r') as the_file:
self.wfile.write(the_file.read())
else: else:
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')

View File

@ -7,6 +7,11 @@ import socket
import BaseHTTPServer import BaseHTTPServer
import SimpleHTTPServer import SimpleHTTPServer
import urlparse import urlparse
import logging
import logging.config
logging.config.fileConfig('pylog.conf')
logger = logging.getLogger('meshenger'+'.nodeserve')
#GOTTMITTUNS #GOTTMITTUNS

34
pylog.conf Normal file
View File

@ -0,0 +1,34 @@
[loggers]
keys=root,meshenger
[handlers]
keys=consoleHandler, fileHandler
[formatters]
keys=simpleFormatter
[logger_root]
level=DEBUG
handlers=consoleHandler
[logger_meshenger]
level=DEBUG
handlers=consoleHandler, fileHandler
qualname=meshenger
propagate=0
[handler_fileHandler]
class=handlers.RotatingFileHandler
level=INFO
args=('log/meshenger.log','a','maxBytes=10000','backupCount=5')
formatter=simpleFormatter
[handler_consoleHandler]
class=StreamHandler
level=DEBUG
formatter=simpleFormatter
args=(sys.stdout,)
[formatter_simpleFormatter]
format=%(asctime)s - %(name)s - %(levelname)s - %(message)s
datefmt=

View File

@ -110,7 +110,7 @@
//background:-moz-linear-gradient(top, #fff, #eee); //background:-moz-linear-gradient(top, #fff, #eee);
} }
#outbox{ #outbox{
display:hidden; display:none;
} }
#header{ #header{
width:100%; width:100%;
@ -118,6 +118,9 @@
} }
#name{ #name{
}
.hops .node{
// display:hidden;
} }
</style> </style>
@ -142,8 +145,6 @@
<option value="dateSend">Sort by Date Send</option> <option value="dateSend">Sort by Date Send</option>
</select> </select>
<ul id="inbox"></ul> <ul id="inbox"></ul>
<!--<h2>outbox</h2>--> <!--<h2>outbox</h2>-->
@ -152,6 +153,7 @@
<script type="text/javascript"> <script type="text/javascript">
localStorage.clear();
/* /*
* OUTBOX STUFF * OUTBOX STUFF
*/ */
@ -165,8 +167,18 @@ document.getElementById( 'send' ).onclick = function() {
var namm= document.getElementById('name').value; var namm= document.getElementById('name').value;
} }
var mess = document.getElementById('message').value.replace(/\r?\n/g, "<br />"); var mess = document.getElementById('message').value.replace(/\r?\n/g, "<br />");
outStr += new Date().getTime() + ' ' + namm + '///' + mess + '\n'; var newMsgs ={};
localStorage.setItem( 'outbox', outStr ); var ddata= new Date().getTime();
var contento = {
"time" : ddata,
"message" : mess,
"name" : namm,
"node" : "local",
"hops" : "0"
}
newMsgs.message = contento;
localStorage.setItem( 'outbox', JSON.stringify(newMsgs) );
updateOutboxView(); updateOutboxView();
checkOutbox(); checkOutbox();
document.getElementById('message').value = ''; document.getElementById('message').value = '';
@ -181,8 +193,10 @@ function checkOutbox() {
if ( lines[i].length === 0 ) { if ( lines[i].length === 0 ) {
continue; continue;
} }
var ts = lines[ i ].substr( 0, lines[ i ].indexOf( ' ' )); var obj = JSON.parse(lines[i]);
var msg = lines[ i ].substr( lines[ i ].indexOf( ' ' )); var ts = obj.message.time;
delete obj.message.time;
var msg = JSON.stringify(obj.message);
sendMessage( ts, msg ); sendMessage( ts, msg );
} }
} }
@ -208,7 +222,8 @@ function removeOutboxItem( timestamp ) {
var outStr = localStorage.getItem( 'outbox' ) || ''; var outStr = localStorage.getItem( 'outbox' ) || '';
var lines = outStr.split( /\n/ ); var lines = outStr.split( /\n/ );
for ( var i in lines ) { for ( var i in lines ) {
var ts = lines[ i ].substr( 0, lines[ i ].indexOf( ' ' )); var obj = JSON.parse(lines[i]);
var ts = obj.message.time;
if ( ts === timestamp ) { if ( ts === timestamp ) {
lines.splice( i, 1 ); lines.splice( i, 1 );
break; break;
@ -226,30 +241,21 @@ function updateOutboxView() {
if ( lines[ i ].length === 0 ) { if ( lines[ i ].length === 0 ) {
continue; continue;
} }
var ts = lines[ i ].substr( 0, lines[ i ].indexOf( ' ' )); var obj = JSON.parse(lines[i]);
var msg = lines[ i ].substr( lines[ i ].indexOf( ' ' )); var ts = obj.message.time;
delete obj.message.time;
var msg = JSON.stringify(obj.message);
contentString += '<li><b>' + ts + ' </b>' + msg + '</li>'; contentString += '<li><b>' + ts + ' </b>' + msg + '</li>';
} }
document.getElementById( 'outbox' ).innerHTML = contentString; document.getElementById( 'outbox' ).innerHTML = contentString;
} }
function doStuffForEach( entriesString, stuffFunction ) {
if ( ! entriesString ) {
return;
}
var lines = entriesString.split( /\n/ );
for ( var i in lines ) {
var ts = lines[ i ].substr( 0, lines[ i ].indexOf( ' ' ));
var msg = lines[ i ].substr( lines[ i ].indexOf( ' ' ) + 1 );
stuffFunction( ts, msg );
}
}
/* /*
* INBOX STUFF * INBOX STUFF
*/ */
function updateInboxView() { function updateInboxView() {
var localStorageArray = new Array(); var localStorageArray = new Array();
var contentString = ''; var contentString = '';
@ -257,15 +263,19 @@ function updateInboxView() {
if (localStorage.length>0) { if (localStorage.length>0) {
for (i=0;i<localStorage.length;i++){ for (i=0;i<localStorage.length;i++){
element=localStorage.getItem(localStorage.key(i));
element=localStorage.getItem(localStorage.key(i)) if ( localStorage.key(i).length < 10 || element === 'outbox' ) {
continue;
elesplit=element.split('///');
if ( localStorage.key(i).length === 0 || element === 'outbox' ) {
continue;
} }
localStorageArray[i] = { time:localStorage.key(i), user:elesplit[0], message:elesplit[1] }; // alert(element);
try {
elementj = JSON.parse(element);
} catch (e) {
continue;
}
localStorageArray[i] = { time:localStorage.key(i), user:elementj.name, message:elementj.message, node:elementj.node, hops:elementj.hops };
} }
} }
orderStorage = localStorageArray.sort(function(a,b) { return b.time - a.time } ); orderStorage = localStorageArray.sort(function(a,b) { return b.time - a.time } );
@ -278,7 +288,7 @@ function updateInboxView() {
var date = new Date(parseInt(orderStorage[i].time)); var date = new Date(parseInt(orderStorage[i].time));
// date.setHours(date.getHours() + 2); // date.setHours(date.getHours() + 2);
var datereadable = date.getDate()+"/"+(date.getMonth()+1)+"/"+date.getFullYear()+" "+date.getHours()+":"+date.getMinutes(); var datereadable = date.getDate()+"/"+(date.getMonth()+1)+"/"+date.getFullYear()+" "+date.getHours()+":"+date.getMinutes();
contentString += '<li><b>' + datereadable + ' </b>' + ' <i>'+ orderStorage[i].user +'</i><br/> '+orderStorage[i].message+'</li>'; contentString += '<li><b>' + datereadable + ' </b>' + ' <i>'+ orderStorage[i].user +'</i><br/> '+orderStorage[i].message+' <span class="node '+orderStorage[i].node+'">'+orderStorage[i].node+'</span> <span class="hops '+orderStorage[i].hops+'">'+orderStorage[i].hops+'</span></li>';
} }
document.getElementById( 'inbox' ).innerHTML = contentString; document.getElementById( 'inbox' ).innerHTML = contentString;
} }