From 304d821050223f5ab40df4e7a20afbc818b6665d Mon Sep 17 00:00:00 2001
From: James Meyer <james.meyer@operamail.com>
Date: Thu, 27 Sep 2012 13:32:06 -0500
Subject: LinHES-system: add the ability to view the msg queue

 msg_client.py is used to add,remove, or list items in the queue of messages that
    will be displayed on screen.  Optionaly messages can be given an identifier or "tag".
    This identifier does not have to be unique.

    Items are processed in the order they arrive, based off their slot number.
    The lowest slot number will be displayed first.
    After the message is displayed it is removed from the queue.

    ADD:
    To add items to the queue:
      msg_client.py --msg "My first message" --tag "tag1"

    REMOVE:
      Removing items are based off the tag.
      To remove all items from the queue that match the tag "tag1":
      msg_client.py --clear --tag tag1

      To remove all items from the queue:
      msg_client.py --clear

    Listing items in the queue or to get the total count:
      msg_client.py --print_list
            >      -------------
            >      msg : slot 1
            >      slot : 1
            >      cmd : msg
            >      tag : None
            >      timeout : None

    To immediately stop displaying the current message, use the kill command.
        msg_client.py --kill
---
 abs/core/LinHES-system/PKGBUILD      |  6 ++--
 abs/core/LinHES-system/msg_client.py | 69 +++++++++++++++++++++++++++++-------
 abs/core/LinHES-system/msg_daemon.py | 39 ++++++++++++--------
 3 files changed, 83 insertions(+), 31 deletions(-)

diff --git a/abs/core/LinHES-system/PKGBUILD b/abs/core/LinHES-system/PKGBUILD
index 9e57a5b..9b3bca7 100755
--- a/abs/core/LinHES-system/PKGBUILD
+++ b/abs/core/LinHES-system/PKGBUILD
@@ -1,6 +1,6 @@
 pkgname=LinHES-system
 pkgver=2
-pkgrel=104
+pkgrel=107
 arch=('i686' 'x86_64')
 MVDIR=$startdir/pkg/usr/LH
 BINDIR=$startdir/pkg/usr/bin
@@ -88,8 +88,8 @@ md5sums=('5bd4938cf41b63787aa4cdfd76423e09'
          '8f474e019d5fcb775497aca355d61b0b'
          '4a3cd8f9b33b2b86fdba47a8f1fa2859'
          '4d5a2441fe681c85986c5626c2944a3d'
-         'e4e6bd9ef85ab1498e5d3fb0d57816d2'
-         '1c8df75728926f0af53c8757c22d8c58'
+         '0e76cd8f320a852a162931450a1fb8b7'
+         '55b33e6a08eeea2a5280da0100f2c33c'
          'ea315f41dcd6c978e546c95fc05546cf'
          'ac61cc460d9e97ba1f5ef69e92cdfbe5'
          '06a628469051237943b7c874f2e29b8a'
diff --git a/abs/core/LinHES-system/msg_client.py b/abs/core/LinHES-system/msg_client.py
index 175933f..54c2e54 100755
--- a/abs/core/LinHES-system/msg_client.py
+++ b/abs/core/LinHES-system/msg_client.py
@@ -5,7 +5,7 @@ import socket
 import sys
 import argparse
 import pickle
-
+import time
 # Create a UDS socket
 sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
 
@@ -18,22 +18,59 @@ def send_message(message):
     except socket.error, msg:
         print >>sys.stderr, msg
         sys.exit(1)
-    try:
-        sock.sendall(message)
-        #amount_received = 0
-        #amount_expected = len(message)
+#try:
+    sock.sendall(message)
+    #amount_received = 0
+    #amount_expected = len(message)
+    print "Waiting for response..."
+    data = sock.recv(1024)
+    print data
+
+#finally:
+    #print >>sys.stderr, 'error occured closing socket'
+
+    sock.close()
+def usage():
+    line = '''
+    Help screen:
+    msg_client.py is used to add,remove, or list items in the queue of messages that
+    will be displayed on screen.  Optionaly messages can be given an identifier or "tag".
+    This identifier does not have to be unique.
+
+    Items are processed in the order they arrive, based off their slot number.
+    The lowest slot number will be displayed first.
+    After the message is displayed it is removed from the queue.
+
+
+    ADD:
+    To add items to the queue:
+      msg_client.py --msg "My first message" --tag "tag1"
+
+    REMOVE:
+      Removing items are based off the tag.
+      To remove all items from the queue that match the tag "tag1":
+      msg_client.py --clear --tag tag1
+
+      To remove all items from the queue:
+      msg_client.py --clear
+
 
-        #while amount_received < amount_expected:
-        #    data = sock.recv(16)
-        #    amount_received += len(data)
-        #    print >>sys.stderr, 'received "%s"' % data
+    Listing items in the queue or to get the total count:
+      msg_client.py --print_list
+            >      -------------
+            >      msg : slot 1
+            >      slot : 1
+            >      cmd : msg
+            >      tag : None
+            >      timeout : None
 
-    finally:
-        #print >>sys.stderr, 'closing socket'
-        print "message sent"
-        sock.close()
 
+    To immediately stop displaying the current message, use the kill command.
+        msg_client.py --kill
 
+    '''
+    print line
+    sys.exit(0)
 if __name__ == "__main__":
     parser = argparse.ArgumentParser()
 
@@ -43,6 +80,8 @@ if __name__ == "__main__":
     action.add_argument('--msg', action='store', dest='msg', help='Add message to the queue')
     action.add_argument('--clear', action='store_true', help='Remove message from queue that match tag')
     action.add_argument('--kill', action='store_true', help='Kill current msg thats displayed')
+    action.add_argument('--print_list', action='store_true', help='Print current queue')
+    action.add_argument('--usage', action='store_true', help='Print help screen')
 
     results = parser.parse_args()
     resultsdict = vars(results)
@@ -53,6 +92,10 @@ if __name__ == "__main__":
         cmd = "clear"
     elif results.kill:
         cmd = "kill"
+    elif results.print_list:
+        cmd = "list"
+    elif results.usage:
+        usage()
 
     arg_dict = {'cmd':cmd , 'msg':resultsdict['msg'] , 'tag':resultsdict['tag'] , 'timeout':resultsdict['timeout']}
     send_message(pickle.dumps(arg_dict))
diff --git a/abs/core/LinHES-system/msg_daemon.py b/abs/core/LinHES-system/msg_daemon.py
index b766aae..0e54824 100755
--- a/abs/core/LinHES-system/msg_daemon.py
+++ b/abs/core/LinHES-system/msg_daemon.py
@@ -224,12 +224,8 @@ class Msg_Queue():
     def search(self,key,value):
         pass
 
-    def print_queue(self):
-        print "############################"
-        for i in self.msglist:
-            for key, value in i.iteritems():
-                print key,":", value
-            print "-----"
+    def get_queue(self):
+        return self.msglist
 
     def get_last_msg_slot(self):
         return self.msg_slot
@@ -289,7 +285,7 @@ class msg_queue_runner(threading.Thread):
                 self.p.start()
                 self.p.join()
             elif style == "default":
-                display_time = int(msgdict['timeout'])
+                #display_time = int(msgdict['timeout'])
                 self.out_xosd.display(msgtext,display_time)
 
         except:
@@ -310,40 +306,53 @@ if __name__ == "__main__":
     # Create a UDS socket
     sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
     # Bind the socket to the port
-    print >>sys.stderr, 'starting up on %s' % server_address
+    #print >>sys.stderr, 'starting up on %s' % server_address
     sock.bind(server_address)
     #change permissions of socket
     os.chmod(server_address,0777)
 
     # Listen for incoming connections
-    sock.listen(1)
+    sock.listen(2)
     #start msg display thread
     msgthread = msg_queue_runner(msg_queue)
     msgthread.setDaemon(True)
     msgthread.start()
 
-
     #main loop that reads in the cmddict, never exits
     while True:
         # Wait for a connection
         connection, client_address = sock.accept()
         try:
-            sf = connection.makefile("r+b", bufsize=0)
-            data = sf.read()
+            #sf = connection.makefile("r+b", bufsize=0)
+            data = connection.recv(1024)
             cmddict = (pickle.loads(data))
-
+            connection.send("   Server acknowledged client \n")
             if cmddict['cmd'] == "msg":
                 #add to msg_queue
                 msg_queue.add_msg(cmddict)
+
             elif cmddict['cmd'] == 'clear':
                 if cmddict['tag'] == '' or cmddict['tag'] == None :
                     msg_queue.clear()
                 else:
                     msg_queue.clear_msg(cmddict['tag'])
+
             elif cmddict['cmd'] == 'kill':
                     msgthread.kill_current()
-                ##    call(["/usr/LH/bin/lh_message.sh", msg])
-                #break
+
+            elif cmddict['cmd'] == 'list':
+                    q = msg_queue.get_queue()
+                    for i in q:
+                        line="-------------\n"
+                        connection.send(line)
+                        for k,v in i.iteritems():
+                            line="   %s : %s\n" %(k,v)
+                            connection.send(line)
+                    line = "Number of messages in queue: %s \n" %(len(q))
+                    connection.send(line)
+                    #connection.send(pickle.dumps(q))
+
+
         finally:
             # Clean up the connection
             connection.close()
-- 
cgit v0.12