[Twisted-Python] PBServerFactory - which client disconnected?

Mike Stoddart stodge at gmail.com
Thu Apr 2 08:02:42 EDT 2009


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/ca084ac5/attachment.htm 


More information about the Twisted-Python mailing list