def errReceivedIsBad(self, text): if self.deferred is not None: self.onProcessEnded = defer.Deferred() err = _UnexpectedErrorOutput(text, self.onProcessEnded) self.deferred.errback(failure.Failure(err)) self.deferred = None self.transport.loseConnection()
def _startLogOn(self, chatui): logonDeferred = defer.Deferred() cc = protocol.ClientCreator(reactor, IRCProto, self, chatui, logonDeferred) d = cc.connectTCP(self.host, self.port) d.addErrback(logonDeferred.errback) return logonDeferred
def requestSelfInfo(self): """ ask for the OSCARUser for ourselves """ d = defer.Deferred() self.sendSNAC(0x01, 0x0E, '').addCallback(self._cbRequestSelfInfo, d) return d
def start(self, interval, now=True): """ Start running function every interval seconds. @param interval: The number of seconds between calls. May be less than one. Precision will depend on the underlying platform, the available hardware, and the load on the system. @param now: If True, run this call right now. Otherwise, wait until the interval has elapsed before beginning. @return: A Deferred whose callback will be invoked with C{self} when C{self.stop} is called, or whose errback will be invoked when the function raises an exception or returned a deferred that has its errback invoked. """ assert not self.running, ("Tried to start an already running " "LoopingCall.") if interval < 0: raise ValueError, "interval must be >= 0" self.running = True d = self.deferred = defer.Deferred() self.starttime = self.clock.seconds() self._expectNextCallAt = self.starttime self.interval = interval self._runAtStart = now if now: self() else: self._reschedule() return d
def deferToThreadPool(reactor, threadpool, f, *args, **kwargs): """ Call the function C{f} using a thread from the given threadpool and return the result as a Deferred. This function is only used by client code which is maintaining its own threadpool. To run a function in the reactor's threadpool, use C{deferToThread}. @param reactor: The reactor in whose main thread the Deferred will be invoked. @param threadpool: An object which supports the C{callInThreadWithCallback} method of C{lib.twisted.python.threadpool.ThreadPool}. @param f: The function to call. @param *args: positional arguments to pass to f. @param **kwargs: keyword arguments to pass to f. @return: A Deferred which fires a callback with the result of f, or an errback with a L{lib.twisted.python.failure.Failure} if f throws an exception. """ d = defer.Deferred() def onResult(success, result): if success: reactor.callFromThread(d.callback, result) else: reactor.callFromThread(d.errback, result) threadpool.callInThreadWithCallback(onResult, f, *args, **kwargs) return d
def stopListening(self): if self.connected: result = self.d = defer.Deferred() else: result = None self._loseConnection() return result
def start(self): """ Start TLS negotiation. This checks if the receiving entity requires TLS, the SSL library is available and uses the C{required} and C{wanted} instance variables to determine what to do in the various different cases. For example, if the SSL library is not available, and wanted and required by the user, it raises an exception. However if it is not required by both parties, initialization silently succeeds, moving on to the next step. """ if self.wanted: if ssl is None: if self.required: return defer.fail(TLSNotSupported()) else: return defer.succeed(None) else: pass elif self.xmlstream.features[self.feature].required: return defer.fail(TLSRequired()) else: return defer.succeed(None) self._deferred = defer.Deferred() self.xmlstream.addOnetimeObserver("/proceed", self.onProceed) self.xmlstream.addOnetimeObserver("/failure", self.onFailure) self.xmlstream.send(domish.Element((NS_XMPP_TLS, "starttls"))) return self._deferred
def _connect(self, method, *args, **kwargs): """ Initiate a connection attempt. @param method: A callable which will actually start the connection attempt. For example, C{reactor.connectTCP}. @param *args: Positional arguments to pass to C{method}, excluding the factory. @param **kwargs: Keyword arguments to pass to C{method}. @return: A L{Deferred} which fires with an instance of the protocol class passed to this L{ClientCreator}'s initializer or fails if the connection cannot be set up for some reason. """ def cancelConnect(deferred): connector.disconnect() if f.pending is not None: f.pending.cancel() d = defer.Deferred(cancelConnect) f = _InstanceFactory( self.reactor, self.protocolClass(*self.args, **self.kwargs), d) connector = method(factory=f, *args, **kwargs) return d
def beginFileTransfer(self, file, consumer, transform=None): """ Begin transferring a file @type file: Any file-like object @param file: The file object to read data from @type consumer: Any implementor of IConsumer @param consumer: The object to write data to @param transform: A callable taking one string argument and returning the same. All bytes read from the file are passed through this before being written to the consumer. @rtype: C{Deferred} @return: A deferred whose callback will be invoked when the file has been completely written to the consumer. The last byte written to the consumer is passed to the callback. """ self.file = file self.consumer = consumer self.transform = transform self.deferred = deferred = defer.Deferred() self.consumer.registerProducer(self, False) return deferred
def deferLater(clock, delay, callable, *args, **kw): """ Call the given function after a certain period of time has passed. @type clock: L{IReactorTime} provider @param clock: The object which will be used to schedule the delayed call. @type delay: C{float} or C{int} @param delay: The number of seconds to wait before calling the function. @param callable: The object to call after the delay. @param *args: The positional arguments to pass to C{callable}. @param **kw: The keyword arguments to pass to C{callable}. @rtype: L{defer.Deferred} @return: A deferred that fires with the result of the callable when the specified time has elapsed. """ def deferLaterCancel(deferred): delayedCall.cancel() d = defer.Deferred(deferLaterCancel) d.addCallback(lambda ignored: callable(*args, **kw)) delayedCall = clock.callLater(delay, d.callback, None) return d
def define(host, port, database, word): """Look up a word using a dict server""" d = defer.Deferred() factory = DictLookupFactory("define", (database, word), d) from lib.twisted.internet import reactor reactor.connectTCP(host, port, factory) return d
def match(host, port, database, strategy, word): """Match a word using a dict server""" d = defer.Deferred() factory = DictLookupFactory("match", (database, strategy, word), d) from lib.twisted.internet import reactor reactor.connectTCP(host, port, factory) return d
def _callProtocolWithDeferred(protocol, executable, args, env, path, reactor=None): if reactor is None: from lib.twisted.internet import reactor d = defer.Deferred() p = protocol(d) reactor.spawnProcess(p, executable, (executable,)+tuple(args), env, path) return d
def lookup(self, portOnServer, portOnClient): """Lookup user information about the specified address pair. """ self.queries.append((defer.Deferred(), portOnServer, portOnClient)) if len(self.queries) > 1: return self.queries[-1][0] self.sendLine('%d, %d' % (portOnServer, portOnClient)) return self.queries[-1][0]
def __init__(self, wrappedFactory, canceller): """ @param wrappedFactory: A provider of I{IProtocolFactory} whose buildProtocol method will be called and whose resulting protocol will be wrapped. @param canceller: An object that will be called to cancel the L{self._onConnection} L{Deferred} """ self._wrappedFactory = wrappedFactory self._onConnection = defer.Deferred(canceller=canceller)
def initialize(self): xs = self.xmlstream hs = domish.Element((self.xmlstream.namespace, "handshake")) hs.addContent( xmlstream.hashPassword(xs.sid, unicode(xs.authenticator.password))) # Setup observer to watch for handshake result xs.addOnetimeObserver("/handshake", self._cbHandshake) xs.send(hs) self._deferred = defer.Deferred() return self._deferred
def start(self): """ Start SASL authentication exchange. """ self.setMechanism() self._deferred = defer.Deferred() self.xmlstream.addObserver('/challenge', self.onChallenge) self.xmlstream.addOnetimeObserver('/success', self.onSuccess) self.xmlstream.addOnetimeObserver('/failure', self.onFailure) self.sendAuth(self.mechanism.getInitialResponse()) return self._deferred
def _cbIdent(self, ident, chatui): if not ident: print 'falsely identified.' return self._ebConnected(Failure(Exception("username or password incorrect"))) print 'Identified!' dl = [] for handlerClass, sname, pname in self.services: d = defer.Deferred() dl.append(d) handler = handlerClass(self, sname, pname, chatui, d) ident.callRemote('attach', sname, pname, handler).addCallback(handler.connected) return defer.DeferredList(dl)
def loseConnection(self, connDone=failure.Failure(main.CONNECTION_DONE)): """ Stop accepting connections on this port. This will shut down my socket and call self.connectionLost(). It returns a deferred which will fire successfully when the port is actually closed. """ self.disconnecting = True if self.connected: self.deferred = defer.Deferred() self.reactor.callLater(0, self.connectionLost, connDone) return self.deferred
def connectService(self, service, wantCallback=0, extraData=''): """ connect to another service if wantCallback, we return a Deferred that gets called back when the service is online. if extraData, append that to our request. """ if wantCallback: d = defer.Deferred() self.sendSNAC(0x01, 0x04, struct.pack('!H', service) + extraData).addCallback( self._cbConnectService, d) return d else: self.sendSNACnr(0x01, 0x04, struct.pack('!H', service))
def sendSNAC(self, fam, sub, data, flags=[0, 0]): """ send a snac and wait for the response by returning a Deferred. """ reqid = self.lastID self.lastID = reqid + 1 d = defer.Deferred() d.reqid = reqid #d.addErrback(self._ebDeferredError,fam,sub,data) # XXX for testing self.requestCallbacks[reqid] = d self.sendFLAP(SNAC(fam, sub, reqid, data)) return d
def coiterate(self, iterator, doneDeferred=None): """ Add an iterator to the list of iterators this L{Cooperator} is currently running. @param doneDeferred: If specified, this will be the Deferred used as the completion deferred. It is suggested that you use the default, which creates a new Deferred for you. @return: a Deferred that will fire when the iterator finishes. """ if doneDeferred is None: doneDeferred = defer.Deferred() CooperativeTask(iterator, self).whenDone().chainDeferred(doneDeferred) return doneDeferred
def whenDone(self): """ Get a L{defer.Deferred} notification of when this task is complete. @return: a L{defer.Deferred} that fires with the C{iterator} that this L{CooperativeTask} was created with when the iterator has been exhausted (i.e. its C{next} method has raised L{StopIteration}), or fails with the exception raised by C{next} if it raises some other exception. @rtype: L{defer.Deferred} """ d = defer.Deferred() if self._completionState is None: self._deferreds.append(d) else: d.callback(self._completionResult) return d
def getHostByName(self, name, timeout = (1, 3, 11, 45)): """ See L{lib.twisted.internet.interfaces.IResolverSimple.getHostByName}. Note that the elements of C{timeout} are summed and the result is used as a timeout for the lookup. Any intermediate timeout or retry logic is left up to the platform via L{socket.gethostbyname}. """ if timeout: timeoutDelay = sum(timeout) else: timeoutDelay = 60 userDeferred = defer.Deferred() lookupDeferred = threads.deferToThreadPool( self.reactor, self.reactor.getThreadPool(), socket.gethostbyname, name) cancelCall = self.reactor.callLater( timeoutDelay, self._cleanup, name, lookupDeferred) self._runningQueries[lookupDeferred] = (userDeferred, cancelCall) lookupDeferred.addBoth(self._checkTimeout, name, lookupDeferred) return userDeferred
def send(self, to=None): """ Send out this iq. Returns a deferred that is fired when an iq response with the same id is received. Result responses will be passed to the deferred callback. Error responses will be transformed into a L{StanzaError<error.StanzaError>} and result in the errback of the deferred being invoked. @rtype: L{defer.Deferred} """ if to is not None: self["to"] = to if not ijabber.IIQResponseTracker.providedBy(self._xmlstream): upgradeWithIQResponseTracker(self._xmlstream) d = defer.Deferred() self._xmlstream.iqDeferreds[self['id']] = d timeout = self.timeout or self._xmlstream.iqDefaultTimeout if timeout is not None: def onTimeout(): del self._xmlstream.iqDeferreds[self['id']] d.errback(TimeoutError("IQ timed out")) call = self._xmlstream._callLater(timeout, onTimeout) def cancelTimeout(result): if call.active(): call.cancel() return result d.addBoth(cancelTimeout) self._xmlstream.send(self) return d
def quit(self): d = defer.Deferred() self.broker.notifyOnDisconnect(lambda: d.callback(None)) self.broker.transport.loseConnection() return d
def __init__(self, protocol): self.disconnected = 0 self.deferred = defer.Deferred() self.protocol = protocol
def _cbRequestSSI(self, snac, args=()): if snac[1] == 0x0f: # same SSI as we have return itemdata = snac[5][3:] if args: revision, groups, permit, deny, permitMode, visibility = args else: version, revision = struct.unpack('!BH', snac[5][:3]) groups = {} permit = [] deny = [] permitMode = None visibility = None while len(itemdata) > 4: nameLength = struct.unpack('!H', itemdata[:2])[0] name = itemdata[2:2 + nameLength] groupID, buddyID, itemType, restLength = \ struct.unpack('!4H', itemdata[2+nameLength:10+nameLength]) tlvs = readTLVs(itemdata[10 + nameLength:10 + nameLength + restLength]) itemdata = itemdata[10 + nameLength + restLength:] if itemType == 0: # buddies groups[groupID].addUser(buddyID, SSIBuddy(name, tlvs)) elif itemType == 1: # group g = SSIGroup(name, tlvs) if groups.has_key(0): groups[0].addUser(groupID, g) groups[groupID] = g elif itemType == 2: # permit permit.append(name) elif itemType == 3: # deny deny.append(name) elif itemType == 4: # permit deny info if not tlvs.has_key(0xcb): continue # this happens with ICQ permitMode = { 1: 'permitall', 2: 'denyall', 3: 'permitsome', 4: 'denysome', 5: 'permitbuddies' }[ord(tlvs[0xca])] visibility = { '\xff\xff\xff\xff': 'all', '\x00\x00\x00\x04': 'notaim' }[tlvs[0xcb]] elif itemType == 5: # unknown (perhaps idle data)? pass else: log.msg('%s %s %s %s %s' % (name, groupID, buddyID, itemType, tlvs)) timestamp = struct.unpack('!L', itemdata)[0] if not timestamp: # we've got more packets coming # which means add some deferred stuff d = defer.Deferred() self.requestCallbacks[snac[4]] = d d.addCallback( self._cbRequestSSI, (revision, groups, permit, deny, permitMode, visibility)) return d return (groups[0].users, permit, deny, permitMode, visibility, timestamp, revision)
def __init__(self, protocol, factory): policies.ProtocolWrapper.__init__(self, protocol, factory) self.deferred = defer.Deferred()
def getChatInfo(self, exchange, name, instance): d = defer.Deferred() self.sendSNAC(0x0d,0x04,struct.pack('!HB',exchange,len(name)) + \ name + struct.pack('!HB',instance,2)). \ addCallback(self._cbGetChatInfo, d) return d