ia64/xen-unstable

view tools/python/xen/xend/server/event.py @ 7160:486f1c3e7805

Fix call to XendCheckpoint.restore, since that method no longer needs a
XendDomain instance passed in.

Remove meaningless indirection of imports through web/reactor.py. Tidy import
statements.

Signed-off-by: Ewan Mellor <ewan@xensource.com>
author emellor@ewan
date Sun Oct 02 18:17:26 2005 +0100 (2005-10-02)
parents 06d84bf87159
children c60036fe7418
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 sys
20 import StringIO
22 from xen.web import protocol, tcp, unix
24 from xen.xend import scheduler
25 from xen.xend import sxp
26 from xen.xend import PrettyPrint
27 from xen.xend import EventServer
28 from xen.xend.XendError import XendError
29 from xen.xend import XendRoot
32 eserver = EventServer.instance()
33 xroot = XendRoot.instance()
36 DEBUG = 0
38 class EventProtocol(protocol.Protocol):
39 """Asynchronous handler for a connected event socket.
40 """
42 def __init__(self, daemon):
43 #protocol.Protocol.__init__(self)
44 self.daemon = daemon
45 # Event queue.
46 self.queue = []
47 # Subscribed events.
48 self.events = []
49 self.parser = sxp.Parser()
50 self.pretty = 0
52 # For debugging subscribe to everything and make output pretty.
53 #self.subscribe(['*'])
54 self.pretty = 1
56 def dataReceived(self, data):
57 try:
58 self.parser.input(data)
59 while(self.parser.ready()):
60 val = self.parser.get_val()
61 res = self.dispatch(val)
62 self.send_result(res)
63 if self.parser.at_eof():
64 self.loseConnection()
65 except SystemExit:
66 raise
67 except:
68 self.send_error()
70 def loseConnection(self):
71 if self.transport:
72 self.transport.loseConnection()
73 if self.connected:
74 scheduler.now(self.connectionLost)
76 def connectionLost(self, reason=None):
77 self.unsubscribe()
79 def send_reply(self, sxpr):
80 io = StringIO.StringIO()
81 if self.pretty:
82 PrettyPrint.prettyprint(sxpr, out=io)
83 else:
84 sxp.show(sxpr, out=io)
85 print >> io
86 io.seek(0)
87 if self.transport:
88 return self.transport.write(io.getvalue())
89 else:
90 return 0
92 def send_result(self, res):
93 if res is None:
94 resp = ['ok']
95 else:
96 resp = ['ok', res]
97 return self.send_reply(resp)
99 def send_error(self):
100 (extype, exval) = sys.exc_info()[:2]
101 return self.send_reply(['err',
102 ['type', str(extype)],
103 ['value', str(exval)]])
105 def send_event(self, val):
106 return self.send_reply(['event', val[0], val[1]])
108 def unsubscribe(self):
109 for event in self.events:
110 eserver.unsubscribe(event, self.queue_event)
112 def subscribe(self, events):
113 self.unsubscribe()
114 for event in events:
115 eserver.subscribe(event, self.queue_event)
116 self.events = events
118 def queue_event(self, name, v):
119 # Despite the name we don't queue the event here.
120 # We send it because the transport will queue it.
121 self.send_event([name, v])
123 def opname(self, name):
124 return 'op_' + name.replace('.', '_')
126 def operror(self, name, req):
127 raise XendError('Invalid operation: ' +name)
129 def dispatch(self, req):
130 op_name = sxp.name(req)
131 op_method_name = self.opname(op_name)
132 op_method = getattr(self, op_method_name, self.operror)
133 return op_method(op_name, req)
135 def op_help(self, name, req):
136 def nameop(x):
137 if x.startswith('op_'):
138 return x[3:].replace('_', '.')
139 else:
140 return x
142 l = [ nameop(k) for k in dir(self) if k.startswith('op_') ]
143 return l
145 def op_quit(self, name, req):
146 self.loseConnection()
148 def op_exit(self, name, req):
149 sys.exit(0)
151 def op_pretty(self, name, req):
152 self.pretty = 1
154 def op_info(self, name, req):
155 val = ['info']
156 #val += self.daemon.blkifs()
157 #val += self.daemon.netifs()
158 #val += self.daemon.usbifs()
159 return val
161 def op_sys_subscribe(self, name, v):
162 # (sys.subscribe event*)
163 # Subscribe to the events:
164 self.subscribe(v[1:])
166 def op_sys_inject(self, name, v):
167 # (sys.inject event)
168 event = v[1]
169 eserver.inject(sxp.name(event), event)
171 def op_trace(self, name, v):
172 mode = (v[1] == 'on')
173 self.daemon.tracing(mode)
175 def op_log_stderr(self, name, v):
176 mode = v[1]
177 logging = xroot.get_logging()
178 if mode == 'on':
179 logging.addLogStderr()
180 else:
181 logging.removeLogStderr()
183 def op_domain_ls(self, name, v):
184 xd = xroot.get_component("xen.xend.XendDomain")
185 return xd.list_names()
187 def op_domain_configure(self, name, v):
188 domid = sxp.child_value(v, "dom")
189 config = sxp.child_value(v, "config")
190 if domid is None:
191 raise XendError("missing domain id")
192 if config is None:
193 raise XendError("missing domain config")
194 xd = xroot.get_component("xen.xend.XendDomain")
195 xd.domain_configure(domid, config)
197 def op_domain_unpause(self, name, v):
198 domid = sxp.child_value(v, "dom")
199 if domid is None:
200 raise XendError("missing domain id")
201 xd = xroot.get_component("xen.xend.XendDomain")
202 xd.domain_unpause(domid)
204 class EventFactory(protocol.ServerFactory):
205 """Asynchronous handler for the event server socket.
206 """
208 def __init__(self, daemon):
209 #protocol.ServerFactory.__init__(self)
210 self.daemon = daemon
212 def buildProtocol(self, addr):
213 return EventProtocol(self.daemon)
215 def listenEvent(daemon):
216 factory = EventFactory(daemon)
217 if xroot.get_xend_unix_server():
218 path = '/var/lib/xend/event-socket'
219 unix.listenUNIX(path, factory)
220 if xroot.get_xend_http_server():
221 port = xroot.get_xend_event_port()
222 interface = xroot.get_xend_address()
223 tcp.listenTCP(port, factory, interface=interface)