source: ralphm-patches/s2s-controller.patch @ 47:f6d222b68f1c

Last change on this file since 47:f6d222b68f1c was 15:3fd4aaba4a99, checked in by Ralph Meijer <ralphm@…>, 13 years ago

Save recent work.

File size: 5.7 KB
  • wokkel/server.py

    diff -r 5a5840e066fb -r 86b4a357ecae wokkel/server.py
    a b  
    557557        else:
    558558            self.service.dispatch(xs, element)
    559559
     560class XMPPS2SClientFactory(BootstrapMixin, protocol.ClientFactory):
    560561
     562    protocol = xmlstream.XmlStream
     563
     564    def __init__(self, thisHost, otherHost, secret)
     565        BootstrapMixin.__init__(self)
     566
     567        self.thisHost = thisHost
     568        self.otherHost = otherHost
     569
     570        self.authenticator = XMPPServerConnectAuthenticator(self.thisHost,
     571                                                            self.otherHost,
     572                                                            secret)
     573
     574
     575    def buildProtocol(self, addr):
     576        """
     577        Create an instance of XmlStream.
     578
     579        A new authenticator instance will be created and passed to the new
     580        XmlStream. Registered bootstrap event observers are installed as well.
     581        """
     582        xs = self.protocol(self.authenticator)
     583        xs.factory = self
     584        self.installBootstraps(xs)
     585        return xs
     586
     587
     588    def startedConnecting(self, connector):
     589        """
     590        Called when a connection has been started.
     591        """
     592
     593
     594    def clientConnectionFailed(self, connector, reason):
     595        """
     596        Called when a connection has failed to connect.
     597        """
     598
     599
     600    def clientConnectionLost(self, connector, reason):
     601        """
     602        Called when an established connection is lost.
     603        """
     604
     605
     606class StreamController(modal.Modal):
     607
     608    initialMode = 'disconnected'
     609
     610    def __init__(self, factory):
     611        factory.addBootstrap(xmlstream.STREAM_AUTHD_EVENT,
     612                             self.streamValid)
     613        factory.addBootstrap(xmlstream.INIT_FAILED_EVENT,
     614                             self.streamInvalid)
     615
     616
     617
     618        self.xmlstream = None
     619
     620
     621    class connecting(modal.mode):
     622        def __enter__(self):
     623            self.pending = []
     624
     625        def send(self, obj):
     626            self.pending.append(obj)
     627
     628
     629    class connected(modal.mode):
     630        def send(self, obj):
     631            self.xmlstream.send(obj)
     632
     633
     634    class disconnected(modal.mode):
     635        pass
     636
     637
     638    def streamValid(self, xs):
     639        """
     640        Called when the stream was validated.
     641        """
     642        connecting = False
     643        self.xmlstream = xs
     644
     645
     646    def streamInvalid(self, xs):
     647        """
     648        Called when the stream was not validated.
     649        """
     650        connecting = False
     651
     652
     653    def send(self, obj):
     654        """
     655        Send or queue obj.
     656        """
     657        if self.xmlstream is None:
     658            # queue up
     659
     660        else:
     661            # send immediately
    561662
    562663class ServerService(object):
    563664    """
     
    601702        xs.addObserver(xmlstream.STREAM_END_EVENT,
    602703                       lambda _: self.outgoingDisconnected(xs))
    603704
    604         if (thisHost, otherHost) in self._outgoingQueues:
    605             for element in self._outgoingQueues[thisHost, otherHost]:
    606                 xs.send(element)
    607             del self._outgoingQueues[thisHost, otherHost]
    608 
    609 
    610705    def outgoingDisconnected(self, xs):
    611706        thisHost = xs.thisEntity.host
    612707        otherHost = xs.otherEntity.host
     
    622717        Initiate an outgoing XMPP server-to-server connection.
    623718        """
    624719
    625         def resetConnecting(_):
     720        def resetConnecting(result):
    626721            self._outgoingConnecting.remove((thisHost, otherHost))
     722            return result
    627723
    628         if (thisHost, otherHost) in self._outgoingConnecting:
    629             return
     724        #if (thisHost, otherHost) in self._outgoingConnecting:
     725        #    return
    630726
    631727        authenticator = XMPPServerConnectAuthenticator(thisHost,
    632728                                                       otherHost,
    633729                                                       self.secret)
    634730        factory = DeferredS2SClientFactory(authenticator)
    635         factory.addBootstrap(xmlstream.STREAM_AUTHD_EVENT,
    636                              self.outgoingInitialized)
     731        #factory.addBootstrap(xmlstream.STREAM_AUTHD_EVENT,
     732        #                     self.outgoingInitialized)
    637733        factory.logTraffic = self.logTraffic
    638734
    639735        self._outgoingConnecting.add((thisHost, otherHost))
    640736
    641737        d = initiateS2S(factory)
    642738        d.addBoth(resetConnecting)
     739        d.addCallback(self.outgoingInitialized)
    643740        return d
    644741
    645742
  • wokkel/test/test_server.py

    diff -r 5a5840e066fb -r 86b4a357ecae wokkel/test/test_server.py
    a b  
    448448        self.service.dispatch(self.xmlstream, stanza)
    449449
    450450        self.assertEqual(1, len(errors))
     451
     452
     453    def test_send(self):
     454        print
     455        factories = []
     456        inits = []
     457
     458        def initiateS2S(factory):
     459            factories.append(factory)
     460            return factory.deferred
     461
     462        self.patch(server, 'initiateS2S', initiateS2S)
     463        #self.service.outgoingInitialized = lambda xs: inits.append(xs)
     464        stanza = domish.Element((None, "presence"))
     465        stanza['to'] = 'other@example.com'
     466        stanza['from'] = 'user@example.org'
     467
     468        print "Sending first stanza"
     469        self.service.send(stanza)
     470        print "Output after first send", self.output
     471        xs = factories[-1].buildProtocol(None)
     472        xs.thisEntity = jid.JID('example.org')
     473        xs.otherEntity = jid.JID('example.com')
     474        xs.serial = 1
     475        xs.send = self.output.append
     476
     477        print "Authenticating"
     478        xs.dispatch(xs, xmlstream.STREAM_AUTHD_EVENT)
     479        print "Output after authd", self.output
     480
     481        print "Sending second stanza"
     482        self.service.send(stanza)
     483        print "Output after second send", self.output
     484
     485
Note: See TracBrowser for help on using the repository browser.