Browse Source

dennis makes me hot

logging
dickreckard 10 years ago
parent
commit
b55229eee8
  1. 19
      main.py
  2. 68
      webapp.html

19
main.py

@ -1,6 +1,6 @@
#!/usr/bin/python
import socket, os, time, select, urllib, sys, threading
import socket, os, time, select, urllib, sys, threading, json
class Meshenger:
devices = {} #the dictionary of all the nodes this this node has seen
@ -63,9 +63,9 @@ class Meshenger:
print 'found', len(self.devices),'device(s)'
for device in self.devices.keys():
nodepath = self.ip_to_hash_path(device) #make a folder for the node (nodes/'hash'/)
nodeupdatepath = os.path.join(self.ip_to_hash_path(device), 'lastupdate')
nodehash = self.hasj(device)
nodepath = os.path.join(os.path.abspath('nodes'), nodehash)
nodeupdatepath = os.path.join(nodepath, 'lastupdate')
print 'Checking age of foreign node index'
print self.devices[device], 'Foreign announce timestamp'
@ -81,7 +81,7 @@ class Meshenger:
print 'Foreign node"s index is newer, proceed to download index'
self.get_index(device, nodepath)
print 'downloading messages'
self.get_messages(device, nodepath)
self.get_messages(device, nodepath, nodehash)
self.node_timestamp(device)
time.sleep(5) #free process or ctrl+c
@ -194,7 +194,7 @@ Download the indices from other nodes.
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
"""
@ -206,6 +206,12 @@ Get new messages from other node based on it's index file
if not os.path.exists(messagepath):
print 'downloading', message, 'to', 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:
print 'Failed to download messages'
pass
@ -223,6 +229,7 @@ Convert a node's ip into a hash and make a directory to store it's files
return nodepath
def hasj(self, ip):
"""
Convert a node's ip into a hash

68
webapp.html

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

Loading…
Cancel
Save