ia64/xen-unstable

changeset 2224:6c85a89e99e3

bitkeeper revision 1.1159.1.52 (411cbd47vM82ZdOs6j1qiL3UlWzebA)

Abstract message dispatch to methods so that more than one major
message type can be handled.
author mjw@wray-m-3.hpl.hp.com
date Fri Aug 13 13:08:23 2004 +0000 (2004-08-13)
parents 37ef3fbdde75
children f8bc691dd640
files tools/python/xen/xend/server/blkif.py tools/python/xen/xend/server/console.py tools/python/xen/xend/server/controller.py tools/python/xen/xend/server/netif.py
line diff
     1.1 --- a/tools/python/xen/xend/server/blkif.py	Fri Aug 13 12:14:11 2004 +0000
     1.2 +++ b/tools/python/xen/xend/server/blkif.py	Fri Aug 13 13:08:23 2004 +0000
     1.3 @@ -20,12 +20,9 @@ class BlkifControllerFactory(controller.
     1.4  
     1.5      def __init__(self):
     1.6          controller.ControllerFactory.__init__(self)
     1.7 -
     1.8 -        self.majorTypes = [ CMSG_BLKIF_BE ]
     1.9 -
    1.10 -        self.subTypes = {
    1.11 -            CMSG_BLKIF_BE_DRIVER_STATUS_CHANGED: self.recv_be_driver_status_changed,
    1.12 -            }
    1.13 +        self.addMethod(CMSG_BLKIF_BE,
    1.14 +                       CMSG_BLKIF_BE_DRIVER_STATUS_CHANGED,
    1.15 +                       self.recv_be_driver_status_changed)
    1.16          self.attached = 1
    1.17          self.registerChannel()
    1.18  
    1.19 @@ -262,15 +259,12 @@ class BlkifController(controller.Control
    1.20      def __init__(self, factory, dom):
    1.21          controller.Controller.__init__(self, factory, dom)
    1.22          self.devices = {}
    1.23 -
    1.24 -        self.majorTypes = [ CMSG_BLKIF_FE ]
    1.25 -
    1.26 -        self.subTypes = {
    1.27 -            CMSG_BLKIF_FE_DRIVER_STATUS_CHANGED:
    1.28 -                self.recv_fe_driver_status_changed,
    1.29 -            CMSG_BLKIF_FE_INTERFACE_CONNECT    :
    1.30 -                self.recv_fe_interface_connect,
    1.31 -            }
    1.32 +        self.addMethod(CMSG_BLKIF_FE,
    1.33 +                       CMSG_BLKIF_FE_DRIVER_STATUS_CHANGED,
    1.34 +                       self.recv_fe_driver_status_changed)
    1.35 +        self.addMethod(CMSG_BLKIF_FE,
    1.36 +                       CMSG_BLKIF_FE_INTERFACE_CONNECT,
    1.37 +                       self.recv_fe_interface_connect)
    1.38          self.attached = 1
    1.39          self.evtchn = None
    1.40          self.registerChannel()
     2.1 --- a/tools/python/xen/xend/server/console.py	Fri Aug 13 12:14:11 2004 +0000
     2.2 +++ b/tools/python/xen/xend/server/console.py	Fri Aug 13 13:08:23 2004 +0000
     2.3 @@ -113,7 +113,7 @@ class ConsoleController(controller.Contr
     2.4  
     2.5      def __init__(self, factory, dom, console_port):
     2.6          controller.Controller.__init__(self, factory, dom)
     2.7 -        self.majorTypes = [ CMSG_CONSOLE ]
     2.8 +        self.addMethod(CMSG_CONSOLE, 0, None)
     2.9          self.status = self.STATUS_NEW
    2.10          self.addr = None
    2.11          self.conn = None
     3.1 --- a/tools/python/xen/xend/server/controller.py	Fri Aug 13 12:14:11 2004 +0000
     3.2 +++ b/tools/python/xen/xend/server/controller.py	Fri Aug 13 13:08:23 2004 +0000
     3.3 @@ -9,7 +9,7 @@ from twisted.internet import defer
     3.4  import channel
     3.5  from messages import msgTypeName, printMsg
     3.6  
     3.7 -DEBUG = 0
     3.8 +DEBUG = 1
     3.9  
    3.10  class Responder:
    3.11      """Handler for a response to a message with a specified id.
    3.12 @@ -56,9 +56,7 @@ class CtrlMsgRcvr:
    3.13      @ivar dom: the domain we are a control interface for
    3.14      @type dom: int
    3.15      @ivar majorTypes: major message types we are interested in
    3.16 -    @type majorTypes: [int]
    3.17 -    @ivar subTypes: mapping of message subtypes to methods
    3.18 -    @ivar subTypes: {int:method}
    3.19 +    @type majorTypes: {int:{int:method}}
    3.20      @ivar timeout: timeout (in seconds) for message handlers
    3.21      @type timeout: int
    3.22      
    3.23 @@ -72,8 +70,7 @@ class CtrlMsgRcvr:
    3.24  
    3.25      def __init__(self):
    3.26          self.channelFactory = channel.channelFactory()
    3.27 -        self.majorTypes = [ ]
    3.28 -        self.subTypes = {}
    3.29 +        self.majorTypes = {}
    3.30          self.dom = None
    3.31          self.channel = None
    3.32          self.idx = None
    3.33 @@ -83,6 +80,37 @@ class CtrlMsgRcvr:
    3.34      def setTimeout(self, timeout):
    3.35          self.timeout = timeout
    3.36  
    3.37 +    def getMethod(self, type, subtype):
    3.38 +        """Get the method for a type and subtype.
    3.39 +
    3.40 +        @param type: major message type
    3.41 +        @param subtype: minor message type
    3.42 +        @return: method or None
    3.43 +        """
    3.44 +        method = None
    3.45 +        subtypes = self.majorTypes.get(type)
    3.46 +        if subtypes:
    3.47 +            method = subtypes.get(subtype)
    3.48 +        return method
    3.49 +
    3.50 +    def addMethod(self, type, subtype, method):
    3.51 +        """Add a method to handle a message type and subtype.
    3.52 +        
    3.53 +        @param type: major message type
    3.54 +        @param subtype: minor message type
    3.55 +        @param method: method
    3.56 +        """
    3.57 +        subtypes = self.majorTypes.get(type)
    3.58 +        if not subtypes:
    3.59 +            subtypes = {}
    3.60 +            self.majorTypes[type] = subtypes
    3.61 +        subtypes[subtype] = method
    3.62 +
    3.63 +    def getMajorTypes(self):
    3.64 +        """Get the list of major message types handled.
    3.65 +        """
    3.66 +        return self.majorTypes.keys()
    3.67 +
    3.68      def requestReceived(self, msg, type, subtype):
    3.69          """Dispatch a request message to handlers.
    3.70          Called by the channel for requests with one of our types.
    3.71 @@ -97,7 +125,7 @@ class CtrlMsgRcvr:
    3.72          if DEBUG:
    3.73              print 'requestReceived>',
    3.74              printMsg(msg, all=1)
    3.75 -        method = self.subTypes.get(subtype)
    3.76 +        method = self.getMethod(type, subtype)
    3.77          if method:
    3.78              method(msg, 1)
    3.79          elif DEBUG:
    3.80 @@ -125,7 +153,7 @@ class CtrlMsgRcvr:
    3.81              printMsg(msg, all=1)
    3.82          if self.callResponders(msg):
    3.83              return
    3.84 -        method = self.subTypes.get(subtype)
    3.85 +        method = self.getMethod(type, subtype)
    3.86          if method:
    3.87              method(msg, 0)
    3.88          elif DEBUG:
    3.89 @@ -190,7 +218,7 @@ class CtrlMsgRcvr:
    3.90          self.channel = self.channelFactory.domChannel(self.dom)
    3.91          self.idx = self.channel.getIndex()
    3.92          if self.majorTypes:
    3.93 -            self.channel.registerDevice(self.majorTypes, self)
    3.94 +            self.channel.registerDevice(self.getMajorTypes(), self)
    3.95          
    3.96      def deregisterChannel(self):
    3.97          """Deregister interest in our major message types with the
     4.1 --- a/tools/python/xen/xend/server/netif.py	Fri Aug 13 12:14:11 2004 +0000
     4.2 +++ b/tools/python/xen/xend/server/netif.py	Fri Aug 13 13:08:23 2004 +0000
     4.3 @@ -25,12 +25,9 @@ class NetifControllerFactory(controller.
     4.4  
     4.5      def __init__(self):
     4.6          controller.ControllerFactory.__init__(self)
     4.7 -
     4.8 -        self.majorTypes = [ CMSG_NETIF_BE ]
     4.9 -
    4.10 -        self.subTypes = {
    4.11 -            CMSG_NETIF_BE_DRIVER_STATUS_CHANGED: self.recv_be_driver_status_changed,
    4.12 -            }
    4.13 +        self.addMethod(CMSG_NETIF_BE,
    4.14 +                       CMSG_NETIF_BE_DRIVER_STATUS_CHANGED,
    4.15 +                       self.recv_be_driver_status_changed)
    4.16          self.attached = 1
    4.17          self.registerChannel()
    4.18  
    4.19 @@ -218,14 +215,12 @@ class NetifController(controller.Control
    4.20          controller.Controller.__init__(self, factory, dom)
    4.21          self.devices = {}
    4.22          
    4.23 -        self.majorTypes = [ CMSG_NETIF_FE ]
    4.24 -
    4.25 -        self.subTypes = {
    4.26 -            CMSG_NETIF_FE_DRIVER_STATUS_CHANGED:
    4.27 -                self.recv_fe_driver_status_changed,
    4.28 -            CMSG_NETIF_FE_INTERFACE_CONNECT    :
    4.29 -                self.recv_fe_interface_connect,
    4.30 -            }
    4.31 +        self.addMethod(CMSG_NETIF_FE,
    4.32 +                       CMSG_NETIF_FE_DRIVER_STATUS_CHANGED,
    4.33 +                       self.recv_fe_driver_status_changed)
    4.34 +        self.addMethod(CMSG_NETIF_FE,
    4.35 +                       CMSG_NETIF_FE_INTERFACE_CONNECT,
    4.36 +                       self.recv_fe_interface_connect)
    4.37          self.registerChannel()
    4.38  
    4.39      def sxpr(self):