xen-vtx-unstable

view tools/python/xen/xend/server/relocate.py @ 6774:4d899a738d59

merge?
author cl349@firebug.cl.cam.ac.uk
date Tue Sep 13 15:05:49 2005 +0000 (2005-09-13)
parents dd668f7527cb
children
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 #============================================================================
18 import socket
19 import sys
20 import StringIO
22 from xen.web import reactor, protocol
24 from xen.xend import scheduler
25 from xen.xend import sxp
26 from xen.xend import EventServer; eserver = EventServer.instance()
27 from xen.xend.XendError import XendError
28 from xen.xend import XendRoot; xroot = XendRoot.instance()
29 from xen.xend.XendLogging import log
30 from xen.xend import XendCheckpoint
32 DEBUG = 0
34 class RelocationProtocol(protocol.Protocol):
35 """Asynchronous handler for a connected relocation socket.
36 """
38 def __init__(self):
39 #protocol.Protocol.__init__(self)
40 self.parser = sxp.Parser()
42 def dataReceived(self, data):
43 try:
44 self.parser.input(data)
45 while(self.parser.ready()):
46 val = self.parser.get_val()
47 res = self.dispatch(val)
48 self.send_result(res)
49 if self.parser.at_eof():
50 self.loseConnection()
51 except SystemExit:
52 raise
53 except:
54 self.send_error()
56 def loseConnection(self):
57 if self.transport:
58 self.transport.loseConnection()
59 if self.connected:
60 scheduler.now(self.connectionLost)
62 def connectionLost(self, reason=None):
63 pass
65 def send_reply(self, sxpr):
66 io = StringIO.StringIO()
67 sxp.show(sxpr, out=io)
68 print >> io
69 io.seek(0)
70 if self.transport:
71 return self.transport.write(io.getvalue())
72 else:
73 return 0
75 def send_result(self, res):
76 if res is None:
77 resp = ['ok']
78 else:
79 resp = ['ok', res]
80 return self.send_reply(resp)
82 def send_error(self):
83 (extype, exval) = sys.exc_info()[:2]
84 return self.send_reply(['err',
85 ['type', str(extype)],
86 ['value', str(exval)]])
88 def opname(self, name):
89 return 'op_' + name.replace('.', '_')
91 def operror(self, name, req):
92 raise XendError('Invalid operation: ' +name)
94 def dispatch(self, req):
95 op_name = sxp.name(req)
96 op_method_name = self.opname(op_name)
97 op_method = getattr(self, op_method_name, self.operror)
98 return op_method(op_name, req)
100 def op_help(self, name, req):
101 def nameop(x):
102 if x.startswith('op_'):
103 return x[3:].replace('_', '.')
104 else:
105 return x
107 l = [ nameop(k) for k in dir(self) if k.startswith('op_') ]
108 return l
110 def op_quit(self, name, req):
111 self.loseConnection()
113 def op_receive(self, name, req):
114 if self.transport:
115 self.send_reply(["ready", name])
116 self.transport.sock.setblocking(1)
117 xd = xroot.get_component("xen.xend.XendDomain")
118 XendCheckpoint.restore(xd, self.transport.sock.fileno())
119 self.transport.sock.setblocking(0)
120 else:
121 log.error(name + ": no transport")
122 raise XendError(name + ": no transport")
124 class RelocationFactory(protocol.ServerFactory):
125 """Asynchronous handler for the relocation server socket.
126 """
128 def __init__(self):
129 #protocol.ServerFactory.__init__(self)
130 pass
132 def buildProtocol(self, addr):
133 return RelocationProtocol()
135 def listenRelocation():
136 factory = RelocationFactory()
137 if xroot.get_xend_unix_server():
138 path = '/var/lib/xend/relocation-socket'
139 reactor.listenUNIX(path, factory)
140 if xroot.get_xend_relocation_server():
141 port = xroot.get_xend_relocation_port()
142 interface = xroot.get_xend_relocation_address()
143 l = reactor.listenTCP(port, factory, interface=interface)
144 l.setCloExec()
146 def setupRelocation(dst, port):
147 try:
148 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
149 sock.connect((dst, port))
150 except socket.error, err:
151 raise XendError("can't connect: %s" % err[1])
153 sock.send("receive\n")
154 print sock.recv(80)
156 return sock