From d6f081f8b0ea1756b146697e807b6c260aad567d Mon Sep 17 00:00:00 2001 From: Roel Date: Tue, 19 May 2015 14:26:48 +0200 Subject: [PATCH] built alias/nickname feature for nodes, in the process I broke the message color system.. can be fixed in main.js line 170 --- main.py | 26 ++++++++++++++++++++------ meshenger_clientserve.py | 9 +++++++++ meshenger_nodeserve.py | 4 ++++ web/main.js | 20 ++++++++++++++++++-- 4 files changed, 51 insertions(+), 8 deletions(-) diff --git a/main.py b/main.py index fd73c1a..61a004b 100755 --- a/main.py +++ b/main.py @@ -31,6 +31,7 @@ class Meshenger: logger.info('Making message directory') self.init_index() + self.make_alias() try: d = threading.Thread(target=self.discover) @@ -70,7 +71,7 @@ class Meshenger: for device in self.devices.keys(): 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') 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): """ -Make an index file of all the messages present on the node. -Save the time of the last update. -""" + Make an index file of all the messages present on the node. + Save the time of the last update. + """ logger.debug('build_index') current_index = os.listdir(self.msg_dir) if current_index != self.previous_index: @@ -198,11 +199,24 @@ 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): """ -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')) diff --git a/meshenger_clientserve.py b/meshenger_clientserve.py index 579bb46..dd62d44 100644 --- a/meshenger_clientserve.py +++ b/meshenger_clientserve.py @@ -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') diff --git a/meshenger_nodeserve.py b/meshenger_nodeserve.py index 011c7f7..b942ef6 100755 --- a/meshenger_nodeserve.py +++ b/meshenger_nodeserve.py @@ -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') diff --git a/web/main.js b/web/main.js index 03363a3..80edcbb 100644 --- a/web/main.js +++ b/web/main.js @@ -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, "
"); 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