Esempio n. 1
0
    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
Esempio n. 2
0
 def stopListening(self):
     if self.connected:
         result = self.d = defer.Deferred()
     else:
         result = None
     self._loseConnection()
     return result
Esempio n. 3
0
 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
Esempio n. 4
0
    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
Esempio n. 5
0
 def requestSelfInfo(self):
     """
     ask for the OSCARUser for ourselves
     """
     d = defer.Deferred()
     self.sendSNAC(0x01, 0x0E, '').addCallback(self._cbRequestSelfInfo, d)
     return d
Esempio n. 6
0
    def test_noAutoAddSubdirectory(self):
        """
        L{inotify.INotify.watch} with autoAdd==False will stop inotify
        from watching subdirectories created under the watched one.
        """
        def _callback(wp, fp, mask):
            # We are notified before we actually process new
            # directories, so we need to defer this check.
            def _():
                try:
                    self.assertFalse(self.inotify._isWatched(subdir.path))
                    d.callback(None)
                except Exception:
                    d.errback()

            reactor.callLater(0, _)

        checkMask = inotify.IN_ISDIR | inotify.IN_CREATE
        self.inotify.watch(self.dirname,
                           mask=checkMask,
                           autoAdd=False,
                           callbacks=[_callback])
        subdir = self.dirname.child('test')
        d = defer.Deferred()
        subdir.createDirectory()
        return d
Esempio n. 7
0
    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
Esempio n. 8
0
    def _notificationTest(self, mask, operation, expectedPath=None):
        """
        Test notification from some filesystem operation.

        @param mask: The event mask to use when setting up the watch.

        @param operation: A function which will be called with the
            name of a file in the watched directory and which should
            trigger the event.

        @param expectedPath: Optionally, the name of the path which is
            expected to come back in the notification event; this will
            also be passed to C{operation} (primarily useful when the
            operation is being done to the directory itself, not a
            file in it).

        @return: A L{Deferred} which fires successfully when the
            expected event has been received or fails otherwise.
        """
        if expectedPath is None:
            expectedPath = self.dirname.child("foo.bar")
        notified = defer.Deferred()

        def cbNotified((watch, filename, events)):
            self.assertEquals(filename, expectedPath)
            self.assertTrue(events & mask)

        notified.addCallback(cbNotified)

        self.inotify.watch(self.dirname,
                           mask=mask,
                           callbacks=[lambda *args: notified.callback(args)])
        operation(expectedPath)
        return notified
Esempio n. 9
0
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{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{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
Esempio n. 10
0
    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
Esempio n. 11
0
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
Esempio n. 12
0
 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()
Esempio n. 13
0
    def test_simpleSubdirectoryAutoAdd(self):
        """
        L{inotify.INotify} when initialized with autoAdd==True adds
        also adds the created subdirectories to the watchlist.
        """
        def _callback(wp, filename, mask):
            # We are notified before we actually process new
            # directories, so we need to defer this check.
            def _():
                try:
                    self.assertTrue(self.inotify._isWatched(subdir))
                    d.callback(None)
                except Exception:
                    d.errback()

            reactor.callLater(0, _)

        checkMask = inotify.IN_ISDIR | inotify.IN_CREATE
        self.inotify.watch(self.dirname,
                           mask=checkMask,
                           autoAdd=True,
                           callbacks=[_callback])
        subdir = self.dirname.child('test')
        d = defer.Deferred()
        subdir.createDirectory()
        return d
Esempio n. 14
0
def define(host, port, database, word):
    """Look up a word using a dict server"""
    d = defer.Deferred()
    factory = DictLookupFactory("define", (database, word), d)

    from reqs.twisted.internet import reactor
    reactor.connectTCP(host, port, factory)
    return d
Esempio n. 15
0
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 reqs.twisted.internet import reactor
    reactor.connectTCP(host, port, factory)
    return d
Esempio n. 16
0
 def _startLogOn(self, *args):
     """
     Set self.loginDeferred to the same as the deferred returned, allowing a
     testcase to .callback or .errback.
     
     @return: A deferred.
     """
     self.loginDeferred = defer.Deferred()
     return self.loginDeferred
Esempio n. 17
0
    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]
Esempio n. 18
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)
Esempio n. 19
0
    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
Esempio n. 20
0
 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))
Esempio n. 21
0
    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
Esempio n. 22
0
 def test_onFailure(self):
     """
     Test that the SASL error condition is correctly extracted.
     """
     failure = domish.Element(
         ('urn:ietf:params:xml:ns:xmpp-sasl', 'failure'))
     failure.addElement('not-authorized')
     self.init._deferred = defer.Deferred()
     self.init.onFailure(failure)
     self.assertFailure(self.init._deferred, sasl.SASLAuthError)
     self.init._deferred.addCallback(
         lambda e: self.assertEquals('not-authorized', e.condition))
     return self.init._deferred
Esempio n. 23
0
    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
Esempio n. 24
0
def _callProtocolWithDeferred(protocol,
                              executable,
                              args,
                              env,
                              path,
                              reactor=None):
    if reactor is None:
        from reqs.twisted.internet import reactor

    d = defer.Deferred()
    p = protocol(d)
    reactor.spawnProcess(p, executable, (executable, ) + tuple(args), env,
                         path)
    return d
Esempio n. 25
0
 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)
Esempio n. 26
0
    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
Esempio n. 27
0
    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
Esempio n. 28
0
 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)
Esempio n. 29
0
    def test_complexSubdirectoryAutoAdd(self):
        """
        L{inotify.INotify} with autoAdd==True for a watched path
        generates events for every file or directory already present
        in a newly created subdirectory under the watched one.

        This tests that we solve a race condition in inotify even though
        we may generate duplicate events.
        """
        calls = set()

        def _callback(wp, filename, mask):
            calls.add(filename)
            if len(calls) == 6:
                try:
                    self.assertTrue(self.inotify._isWatched(subdir))
                    self.assertTrue(self.inotify._isWatched(subdir2))
                    self.assertTrue(self.inotify._isWatched(subdir3))
                    created = someFiles + [subdir, subdir2, subdir3]
                    self.assertEquals(len(calls), len(created))
                    self.assertEquals(calls, set(created))
                except Exception:
                    d.errback()
                else:
                    d.callback(None)

        checkMask = inotify.IN_ISDIR | inotify.IN_CREATE
        self.inotify.watch(self.dirname,
                           mask=checkMask,
                           autoAdd=True,
                           callbacks=[_callback])
        subdir = self.dirname.child('test')
        subdir2 = subdir.child('test2')
        subdir3 = subdir2.child('test3')
        d = defer.Deferred()
        subdir3.makedirs()

        someFiles = [
            subdir.child('file1.dat'),
            subdir2.child('file2.dat'),
            subdir3.child('file3.dat')
        ]
        # Add some files in pretty much all the directories so that we
        # see that we process all of them.
        for i, filename in enumerate(someFiles):
            filename.setContent(filename.path)
        return d
Esempio n. 30
0
    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