[Twisted-Python] Re: PBServerFactory - which client disconnected?

Mike Stoddart stodge at gmail.com
Thu Apr 2 14:18:07 EDT 2009


So I can do the following but the onDisconnect and onConnect functions don't
receive the broker - odd design decision?
Also, there's a comment in PBServerFactory.clientConnectionMade --> "# XXX
does this method make any sense?".  Weird.

class MyServerObject(pb.Root):
    def __init__(self):
        pass

    def onConnect(self):
        log.msg("MyServerObject::onConnect")

    def onDisconnect(self):
        log.msg("MyServerObject::onDisconnect")

class MyServerFactory(pb.PBServerFactory):

    def buildProtocol(self, addr):
        s = addr.host + ":" + str(addr.port)
        log.msg("MyServerFactory::buildProtocol - building protocol for " +
s)
        broker = pb.PBServerFactory.buildProtocol(self, addr)
        broker.notifyOnDisconnect(self.root.onDisconnect)
        broker.notifyOnConnect(self.root.onConnect)
        return broker

reactor.listenTCP(9999, MyServerFactory(MyServerObject()))
reactor.run()

On Thu, Apr 2, 2009 at 8:02 AM, Mike Stoddart <stodge at gmail.com> wrote:

> I'm experimenting with Twisted Spread for a registry server, among other
> things. I'm just curious how the onDisconnect function knows which client
> disconnected? I'm also curious why the PBServerFactory class doesn't have a
> disconnection function to complement clientConnectionMade? I don't need
> security - this is running on a closed, secure LAN and I don't need
> "Avatars". Any suggestions on how to improve this?
>
> from twisted.internet import protocol, reactor
> from twisted.python import log
> from twisted.spread import pb
> import sys
>
> log.startLogging(sys.stdout)
>
>
> class RegistryService(pb.Root):
>     def __init__(self):
>         self._clientCount = 0
>         self._clients = {}
>
>     def remote_register(self, name, service):
>         log.msg("Registering service " + name)
>         log.msg(str(service))
>
>         # Welcome the client.
>         service.callRemote("welcome")
>
>     def onDisconnect(self):
>         log.msg("onDisconnect")
>
>         log.msg("Client count = " + str(len(self._clients)))
>
>     def onConnect(self, broker):
>         s = broker.transport.getPeer().host + ":" +
> str(broker.transport.getPeer().port)
>         log.msg("onConnect - client at " + s)
>
>         # Add the client.
>         self._clients[s] = broker.transport
>         log.msg("Client count = " + str(len(self._clients)))
>
>         # Set a callback in case they disconnect.
>         broker.notifyOnDisconnect(self.onDisconnect)
>
> class MyPBFactory(pb.PBServerFactory):
>     def __init__(self, root):
>         pb.PBServerFactory.__init__(self, root)
>
>     def clientConnectionMade(self, broker):
>         log.msg("clientConnectionMade")
>         self.root.onConnect(broker)
>
> registry = RegistryService()
> f = MyPBFactory(RegistryService())
> reactor.listenTCP(9999, f)
> reactor.run()
>
>
> Thanks
> Mike
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://twistedmatrix.com/pipermail/twisted-python/attachments/20090402/30a73876/attachment.htm 


More information about the Twisted-Python mailing list