Initial import to Tizen
[profile/ivi/python-twisted.git] / doc / conch / examples / demo_draw.tac
1 # Copyright (c) Twisted Matrix Laboratories.
2 # See LICENSE for details.
3
4 # You can run this .tac file directly with:
5 #    twistd -ny demo_draw.tac
6
7 """A trivial drawing application.
8
9 Clients are allowed to connect and spew various characters out over
10 the terminal.  Spacebar changes the drawing character, while the arrow
11 keys move the cursor.
12 """
13
14 from twisted.conch.insults import insults
15 from twisted.conch.telnet import TelnetTransport, TelnetBootstrapProtocol
16 from twisted.conch.manhole_ssh import ConchFactory, TerminalRealm
17
18 from twisted.internet import protocol
19 from twisted.application import internet, service
20 from twisted.cred import checkers, portal
21
22 class Draw(insults.TerminalProtocol):
23     """Protocol which accepts arrow key and spacebar input and places
24     the requested characters onto the terminal.
25     """
26     cursors = list('!@#$%^&*()_+-=')
27
28     def connectionMade(self):
29         self.terminal.eraseDisplay()
30         self.terminal.resetModes([insults.modes.IRM])
31         self.cursor = self.cursors[0]
32
33     def keystrokeReceived(self, keyID, modifier):
34         if keyID == self.terminal.UP_ARROW:
35             self.terminal.cursorUp()
36         elif keyID == self.terminal.DOWN_ARROW:
37             self.terminal.cursorDown()
38         elif keyID == self.terminal.LEFT_ARROW:
39             self.terminal.cursorBackward()
40         elif keyID == self.terminal.RIGHT_ARROW:
41             self.terminal.cursorForward()
42         elif keyID == ' ':
43             self.cursor = self.cursors[(self.cursors.index(self.cursor) + 1) % len(self.cursors)]
44         else:
45             return
46         self.terminal.write(self.cursor)
47         self.terminal.cursorBackward()
48
49 def makeService(args):
50     checker = checkers.InMemoryUsernamePasswordDatabaseDontUse(username="password")
51
52     f = protocol.ServerFactory()
53     f.protocol = lambda: TelnetTransport(TelnetBootstrapProtocol,
54                                          insults.ServerProtocol,
55                                          args['protocolFactory'],
56                                          *args.get('protocolArgs', ()),
57                                          **args.get('protocolKwArgs', {}))
58     tsvc = internet.TCPServer(args['telnet'], f)
59
60     def chainProtocolFactory():
61         return insults.ServerProtocol(
62             args['protocolFactory'],
63             *args.get('protocolArgs', ()),
64             **args.get('protocolKwArgs', {}))
65
66     rlm = TerminalRealm()
67     rlm.chainedProtocolFactory = chainProtocolFactory
68     ptl = portal.Portal(rlm, [checker])
69     f = ConchFactory(ptl)
70     csvc = internet.TCPServer(args['ssh'], f)
71
72     m = service.MultiService()
73     tsvc.setServiceParent(m)
74     csvc.setServiceParent(m)
75     return m
76
77 application = service.Application("Insults Demo App")
78 makeService({'protocolFactory': Draw,
79              'telnet': 6023,
80              'ssh': 6022}).setServiceParent(application)