ia64/xen-unstable

view tools/python/xen/web/protocol.py @ 6538:84ee014ebd41

Merge xen-vtx-unstable.hg
author adsharma@los-vmm.sc.intel.com
date Wed Aug 17 12:34:38 2005 -0800 (2005-08-17)
parents 23979fb12c49 f40c6650152e
children 99914b54f7bf
line source
1 #============================================================================
2 # This library is free software; you can redistribute it and/or
3 # modify it under the terms of version 2.1 of the GNU Lesser General Public
4 # License as published by the Free Software Foundation.
5 #
6 # This library is distributed in the hope that it will be useful,
7 # but WITHOUT ANY WARRANTY; without even the implied warranty of
8 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
9 # Lesser General Public License for more details.
10 #
11 # You should have received a copy of the GNU Lesser General Public
12 # License along with this library; if not, write to the Free Software
13 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
14 #============================================================================
15 # Copyright (C) 2005 Mike Wray <mike.wray@hp.com>
16 #============================================================================
18 class Factory:
19 """Generic protocol factory.
20 """
22 starts = 0
24 def __init__(self):
25 pass
27 def doStart(self):
28 if self.starts == 0:
29 self.startFactory()
30 self.starts += 1
32 def doStop(self):
33 if self.starts > 0:
34 self.starts -= 1
35 else:
36 return
37 if self.starts == 0:
38 self.stopFactory()
40 def buildProtocol(self, addr):
41 return Protocol(self)
43 def startFactory(self):
44 pass
46 def stopFactory(self):
47 pass
49 class ServerFactory(Factory):
50 """Factory for server protocols.
51 """
52 pass
54 class ClientFactory(Factory):
55 """Factory for client protocols.
56 """
58 def startedConnecting(self, connector):
59 pass
61 def clientConnectionLost(self, connector, reason):
62 pass
64 def clientConnectionFailed(self, connector, reason):
65 pass
68 class Protocol:
70 factory = None
71 transport = None
72 connected = False
74 def __init__(self, factory):
75 self.factory = factory
77 def setTransport(self, transport):
78 self.transport = transport
79 self.connected = bool(transport)
81 def getTransport(self):
82 return self.transport
84 def connectionMade(self, addr):
85 print 'Protocol>connectionMade>', addr
86 pass
88 def connectionLost(self, reason=None):
89 print 'Protocol>connectionLost>', reason
90 pass
92 def dataReceived(self, data):
93 print 'Protocol>dataReceived>'
94 pass
96 def write(self, data):
97 if self.transport:
98 return self.transport.write(data)
99 else:
100 return 0
102 def read(self):
103 if self.transport:
104 return self.transport.read()
105 else:
106 return None
108 class TestClientFactory(ClientFactory):
110 def buildProtocol(self, addr):
111 print 'TestClientFactory>buildProtocol>', addr
112 return TestClientProtocol(self)
114 def startedConnecting(self, connector):
115 print 'TestClientFactory>startedConnecting>', connector
117 def clientConnectionLost(self, connector, reason):
118 print 'TestClientFactory>clientConnectionLost>', connector, reason
120 def clientConnectionFailed(self, connector, reason):
121 print 'TestClientFactory>clientConnectionFailed>', connector, reason
123 class TestClientProtocol(Protocol):
125 def connectionMade(self, addr):
126 print 'TestClientProtocol>connectionMade>', addr
127 self.write("hello")
128 self.write("there")
130 class TestServerFactory(Factory):
132 def buildProtocol(self, addr):
133 print 'TestServerFactory>buildProtocol>', addr
134 return TestServerProtocol(self)
136 class TestServerProtocol(Protocol):
138 def dataReceived(self, data):
139 print 'TestServerProtocol>dataReceived>', len(data), data
140 #sys.exit(0)
141 import os
142 os._exit(0)