Commits

Victor Ng  committed 3a5c85a

couple of small patches to enable queues and topics when talking to
JMS as well as setting the client-id on login

  • Participants
  • Parent commits 4aa49d6

Comments (0)

Files changed (2)

File stomp/frame.py

 #!/usr/bin/env python
 import socket
 import random
+from pprint import pformat
 
 class Frame(object):
     """Build and manage a STOMP Frame.
     </connector>
 
     """
-    def __init__(self,sock=None):
+    def __init__(self,sock=None, debug=False):
         """Initialize Frame object
         Passing a socket object is optional
         >>> frameobj = Frame(socket)
         self.session  = None
         self.my_name  = socket.gethostbyname(socket.gethostname())
         self.sock     = sock
+        self.debug    = debug
 
     def connect(self,sock, conf):
         """Connect to the STOMP server, get session id
         >>> frameobj.connect(sock)
         """
         self.sock = sock
+        if not conf:
+            conf = {}
         frame = self.build_frame({'command':'CONNECT','headers':conf})
         self.send_frame(frame.as_string())
         self._set_session()
         self.body    = args.get('body')
         if want_receipt:
             receipt_stamp = str(random.randint(0,10000000))
-            self.headers['receipt'] = self.session.get('session') + "_" + receipt_stamp
+            self.headers['receipt'] = self.session.get('session') + "-" + receipt_stamp
         return self
 
     def as_string(self):
         body    = self.body
         frame   = "%s\n" % command
         
-        headers['x-client'] = self.my_name
+        headers['x_client'] = self.my_name
 
         bytes_message = False
         if 'bytes_message' in headers:
 
         while True:
             line = self._getline()
+            if self.debug:
+                print "Received: " +  '=' * 20
+                print line
+                print '-' * 20
+
             command = self.parse_command(line)
             line = line[len(command)+1:]
             headers_str, body = line.split('\n\n')
             headers[key] = value
         return headers
 
-    def send_frame(self,frame):
+    def send_frame(self,frame, debug=False):
         """Send frame to server, get receipt if needed
         >>> frameobj.send_frame(frame)
         """
+        if debug:
+            print "Sending: " +  '=' * 20
+            print frame
+            print '-' * 20
         self.sock.sendall(frame)
+
         if 'receipt' in self.headers:
             frame = self.parse_frame()
             return frame
         while not buffer.endswith('\x00\n'):
             buffer += self.sock.recv(1)
         return buffer[:-2]
+
+    def __repr__(self):
+        return "<Frame %s>" % pformat(self.headers)

File stomp/stomp.py

         <serverTransport uri="stomp://localhost:61613"/>
     </connector>
     """
-    def __init__(self,hostname,port):
+    def __init__(self,hostname,port, debug=False):
         """Initialize Stomp object
         Also accepts arguments needed to build the TCP connection.
         
         self.sock        = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
         self._subscribed = None
         self._connected  = None
-        self.frame       = Frame()
+        self.debug       = debug
+        self.frame       = Frame(debug=self.debug)
+
 
     def _get_subscribed(self):
         return self._subscribed
         >>> stomp.send_frame(frame)
         """
         self._is_connected()
-        frame = self.frame.send_frame(frame.as_string())
+        frame = self.frame.send_frame(frame.as_string(), self.debug)
         return frame
+
+    def __repr__(self):
+        return "<Stomp %s:%s connected:%s>" % (self.host, self.port, self._is_connected())
+