source: doc/examples/echo_server.tac @ 162:8bffd58d7b6d

Last change on this file since 162:8bffd58d7b6d was 69:8ed65ffed8b2, checked in by Ralph Meijer <ralphm@…>, 13 years ago

Fix docstring.

File size: 3.6 KB
Line 
1"""
2An XMPP echo server as a standalone server via s2s.
3
4This echo server accepts and initiates server-to-server connections using
5dialback and listens on C{127.0.0.1} with the domain C{localhost}. It will
6accept subscription requests for any potential entity at the domain and
7send back messages sent to it.
8"""
9
10from twisted.application import service, strports
11from twisted.words.protocols.jabber.xmlstream import toResponse
12from wokkel import component, server, xmppim
13
14# Configuration parameters
15
16S2S_PORT = 'tcp:5269:interface=127.0.0.1'
17SECRET = 'secret'
18DOMAIN = 'localhost'
19LOG_TRAFFIC = True
20
21
22# Protocol handlers
23
24class PresenceAcceptingHandler(xmppim.PresenceProtocol):
25    """
26    Presence accepting XMPP subprotocol handler.
27
28    This handler blindly accepts incoming presence subscription requests,
29    confirms unsubscription requests and responds to presence probes.
30
31    Note that this handler does not remember any contacts, so it will not
32    send presence when starting.
33    """
34    def subscribedReceived(self, presence):
35        """
36        Subscription approval confirmation was received.
37
38        This is just a confirmation. Don't respond.
39        """
40        pass
41
42
43    def unsubscribedReceived(self, presence):
44        """
45        Unsubscription confirmation was received.
46
47        This is just a confirmation. Don't respond.
48        """
49        pass
50
51
52    def subscribeReceived(self, presence):
53        """
54        Subscription request was received.
55
56        Always grant permission to see our presence.
57        """
58        self.subscribed(recipient=presence.sender,
59                        sender=presence.recipient)
60        self.available(recipient=presence.sender,
61                       status=u"I'm here",
62                       sender=presence.recipient)
63
64
65    def unsubscribeReceived(self, presence):
66        """
67        Unsubscription request was received.
68
69        Always confirm unsubscription requests.
70        """
71        self.unsubscribed(recipient=presence.sender,
72                          sender=presence.recipient)
73
74
75
76    def probeReceived(self, presence):
77        """
78        A presence probe was received.
79
80        Always send available presence to whoever is asking.
81        """
82        self.available(recipient=presence.sender,
83                       status=u"I'm here",
84                       sender=presence.recipient)
85
86
87
88class EchoHandler(xmppim.MessageProtocol):
89    """
90    Message echoing XMPP subprotocol handler.
91    """
92
93    def onMessage(self, message):
94        """
95        Called when a message stanza was received.
96        """
97
98        # Ignore error messages
99        if message.getAttribute('type') == 'error':
100            return
101
102        # Echo incoming messages, if they have a body.
103        if message.body and unicode(message.body):
104            response = toResponse(message, message.getAttribute('type'))
105            response.addElement('body', content=unicode(message.body))
106            self.send(response)
107
108
109
110# Set up the Twisted application
111
112application = service.Application("Ping Server")
113
114router = component.Router()
115
116serverService = server.ServerService(router, domain=DOMAIN, secret=SECRET)
117serverService.logTraffic = LOG_TRAFFIC
118
119s2sFactory = server.XMPPS2SServerFactory(serverService)
120s2sFactory.logTraffic = LOG_TRAFFIC
121s2sService = strports.service(S2S_PORT, s2sFactory)
122s2sService.setServiceParent(application)
123
124echoComponent = component.InternalComponent(router, DOMAIN)
125echoComponent.logTraffic = LOG_TRAFFIC
126echoComponent.setServiceParent(application)
127
128presenceHandler = PresenceAcceptingHandler()
129presenceHandler.setHandlerParent(echoComponent)
130
131echoHandler = EchoHandler()
132echoHandler.setHandlerParent(echoComponent)
Note: See TracBrowser for help on using the repository browser.