1. Ask Solem Hoel
  2. python-stomp

Commits

Ask Solem  committed c1c92f9

Can now be subscribed to more than one destination at once (and we keep track
of them)

  • Participants
  • Parent commits 5b5429b
  • Branches default

Comments (0)

Files changed (2)

File four/stomp.py

View file
  • Ignore whitespace
         self.host = hostname
         self.port = port
         self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+        self._subscribed_to = {}
         self._subscribed = None
         self._connected = None
         self.frame = Frame()
 
     def disconnect(self, conf=None):
         """Disconnect from the server."""
-        if self.subscribed:
-            self.unsubscribe({'destination': self.subscribed})
-        if conf is None:
-            conf = {}
+        conf = conf or {}
+        for destination in self._subscribed_to.keys():
+            self.unsubscribe({'destination': destination})
         frame = self.frame.build_frame({'command': 'DISCONNECT',
                                         'headers': conf})
         self.send_frame(frame)
         frame = self.frame.build_frame({'command': 'SUBSCRIBE',
                                         'headers': conf})
         self.send_frame(frame)
-        self.subscribed = conf.get('destination')
+        destination = conf["destination"]
+        self._subscribed_to[destination] = True
 
     def begin(self, conf=None):
         """Begin transaction.
         frame = self.frame.build_frame({'command': 'UNSUBSCRIBE',
                                         'headers': conf})
         self.send_frame(frame)
-        self.subscribed = None
+        destination = conf["destination"]
+        self._subscribed_to.pop(destination, None)
 
     def ack(self, frame):
         """Acknowledge receipt of a message
         if not self.connected:
             raise self.NotConnectedError("Not connected to STOMP server.")
 
-    def _get_subscribed(self):
-        return self._subscribed
+    @property
+    def subscribed(self):
+        """**DEPRECATED** The queue or topic currently subscribed to."""
+        as_list = self._subscribed_to.keys()
+        if not as_list:
+            return
+        return as_list[0]
 
-    def _set_subscribed(self, sub):
-        self._subscribed = sub
-
-    # XXX This is a problem, because we can be subscribed to more than
-    # one topic/queue at the same time.
-    subscribed = property(_get_subscribed, _set_subscribed,
-                          "The queue or topic currently subscribed to")
 
     def _get_connected(self):
         return self._connected

File tests/test_stomp.py

View file
  • Ignore whitespace
         assert self.frame.calls('send_frame',send_args.as_string())
 
     def should_unsubscribe(self):
-        self.stomp.unsubscribe()
-        built_frame = self.frame.calls('build_frame',DontCare).one()
+        self.stomp.unsubscribe({'destination': '/queue/nose-test'})
+        built_frame = self.frame.calls('build_frame', DontCare).one()
         built_frame_args = built_frame.args[0]
         send_args = built_frame[3]
 
         assert not self.stomp.subscribed
 
     def should_unsub_via_disco(self):
-        self.stomp.subscribed = True
+        self.stomp._subscribed_to["/queue/nose-test"] = True
         self.stomp.disconnect()
         assert not self.stomp.subscribed
 
 class WhenUsingProperties(TestCase):
     def should_set_sub(self):
         mystomp = Stomp('localhost',99999)
-        mystomp._set_subscribed({'destination':'/queue/nose_test'})
+        mystomp._subscribed_to['/queue/nose_test'] = True
         assert mystomp.subscribed is not None
 
     def should_set_son(self):