built alias/nickname feature for nodes, in the process I broke the message color system.. can be fixed in main.js line 170

This commit is contained in:
Roel 2015-05-19 14:26:48 +02:00
parent 5e0fd9c935
commit d6f081f8b0
4 changed files with 51 additions and 8 deletions

26
main.py
View File

@ -31,6 +31,7 @@ class Meshenger:
logger.info('Making message directory') logger.info('Making message directory')
self.init_index() self.init_index()
self.make_alias()
try: try:
d = threading.Thread(target=self.discover) d = threading.Thread(target=self.discover)
@ -70,7 +71,7 @@ class Meshenger:
for device in self.devices.keys(): for device in self.devices.keys():
nodehash = self.hasj(device) nodehash = self.hasj(device)
nodepath = os.path.join(os.path.abspath('nodes'), nodehash) nodepath = os.path.join(os.path.abspath('nodes'), nodehash)
nodeupdatepath = os.path.join(nodepath, 'lastupdate') nodeupdatepath = os.path.join(nodepath, 'lastupdate')
logger.info('Checking age of foreign node index') logger.info('Checking age of foreign node index')
@ -179,9 +180,9 @@ Initialize the index. Read from disk or create new.
def build_index(self): def build_index(self):
""" """
Make an index file of all the messages present on the node. 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.
""" """
logger.debug('build_index') logger.debug('build_index')
current_index = os.listdir(self.msg_dir) current_index = os.listdir(self.msg_dir)
if current_index != self.previous_index: if current_index != self.previous_index:
@ -198,11 +199,24 @@ Save the time of the last update.
self.previous_index = current_index self.previous_index = current_index
def make_alias(self):
"""
See if the node already has an alias (nickname) if not just use the IP-Hash
"""
if not os.path.exists('alias'):
with open('alias','wb') as alias:
self.alias = self.own_hash
alias.write(self.own_hash)
else:
self.alias = open('alias','rb').read().replace('\n','') #should we replace the newline?
pass
logger.debug('Alias is "'+self.alias+'"')
def get_index(self,ip, path): def get_index(self,ip, path):
""" """
Download the indices from other nodes. 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'))

View File

@ -32,6 +32,15 @@ Serve index and messages
self.wfile.write(meshenger.own_hash) self.wfile.write(meshenger.own_hash)
else: else:
self.send_error(404,'Id not yet available') self.send_error(404,'Id not yet available')
elif self.path == '/alias':
if meshenger and meshenger.alias:
self.send_response(200)
self.send_header('Content-type', 'text-html')
self.end_headers()
self.wfile.write(meshenger.alias)
else:
self.send_error(404,'Alias not yet available')
elif self.path == '/log': elif self.path == '/log':
self.send_response(200) self.send_response(200)
self.send_header('Content-type', 'text-html') self.send_header('Content-type', 'text-html')

View File

@ -38,6 +38,10 @@ Serve index and messages
if 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) return SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)
if self.path == '/alias' or self.path.startswith( self.messageDir ):
return SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)
else: else:
self.send_response(404) self.send_response(404)
self.send_header('Content-type', 'text/html') self.send_header('Content-type', 'text/html')

View File

@ -2,7 +2,7 @@ localStorage.clear();
//These need to be obtained from the node //These need to be obtained from the node
var ownId, ownColor; var ownId, ownColor, ownAlias;
/* /*
* OUTBOX STUFF * OUTBOX STUFF
@ -19,11 +19,12 @@ document.getElementById('message-form').onsubmit = function(){
var mess = document.getElementById('message').value.replace(/\r?\n/g, "<br />"); var mess = document.getElementById('message').value.replace(/\r?\n/g, "<br />");
var newMsgs = {}; var newMsgs = {};
var ddata = new Date().getTime(); var ddata = new Date().getTime();
var alias = ownAlias //to do: build a check to see if ownAlias == ownId, if so, alias should become 'local'
var contento = { var contento = {
"time" : ddata, "time" : ddata,
"message" : mess, "message" : mess,
"name" : namm, "name" : namm,
"node" : "local", "node" : alias,
"hops" : "0" "hops" : "0"
} }
newMsgs.message = contento; newMsgs.message = contento;
@ -233,6 +234,7 @@ function downloadMessage(filename) {
xhr.open( "GET", 'msg/'+filename, true); xhr.open( "GET", 'msg/'+filename, true);
xhr.send(); xhr.send();
} }
function checkInbox() { function checkInbox() {
var xhr = new XMLHttpRequest(); var xhr = new XMLHttpRequest();
@ -256,6 +258,17 @@ function getOwnId() {
xhr.send(); xhr.send();
} }
function getOwnAlias() {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(){
if (xhr.readyState == 4 && xhr.status == 200){
ownAlias = xhr.responseText;
}
}
xhr.open( "GET", 'alias', true);
xhr.send();
}
/* /*
* INIT * INIT
@ -265,6 +278,9 @@ function update(){
if ( !ownId ){ if ( !ownId ){
getOwnId(); getOwnId();
} }
if ( !ownAlias){
getOwnAlias();
}
checkInbox(); checkInbox();
// also check for outbox items on interval, // also check for outbox items on interval,
// necessary in case connection is lost and messages are not yet sent // necessary in case connection is lost and messages are not yet sent