Esempio n. 1
0
 def unsubscribe(self):
     self._args = None
     if self.is_failed():
         self.mode = 'unlinked'
         self._sm.advance(lambda sm: sm.UnsubscribeOK())
     else:
         logger.debug('unsubscribe')
         self.mode = 'unlinked'
         self._sm.advance(lambda sm: sm.ModeChange())
Esempio n. 2
0
    def subscribe(self, args=None, observer=None):
        logger.debug('subscribe')

        if self.mode in ['linked', 'cycle']:
            logger.error('Already subscribed or trying to')
            return

        if self._next_observer:
            try:
                self._next_observer.on_deleted()
            except Exception:
                pass

        self._next_observer = observer
        self._next_args = args
        self.mode = 'cycle'

        return self._sm.advance(lambda sm: sm.ModeChange())
Esempio n. 3
0
    def _change_mode_from_cycle_to_linked(self):
        if self.mode == 'cycle':
            self.mode = 'linked'
            if self._next_args:
                self._args = self._next_args

            if self.observer:
                self._perform_state_callback('on_deleted')

            if self._next_observer:
                self.observer = self._next_observer
                self._perform_state_callback('on_created')
                self._next_observer = None
            else:
                self.observer = None
            self._sm.advance(lambda sm: sm.ModeChange())
        elif self.mode == 'unlinked':
            self._perform_state_callback('on_deleted')
            self.observer = None
Esempio n. 4
0
    def __init__(self,
                 delivery_mode,
                 send_subscribe_request,
                 send_unsubscribe_request,
                 args=None,
                 observer=None):
        self.mode = 'linked'

        _lint_args(args)
        self._args = args

        self._delivery_mode = delivery_mode
        self._send_subscribe_request_ = send_subscribe_request
        self._send_unsubscribe_request_ = send_unsubscribe_request
        self._connected = False
        self.observer = observer
        self._next_observer = None
        self._next_args = {}
        self._sm = StateMachineWrapper(sm.Subscription_sm, self)
        self._last_error = None
        self._sm.advance(lambda sm: sm.ModeChange())