ia64/xen-unstable

changeset 5492:a9acc88e9d79

bitkeeper revision 1.1713.3.14 (42b31404a5lBMKNKYzOsHXWQ0k_d-Q)

xsnode.py:
Updated watches/event code from Mike Wray.
Signed-off-by: Mike Wray <mike.wray@hp.com>
Signed-off-by: Christian Limpach <Christian.Limpach@cl.cam.ac.uk>
author cl349@firebug.cl.cam.ac.uk
date Fri Jun 17 18:18:44 2005 +0000 (2005-06-17)
parents c3f4626710c4
children 999293916aa7 a263fcd442b2
files tools/python/xen/xend/xenstore/xsnode.py
line diff
     1.1 --- a/tools/python/xen/xend/xenstore/xsnode.py	Fri Jun 17 17:59:02 2005 +0000
     1.2 +++ b/tools/python/xen/xend/xenstore/xsnode.py	Fri Jun 17 18:18:44 2005 +0000
     1.3 @@ -46,12 +46,12 @@ class Subscription:
     1.4              watcher.delSubs(self)
     1.5          return watcher
     1.6  
     1.7 -    def notify(self, path, val):
     1.8 +    def notify(self, token, path, val):
     1.9          try:
    1.10 -            self.fn(self, path, val)
    1.11 -        except SystemExitException:
    1.12 +            self.fn(self, token, path, val)
    1.13 +        except SystemExit:
    1.14              raise
    1.15 -        except:
    1.16 +        except Exception, ex:
    1.17              pass
    1.18  
    1.19  class Watcher:
    1.20 @@ -71,6 +71,9 @@ class Watcher:
    1.21      def getPath(self):
    1.22          return self.path
    1.23  
    1.24 +    def getToken(self):
    1.25 +        return self.path
    1.26 +
    1.27      def addSubs(self, subs):
    1.28          self.subscriptions.append(subs)
    1.29          self.watch()
    1.30 @@ -83,14 +86,15 @@ class Watcher:
    1.31      def watch(self):
    1.32          if self.xs: return
    1.33          self.xs = xs.open()
    1.34 -        self.xs.watch(self.path)
    1.35 +        self.xs.watch(path=self.getPath(), token=self.getToken())
    1.36  
    1.37      def unwatch(self):
    1.38          if self.xs:
    1.39 -            try:
    1.40 -                self.xs.unwatch(self.path)
    1.41 -            except Exception, ex:
    1.42 -                print 'Watcher>unwatch>', ex
    1.43 +## Possibly crashes xenstored.
    1.44 +##            try:
    1.45 +##                self.xs.unwatch(path=self.getPath(), token=self.getToken())
    1.46 +##            except Exception, ex:
    1.47 +##                print 'Watcher>unwatch>', ex
    1.48              try:
    1.49                  self.xs.close()
    1.50              except Exception, ex:
    1.51 @@ -102,17 +106,22 @@ class Watcher:
    1.52  
    1.53      def getNotification(self):
    1.54          p = self.xs.read_watch()
    1.55 -        self.xs.acknowledge_watch()
    1.56 +        self.xs.acknowledge_watch(p[1])
    1.57          return p
    1.58  
    1.59      def notify(self):
    1.60          try:
    1.61 -            p = self.getNotification()
    1.62 -            v = self.xs.read(p)
    1.63 -            for s in subscriptions:
    1.64 -                s.notify(p, v)
    1.65 +            (path, token) = self.getNotification()
    1.66 +            if path.endswith("@eid"):
    1.67 +                pass
    1.68 +            else:
    1.69 +                val = self.xs.read(path)
    1.70 +                for subs in self.subscriptions:
    1.71 +                    subs.notify(token, path, val)
    1.72 +        except SystemExit:
    1.73 +            raise
    1.74          except Exception, ex:
    1.75 -            print 'Notify exception:', ex
    1.76 +            raise
    1.77  
    1.78  class EventWatcher(Watcher):
    1.79  
    1.80 @@ -126,6 +135,9 @@ class EventWatcher(Watcher):
    1.81      def getEvent(self):
    1.82          return self.event
    1.83  
    1.84 +    def getToken(self):
    1.85 +        return self.event
    1.86 +
    1.87  class XenStore:
    1.88  
    1.89      xs = None
    1.90 @@ -145,8 +157,10 @@ class XenStore:
    1.91                      self.xs = xs.open()
    1.92                      ex = None
    1.93                      break
    1.94 +                except SystemExit:
    1.95 +                    raise
    1.96                  except Exception, ex:
    1.97 -                    print >>sys.stderr, "Exception connecting to xsdaemon:", ex
    1.98 +                    print >>sys.stderr, "Exception connecting to xenstored:", ex
    1.99                      print >>sys.stderr, "Trying again..."
   1.100                      time.sleep(1)
   1.101              else:
   1.102 @@ -241,7 +255,10 @@ class XenStore:
   1.103  
   1.104      def write(self, path, data, create=True, excl=False):
   1.105          self.mkdirs(path)
   1.106 -        self.getxs().write(path, data, create=create, excl=excl)
   1.107 +        try:
   1.108 +            self.getxs().write(path, data, create=create, excl=excl)
   1.109 +        except Exception, ex:
   1.110 +            raise
   1.111  
   1.112      def begin(self, path):
   1.113          self.getxs().transaction_start(path)
   1.114 @@ -261,7 +278,10 @@ class XenStore:
   1.115          del self.subscriptions[s.sid]
   1.116          watcher = s.unwatch()
   1.117          if watcher and not watcher.watching():
   1.118 -            del self.watchers[path]
   1.119 +            try:
   1.120 +                del self.watchers[watcher.getPath()]
   1.121 +            except:
   1.122 +                pass
   1.123  
   1.124      def subscribe(self, event, fn):
   1.125          path = getEventPath(event)
   1.126 @@ -280,13 +300,10 @@ class XenStore:
   1.127              self.mkdirs(eventPath)
   1.128              eid = 1
   1.129              if self.exists(eidPath):
   1.130 -                data = self.read(eidPath)
   1.131 -                print 'sendEvent>', 'data=', data, type(data)
   1.132                  try:
   1.133                      eid = int(self.read(eidPath))
   1.134                      eid += 1
   1.135                  except Exception, ex:
   1.136 -                    print 'sendEvent>', ex
   1.137                      pass
   1.138              self.write(eidPath, str(eid))
   1.139              self.write(os.path.join(eventPath, str(eid)), data)
   1.140 @@ -346,6 +363,15 @@ def getXenStore():
   1.141          xenstore = XenStore()
   1.142          return xenstore
   1.143  
   1.144 +def sendEvent(event, val):
   1.145 +    getXenStore.sendEvent(event, val)
   1.146 +
   1.147 +def subscribe(event, fn):
   1.148 +    return getXenStore().subscribe(event, fn)
   1.149 +
   1.150 +def unsubscribe(sid):
   1.151 +    getXenStore().unsubscribe(sid)
   1.152 +
   1.153  class XenNode:
   1.154  
   1.155      def __init__(self, path="/", create=True):
   1.156 @@ -389,9 +415,7 @@ class XenNode:
   1.157              return None
   1.158  
   1.159      def setData(self, data, path=""):
   1.160 -        path = self.relPath(path)
   1.161 -        #print 'XenNode>setData>', 'path=', path, 'data=', data
   1.162 -        return self.store.write(path, data)
   1.163 +        return self.store.write(self.relPath(path), data)
   1.164  
   1.165      def getLock(self):
   1.166          return None