def send_subscribe_request(args_): try: self.connection.subscribe(channel, args_, callback=subscribe_callback) except Exception as e: logger.exception(e)
def _start_disconnecting(self): logger.info('_start_disconnecting') if self.connection: try: self.connection.stop() except Exception as e: logger.exception(e)
def _authenticate(self, auth_delegate, callback): def callback_(outcome): if type(outcome) == auth.Done: if auth_delegate not in self._successful_auth_delegates: self._successful_auth_delegates.append(auth_delegate) callback(outcome) try: self.connection.authenticate(auth_delegate, callback=callback_) except Exception as e: logger.exception(e)
def _connect(self): logger.info('_connect') self._time_of_last_reconnect = time.time() self.connection = Connection(self._endpoint, self._appkey, self) try: self.connection.start() self._queue.put(a.ConnectingComplete()) except Exception as e: logger.exception(e) self.last_connecting_error = e self._queue.put(a.ConnectingFailed())
def _perform_state_callback(self, callback_name, *args): try: logger.info('entering callback %s', callback_name) try: callback = getattr(self.observer, callback_name) callback(*args) except AttributeError: pass except Exception as e: logger.error('Caught exception in state callback') logger.exception(e) finally: logger.info('exiting callback %s', callback_name)
def _perform_state_callback(self, callback_name, *args): if self.observer: current_thread_name = threading.current_thread().name logger.debug('entering subscription callback %s on %s', callback_name, current_thread_name) callback = None try: callback = getattr(self.observer, callback_name) except AttributeError: pass try: if callback: callback(*args) except Exception as e: logger.error('Caught exception in subscription callback') logger.exception(e) finally: logger.debug('exiting subscription callback %s on %s', callback_name, current_thread_name)
def _restore_auth_and_return_true_if_failed(self): logger.info('_restore_auth_and_return_true_if_failed') if not self.restore_auth_on_reconnect: return False counter = [len(self._successful_auth_delegates)] logger.debug('Restoring %d authentications', counter[0]) if counter[0] == 0: return False ready_event = threading.Event() def callback(outcome): logger.debug('Outcome: %s', outcome) if type(outcome) == auth.Done: logger.debug('Restored auth') counter[0] -= 1 if counter[0] == 0: ready_event.set() else: ready_event.set() for ad in self._successful_auth_delegates: ready_event.clear() try: self.connection.authenticate(ad, callback) except Exception as e: logger.exception(e) ready_event.wait(10) if counter[0] == 0: logger.debug('Restoring authentications: done') return False else: logger.error('Failed to restore %d authentications', counter[0]) return True
def process_one_message(self, timeout=1): '''Must be called from a single thread returns True if the message was Dispose()''' try: m = self._queue.get(block=True, timeout=timeout) except queue.Empty: logger.debug('queue is empty') return False t = type(m) logger.info('Begin handling %s', t.__name__) if t == a.ChannelData: data = m.data channel = data['subscription_id'] subscription = self.subscriptions.get(channel) if subscription: subscription.on_subscription_data(data) else: logger.error('Subscription for %s not found', data) elif t == a.Start: self._sm.Start() elif t == a.Stop: self._sm.Stop() elif t == a.Dispose: self._sm.Dispose() self._queue.task_done() return True elif t == a.Publish: if self.is_connected(): try: self.connection.publish(m.channel, m.message, m.callback) except Exception as e: logger.exception(e) else: self._offline_queue.append(m) elif t == a.Subscribe: self._subscribe(m.channel_or_subscription_id, m.mode, m.observer, args=m.args) elif t == a.Unsubscribe: self._unsubscribe(m.channel_or_subscription_id) elif t == a.Read: self.connection.read(m.key, m.args, m.callback) elif t == a.Write: self.connection.write(m.key, m.value, m.callback) elif t == a.Delete: self.connection.delete(m.key, m.callback) elif t == a.Search: self.connection.search(m.prefix, m.callback) elif t == a.Authenticate: if self.is_connected(): self._authenticate(m.auth_delegate, m.callback) else: self._offline_queue.append(m) elif t == a.Tick: self._sm.Tick() elif t == a.ConnectingComplete: self._sm.ConnectingComplete() elif t == a.ConnectingFailed: self._sm.ConnectingFailed() elif t == a.ConnectionClosed: self._sm.ConnectionClosed() elif t == a.ChannelError: self._sm.ChannelError(m.channel, m.payload) elif t == a.InternalError: self._sm.InternalError(m.payload) elif t == a.FastForward: self._perform_state_callback('on_fast_forward', m.channel) else: logger.error('Unexpected event %s: %s', m, t) self._queue.task_done() logger.info('Finish handling %s', t.__name__) return False
def send_unsubscribe_request(): try: self.connection.unsubscribe(channel, unsubscribe_callback) except Exception as e: logger.exception(e)