1 | |
---|
2 | # Copyright (c) 2001-2006 Twisted Matrix Laboratories. |
---|
3 | # See LICENSE for details. |
---|
4 | import sys |
---|
5 | from zope.interface import implements |
---|
6 | from twisted.internet import iocpreactor |
---|
7 | iocpreactor.install() |
---|
8 | |
---|
9 | from twisted.spread import pb |
---|
10 | from twisted.cred.portal import IRealm |
---|
11 | |
---|
12 | class SimplePerspective(pb.Avatar): |
---|
13 | |
---|
14 | def perspective_echo(self, text): |
---|
15 | print 'echoing',text |
---|
16 | return text |
---|
17 | |
---|
18 | def perspective_transfer(self, data, msg): |
---|
19 | print 'Transfer', data['description'], msg |
---|
20 | print 'Samples ', len(data['values']) |
---|
21 | return msg |
---|
22 | |
---|
23 | def logout(self): |
---|
24 | print self, "logged out" |
---|
25 | reactor.callLater(10, reactor.stop) |
---|
26 | |
---|
27 | |
---|
28 | class SimpleRealm: |
---|
29 | implements(IRealm) |
---|
30 | |
---|
31 | def requestAvatar(self, avatarId, mind, *interfaces): |
---|
32 | if pb.IPerspective in interfaces: |
---|
33 | avatar = SimplePerspective() |
---|
34 | |
---|
35 | return pb.IPerspective, avatar, avatar.logout |
---|
36 | else: |
---|
37 | raise NotImplementedError("no interface") |
---|
38 | |
---|
39 | class MyPBFactory(pb.PBServerFactory): |
---|
40 | |
---|
41 | def __init__(self, root): |
---|
42 | pb.PBServerFactory.__init__(self, root) |
---|
43 | |
---|
44 | def clientConnectionMade(self, broker): |
---|
45 | broker.transport.setTcpNoDelay(1) |
---|
46 | |
---|
47 | if __name__ == '__main__': |
---|
48 | from twisted.internet import reactor |
---|
49 | from twisted.cred.portal import Portal |
---|
50 | from twisted.cred.checkers import InMemoryUsernamePasswordDatabaseDontUse |
---|
51 | portal = Portal(SimpleRealm()) |
---|
52 | checker = InMemoryUsernamePasswordDatabaseDontUse() |
---|
53 | checker.addUser("guest", "guest") |
---|
54 | portal.registerChecker(checker) |
---|
55 | reactor.listenTCP(pb.portno, MyPBFactory(portal)) |
---|
56 | reactor.run() |
---|