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

14
main.py
View File

@ -31,6 +31,7 @@ class Meshenger:
logger.info('Making message directory')
self.init_index()
self.make_alias()
try:
d = threading.Thread(target=self.discover)
@ -198,6 +199,19 @@ Save the time of the last update.
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):
"""

View File

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

View File

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