ia64/xen-unstable
changeset 7215:c60036fe7418
Removed the EventServer, replacing the events fired with simple logging
messages. This closes bug #281. Removed references to XendRoot where these
have become unnecessary either through the work above or just through time.
Renamed some parameters in event.py, to mark them as unused. Fix a call to a
base class constructor there too.
Signed-off-by: Ewan Mellor <ewan@xensource.com>
messages. This closes bug #281. Removed references to XendRoot where these
have become unnecessary either through the work above or just through time.
Renamed some parameters in event.py, to mark them as unused. Fix a call to a
base class constructor there too.
Signed-off-by: Ewan Mellor <ewan@xensource.com>
author | emellor@ewan |
---|---|
date | Wed Oct 05 11:43:23 2005 +0100 (2005-10-05) |
parents | 33cd9a7a903b |
children | aad1d3e231ff b4800e204757 |
files | tools/python/xen/xend/XendDomain.py tools/python/xen/xend/XendRoot.py tools/python/xen/xend/server/SrvDaemon.py tools/python/xen/xend/server/SrvRoot.py tools/python/xen/xend/server/event.py tools/python/xen/xend/server/relocate.py |
line diff
1.1 --- a/tools/python/xen/xend/EventServer.py Wed Oct 05 00:11:30 2005 +0100 1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 1.3 @@ -1,265 +0,0 @@ 1.4 -#============================================================================ 1.5 -# This library is free software; you can redistribute it and/or 1.6 -# modify it under the terms of version 2.1 of the GNU Lesser General Public 1.7 -# License as published by the Free Software Foundation. 1.8 -# 1.9 -# This library is distributed in the hope that it will be useful, 1.10 -# but WITHOUT ANY WARRANTY; without even the implied warranty of 1.11 -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 1.12 -# Lesser General Public License for more details. 1.13 -# 1.14 -# You should have received a copy of the GNU Lesser General Public 1.15 -# License along with this library; if not, write to the Free Software 1.16 -# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 1.17 -#============================================================================ 1.18 -# Copyright (C) 2004, 2005 Mike Wray <mike.wray@hp.com> 1.19 -#============================================================================ 1.20 - 1.21 -"""Simple publish/subscribe event server. 1.22 - 1.23 -""" 1.24 -import string 1.25 -from threading import Lock 1.26 - 1.27 -import scheduler 1.28 - 1.29 -# subscribe a.b.c h: map a.b.c -> h 1.30 -# subscribe a.b.* h: map a.b.* -> h 1.31 -# subscribe a.b.? h: map a.b.? -> h 1.32 -# 1.33 -# for event a.b.c.d: 1.34 -# 1.35 -# lookup a.b.c.d, call handlers 1.36 -# 1.37 -# lookup a.b.c.?, call handlers 1.38 -# 1.39 -# lookup a.b.c.d.*, call handlers 1.40 -# lookup a.b.c.*, call handlers 1.41 -# lookup a.b.*, call handlers 1.42 -# lookup a.*, call handlers 1.43 -# lookup *, call handlers 1.44 - 1.45 -# a.b.c.d = (a b c d) 1.46 -# a.b.c.? = (a b c _) 1.47 -# a.b.c.* = (a b c . _) 1.48 - 1.49 -class EventServer: 1.50 - 1.51 - DOT = '.' 1.52 - QUERY = '?' 1.53 - DOT_QUERY = DOT + QUERY 1.54 - STAR = '*' 1.55 - DOT_STAR = DOT + STAR 1.56 - 1.57 - def __init__(self, run=0): 1.58 - self.handlers = {} 1.59 - self.run = run 1.60 - self.queue = [] 1.61 - self.lock = Lock() 1.62 - 1.63 - def start(self): 1.64 - """Enable event handling. Sends any queued events. 1.65 - """ 1.66 - try: 1.67 - self.lock.acquire() 1.68 - self.run = 1 1.69 - queue = self.queue 1.70 - self.queue = [] 1.71 - finally: 1.72 - self.lock.release() 1.73 - for (e,v) in queue: 1.74 - self.inject(e, v) 1.75 - 1.76 - def stop(self): 1.77 - """Suspend event handling. Events injected while suspended 1.78 - are queued until we are started again. 1.79 - """ 1.80 - try: 1.81 - self.lock.acquire() 1.82 - self.run = 0 1.83 - finally: 1.84 - self.lock.release() 1.85 - 1.86 - def subscribe(self, event, handler): 1.87 - """Subscribe to an event. For example 'a.b.c.d'. 1.88 - A subcription like 'a.b.c.?' ending in '?' matches any value 1.89 - for the '?'. A subscription like 'a.b.c.*' ending in '*' matches 1.90 - any event type with the same prefix, 'a.b.c' in this case. 1.91 - 1.92 - event event name 1.93 - handler event handler fn(event, val) 1.94 - """ 1.95 - try: 1.96 - self.lock.acquire() 1.97 - hl = self.handlers.get(event) 1.98 - if hl is None: 1.99 - self.handlers[event] = [handler] 1.100 - else: 1.101 - hl.append(handler) 1.102 - finally: 1.103 - self.lock.release() 1.104 - 1.105 - def unsubscribe_all(self, event=None): 1.106 - """Unsubscribe all handlers for a given event, or all handlers. 1.107 - 1.108 - event event (optional) 1.109 - """ 1.110 - try: 1.111 - self.lock.acquire() 1.112 - if event == None: 1.113 - self.handlers.clear() 1.114 - elif event in self.handlers: 1.115 - del self.handlers[event] 1.116 - finally: 1.117 - self.lock.release() 1.118 - 1.119 - def unsubscribe(self, event, handler): 1.120 - """Unsubscribe a given event and handler. 1.121 - 1.122 - event event 1.123 - handler handler 1.124 - """ 1.125 - try: 1.126 - self.lock.acquire() 1.127 - hl = self.handlers.get(event) 1.128 - if hl is None: 1.129 - return 1.130 - if handler in hl: 1.131 - hl.remove(handler) 1.132 - finally: 1.133 - self.lock.release() 1.134 - 1.135 - def inject(self, event, val, async=1): 1.136 - """Inject an event. Handlers for it are called if running, otherwise 1.137 - it is queued. 1.138 - 1.139 - event event type 1.140 - val event value 1.141 - """ 1.142 - try: 1.143 - self.lock.acquire() 1.144 - if not self.run: 1.145 - self.queue.append( (event, val) ) 1.146 - return 1.147 - finally: 1.148 - self.lock.release() 1.149 - 1.150 - if async: 1.151 - scheduler.now(self.call_handlers, event, val) 1.152 - else: 1.153 - self.call_handlers(event, val) 1.154 - 1.155 - def call_handlers(self, event, val): 1.156 - """Internal method to call event handlers. 1.157 - """ 1.158 - #print ">event", event, val 1.159 - self.call_event_handlers(event, event, val) 1.160 - self.call_query_handlers(event, val) 1.161 - self.call_star_handlers(event, val) 1.162 - 1.163 - def call_event_handlers(self, key, event, val): 1.164 - """Call the handlers for an event. 1.165 - It is safe for handlers to subscribe or unsubscribe. 1.166 - 1.167 - key key for handler list 1.168 - event event type 1.169 - val event value 1.170 - """ 1.171 - try: 1.172 - self.lock.acquire() 1.173 - hl = self.handlers.get(key) 1.174 - if hl is None: 1.175 - return 1.176 - # Copy the handler list so that handlers can call 1.177 - # subscribe/unsubscribe safely - python list iteration 1.178 - # is not safe against list modification. 1.179 - hl = hl[:] 1.180 - finally: 1.181 - self.lock.release() 1.182 - # Must not hold the lock while calling the handlers. 1.183 - for h in hl: 1.184 - try: 1.185 - h(event, val) 1.186 - except: 1.187 - pass 1.188 - 1.189 - def call_query_handlers(self, event, val): 1.190 - """Call regex handlers for events matching 'event' that end in '?'. 1.191 - 1.192 - event event type 1.193 - val event value 1.194 - """ 1.195 - dot_idx = event.rfind(self.DOT) 1.196 - if dot_idx == -1: 1.197 - self.call_event_handlers(self.QUERY, event, val) 1.198 - else: 1.199 - event_query = event[0:dot_idx] + self.DOT_QUERY 1.200 - self.call_event_handlers(event_query, event, val) 1.201 - 1.202 - def call_star_handlers(self, event, val): 1.203 - """Call regex handlers for events matching 'event' that end in '*'. 1.204 - 1.205 - event event type 1.206 - val event value 1.207 - """ 1.208 - etype = string.split(event, self.DOT) 1.209 - for i in range(len(etype), 0, -1): 1.210 - event_star = self.DOT.join(etype[0:i]) + self.DOT_STAR 1.211 - self.call_event_handlers(event_star, event, val) 1.212 - self.call_event_handlers(self.STAR, event, val) 1.213 - 1.214 -def instance(): 1.215 - global inst 1.216 - try: 1.217 - inst 1.218 - except: 1.219 - inst = EventServer() 1.220 - inst.start() 1.221 - return inst 1.222 - 1.223 -def main(): 1.224 - def sys_star(event, val): 1.225 - print 'sys_star', event, val 1.226 - 1.227 - def sys_foo(event, val): 1.228 - print 'sys_foo', event, val 1.229 - s.unsubscribe('sys.foo', sys_foo) 1.230 - 1.231 - def sys_foo2(event, val): 1.232 - print 'sys_foo2', event, val 1.233 - 1.234 - def sys_bar(event, val): 1.235 - print 'sys_bar', event, val 1.236 - 1.237 - def sys_foo_bar(event, val): 1.238 - print 'sys_foo_bar', event, val 1.239 - 1.240 - def foo_bar(event, val): 1.241 - print 'foo_bar', event, val 1.242 - 1.243 - s = EventServer() 1.244 - s.start() 1.245 - s.subscribe('sys.*', sys_star) 1.246 - s.subscribe('sys.foo', sys_foo) 1.247 - s.subscribe('sys.foo', sys_foo2) 1.248 - s.subscribe('sys.bar', sys_bar) 1.249 - s.subscribe('sys.foo.bar', sys_foo_bar) 1.250 - s.subscribe('foo.bar', foo_bar) 1.251 - s.inject('sys.foo', 'hello') 1.252 - print 1.253 - s.inject('sys.bar', 'hello again') 1.254 - print 1.255 - s.inject('sys.foo.bar', 'hello again') 1.256 - print 1.257 - s.inject('foo.bar', 'hello again') 1.258 - print 1.259 - s.inject('foo', 'hello again') 1.260 - print 1.261 - s.start() 1.262 - s.unsubscribe('sys.*', sys_star) 1.263 - s.unsubscribe_all('sys.*') 1.264 - s.inject('sys.foo', 'hello') 1.265 - 1.266 -if __name__ == "__main__": 1.267 - main() 1.268 -
2.1 --- a/tools/python/xen/xend/XendDomain.py Wed Oct 05 00:11:30 2005 +0100 2.2 +++ b/tools/python/xen/xend/XendDomain.py Wed Oct 05 11:43:23 2005 +0100 2.3 @@ -30,7 +30,6 @@ import XendDomainInfo 2.4 2.5 from xen.xend import XendRoot 2.6 from xen.xend import XendCheckpoint 2.7 -from xen.xend import EventServer 2.8 from xen.xend.XendError import XendError 2.9 from xen.xend.XendLogging import log 2.10 from xen.xend.server import relocate 2.11 @@ -38,7 +37,6 @@ from xen.xend.server import relocate 2.12 2.13 xc = xen.lowlevel.xc.new() 2.14 xroot = XendRoot.instance() 2.15 -eserver = EventServer.instance() 2.16 2.17 2.18 __all__ = [ "XendDomain" ] 2.19 @@ -329,20 +327,21 @@ class XendDomain: 2.20 2.21 def domain_unpause(self, domid): 2.22 """Unpause domain execution.""" 2.23 - dominfo = self.domain_lookup(domid) 2.24 - eserver.inject('xend.domain.unpause', [dominfo.getName(), 2.25 - dominfo.getDomid()]) 2.26 try: 2.27 + dominfo = self.domain_lookup(domid) 2.28 + log.info("Domain %s (%d) unpaused.", dominfo.getName(), 2.29 + dominfo.getDomid()) 2.30 return xc.domain_unpause(dom=dominfo.getDomid()) 2.31 except Exception, ex: 2.32 raise XendError(str(ex)) 2.33 - 2.34 + 2.35 + 2.36 def domain_pause(self, domid): 2.37 """Pause domain execution.""" 2.38 - dominfo = self.domain_lookup(domid) 2.39 - eserver.inject('xend.domain.pause', [dominfo.getName(), 2.40 - dominfo.getDomid()]) 2.41 try: 2.42 + dominfo = self.domain_lookup(domid) 2.43 + log.info("Domain %s (%d) paused.", dominfo.getName(), 2.44 + dominfo.getDomid()) 2.45 return xc.domain_pause(dom=dominfo.getDomid()) 2.46 except Exception, ex: 2.47 raise XendError(str(ex))
3.1 --- a/tools/python/xen/xend/XendRoot.py Wed Oct 05 00:11:30 2005 +0100 3.2 +++ b/tools/python/xen/xend/XendRoot.py Wed Oct 05 11:43:23 2005 +0100 3.3 @@ -28,14 +28,11 @@ import os 3.4 import os.path 3.5 import sys 3.6 3.7 -import EventServer 3.8 from XendLogging import XendLogging 3.9 from XendError import XendError 3.10 3.11 -# Initial create of the event server. 3.12 -eserver = EventServer.instance() 3.13 +import sxp 3.14 3.15 -import sxp 3.16 3.17 class XendRoot: 3.18 """Root of the management classes.""" 3.19 @@ -96,9 +93,7 @@ class XendRoot: 3.20 self.config = None 3.21 self.logging = None 3.22 self.configure() 3.23 - eserver.subscribe('xend.*', self.event_handler) 3.24 - #eserver.subscribe('xend.domain.created', self.event_handler) 3.25 - #eserver.subscribe('xend.domain.died', self.event_handler) 3.26 + 3.27 3.28 def add_component(self, name, val): 3.29 """Add a xend component. 3.30 @@ -118,9 +113,6 @@ class XendRoot: 3.31 """ 3.32 return self.components.get(name) 3.33 3.34 - def start(self): 3.35 - eserver.inject('xend.start', 0) 3.36 - 3.37 def _format(self, msg, args): 3.38 if args: 3.39 return str(msg) % args
4.1 --- a/tools/python/xen/xend/server/SrvDaemon.py Wed Oct 05 00:11:30 2005 +0100 4.2 +++ b/tools/python/xen/xend/server/SrvDaemon.py Wed Oct 05 11:43:23 2005 +0100 4.3 @@ -2,6 +2,7 @@ 4.4 ## Xen controller daemon 4.5 ## Copyright (c) 2004, K A Fraser (University of Cambridge) 4.6 ## Copyright (C) 2004, Mike Wray <mike.wray@hp.com> 4.7 +## Copyright (C) 2005, XenSource Ltd 4.8 ########################################################### 4.9 4.10 import os 4.11 @@ -13,20 +14,14 @@ import pwd 4.12 import re 4.13 import traceback 4.14 4.15 -from xen.xend import EventServer 4.16 from xen.xend.server import SrvServer 4.17 from xen.xend.XendLogging import log 4.18 -from xen.xend import XendRoot 4.19 4.20 import event 4.21 import relocate 4.22 from params import * 4.23 4.24 4.25 -eserver = EventServer.instance() 4.26 -xroot = XendRoot.instance() 4.27 - 4.28 - 4.29 class Daemon: 4.30 """The xend daemon. 4.31 """
5.1 --- a/tools/python/xen/xend/server/SrvRoot.py Wed Oct 05 00:11:30 2005 +0100 5.2 +++ b/tools/python/xen/xend/server/SrvRoot.py Wed Oct 05 11:43:23 2005 +0100 5.3 @@ -13,10 +13,9 @@ 5.4 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 5.5 #============================================================================ 5.6 # Copyright (C) 2004, 2005 Mike Wray <mike.wray@hp.com> 5.7 +# Copyright (C) 2005 XenSource Ltd 5.8 #============================================================================ 5.9 5.10 -from xen.xend import XendRoot 5.11 -xroot = XendRoot.instance() 5.12 from xen.web.SrvDir import SrvDir 5.13 5.14 class SrvRoot(SrvDir): 5.15 @@ -39,8 +38,6 @@ class SrvRoot(SrvDir): 5.16 self.add(name, klass) 5.17 for (name, klass) in self.subdirs: 5.18 self.get(name) 5.19 - xroot.start() 5.20 5.21 def __repr__(self): 5.22 return "<SrvRoot %x %s>" %(id(self), self.table.keys()) 5.23 -
6.1 --- a/tools/python/xen/xend/server/event.py Wed Oct 05 00:11:30 2005 +0100 6.2 +++ b/tools/python/xen/xend/server/event.py Wed Oct 05 11:43:23 2005 +0100 6.3 @@ -24,12 +24,10 @@ from xen.web import protocol, tcp, unix 6.4 from xen.xend import scheduler 6.5 from xen.xend import sxp 6.6 from xen.xend import PrettyPrint 6.7 -from xen.xend import EventServer 6.8 from xen.xend.XendError import XendError 6.9 from xen.xend import XendRoot 6.10 6.11 6.12 -eserver = EventServer.instance() 6.13 xroot = XendRoot.instance() 6.14 6.15 6.16 @@ -44,13 +42,7 @@ class EventProtocol(protocol.Protocol): 6.17 self.daemon = daemon 6.18 # Event queue. 6.19 self.queue = [] 6.20 - # Subscribed events. 6.21 - self.events = [] 6.22 self.parser = sxp.Parser() 6.23 - self.pretty = 0 6.24 - 6.25 - # For debugging subscribe to everything and make output pretty. 6.26 - #self.subscribe(['*']) 6.27 self.pretty = 1 6.28 6.29 def dataReceived(self, data): 6.30 @@ -74,7 +66,7 @@ class EventProtocol(protocol.Protocol): 6.31 scheduler.now(self.connectionLost) 6.32 6.33 def connectionLost(self, reason=None): 6.34 - self.unsubscribe() 6.35 + pass 6.36 6.37 def send_reply(self, sxpr): 6.38 io = StringIO.StringIO() 6.39 @@ -105,16 +97,6 @@ class EventProtocol(protocol.Protocol): 6.40 def send_event(self, val): 6.41 return self.send_reply(['event', val[0], val[1]]) 6.42 6.43 - def unsubscribe(self): 6.44 - for event in self.events: 6.45 - eserver.unsubscribe(event, self.queue_event) 6.46 - 6.47 - def subscribe(self, events): 6.48 - self.unsubscribe() 6.49 - for event in events: 6.50 - eserver.subscribe(event, self.queue_event) 6.51 - self.events = events 6.52 - 6.53 def queue_event(self, name, v): 6.54 # Despite the name we don't queue the event here. 6.55 # We send it because the transport will queue it. 6.56 @@ -132,7 +114,7 @@ class EventProtocol(protocol.Protocol): 6.57 op_method = getattr(self, op_method_name, self.operror) 6.58 return op_method(op_name, req) 6.59 6.60 - def op_help(self, name, req): 6.61 + def op_help(self, _1, _2): 6.62 def nameop(x): 6.63 if x.startswith('op_'): 6.64 return x[3:].replace('_', '.') 6.65 @@ -142,37 +124,27 @@ class EventProtocol(protocol.Protocol): 6.66 l = [ nameop(k) for k in dir(self) if k.startswith('op_') ] 6.67 return l 6.68 6.69 - def op_quit(self, name, req): 6.70 + def op_quit(self, _1, _2): 6.71 self.loseConnection() 6.72 6.73 - def op_exit(self, name, req): 6.74 + def op_exit(self, _1, _2): 6.75 sys.exit(0) 6.76 6.77 - def op_pretty(self, name, req): 6.78 + def op_pretty(self, _1, _2): 6.79 self.pretty = 1 6.80 6.81 - def op_info(self, name, req): 6.82 + def op_info(self, _1, _2): 6.83 val = ['info'] 6.84 #val += self.daemon.blkifs() 6.85 #val += self.daemon.netifs() 6.86 #val += self.daemon.usbifs() 6.87 return val 6.88 6.89 - def op_sys_subscribe(self, name, v): 6.90 - # (sys.subscribe event*) 6.91 - # Subscribe to the events: 6.92 - self.subscribe(v[1:]) 6.93 - 6.94 - def op_sys_inject(self, name, v): 6.95 - # (sys.inject event) 6.96 - event = v[1] 6.97 - eserver.inject(sxp.name(event), event) 6.98 - 6.99 - def op_trace(self, name, v): 6.100 + def op_trace(self, _, v): 6.101 mode = (v[1] == 'on') 6.102 self.daemon.tracing(mode) 6.103 6.104 - def op_log_stderr(self, name, v): 6.105 + def op_log_stderr(self, _, v): 6.106 mode = v[1] 6.107 logging = xroot.get_logging() 6.108 if mode == 'on': 6.109 @@ -180,11 +152,11 @@ class EventProtocol(protocol.Protocol): 6.110 else: 6.111 logging.removeLogStderr() 6.112 6.113 - def op_domain_ls(self, name, v): 6.114 + def op_domain_ls(self, _1, _2): 6.115 xd = xroot.get_component("xen.xend.XendDomain") 6.116 return xd.list_names() 6.117 6.118 - def op_domain_configure(self, name, v): 6.119 + def op_domain_configure(self, _, v): 6.120 domid = sxp.child_value(v, "dom") 6.121 config = sxp.child_value(v, "config") 6.122 if domid is None: 6.123 @@ -194,7 +166,7 @@ class EventProtocol(protocol.Protocol): 6.124 xd = xroot.get_component("xen.xend.XendDomain") 6.125 xd.domain_configure(domid, config) 6.126 6.127 - def op_domain_unpause(self, name, v): 6.128 + def op_domain_unpause(self, _, v): 6.129 domid = sxp.child_value(v, "dom") 6.130 if domid is None: 6.131 raise XendError("missing domain id") 6.132 @@ -206,10 +178,10 @@ class EventFactory(protocol.ServerFactor 6.133 """ 6.134 6.135 def __init__(self, daemon): 6.136 - #protocol.ServerFactory.__init__(self) 6.137 + protocol.ServerFactory.__init__(self) 6.138 self.daemon = daemon 6.139 6.140 - def buildProtocol(self, addr): 6.141 + def buildProtocol(self, _): 6.142 return EventProtocol(self.daemon) 6.143 6.144 def listenEvent(daemon):
7.1 --- a/tools/python/xen/xend/server/relocate.py Wed Oct 05 00:11:30 2005 +0100 7.2 +++ b/tools/python/xen/xend/server/relocate.py Wed Oct 05 11:43:23 2005 +0100 7.3 @@ -24,13 +24,11 @@ from xen.web import protocol, tcp, unix 7.4 7.5 from xen.xend import scheduler 7.6 from xen.xend import sxp 7.7 -from xen.xend import EventServer 7.8 from xen.xend.XendError import XendError 7.9 from xen.xend import XendRoot 7.10 from xen.xend.XendLogging import log 7.11 7.12 7.13 -eserver = EventServer.instance() 7.14 xroot = XendRoot.instance() 7.15 7.16