debuggers.hg

view tools/python/xen/web/protocol.py @ 4672:d781b9d08e80

bitkeeper revision 1.1327.2.4 (426918a34Af7gihN8mTkq-P3KrAZXg)

Remove twisted from save/migrate handling.
This needs to use threads, so add thread support for
http server requests.

Signed-off-by: Mike Wray <mike.wray@hp.com>
author mjw@wray-m-3.hpl.hp.com
date Fri Apr 22 15:30:43 2005 +0000 (2005-04-22)
parents a838a908e38e
children 16efdf7bbd57
line source
1 class Factory:
2 """Generic protocol factory.
3 """
5 starts = 0
7 def __init__(self):
8 pass
10 def doStart(self):
11 if self.starts == 0:
12 self.startFactory()
13 self.starts += 1
15 def doStop(self):
16 if self.starts > 0:
17 self.starts -= 1
18 else:
19 return
20 if self.starts == 0:
21 self.stopFactory()
23 def buildProtocol(self, addr):
24 return Protocol(self)
26 def startFactory(self):
27 pass
29 def stopFactory(self):
30 pass
32 class ServerFactory(Factory):
33 """Factory for server protocols.
34 """
35 pass
37 class ClientFactory(Factory):
38 """Factory for client protocols.
39 """
41 def startedConnecting(self, connector):
42 pass
44 def clientConnectionLost(self, connector, reason):
45 pass
47 def clientConnectionFailed(self, connector, reason):
48 pass
51 class Protocol:
53 factory = None
54 transport = None
55 connected = False
57 def __init__(self, factory):
58 self.factory = factory
60 def setTransport(self, transport):
61 self.transport = transport
62 self.connected = bool(transport)
64 def getTransport(self):
65 return self.transport
67 def connectionMade(self, addr):
68 print 'Protocol>connectionMade>', addr
69 pass
71 def connectionLost(self, reason=None):
72 print 'Protocol>connectionLost>', reason
73 pass
75 def dataReceived(self, data):
76 print 'Protocol>dataReceived>'
77 pass
79 def write(self, data):
80 if self.transport:
81 return self.transport.write(data)
82 else:
83 return 0
85 def read(self):
86 if self.transport:
87 return self.transport.read()
88 else:
89 return None
91 class TestClientFactory(ClientFactory):
93 def buildProtocol(self, addr):
94 print 'TestClientFactory>buildProtocol>', addr
95 return TestClientProtocol(self)
97 def startedConnecting(self, connector):
98 print 'TestClientFactory>startedConnecting>', connector
100 def clientConnectionLost(self, connector, reason):
101 print 'TestClientFactory>clientConnectionLost>', connector, reason
103 def clientConnectionFailed(self, connector, reason):
104 print 'TestClientFactory>clientConnectionFailed>', connector, reason
106 class TestClientProtocol(Protocol):
108 def connectionMade(self, addr):
109 print 'TestClientProtocol>connectionMade>', addr
110 self.write("hello")
111 self.write("there")
113 class TestServerFactory(Factory):
115 def buildProtocol(self, addr):
116 print 'TestServerFactory>buildProtocol>', addr
117 return TestServerProtocol(self)
119 class TestServerProtocol(Protocol):
121 def dataReceived(self, data):
122 print 'TestServerProtocol>dataReceived>', len(data), data
123 #sys.exit(0)
124 import os
125 os._exit(0)