ia64/xen-unstable

view tools/python/xen/xend/server/event.py @ 6689:7d0fb56b4a91

merge?
author cl349@firebug.cl.cam.ac.uk
date Wed Sep 07 19:01:31 2005 +0000 (2005-09-07)
parents 0e2b1e04d4cb dd668f7527cb
children b2f4823b6ff0 b35215021b32 9af349b055e5 3233e7ecfa9f
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 sys
19 import StringIO
21 from xen.web import reactor, protocol
23 from xen.xend import scheduler
24 from xen.xend import sxp
25 from xen.xend import PrettyPrint
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()
30 DEBUG = 0
32 class EventProtocol(protocol.Protocol):
33 """Asynchronous handler for a connected event socket.
34 """
36 def __init__(self, daemon):
37 #protocol.Protocol.__init__(self)
38 self.daemon = daemon
39 # Event queue.
40 self.queue = []
41 # Subscribed events.
42 self.events = []
43 self.parser = sxp.Parser()
44 self.pretty = 0
46 # For debugging subscribe to everything and make output pretty.
47 #self.subscribe(['*'])
48 self.pretty = 1
50 def dataReceived(self, data):
51 try:
52 self.parser.input(data)
53 while(self.parser.ready()):
54 val = self.parser.get_val()
55 res = self.dispatch(val)
56 self.send_result(res)
57 if self.parser.at_eof():
58 self.loseConnection()
59 except SystemExit:
60 raise
61 except:
62 self.send_error()
64 def loseConnection(self):
65 if self.transport:
66 self.transport.loseConnection()
67 if self.connected:
68 scheduler.now(self.connectionLost)
70 def connectionLost(self, reason=None):
71 self.unsubscribe()
73 def send_reply(self, sxpr):
74 io = StringIO.StringIO()
75 if self.pretty:
76 PrettyPrint.prettyprint(sxpr, out=io)
77 else:
78 sxp.show(sxpr, out=io)
79 print >> io
80 io.seek(0)
81 if self.transport:
82 return self.transport.write(io.getvalue())
83 else:
84 return 0
86 def send_result(self, res):
87 if res is None:
88 resp = ['ok']
89 else:
90 resp = ['ok', res]
91 return self.send_reply(resp)
93 def send_error(self):
94 (extype, exval) = sys.exc_info()[:2]
95 return self.send_reply(['err',
96 ['type', str(extype)],
97 ['value', str(exval)]])
99 def send_event(self, val):
100 return self.send_reply(['event', val[0], val[1]])
102 def unsubscribe(self):
103 for event in self.events:
104 eserver.unsubscribe(event, self.queue_event)
106 def subscribe(self, events):
107 self.unsubscribe()
108 for event in events:
109 eserver.subscribe(event, self.queue_event)
110 self.events = events
112 def queue_event(self, name, v):
113 # Despite the name we don't queue the event here.
114 # We send it because the transport will queue it.
115 self.send_event([name, v])
117 def opname(self, name):
118 return 'op_' + name.replace('.', '_')
120 def operror(self, name, req):
121 raise XendError('Invalid operation: ' +name)
123 def dispatch(self, req):
124 op_name = sxp.name(req)
125 op_method_name = self.opname(op_name)
126 op_method = getattr(self, op_method_name, self.operror)
127 return op_method(op_name, req)
129 def op_help(self, name, req):
130 def nameop(x):
131 if x.startswith('op_'):
132 return x[3:].replace('_', '.')
133 else:
134 return x
136 l = [ nameop(k) for k in dir(self) if k.startswith('op_') ]
137 return l
139 def op_quit(self, name, req):
140 self.loseConnection()
142 def op_exit(self, name, req):
143 sys.exit(0)
145 def op_pretty(self, name, req):
146 self.pretty = 1
148 def op_info(self, name, req):
149 val = ['info']
150 #val += self.daemon.blkifs()
151 #val += self.daemon.netifs()
152 #val += self.daemon.usbifs()
153 return val
155 def op_sys_subscribe(self, name, v):
156 # (sys.subscribe event*)
157 # Subscribe to the events:
158 self.subscribe(v[1:])
160 def op_sys_inject(self, name, v):
161 # (sys.inject event)
162 event = v[1]
163 eserver.inject(sxp.name(event), event)
165 def op_trace(self, name, v):
166 mode = (v[1] == 'on')
167 self.daemon.tracing(mode)
169 def op_log_stderr(self, name, v):
170 mode = v[1]
171 logging = xroot.get_logging()
172 if mode == 'on':
173 logging.addLogStderr()
174 else:
175 logging.removeLogStderr()
177 def op_debug_controller(self, name, v):
178 mode = v[1]
179 import controller
180 controller.DEBUG = (mode == 'on')
182 def op_domain_ls(self, name, v):
183 xd = xroot.get_component("xen.xend.XendDomain")
184 return xd.list_names()
186 def op_domain_configure(self, name, v):
187 domid = sxp.child_value(v, "dom")
188 config = sxp.child_value(v, "config")
189 if domid is None:
190 raise XendError("missing domain id")
191 if config is None:
192 raise XendError("missing domain config")
193 xd = xroot.get_component("xen.xend.XendDomain")
194 xd.domain_configure(domid, config)
196 def op_domain_unpause(self, name, v):
197 domid = sxp.child_value(v, "dom")
198 if domid is None:
199 raise XendError("missing domain id")
200 xd = xroot.get_component("xen.xend.XendDomain")
201 xd.domain_unpause(domid)
203 class EventFactory(protocol.ServerFactory):
204 """Asynchronous handler for the event server socket.
205 """
207 def __init__(self, daemon):
208 #protocol.ServerFactory.__init__(self)
209 self.daemon = daemon
211 def buildProtocol(self, addr):
212 return EventProtocol(self.daemon)
214 def listenEvent(daemon):
215 factory = EventFactory(daemon)
216 if xroot.get_xend_unix_server():
217 path = '/var/lib/xend/event-socket'
218 reactor.listenUNIX(path, factory)
219 if xroot.get_xend_http_server():
220 port = xroot.get_xend_event_port()
221 interface = xroot.get_xend_address()
222 reactor.listenTCP(port, factory, interface=interface)