Changeset 66:b713f442b222 in ralphm-patches for c2s_server_factory.patch


Ignore:
Timestamp:
Sep 1, 2012, 12:40:53 PM (8 years ago)
Author:
Ralph Meijer <ralphm@…>
Branch:
default
Message:

Add many tests, docstrings for authenticator, make example functional.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • c2s_server_factory.patch

    r57 r66  
    11# HG changeset patch
    2 # Parent d76497171af8f3acf1efd2c8433fbdc3c4a55f92
     2# Parent 23dbe722e4482b8286153d29bf87902b464f919f
    33Add factory for accepting client connections.
    44
     
    2121 * Add tests.
    2222
    23 diff -r d76497171af8 wokkel/client.py
    24 --- a/wokkel/client.py  Wed Nov 30 09:31:07 2011 +0100
    25 +++ b/wokkel/client.py  Wed Nov 30 09:32:01 2011 +0100
    26 @@ -20,6 +20,7 @@
     23diff --git a/wokkel/client.py b/wokkel/client.py
     24--- a/wokkel/client.py
     25+++ b/wokkel/client.py
     26@@ -21,6 +21,7 @@
    2727 from twisted.words.xish import domish
    2828 
    2929 from wokkel import generic
    3030+from wokkel.compat import XmlStreamServerFactory
     31 from wokkel.iwokkel import IUserSession
    3132 from wokkel.subprotocols import StreamManager
    3233 
    33  NS_CLIENT = 'jabber:client'
    34 @@ -347,3 +348,98 @@
    35  
    36  
    37  
    38 +class RecipientUnavailable(Exception):
    39 +    """
    40 +    The addressed entity is not, or no longer, available.
    41 +    """
     34@@ -401,3 +402,61 @@
     35             self.portal = self.portals[self.xmlstream.thisEntity]
     36         except KeyError:
     37             raise error.StreamError('host-unknown')
    4238+
    4339+
     
    4541+class XMPPC2SServerFactory(XmlStreamServerFactory):
    4642+
    47 +    def __init__(self, service):
    48 +        self.service = service
     43+    def __init__(self, portal):
     44+        self.portal = portal
    4945+
    5046+        def authenticatorFactory():
    51 +            return XMPPClientListenAuthenticator(service)
     47+            return XMPPClientListenAuthenticator(portal)
    5248+
    5349+        XmlStreamServerFactory.__init__(self, authenticatorFactory)
     
    5854+
    5955+        self.serial = 0
    60 +        self.streams = {}
    6156+
    6257+
     
    8277+            xs.rawDataOutFn = logDataOut
    8378+
     79+        xs.addObserver(xmlstream.STREAM_END_EVENT, self.onConnectionLost,
     80+                                                   0, xs)
    8481+        xs.addObserver(xmlstream.STREAM_ERROR_EVENT, self.onError)
    8582+
     
    8885+        log.msg("Client connection %d authenticated" % xs.serial)
    8986+
    90 +        xs.addObserver(xmlstream.STREAM_END_EVENT, self.onConnectionLost,
    91 +                                                   0, xs)
    92 +        xs.addObserver('/*', self.onElement, 0, xs)
    93 +
    94 +        # Record this stream as bound to the authenticated JID
    95 +        self.streams[xs.otherEntity] = xs
     87+        xs.addObserver('/*', xs.avatar.send)
    9688+
    9789+
     
    9991+        log.msg("Client connection %d disconnected" % xs.serial)
    10092+
    101 +        entity = xs.otherEntity
    102 +        self.service.unbindResource(entity.user,
    103 +                                    entity.host,
    104 +                                    entity.resource,
    105 +                                    reason)
    106 +
    107 +        # If the lost connections had been bound, remove the reference
    108 +        if xs.otherEntity in self.streams:
    109 +            del self.streams[xs.otherEntity]
    110 +
    11193+
    11294+    def onError(self, reason):
    11395+        log.err(reason, "Stream Error")
    114 +
    115 +
    116 +    def onElement(self, xs, stanza):
    117 +        """
    118 +        Called when an element was received from one of the connected streams.
    119 +
    120 +        """
    121 +        if stanza.handled:
    122 +            return
    123 +        else:
    124 +            self.service.onElement(stanza, xs.otherEntity)
    125 +
    126 +
    127 +    def deliverStanza(self, element, recipient):
    128 +        if recipient in self.streams:
    129 +            self.streams[recipient].send(element)
    130 +        else:
    131 +            raise RecipientUnavailable(u"There is no connection for %s" %
    132 +                                       recipient.full())
Note: See TracChangeset for help on using the changeset viewer.