~ntt-pf-lab/nova/monkey_patch_notification

« back to all changes in this revision

Viewing changes to vendor/Twisted-10.0.0/doc/core/howto/tutorial/listings/finger/fingerproxy.tac

  • Committer: Jesse Andrews
  • Date: 2010-05-28 06:05:26 UTC
  • Revision ID: git-v1:bf6e6e718cdc7488e2da87b21e258ccc065fe499
initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# finger proxy
 
2
from twisted.application import internet, service
 
3
from twisted.internet import defer, protocol, reactor
 
4
from twisted.protocols import basic
 
5
from twisted.python import components
 
6
from zope.interface import Interface, implements
 
7
 
 
8
 
 
9
def catchError(err):
 
10
    return "Internal error in server"
 
11
 
 
12
class IFingerService(Interface):
 
13
 
 
14
    def getUser(user):
 
15
        """Return a deferred returning a string"""
 
16
 
 
17
    def getUsers():
 
18
        """Return a deferred returning a list of strings"""
 
19
 
 
20
 
 
21
class IFingerFactory(Interface):
 
22
 
 
23
    def getUser(user):
 
24
        """Return a deferred returning a string"""
 
25
 
 
26
    def buildProtocol(addr):
 
27
        """Return a protocol returning a string"""
 
28
 
 
29
class FingerProtocol(basic.LineReceiver):
 
30
            
 
31
    def lineReceived(self, user):
 
32
        d = self.factory.getUser(user)
 
33
        d.addErrback(catchError)
 
34
        def writeValue(value):
 
35
            self.transport.write(value)
 
36
            self.transport.loseConnection()
 
37
        d.addCallback(writeValue)
 
38
 
 
39
 
 
40
 
 
41
class FingerFactoryFromService(protocol.ClientFactory):
 
42
    
 
43
    implements(IFingerFactory)
 
44
 
 
45
    protocol = FingerProtocol
 
46
    
 
47
    def __init__(self, service):
 
48
        self.service = service
 
49
        
 
50
    def getUser(self, user):
 
51
        return self.service.getUser(user)
 
52
 
 
53
 
 
54
components.registerAdapter(FingerFactoryFromService,
 
55
                           IFingerService,
 
56
                           IFingerFactory)
 
57
 
 
58
class FingerClient(protocol.Protocol):
 
59
                                
 
60
    def connectionMade(self):
 
61
        self.transport.write(self.factory.user+"\r\n")
 
62
        self.buf = []                        
 
63
 
 
64
    def dataReceived(self, data):
 
65
        self.buf.append(data)
 
66
 
 
67
    def connectionLost(self, reason):
 
68
        self.factory.gotData(''.join(self.buf))
 
69
 
 
70
class FingerClientFactory(protocol.ClientFactory):
 
71
 
 
72
    protocol = FingerClient
 
73
 
 
74
    def __init__(self, user):
 
75
        self.user = user
 
76
        self.d = defer.Deferred()
 
77
 
 
78
    def clientConnectionFailed(self, _, reason):
 
79
        self.d.errback(reason)
 
80
 
 
81
    def gotData(self, data):
 
82
        self.d.callback(data)
 
83
 
 
84
        
 
85
def finger(user, host, port=79):
 
86
    f = FingerClientFactory(user)
 
87
    reactor.connectTCP(host, port, f)                   
 
88
    return f.d
 
89
 
 
90
 
 
91
class ProxyFingerService(service.Service):
 
92
    implements(IFingerService)
 
93
 
 
94
    def getUser(self, user):
 
95
        try:
 
96
            user, host = user.split('@', 1)
 
97
        except:
 
98
            user = user.strip()
 
99
            host = '127.0.0.1'
 
100
        ret = finger(user, host)
 
101
        ret.addErrback(lambda _: "Could not connect to remote host")
 
102
        return ret
 
103
 
 
104
    def getUsers(self):
 
105
        return defer.succeed([])
 
106
                             
 
107
application = service.Application('finger', uid=1, gid=1) 
 
108
f = ProxyFingerService()
 
109
internet.TCPServer(7779, IFingerFactory(f)).setServiceParent(
 
110
    service.IServiceCollection(application))