def _set_state(self, state): if not self.state or not (state == self.state): self.log('Changing state from %r to %r', self.state, state) self.state = state if self._notifier: time.call_next(self._notifier.callback, state, self)
def dispatch(self, uri, data): recip = tunneling.parse(uri) if recip.key not in self._channels: self.warning("Dropping message to unknown recipient %s", recip) return self._pending_dispatches += 1 time.call_next(self._dispatch_message, recip, data)
def _trigger_callbacks(self, result): if not self._fnotifier: return if isinstance(result, (Failure, Exception)): self.log("Firing errback of notifier with result: %r.", result) time.call_next(self._fnotifier.errback, "finish", result) else: self.log("Firing callback of notifier with result: %r.", result) time.call_next(self._fnotifier.callback, "finish", result) self._fnotifier = None
def finalize(self, result): if self._finalize_called: return self._finalize_called = True self._result = result if isinstance(result, (failure.Failure, Exception)): self.log("Firing errback of notifier with result: %r.", result) time.call_next(self._fnotifier.errback, 'finish', result) else: self.log("Firing callback of notifier with result: %r.", result) time.call_next(self._fnotifier.callback, 'finish', result) self.cleanup()
def dispatch(self, uri, msg): self.log('Tunnel received message from uri: %s.', uri) self._pending_dispatches += 1 if IDialogMessage.providedBy(msg): recp = msg.reply_to self.log("Message is a dialog message, recipient: %r", recp) if recp not in self._uris or self._uris[recp] != uri: self.debug("Uri not known or doesn't match the recipient, " "registering external route") self._channel.create_external_route('tunnel', recipient=recp, uri=uri) time.call_next(self._dispatch_message, msg)
def clientConnectionFailed(self, connector, reason): if reason.check(terror.TimeoutError): msg = ('Timeout of %s seconds expired while trying to connected to' ' %s:%s' % (connector.timeout, connector.host, connector.port)) reason = failure.Failure(terror.TimeoutError(string=msg)) reason.cleanFailure() elif reason.check(terror.ConnectError): # all the other connection errors which are not directly related # to the timeout msg = ('Failed to connect to %s:%s' % (connector.host, connector.port)) reason = failure.Failure(type(reason.value)(string=msg)) reason.cleanFailure() time.call_next(self._deferred.errback, reason) del self._deferred if self.owner: self.owner.onClientConnectionFailed(reason) self._cleanup()
def dispatch(self, source_backend, recip, message): logger = source_backend if recip.route not in self._backends: logger.warning("Dropping message to unknown route %r", recip) return target_backend = self._backends[recip.route] source_in_ver = source_backend.version target_out_ver = target_backend.version is_master = source_in_ver >= target_out_ver source_out_ver = target_out_ver if is_master else source_in_ver target_in_ver = source_out_ver serializer = source_backend._create_serializer(source_out_ver) unserializer = target_backend._create_unserializer(target_in_ver) out_message = unserializer.convert(serializer.convert(message)) self._pending_calls += 1 time.call_next(self._dispatch_message, target_backend, recip, out_message)
def call_agent_side(self, method, *args, **kwargs): ''' Call the method, wrap it in Deferred and bind error handler. ''' assert not self._finalize_called, ("Attempt to call agent side code " "after finalize() method has been " "called. Method: %r" % (method, )) ensure_state = kwargs.pop('ensure_state', None) d = defer.Deferred(canceller=self._cancel_agent_side_call) self._agent_jobs.append(d) if ensure_state: # call method only if state check is checks in d.addCallback( lambda _: (self._ensure_state(ensure_state) and method(*args, **kwargs))) else: d.addCallback(defer.drop_param, method, *args, **kwargs) d.addErrback(self._error_handler, method) d.addBoth(defer.bridge_param, self._remove_agent_job, d) time.call_next(d.callback, None) return d
def onConnectionMade(self, protocol): time.call_next(self._deferred.callback, protocol) del self._deferred if self.owner: self.owner.onClientConnectionMade(protocol)
def _schedule_flush(self): if self._flush_task is None: self._flush_task = time.call_next(self._flush)
def clientConnectionFailed(self, connector, reason): time.call_next(self._deferred.errback, reason) del self._deferred if self.owner: self.owner.onClientConnectionFailed(reason) self._cleanup()
def create_protocol(self, *args, **kwargs): proto = self.protocol(*args, **kwargs) proto.factory = self time.call_next(self._deferred.callback, proto) del self._deferred return proto
def dispatch(self, message, outgoing=True): self._pending_dispatches += 1 time.call_next(self._dispatch_internal, message, outgoing)
def delay(value, context, **params): d = defer.Deferred() d.addCallback(effect, context, **params) time.call_next(d.callback, value) return result
def initiate(self): time.call_next(self._bind) return self
def notify_failed(self, failure, agent_id): error.handle_failure(self, failure, "Failed to spawn the agent. I will terminate") time.call_next(self._shutdown, stop_process=True) self.info("Pushing failed notification to master.") return self._broker.fail_event(failure, agent_id, 'started')