def _post_connect(self): "Greeting stuff" init_event = Event() error = [None] # so that err_cb can bind error[0]. just how it is. # callbacks def ok_cb(id, capabilities): self._id = id self._server_capabilities = capabilities init_event.set() def err_cb(err): error[0] = err init_event.set() listener = HelloHandler(ok_cb, err_cb) self.add_listener(listener) self.send( HelloHandler.build(self._client_capabilities, self._device_handler)) logger.debug('starting main loop') self.start() # we expect server's hello message, if server doesn't responds in 60 seconds raise exception init_event.wait(60) if not init_event.is_set(): raise SessionError("Capability exchange timed out") # received hello message or an error happened self.remove_listener(listener) if error[0]: raise error[0] #if ':base:1.0' not in self.server_capabilities: # raise MissingCapabilityError(':base:1.0') logger.info('initialized: session-id=%s | server_capabilities=%s' % (self._id, self._server_capabilities))
def add_listener(self, listener): """Register a listener that will be notified of incoming messages and errors. :type listener: :class:`SessionListener` """ logger.debug('installing listener %r' % listener) if not isinstance(listener, SessionListener): raise SessionError("Listener must be a SessionListener type") with self._lock: self._listeners.add(listener)