source: trunk/packages/xen-3.1/xen-3.1/tools/python/xen/xend/server/relocate.py @ 34

Last change on this file since 34 was 34, checked in by hartmans, 18 years ago

Add xen and xen-common

File size: 4.3 KB
Line 
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#============================================================================
18
19import re
20import sys
21import StringIO
22
23from xen.web import protocol, tcp, unix
24
25from xen.xend import sxp
26from xen.xend import XendDomain
27from xen.xend import XendOptions
28from xen.xend.XendError import XendError
29from xen.xend.XendLogging import log
30
31
32class RelocationProtocol(protocol.Protocol):
33    """Asynchronous handler for a connected relocation socket.
34    """
35
36    def __init__(self):
37        protocol.Protocol.__init__(self)
38        self.parser = sxp.Parser()
39
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()
53
54    def close(self):
55        if self.transport:
56            self.transport.close()
57
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
67
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)
74
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)]])
80
81    def opname(self, name):
82         return 'op_' + name.replace('.', '_')
83
84    def operror(self, name, _):
85        raise XendError('Invalid operation: ' +name)
86
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)
92
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
99       
100        l = [ nameop(k) for k in dir(self) if k.startswith('op_') ]
101        return l
102
103    def op_quit(self, _1, _2):
104        self.close()
105
106    def op_receive(self, name, _):
107        if self.transport:
108            self.send_reply(["ready", name])
109            try:
110                XendDomain.instance().domain_restore_fd(
111                    self.transport.sock.fileno())
112            except:
113                self.send_error()
114                self.close()
115        else:
116            log.error(name + ": no transport")
117            raise XendError(name + ": no transport")
118
119
120def listenRelocation():
121    xoptions = XendOptions.instance()
122    if xoptions.get_xend_unix_server():
123        path = '/var/lib/xend/relocation-socket'
124        unix.UnixListener(path, RelocationProtocol)
125    if xoptions.get_xend_relocation_server():
126        port = xoptions.get_xend_relocation_port()
127        interface = xoptions.get_xend_relocation_address()
128
129        hosts_allow = xoptions.get_xend_relocation_hosts_allow()
130        if hosts_allow == '':
131            hosts_allow = None
132        else:
133            hosts_allow = map(re.compile, hosts_allow.split(" "))
134
135        tcp.TCPListener(RelocationProtocol, port, interface = interface,
136                        hosts_allow = hosts_allow)
Note: See TracBrowser for help on using the repository browser.