aboutsummaryrefslogtreecommitdiffstats
path: root/tools/python/xen/xend/server/controller.py
diff options
context:
space:
mode:
Diffstat (limited to 'tools/python/xen/xend/server/controller.py')
-rwxr-xr-xtools/python/xen/xend/server/controller.py169
1 files changed, 169 insertions, 0 deletions
diff --git a/tools/python/xen/xend/server/controller.py b/tools/python/xen/xend/server/controller.py
new file mode 100755
index 0000000000..900c2d55b0
--- /dev/null
+++ b/tools/python/xen/xend/server/controller.py
@@ -0,0 +1,169 @@
+from twisted.internet import defer
+
+import channel
+from messages import msgTypeName
+
+class CtrlMsgRcvr:
+ """Abstract class for things that deal with a control interface to a domain.
+ """
+
+
+ def __init__(self):
+ self.channelFactory = channel.channelFactory()
+ self.majorTypes = [ ]
+ self.subTypes = {}
+ self.dom = None
+ self.channel = None
+ self.idx = None
+
+ def requestReceived(self, msg, type, subtype):
+ method = self.subTypes.get(subtype)
+ if method:
+ method(msg, 1)
+ else:
+ print ('requestReceived> No handler: Message type %s %d:%d'
+ % (msgTypeName(type, subtype), type, subtype)), self
+
+ def responseReceived(self, msg, type, subtype):
+ method = self.subTypes.get(subtype)
+ if method:
+ method(msg, 0)
+ else:
+ print ('responseReceived> No handler: Message type %s %d:%d'
+ % (msgTypeName(type, subtype), type, subtype)), self
+
+ def lostChannel(self):
+ pass
+
+ def registerChannel(self):
+ #print 'CtrlMsgRcvr>registerChannel>', self
+ self.channel = self.channelFactory.domChannel(self.dom)
+ self.idx = self.channel.getIndex()
+ if self.majorTypes:
+ self.channel.registerDevice(self.majorTypes, self)
+
+ def deregisterChannel(self):
+ #print 'CtrlMsgRcvr>deregisterChannel>', self
+ if self.channel:
+ self.channel.deregisterDevice(self)
+ del self.channel
+
+ def produceRequests(self):
+ return 0
+
+ def writeRequest(self, msg):
+ if self.channel:
+ self.channel.writeRequest(msg)
+ else:
+ print 'CtrlMsgRcvr>writeRequest>', 'no channel!', self
+
+ def writeResponse(self, msg):
+ if self.channel:
+ self.channel.writeResponse(msg)
+ else:
+ print 'CtrlMsgRcvr>writeResponse>', 'no channel!', self
+
+class ControllerFactory(CtrlMsgRcvr):
+ """Abstract class for factories creating controllers.
+ Maintains a table of instances.
+ """
+
+ def __init__(self):
+ CtrlMsgRcvr.__init__(self)
+ self.instances = {}
+ self.dlist = []
+ self.dom = 0
+ # Timeout (in seconds) for deferreds.
+ self.timeout = 10
+
+ def addInstance(self, instance):
+ self.instances[instance.idx] = instance
+
+ def getInstance(self, idx):
+ return self.instances.get(idx)
+
+ def getInstances(self):
+ return self.instances.values()
+
+ def getInstanceByDom(self, dom):
+ for inst in self.instances.values():
+ if inst.dom == dom:
+ return inst
+ return None
+
+ def delInstance(self, instance):
+ #print 'ControllerFactory>delInstance>', instance.idx
+ if instance.idx in self.instances:
+ #print 'ControllerFactory>delInstance> remove', instance.idx
+ del self.instances[instance.idx]
+
+ def createInstance(self, dom, recreate=0):
+ raise NotImplementedError()
+
+ def instanceClosed(self, instance):
+ #print 'ControllerFactory>instanceClosed>', instance.idx, instance
+ self.delInstance(instance)
+
+ def addDeferred(self):
+ d = defer.Deferred()
+ if self.timeout > 0:
+ # The deferred will error if not called before timeout.
+ d.setTimeout(self.timeout)
+ self.dlist.append(d)
+ return d
+
+ def callDeferred(self, *args):
+ if self.dlist:
+ d = self.dlist.pop(0)
+ d.callback(*args)
+
+ def errDeferred(self, *args):
+ if self.dlist:
+ d = self.dlist.pop(0)
+ d.errback(*args)
+
+class Controller(CtrlMsgRcvr):
+ """Abstract class for a device controller attached to a domain.
+ """
+
+ def __init__(self, factory, dom):
+ CtrlMsgRcvr.__init__(self)
+ self.factory = factory
+ self.dom = int(dom)
+ self.channel = None
+ self.idx = None
+
+ def close(self):
+ self.deregisterChannel()
+ self.lostChannel()
+
+ def lostChannel(self):
+ #print 'Controller>lostChannel>', self, self.factory
+ self.factory.instanceClosed(self)
+
+class Dev:
+
+ def __init__(self, controller):
+ self.controller = controller
+ self.props = {}
+
+ def setprop(self, k, v):
+ self.props[k] = v
+
+ def getprop(self, k, v=None):
+ return self.props.get(k, v)
+
+ def hasprop(self, k):
+ return k in self.props
+
+ def delprop(self, k):
+ if k in self.props:
+ del self.props[k]
+
+ #def __repr__(self):
+ # return str(self.sxpr())
+
+ def sxpr(self):
+ raise NotImplementedError()
+
+