Ejemplo n.º 1
0
    def subscriptions(self):
        """
        A tuple containing the topics that the client is currently subscribed to

        See :func:`subscribe` for more information on adding subscriptions
        """
        logger.debug("%s(): Waiting for Subscriptions lock...",
                     DxlUtils.func_name())
        self._subscriptions_lock.acquire()
        try:
            return tuple(self._subscriptions)
        finally:
            logger.debug("%s(): Releasing Subscriptions lock.",
                         DxlUtils.func_name())
            self._subscriptions_lock.release()
Ejemplo n.º 2
0
    def unsubscribe(self, topic):
        """
        Unsubscribes from the specified topic on the DXL fabric.

        See the :func:`subscribe` method for more information on subscriptions.

        :param topic: The topic to unsubscribe from
        """
        logger.debug("%s(): Waiting for Subscriptions lock...",
                     DxlUtils.func_name())
        self._subscriptions_lock.acquire()
        try:
            if topic in self._subscriptions:
                if self.connected:
                    self._client.unsubscribe(topic)
        finally:
            self._subscriptions.remove(topic)
            logger.debug("%s(): Releasing Subscriptions lock.",
                         DxlUtils.func_name())
            self._subscriptions_lock.release()
Ejemplo n.º 3
0
    def subscribe(self, topic):
        """
        Subscribes to the specified topic on the DXL fabric. This method is typically used in
        conjunction with the registration of :class:`dxlclient.callbacks.EventCallback` instances
        via the :func:`add_event_callback` method.

        The following is a simple example of using this:

        .. code-block:: python

            from dxlclient.callbacks import EventCallback

            class MyEventCallback(EventCallback):
                def on_event(self, event):
                    print "Received event! " + event.source_client_id

            dxl_client.add_event_callback("/testeventtopic", MyEventCallback(), False)
            dxl_client.subscribe("/testeventtopic")

        **NOTE:** By default when registering an event callback the client will automatically subscribe to the topic.
        In this example the :func:`dxlclient.client.DxlClient.add_event_callback` method is invoked with the
        ``subscribe_to_topic`` parameter set to ``False`` preventing the automatic subscription.

        :param topic: The topic to subscribe to
        """
        logger.debug("%s(): Waiting for Subscriptions lock...",
                     DxlUtils.func_name())
        self._subscriptions_lock.acquire()
        try:
            if topic not in self._subscriptions:
                self._subscriptions.add(topic)
                if self.connected:
                    self._client.subscribe(topic)
        except Exception as ex:
            logger.error("Error during subscribe: %s", ex.message)
            logger.debug(traceback.format_exc())
            raise DxlException("Error during subscribe" + str(ex))
        finally:
            logger.debug("%s(): Releasing Subscriptions lock.",
                         DxlUtils.func_name())
            self._subscriptions_lock.release()
Ejemplo n.º 4
0
    def unsubscribe(self, topic):
        """
        Unsubscribes from the specified topic on the DXL fabric.

        See the :func:`subscribe` method for more information on subscriptions.

        :param topic: The topic to unsubscribe from
        """
        logger.debug("%s(): Waiting for Subscriptions lock...",
                     DxlUtils.func_name())
        self._subscriptions_lock.acquire()
        try:
            if topic in self._subscriptions:
                if self.connected:
                    self._client.unsubscribe(topic)
        except Exception as ex:
            logger.error("Error during unsubscribe: %s", ex.message)
            logger.debug(traceback.format_exc())
            raise DxlException("Error during unsubscribe")
        finally:
            self._subscriptions.remove(topic)
            logger.debug("%s(): Releasing Subscriptions lock.",
                         DxlUtils.func_name())
            self._subscriptions_lock.release()