Regular poll now prints the messages as they should appear.
Regular poll now prints the messages as they should appear.

What's missing at the moment is upstream user data - it's not in the current test payloads I'm using, so haven't put anything in to extract it yet. Once I've built the send message functionality into the interface I'll look at updating

--- a/client/LocalChatClient.py
+++ b/client/LocalChatClient.py
@@ -11,6 +11,106 @@
 from collections import deque
 from threading import Thread
 import threading
+
+import json
+import urllib2
+
+import datetime as dt
+
+
+# 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. Also means test that part of the next block
+            
+            color = "green"
+            upstruser = 'foo' # Temporary placeholder
+            
+            if upstruser == USER:
+                # One of our own, change the color
+                color = "red"
+            
+            ts = dt.datetime.utcfromtimestamp(i[2]).strftime("[%H:%M:%S]")
+            
+            line = [
+                ts, # timestamp
+                "%s>" % (upstruser,), # To be replaced later
+                msgbody
+                ]
+            
+            to_print.append([color,' '.join(line)])
+        
+        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 +300,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 +316,24 @@
         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')
+                continue
+                
+            if m:
+                for i in m:
+                    c.output(i[1], i[0])
+                
     t=Thread(target=run)
     t.daemon=True
     t.start()