# The Evolution of Finger: Twisted client support using Perspective Broker

## Introduction

This is the seventh part of the Twisted tutorial Twisted from Scratch, or The Evolution of Finger.

In this part, we add a Perspective Broker service to the finger application so that Twisted clients can access the finger server.

## Use Perspective Broker

We add support for perspective broker, Twisted's native remote object protocol. Now, Twisted clients will not have to go through XML-RPCish contortions to get information about users.

    # Do everything properly, and componentize
    from twisted.application import internet, service
    from twisted.internet import protocol, reactor, defer
    from twisted.protocols import basic, irc
    from twisted.python import components
    from twisted.web import resource, server, static, xmlrpc, microdom
    from twisted.spread import pb
    from zope.interface import Interface, implements
    import cgi

    class IFingerService(Interface):

        def getUser(self, user):
            """Return a deferred returning a string"""

        def getUsers(self):
            """Return a deferred returning a list of strings"""

    class IFingerSetterService(Interface):

        def setUser(self, user, status):
            """Set the user's status to something"""

    def catchError(err):
        return "Internal error in server"

    class FingerProtocol(basic.LineReceiver):

        def lineReceived(self, user):
            d = self.factory.getUser(user)
            d.addErrback(catchError)
            def writeValue(value):
                self.transport.write(value+'\r\n')
                self.transport.loseConnection()
            d.addCallback(writeValue)

    class IFingerFactory(Interface):

        def getUser(self, user):
            """Return a deferred returning a string"""

        def buildProtocol(self, addr):
            """Return a protocol returning a string"""

    class FingerFactoryFromService(protocol.ServerFactory):

        implements(IFingerFactory)

        protocol = FingerProtocol

        def __init__(self, service):
            self.service = service

        def getUser(self, user):
            return self.service.getUser(user)

    components.registerAdapter(FingerFactoryFromService,
                               IFingerService,
                               IFingerFactory)

    class FingerSetterProtocol(basic.LineReceiver):

        def connectionMade(self):
            self.lines = []

        def lineReceived(self, line):
            self.lines.append(line)

        def connectionLost(self, reason):
            if len(self.lines) == 2:
                self.factory.setUser(*self.lines)

    class IFingerSetterFactory(Interface):

        def setUser(self, user, status):
            """Return a deferred returning a string"""

        def buildProtocol(self, addr):
            """Return a protocol returning a string"""

    class FingerSetterFactoryFromService(protocol.ServerFactory):

        implements(IFingerSetterFactory)

        protocol = FingerSetterProtocol

        def __init__(self, service):
            self.service = service

        def setUser(self, user, status):
            self.service.setUser(user, status)

    components.registerAdapter(FingerSetterFactoryFromService,
                               IFingerSetterService,
                               IFingerSetterFactory)

    class IRCReplyBot(irc.IRCClient):

        def connectionMade(self):
            self.nickname = self.factory.nickname
            irc.IRCClient.connectionMade(self)

        def privmsg(self, user, channel, msg):
            user = user.split('!')[0]
            if self.nickname.lower() == channel.lower():
                d = self.factory.getUser(msg)
                d.addErrback(catchError)
                d.addCallback(lambda m: "Status of %s: %s" % (msg, m))
                d.addCallback(lambda m: self.msg(user, m))

    class IIRCClientFactory(Interface):

        """
        @ivar nickname
        """

        def getUser(self, user):
            """Return a deferred returning a string"""

        def buildProtocol(self, addr):
            """Return a protocol"""

    class IRCClientFactoryFromService(protocol.ClientFactory):

        implements(IIRCClientFactory)

        protocol = IRCReplyBot
        nickname = None

        def __init__(self, service):
            self.service = service

        def getUser(self, user):
            return self.service.getUser(user)

    components.registerAdapter(IRCClientFactoryFromService,
                               IFingerService,
                               IIRCClientFactory)

    class UserStatusTree(resource.Resource):

        def __init__(self, service):
            resource.Resource.__init__(self)
            self.service=service

            # add a specific child for the path "RPC2"
            self.putChild("RPC2", UserStatusXR(self.service))

            # need to do this for resources at the root of the site
            self.putChild("", self)

        def _cb_render_GET(self, users, request):
            userOutput = ''.join(["<li><a href=\"%s\">%s</a></li>" % (user, user)
                    for user in users])
            request.write("""
                <html><head><title>Users</title></head><body>
                <h1>Users</h1>
                <ul>
                %s
                </ul></body></html>""" % userOutput)
            request.finish()

        def render_GET(self, request):
            d = self.service.getUsers()
            d.addCallback(self._cb_render_GET, request)

            # signal that the rendering is not complete
            return server.NOT_DONE_YET

        def getChild(self, path, request):
            return UserStatus(user=path, service=self.service)

    components.registerAdapter(UserStatusTree, IFingerService, resource.IResource)

    class UserStatus(resource.Resource):

        def __init__(self, user, service):
            resource.Resource.__init__(self)
            self.user = user
            self.service = service

        def _cb_render_GET(self, status, request):
            request.write("""<html><head><title>%s</title></head>
            <body><h1>%s</h1>
            <p>%s</p>
            </body></html>""" % (self.user, self.user, status))
            request.finish()

        def render_GET(self, request):
            d = self.service.getUser(self.user)
            d.addCallback(self._cb_render_GET, request)

            # signal that the rendering is not complete
            return server.NOT_DONE_YET

    class UserStatusXR(xmlrpc.XMLRPC):

        def __init__(self, service):
            xmlrpc.XMLRPC.__init__(self)
            self.service = service

        def xmlrpc_getUser(self, user):
            return self.service.getUser(user)

        def xmlrpc_getUsers(self):
            return self.service.getUsers()

    class IPerspectiveFinger(Interface):

        def remote_getUser(self, username):
            """return a user's status"""

        def remote_getUsers(self):
            """return a user's status"""

    class PerspectiveFingerFromService(pb.Root):

        implements(IPerspectiveFinger)

        def __init__(self, service):
            self.service = service

        def remote_getUser(self, username):
            return self.service.getUser(username)

        def remote_getUsers(self):
            return self.service.getUsers()

    components.registerAdapter(PerspectiveFingerFromService,
                               IFingerService,
                               IPerspectiveFinger)

    class FingerService(service.Service):

        implements(IFingerService)

        def __init__(self, filename):
            self.filename = filename
            self._read()

        def _read(self):
            self.users = {}
            for line in file(self.filename):
                user, status = line.split(':', 1)
                user = user.strip()
                status = status.strip()
                self.users[user] = status
            self.call = reactor.callLater(30, self._read)

        def getUser(self, user):
            return defer.succeed(self.users.get(user, "No such user"))

        def getUsers(self):
            return defer.succeed(self.users.keys())

    application = service.Application('finger', uid=1, gid=1)
    f = FingerService('/etc/users')
    serviceCollection = service.IServiceCollection(application)
    internet.TCPServer(79, IFingerFactory(f)
                       ).setServiceParent(serviceCollection)
    internet.TCPServer(8000, server.Site(resource.IResource(f))
                       ).setServiceParent(serviceCollection)
    i = IIRCClientFactory(f)
    i.nickname = 'fingerbot'
    internet.TCPClient('irc.freenode.org', 6667, i
                       ).setServiceParent(serviceCollection)
    internet.TCPServer(8889, pb.PBServerFactory(IPerspectiveFinger(f))
                       ).setServiceParent(serviceCollection)

Source listing - listings/finger/finger21.py

A simple client to test the perspective broker finger:

    # test the PB finger on port 8889
    # this code is essentially the same as
    # the first example in howto/pb-usage

    from twisted.spread import pb
    from twisted.internet import reactor

    def gotObject(object):
        print "got object:", object
        object.callRemote("getUser","moshez").addCallback(gotData)
    # or
    #   object.callRemote("getUsers").addCallback(gotData)

    def gotData(data):
        print 'server sent:', data
        reactor.stop()

    def gotNoObject(reason):
        print "no object:",reason
        reactor.stop()

    factory = pb.PBClientFactory()
    reactor.connectTCP("127.0.0.1",8889, factory)
    factory.getRootObject().addCallbacks(gotObject,gotNoObject)
    reactor.run()

Source listing - listings/finger/fingerPBclient.py

Index

Version: 2.0.1