Changeset 68:14b59e18ecb6 in ralphm-patches for c2s_server_factory.patch


Ignore:
Timestamp:
Oct 21, 2012, 6:04:01 AM (8 years ago)
Author:
Ralph Meijer <ralphm@…>
Branch:
default
Message:

Checkpoint

File:
1 edited

Legend:

Unmodified
Added
Removed
  • c2s_server_factory.patch

    r66 r68  
    11# HG changeset patch
    2 # Parent 23dbe722e4482b8286153d29bf87902b464f919f
     2# Parent f164cc75b8337933cc6dd9c7f43424f3dbd74dee
    33Add factory for accepting client connections.
    44
     
    2424--- a/wokkel/client.py
    2525+++ b/wokkel/client.py
    26 @@ -21,6 +21,7 @@
     26@@ -21,7 +21,9 @@
    2727 from twisted.words.xish import domish
    2828 
     
    3030+from wokkel.compat import XmlStreamServerFactory
    3131 from wokkel.iwokkel import IUserSession
     32+from wokkel.subprotocols import ServerStreamManager
    3233 from wokkel.subprotocols import StreamManager
    3334 
    34 @@ -401,3 +402,61 @@
     35 NS_CLIENT = 'jabber:client'
     36@@ -442,3 +444,41 @@
    3537             self.portal = self.portals[self.xmlstream.thisEntity]
    3638         except KeyError:
     
    4143+class XMPPC2SServerFactory(XmlStreamServerFactory):
    4244+
    43 +    def __init__(self, portal):
    44 +        self.portal = portal
    45 +
     45+    def __init__(self, portals):
    4646+        def authenticatorFactory():
    47 +            return XMPPClientListenAuthenticator(portal)
     47+            return XMPPClientListenAuthenticator(portals)
    4848+
    4949+        XmlStreamServerFactory.__init__(self, authenticatorFactory)
    5050+        self.addBootstrap(xmlstream.STREAM_CONNECTED_EVENT,
    5151+                          self.onConnectionMade)
    52 +        self.addBootstrap(xmlstream.STREAM_AUTHD_EVENT,
    53 +                          self.onAuthenticated)
    54 +
    55 +        self.serial = 0
    5652+
    5753+
    5854+    def onConnectionMade(self, xs):
    5955+        """
    60 +        Called when a client-to-server connection was made.
    61 +
    62 +        This enables traffic debugging on incoming streams.
    63 +        """
    64 +        xs.serial = self.serial
    65 +        self.serial += 1
    66 +
    67 +        log.msg("Client connection %d made" % xs.serial)
    68 +
    69 +        def logDataIn(buf):
    70 +            log.msg("RECV (%d): %r" % (xs.serial, buf))
    71 +
    72 +        def logDataOut(buf):
    73 +            log.msg("SEND (%d): %r" % (xs.serial, buf))
    74 +
     56+        Called when a connection is made.
     57+
     58+        This creates a stream manager, calls L{setupHandlers} to attach
     59+        subprotocol handlers and then signals the stream manager that
     60+        the connection was made.
     61+        """
     62+        sm = ServerStreamManager()
     63+        sm.logTraffic = self.logTraffic
     64+
     65+        for handler in self.setupHandlers():
     66+            handler.setHandlerParent(sm)
     67+
     68+        sm.makeConnection(xs)
     69+
     70+
     71+    def setupHandlers(self):
     72+        """
     73+        Set up XMPP subprotocol handlers.
     74+        """
     75+        return [
     76+            generic.StanzaForwarder()
     77+        ]
     78diff --git a/wokkel/generic.py b/wokkel/generic.py
     79--- a/wokkel/generic.py
     80+++ b/wokkel/generic.py
     81@@ -615,3 +615,55 @@
     82 
     83         self._initializers = self.getInitializers()
     84         self._initializeStream()
     85+
     86+
     87+
     88+class StanzaForwarder(XMPPHandler):
     89+    """
     90+    XMPP protocol for passing incoming stanzas to the stream avatar.
     91+
     92+    This handler adds an observer for all XML Stanzas to forward to the C{send}
     93+    method on the cred avatar set on the XML Stream, unless it has been handled
     94+    by other observers.
     95+
     96+    Stream errors are logged.
     97+    """
     98+
     99+    def connectionMade(self):
     100+        """
     101+        Called when a connection is made.
     102+        """
     103+        self.xmlstream.addObserver(xmlstream.STREAM_ERROR_EVENT, self.onError)
     104+
     105+
     106+    def connectionInitialized(self):
     107+        """
     108+        Called when the stream has been initialized.
     109+        """
     110+        self.xmlstream.addObserver(
     111+            '/*[@xmlns="%s"]' % self.xmlstream.namespace,
     112+            self.onStanza, priority=-1)
     113+
     114+
     115+    def onStanza(self, element):
     116+        """
     117+        Called when a stanza element was received.
     118+
     119+        Unless a stanza has already been handled, or the name of the element is
     120+        not one of C{'iq'}, C{'message'}, C{'presence'}, the stanza is passed
     121+        on to the avatar's C{send} method.
     122+        """
     123+        if element.handled:
     124+            return
     125+
     126+        if element.name not in ('iq', 'message', 'presence'):
     127+            return
     128+
     129+        self.xmlstream.avatar.send(element)
     130+
     131+
     132+    def onError(reason):
     133+        """
     134+        Log a stream error.
     135+        """
     136+        log.err(reason, "Stream error")
     137diff --git a/wokkel/subprotocols.py b/wokkel/subprotocols.py
     138--- a/wokkel/subprotocols.py
     139+++ b/wokkel/subprotocols.py
     140@@ -142,10 +142,13 @@
     141     @type timeout: C{int}
     142 
     143     @ivar _reactor: A provider of L{IReactorTime} to track timeouts.
     144+
     145+    @cvar __streamCount: Global stream count for distinguishing streams.
     146     """
     147 
     148     timeout = None
     149     _reactor = None
     150+    __streamCount = 0
     151 
     152     logTraffic = False
     153 
     154@@ -195,20 +198,24 @@
     155         and call each handler's C{makeConnection} method with the L{XmlStream}
     156         instance.
     157         """
     158-        def logDataIn(buf):
     159-            log.msg("RECV: %r" % buf)
     160+        xs.serial = self.__streamCount
     161+        BaseStreamManager.__streamCount += 1
     162 
     163-        def logDataOut(buf):
     164-            log.msg("SEND: %r" % buf)
     165 
     166         if self.logTraffic:
     167-            xs.rawDataInFn = logDataIn
     168-            xs.rawDataOutFn = logDataOut
     169+            def logData(direction, data):
     170+                log.msg(format="%(direction)s (%(streamID)s): %(data)r",
     171+                        direction=direction, streamID=xs.serial, data=data)
     172+
     173+            log.msg(format="Connection %(streamID) made", streamID=xs.serial)
     174+            xs.rawDataInFn = lambda data: logData("RECV", data)
     175+            xs.rawDataOutFn = lambda data: logData("SEND", data)
     176 
     177         xs.addObserver(xmlstream.STREAM_AUTHD_EVENT,
     178                        self.connectionInitialized)
     179         xs.addObserver(xmlstream.STREAM_END_EVENT,
     180                        self.connectionLost)
     181+
     182         self.xmlstream = xs
     183 
     184         for e in list(self):
     185@@ -222,6 +229,9 @@
     186         Send out cached stanzas and call each handler's
     187         C{connectionInitialized} method.
     188         """
    75189+        if self.logTraffic:
    76 +            xs.rawDataInFn = logDataIn
    77 +            xs.rawDataOutFn = logDataOut
    78 +
    79 +        xs.addObserver(xmlstream.STREAM_END_EVENT, self.onConnectionLost,
    80 +                                                   0, xs)
    81 +        xs.addObserver(xmlstream.STREAM_ERROR_EVENT, self.onError)
    82 +
    83 +
    84 +    def onAuthenticated(self, xs):
    85 +        log.msg("Client connection %d authenticated" % xs.serial)
    86 +
    87 +        xs.addObserver('/*', xs.avatar.send)
    88 +
    89 +
    90 +    def onConnectionLost(self, xs, reason):
    91 +        log.msg("Client connection %d disconnected" % xs.serial)
    92 +
    93 +
    94 +    def onError(self, reason):
    95 +        log.err(reason, "Stream Error")
     190+            log.msg(format="Connection %(streamID) initialized",
     191+                    streamID=xs.serial)
     192 
     193         xs.addObserver('/iq[@type="result"]', self._onIQResponse)
     194         xs.addObserver('/iq[@type="error"]', self._onIQResponse)
     195@@ -247,6 +257,10 @@
     196         L{XmlStream} anymore and notifies each handler that the connection
     197         was lost by calling its C{connectionLost} method.
     198         """
     199+        if self.logTraffic:
     200+            log.msg(format="Connection %(streamID) lost",
     201+                    streamID=self.xmlstream.serial)
     202+
     203         self.xmlstream = None
     204         self._initialized = False
     205 
Note: See TracChangeset for help on using the changeset viewer.