ia64/xen-unstable

view tools/python/xen/xend/server/relocate.py @ 13642:ba3ec84c9423

[XEND] Add missing ConsoleController.py

Signed-off-by: Alastair Tse <atse@xensource.com>
author Alastair Tse <atse@xensource.com>
date Fri Jan 26 02:44:35 2007 +0000 (2007-01-26)
parents a18ae238eb53
children 4f67d849e788
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) 2004, 2005 Mike Wray <mike.wray@hp.com>
16 # Copyright (C) 2005 XenSource Ltd
17 #============================================================================
19 import re
20 import sys
21 import StringIO
23 from xen.web import protocol, tcp, unix
25 from xen.xend import sxp
26 from xen.xend import XendDomain
27 from xen.xend import XendOptions
28 from xen.xend.XendError import XendError
29 from xen.xend.XendLogging import log
32 class RelocationProtocol(protocol.Protocol):
33 """Asynchronous handler for a connected relocation socket.
34 """
36 def __init__(self):
37 protocol.Protocol.__init__(self)
38 self.parser = sxp.Parser()
40 def dataReceived(self, data):
41 try:
42 self.parser.input(data)
43 while(self.parser.ready()):
44 val = self.parser.get_val()
45 res = self.dispatch(val)
46 self.send_result(res)
47 if self.parser.at_eof():
48 self.close()
49 except SystemExit:
50 raise
51 except:
52 self.send_error()
54 def close(self):
55 if self.transport:
56 self.transport.close()
58 def send_reply(self, sxpr):
59 io = StringIO.StringIO()
60 sxp.show(sxpr, out=io)
61 print >> io
62 io.seek(0)
63 if self.transport:
64 return self.transport.write(io.getvalue())
65 else:
66 return 0
68 def send_result(self, res):
69 if res is None:
70 resp = ['ok']
71 else:
72 resp = ['ok', res]
73 return self.send_reply(resp)
75 def send_error(self):
76 (extype, exval) = sys.exc_info()[:2]
77 return self.send_reply(['err',
78 ['type', str(extype)],
79 ['value', str(exval)]])
81 def opname(self, name):
82 return 'op_' + name.replace('.', '_')
84 def operror(self, name, _):
85 raise XendError('Invalid operation: ' +name)
87 def dispatch(self, req):
88 op_name = sxp.name(req)
89 op_method_name = self.opname(op_name)
90 op_method = getattr(self, op_method_name, self.operror)
91 return op_method(op_name, req)
93 def op_help(self, _1, _2):
94 def nameop(x):
95 if x.startswith('op_'):
96 return x[3:].replace('_', '.')
97 else:
98 return x
100 l = [ nameop(k) for k in dir(self) if k.startswith('op_') ]
101 return l
103 def op_quit(self, _1, _2):
104 self.close()
106 def op_receive(self, name, _):
107 if self.transport:
108 self.send_reply(["ready", name])
109 XendDomain.instance().domain_restore_fd(
110 self.transport.sock.fileno())
111 else:
112 log.error(name + ": no transport")
113 raise XendError(name + ": no transport")
116 def listenRelocation():
117 xoptions = XendOptions.instance()
118 if xoptions.get_xend_unix_server():
119 path = '/var/lib/xend/relocation-socket'
120 unix.UnixListener(path, RelocationProtocol)
121 if xoptions.get_xend_relocation_server():
122 port = xoptions.get_xend_relocation_port()
123 interface = xoptions.get_xend_relocation_address()
125 hosts_allow = xoptions.get_xend_relocation_hosts_allow()
126 if hosts_allow == '':
127 hosts_allow = None
128 else:
129 hosts_allow = map(re.compile, hosts_allow.split(" "))
131 tcp.TCPListener(RelocationProtocol, port, interface = interface,
132 hosts_allow = hosts_allow)