Interface now polls for new messages in the background
Interface now polls for new messages in the background

With it running, if the following are run (in a seperate terminal) it correctly displays the expected output

curl -v -X POST http://127.0.0.1:8090/ -H "Content-Type: application/json" --data '{"action":"sendMsg","payload":"{\"roomName\":\"BenTest\", \"msg\":\"ENCRYPTED-DATA3\",\"user\":\"ben2\"}"}'
curl -v -X POST http://127.0.0.1:8090/ -H "Content-Type: application/json" --data '{"action":"sendMsg","payload":"{\"roomName\":\"BenTest\", \"msg\":\"ENCRYPTED-DATA4\",\"user\":\"ben2\"}"}'
curl -v -X POST http://127.0.0.1:8090/ -H "Content-Type: application/json" --data '{"action":"sendMsg","payload":"{\"roomName\":\"BenTest\", \"msg\":\"ENCRYPTED-DATA5\",\"user\":\"ben2\"}"}'

However, the caveat is that the test curls do not contain a JSON structure within the msg payload, so there's currently no functionality implemented to show time and sending user.

--- a/client/LocalChatClient.py
+++ b/client/LocalChatClient.py
@@ -11,6 +11,88 @@
 from collections import deque
 from threading import Thread
 import threading
+
+import json
+import urllib2
+
+# We'll get these from the commandline later
+USER='ben2'
+SERVER='http://127.0.0.1:8090'
+ROOMNAME='BenTest'
+
+
+class msgHandler(object):
+    
+    def __init__(self):
+        self.last = 0
+        self.user = USER
+        self.server = SERVER
+        self.room = ROOMNAME
+    
+    
+    def pollForMessage(self):
+        
+        if not self.room:
+            # We're not connected to a room
+            return False
+        
+        
+        payload = {"roomName": self.room, 
+                   "mylast":self.last,
+                   "user": self.user
+                   }
+        
+        request = {"action":"pollMsg",
+                   "payload": json.dumps(payload)
+                   }
+
+        resp = self.sendRequest(request)
+        
+        if resp == "BROKENLINK":
+            return resp
+        
+        
+        if resp['status'] == "unchanged":
+            return False
+        
+        to_print = []
+        # Otherwise, process the messages
+        for i in resp["messages"]:
+            self.last = i[0]
+            msgbody = self.decrypt(i[1])
+            
+            # TODO - We'll need to json decode and extract the sending user's name
+            # but not currently including that info in my curl tests
+            
+            to_print.append(msgbody)
+        
+        return to_print
+        
+        
+
+
+    def sendRequest(self,data):
+        data = json.dumps(data)
+        
+        try:
+            req = urllib2.Request(self.server, data, {'Content-Type': 'application/json'})
+            f = urllib2.urlopen(req)
+            response = f.read()
+            f.close()
+            return json.loads(response)
+        except:
+            return "BROKENLINK"
+
+
+
+    def decrypt(self,msg):
+        ''' Placeholder
+        '''
+        return msg
+    
+
+
+
 
 class UnknownCommand(Exception):
     def __init__(self,cmd):
@@ -200,8 +282,15 @@
             self.switch_focus()
         return urwid.Frame.keypress(self, size, key)
         
+
+
     
 if __name__=='__main__':
+    
+    
+    msg = msgHandler()
+    
+    
     class TestCmd(Command):
         def do_echo(self, *args):
             '''echo - Just echos all arguments'''
@@ -209,14 +298,23 @@
         def do_raise(self, *args):
             raise Exception('Some Error')
         
-    c=Commander('Test', cmd_cb=TestCmd())
+    c=Commander('LocalChat', cmd_cb=TestCmd())
     
     #Test asynch output -  e.g. comming from different thread
     import time
     def run():
         while True:
             time.sleep(1)
-            c.output('Tick', 'green')
+            
+            m = msg.pollForMessage()
+            
+            if m == "BROKENLINK":
+                c.output("Server went away", 'Red')
+                
+            if m:
+                for i in m:
+                    c.output(i, 'green')
+                
     t=Thread(target=run)
     t.daemon=True
     t.start()