xen-vtx-unstable

annotate tools/python/xen/xend/server/controller.py @ 1623:7a572a6fa64d

bitkeeper revision 1.1026.1.8 (40e1b09foCFBM0EuIgrSA1uLJrWuzA)

Restructuring the python code and libs - first stage.
author mjw@wray-m-3.hpl.hp.com
date Tue Jun 29 18:10:39 2004 +0000 (2004-06-29)
parents
children 236a9f2698a3
rev   line source
mjw@1623 1 from twisted.internet import defer
mjw@1623 2
mjw@1623 3 import channel
mjw@1623 4 from messages import msgTypeName
mjw@1623 5
mjw@1623 6 class CtrlMsgRcvr:
mjw@1623 7 """Abstract class for things that deal with a control interface to a domain.
mjw@1623 8 """
mjw@1623 9
mjw@1623 10
mjw@1623 11 def __init__(self):
mjw@1623 12 self.channelFactory = channel.channelFactory()
mjw@1623 13 self.majorTypes = [ ]
mjw@1623 14 self.subTypes = {}
mjw@1623 15 self.dom = None
mjw@1623 16 self.channel = None
mjw@1623 17 self.idx = None
mjw@1623 18
mjw@1623 19 def requestReceived(self, msg, type, subtype):
mjw@1623 20 method = self.subTypes.get(subtype)
mjw@1623 21 if method:
mjw@1623 22 method(msg, 1)
mjw@1623 23 else:
mjw@1623 24 print ('requestReceived> No handler: Message type %s %d:%d'
mjw@1623 25 % (msgTypeName(type, subtype), type, subtype)), self
mjw@1623 26
mjw@1623 27 def responseReceived(self, msg, type, subtype):
mjw@1623 28 method = self.subTypes.get(subtype)
mjw@1623 29 if method:
mjw@1623 30 method(msg, 0)
mjw@1623 31 else:
mjw@1623 32 print ('responseReceived> No handler: Message type %s %d:%d'
mjw@1623 33 % (msgTypeName(type, subtype), type, subtype)), self
mjw@1623 34
mjw@1623 35 def lostChannel(self):
mjw@1623 36 pass
mjw@1623 37
mjw@1623 38 def registerChannel(self):
mjw@1623 39 #print 'CtrlMsgRcvr>registerChannel>', self
mjw@1623 40 self.channel = self.channelFactory.domChannel(self.dom)
mjw@1623 41 self.idx = self.channel.getIndex()
mjw@1623 42 if self.majorTypes:
mjw@1623 43 self.channel.registerDevice(self.majorTypes, self)
mjw@1623 44
mjw@1623 45 def deregisterChannel(self):
mjw@1623 46 #print 'CtrlMsgRcvr>deregisterChannel>', self
mjw@1623 47 if self.channel:
mjw@1623 48 self.channel.deregisterDevice(self)
mjw@1623 49 del self.channel
mjw@1623 50
mjw@1623 51 def produceRequests(self):
mjw@1623 52 return 0
mjw@1623 53
mjw@1623 54 def writeRequest(self, msg):
mjw@1623 55 if self.channel:
mjw@1623 56 self.channel.writeRequest(msg)
mjw@1623 57 else:
mjw@1623 58 print 'CtrlMsgRcvr>writeRequest>', 'no channel!', self
mjw@1623 59
mjw@1623 60 def writeResponse(self, msg):
mjw@1623 61 if self.channel:
mjw@1623 62 self.channel.writeResponse(msg)
mjw@1623 63 else:
mjw@1623 64 print 'CtrlMsgRcvr>writeResponse>', 'no channel!', self
mjw@1623 65
mjw@1623 66 class ControllerFactory(CtrlMsgRcvr):
mjw@1623 67 """Abstract class for factories creating controllers.
mjw@1623 68 Maintains a table of instances.
mjw@1623 69 """
mjw@1623 70
mjw@1623 71 def __init__(self):
mjw@1623 72 CtrlMsgRcvr.__init__(self)
mjw@1623 73 self.instances = {}
mjw@1623 74 self.dlist = []
mjw@1623 75 self.dom = 0
mjw@1623 76 # Timeout (in seconds) for deferreds.
mjw@1623 77 self.timeout = 10
mjw@1623 78
mjw@1623 79 def addInstance(self, instance):
mjw@1623 80 self.instances[instance.idx] = instance
mjw@1623 81
mjw@1623 82 def getInstance(self, idx):
mjw@1623 83 return self.instances.get(idx)
mjw@1623 84
mjw@1623 85 def getInstances(self):
mjw@1623 86 return self.instances.values()
mjw@1623 87
mjw@1623 88 def getInstanceByDom(self, dom):
mjw@1623 89 for inst in self.instances.values():
mjw@1623 90 if inst.dom == dom:
mjw@1623 91 return inst
mjw@1623 92 return None
mjw@1623 93
mjw@1623 94 def delInstance(self, instance):
mjw@1623 95 #print 'ControllerFactory>delInstance>', instance.idx
mjw@1623 96 if instance.idx in self.instances:
mjw@1623 97 #print 'ControllerFactory>delInstance> remove', instance.idx
mjw@1623 98 del self.instances[instance.idx]
mjw@1623 99
mjw@1623 100 def createInstance(self, dom, recreate=0):
mjw@1623 101 raise NotImplementedError()
mjw@1623 102
mjw@1623 103 def instanceClosed(self, instance):
mjw@1623 104 #print 'ControllerFactory>instanceClosed>', instance.idx, instance
mjw@1623 105 self.delInstance(instance)
mjw@1623 106
mjw@1623 107 def addDeferred(self):
mjw@1623 108 d = defer.Deferred()
mjw@1623 109 if self.timeout > 0:
mjw@1623 110 # The deferred will error if not called before timeout.
mjw@1623 111 d.setTimeout(self.timeout)
mjw@1623 112 self.dlist.append(d)
mjw@1623 113 return d
mjw@1623 114
mjw@1623 115 def callDeferred(self, *args):
mjw@1623 116 if self.dlist:
mjw@1623 117 d = self.dlist.pop(0)
mjw@1623 118 d.callback(*args)
mjw@1623 119
mjw@1623 120 def errDeferred(self, *args):
mjw@1623 121 if self.dlist:
mjw@1623 122 d = self.dlist.pop(0)
mjw@1623 123 d.errback(*args)
mjw@1623 124
mjw@1623 125 class Controller(CtrlMsgRcvr):
mjw@1623 126 """Abstract class for a device controller attached to a domain.
mjw@1623 127 """
mjw@1623 128
mjw@1623 129 def __init__(self, factory, dom):
mjw@1623 130 CtrlMsgRcvr.__init__(self)
mjw@1623 131 self.factory = factory
mjw@1623 132 self.dom = int(dom)
mjw@1623 133 self.channel = None
mjw@1623 134 self.idx = None
mjw@1623 135
mjw@1623 136 def close(self):
mjw@1623 137 self.deregisterChannel()
mjw@1623 138 self.lostChannel()
mjw@1623 139
mjw@1623 140 def lostChannel(self):
mjw@1623 141 #print 'Controller>lostChannel>', self, self.factory
mjw@1623 142 self.factory.instanceClosed(self)
mjw@1623 143
mjw@1623 144 class Dev:
mjw@1623 145
mjw@1623 146 def __init__(self, controller):
mjw@1623 147 self.controller = controller
mjw@1623 148 self.props = {}
mjw@1623 149
mjw@1623 150 def setprop(self, k, v):
mjw@1623 151 self.props[k] = v
mjw@1623 152
mjw@1623 153 def getprop(self, k, v=None):
mjw@1623 154 return self.props.get(k, v)
mjw@1623 155
mjw@1623 156 def hasprop(self, k):
mjw@1623 157 return k in self.props
mjw@1623 158
mjw@1623 159 def delprop(self, k):
mjw@1623 160 if k in self.props:
mjw@1623 161 del self.props[k]
mjw@1623 162
mjw@1623 163 #def __repr__(self):
mjw@1623 164 # return str(self.sxpr())
mjw@1623 165
mjw@1623 166 def sxpr(self):
mjw@1623 167 raise NotImplementedError()
mjw@1623 168
mjw@1623 169