ia64/xen-unstable

view tools/python/xen/xend/server/relocate.py @ 7191:83452b6cdcdb

Merged.
author emellor@ewan
date Tue Oct 04 11:33:17 2005 +0100 (2005-10-04)
parents 486f1c3e7805
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 # Copyright (C) 2005 XenSource Ltd
17 #============================================================================
19 import socket
20 import sys
21 import StringIO
23 from xen.web import protocol, tcp, unix
25 from xen.xend import scheduler
26 from xen.xend import sxp
27 from xen.xend import EventServer
28 from xen.xend.XendError import XendError
29 from xen.xend import XendRoot
30 from xen.xend.XendLogging import log
31 from xen.xend import XendCheckpoint
34 eserver = EventServer.instance()
35 xroot = XendRoot.instance()
38 DEBUG = 0
40 class RelocationProtocol(protocol.Protocol):
41 """Asynchronous handler for a connected relocation socket.
42 """
44 def __init__(self):
45 #protocol.Protocol.__init__(self)
46 self.parser = sxp.Parser()
48 def dataReceived(self, data):
49 try:
50 self.parser.input(data)
51 while(self.parser.ready()):
52 val = self.parser.get_val()
53 res = self.dispatch(val)
54 self.send_result(res)
55 if self.parser.at_eof():
56 self.loseConnection()
57 except SystemExit:
58 raise
59 except:
60 self.send_error()
62 def loseConnection(self):
63 if self.transport:
64 self.transport.loseConnection()
65 if self.connected:
66 scheduler.now(self.connectionLost)
68 def connectionLost(self, reason=None):
69 pass
71 def send_reply(self, sxpr):
72 io = StringIO.StringIO()
73 sxp.show(sxpr, out=io)
74 print >> io
75 io.seek(0)
76 if self.transport:
77 return self.transport.write(io.getvalue())
78 else:
79 return 0
81 def send_result(self, res):
82 if res is None:
83 resp = ['ok']
84 else:
85 resp = ['ok', res]
86 return self.send_reply(resp)
88 def send_error(self):
89 (extype, exval) = sys.exc_info()[:2]
90 return self.send_reply(['err',
91 ['type', str(extype)],
92 ['value', str(exval)]])
94 def opname(self, name):
95 return 'op_' + name.replace('.', '_')
97 def operror(self, name, req):
98 raise XendError('Invalid operation: ' +name)
100 def dispatch(self, req):
101 op_name = sxp.name(req)
102 op_method_name = self.opname(op_name)
103 op_method = getattr(self, op_method_name, self.operror)
104 return op_method(op_name, req)
106 def op_help(self, name, req):
107 def nameop(x):
108 if x.startswith('op_'):
109 return x[3:].replace('_', '.')
110 else:
111 return x
113 l = [ nameop(k) for k in dir(self) if k.startswith('op_') ]
114 return l
116 def op_quit(self, name, req):
117 self.loseConnection()
119 def op_receive(self, name, req):
120 if self.transport:
121 self.send_reply(["ready", name])
122 self.transport.sock.setblocking(1)
123 XendCheckpoint.restore(self.transport.sock.fileno())
124 self.transport.sock.setblocking(0)
125 else:
126 log.error(name + ": no transport")
127 raise XendError(name + ": no transport")
129 class RelocationFactory(protocol.ServerFactory):
130 """Asynchronous handler for the relocation server socket.
131 """
133 def __init__(self):
134 #protocol.ServerFactory.__init__(self)
135 pass
137 def buildProtocol(self, addr):
138 return RelocationProtocol()
140 def listenRelocation():
141 factory = RelocationFactory()
142 if xroot.get_xend_unix_server():
143 path = '/var/lib/xend/relocation-socket'
144 unix.listenUNIX(path, factory)
145 if xroot.get_xend_relocation_server():
146 port = xroot.get_xend_relocation_port()
147 interface = xroot.get_xend_relocation_address()
148 l = tcp.listenTCP(port, factory, interface=interface)
149 l.setCloExec()
151 def setupRelocation(dst, port):
152 try:
153 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
154 sock.connect((dst, port))
155 except socket.error, err:
156 raise XendError("can't connect: %s" % err[1])
158 sock.send("receive\n")
159 print sock.recv(80)
161 return sock