Ejemplo n.º 1
0
    def connect(self, masterUrl, deferred):
        """ Connect to RCE.

            @param masterUrl:   URL of Authentication Handler of Master Manager
            @type  masterUrl:   str

            @param deferred:    Deferred which is called as soon as the
                                connection was successfully established.
            @type  deferred:    twisted.internet.defer.Deferred

            @raise:             ConnectionError, if no connection could be
                                established.
        """
        if self._rce:
            raise ConnectionError('There is already a connection registered.')

        self._rce = RCE(self, self._userID, self._robotID, self._password,
                        self._reactor)

        # Connect
        self._rce.connect(masterUrl, deferred)
Ejemplo n.º 2
0
    def connect(self, masterUrl, deferred):
        """ Connect to RCE.

            @param masterUrl:   URL of Authentication Handler of Master Manager
            @type  masterUrl:   str

            @param deferred:    Deferred which is called as soon as the
                                connection was successfully established.
            @type  deferred:    twisted.internet.defer.Deferred

            @raise:             ConnectionError, if no connection could be
                                established.
        """
        if self._rce:
            raise ConnectionError('There is already a connection registered.')

        self._rce = RCE(self, self._userID, self._robotID, self._password,
                        self._reactor)

        # Connect
        self._rce.connect(masterUrl, deferred)
Ejemplo n.º 3
0
class _Connection(object):
    """ Abstract implementation of a Connection.
    """
    implements(IRobot, IClient)

    INTERFACE_MAP = {}

    def __init__(self, userID, robotID, password, reactor):
        """ Initialize the Connection.

            @param userID:      User ID which will be used to authenticate the
                                connection.
            @type  userID:      str

            @param robotID:     Robot ID which will be used to authenticate the
                                connection.
            @type  robotID:     str

            @param password:    Password which will be used to authenticate the
                                connection.
            @type  password:    str

            @param reactor:     Reference to reactor which is used for this
                                connection.
            @type  reactor:     twisted::reactor
        """
        self._userID = userID
        self._robotID = robotID
        self._password = password
        self._reactor = reactor

        self._rce = None
        self._interfaces = {}

        self.disconnect()

    def __del__(self):
        self.disconnect()

    @property
    def reactor(self):
        """ Reference to twisted::reactor. """
        return self._reactor

    def connect(self, masterUrl, deferred):
        """ Connect to RCE.

            @param masterUrl:   URL of Authentication Handler of Master Manager
            @type  masterUrl:   str

            @param deferred:    Deferred which is called as soon as the
                                connection was successfully established.
            @type  deferred:    twisted.internet.defer.Deferred

            @raise:             ConnectionError, if no connection could be
                                established.
        """
        if self._rce:
            raise ConnectionError('There is already a connection registered.')

        self._rce = RCE(self, self._userID, self._robotID, self._password,
                        self._reactor)

        # Connect
        self._rce.connect(masterUrl, deferred)

    def disconnect(self):
        """ Disconnect from RCE.
        """
        if self._rce:
            self._rce.close()

        self._rce = None

    # Callback Interface objects

    def registerInterface(self, iTag, interface):
        """ Callback for Interface.

            @param iTag:        Tag of interface which should be registered.
            @type  iTag:        str

            @param interface:   Interface instance which should be registered.
            @type  interface:   rce.client.interface.*
        """
        if iTag not in self._interfaces:
            self._interfaces[iTag] = weakref.WeakSet()
        elif interface.UNIQUE:
            raise ValueError('Can not have multiple interfaces with the same '
                             'tag.')

        self._interfaces[iTag].add(interface)

    def unregisterInterface(self, iTag, interface):
        """ Callback for Interfaces.

            @param iTag:        Tag of interface which should be unregistered.
            @type  iTag:        str

            @param interface:   Interface instance which should be
                                unregistered.
            @type  interface:   rce.client.interface.*
        """
        if iTag not in self._interfaces:
            raise ValueError('No Interface registered with tag '
                             "'{0}'.".format(iTag))

        interfaces = self._interfaces[iTag]
        interfaces.discard(interface)

        if not interfaces:
            del self._interfaces[iTag]

    # Callback Client Protocol

    def processReceivedMessage(self, iTag, clsName, msgID, msg):
        try:
            interfaces = self._interfaces[iTag].copy()
        except (KeyError, weakref.ReferenceError):
            interfaces = []

        for interface in interfaces:
            if interface.CALLABLE:
                interface.callback(clsName, msg, msgID)

    processReceivedMessage.__doc__ = \
        IClient.get('processReceivedMessage').getDoc()

    def processInterfaceStatusUpdate(self, iTag, status):
        try:
            interfaces = self._interfaces[iTag].copy()
        except (KeyError, weakref.ReferenceError):
            interfaces = []

        for interface in interfaces:
            interface.setEnabled(status)

    processInterfaceStatusUpdate.__doc__ = \
        IClient.get('processInterfaceStatusUpdate').getDoc()

    # Forwarding

    def sendMessage(self, dest, msgType, msg, msgID):
        if not self._rce:
            raise ConnectionError('No connection to RCE.')

        self._rce.sendMessage(dest, msgType, msg, msgID)

    sendMessage.__doc__ = RCE.sendMessage.__doc__  #@UndefinedVariable

    def createContainer(self, cTag, group='', groupIp='', size=1, cpu=0,
                        memory=0, bandwidth=0, specialFeatures=[]):
        if not self._rce:
            raise ConnectionError('No connection to RCE.')

        # ensure all whitespace characters around group are stripped
        group = group.strip()

        if groupIp and not _IP_V4_REGEX.match(groupIp):
            raise ValueError('Invalid IPv4 address')

        self._rce.createContainer(cTag, group, groupIp, size, cpu, memory,
                                  bandwidth, specialFeatures)

    createContainer.__doc__ = RCE.createContainer.__doc__  #@UndefinedVariable

    def destroyContainer(self, cTag):
        if not self._rce:
            raise ConnectionError('No connection to RCE.')

        self._rce.destroyContainer(cTag)

    destroyContainer.__doc__ = RCE.destroyContainer.__doc__  #@UndefinedVariable

    def addNode(self, cTag, nTag, pkg, exe, args='', name='', namespace=''):
        if not self._rce:
            raise ConnectionError('No connection to RCE.')

        self._rce.addNode(cTag, nTag, pkg, exe, args, name, namespace)

    addNode.__doc__ = RCE.addNode.__doc__  #@UndefinedVariable

    def removeNode(self, cTag, nTag):
        if not self._rce:
            raise ConnectionError('No connection to RCE.')

        self._rce.removeNode(cTag, nTag)

    removeNode.__doc__ = RCE.removeNode.__doc__  #@UndefinedVariable

    def addParameter(self, cTag, name, value):
        if not self._rce:
            raise ConnectionError('No connection to RCE.')

        self._rce.addParameter(cTag, name, value)

    addParameter.__doc__ = RCE.addParameter.__doc__  #@UndefinedVariable

    def removeParameter(self, cTag, name):
        if not self._rce:
            raise ConnectionError('No connection to RCE.')

        self._rce.removeParameter(cTag, name)

    removeParameter.__doc__ = RCE.removeParameter.__doc__  #@UndefinedVariable

    def addInterface(self, eTag, iTag, iType, iCls, addr=''):
        if not self._rce:
            raise ConnectionError('No connection to RCE.')

        iType = self.INTERFACE_MAP.get(iType, iType)
        self._rce.addInterface(eTag, iTag, iType, iCls, addr)

    addInterface.__doc__ = RCE.addInterface.__doc__  #@UndefinedVariable

    def removeInterface(self, eTag, iTag):
        if not self._rce:
            raise ConnectionError('No connection to RCE.')

        self._rce.removeInterface(eTag, iTag)

    removeInterface.__doc__ = RCE.removeInterface.__doc__  #@UndefinedVariable

    def addConnection(self, tagA, tagB):
        if not self._rce:
            raise ConnectionError('No connection to RCE.')

        self._rce.addConnection(tagA, tagB)

    addConnection.__doc__ = RCE.addConnection.__doc__  #@UndefinedVariable

    def removeConnection(self, tagA, tagB):
        if not self._rce:
            raise ConnectionError('No connection to RCE.')

        self._rce.removeConnection(tagA, tagB)

    removeConnection.__doc__ = RCE.removeConnection.__doc__  #@UndefinedVariable
Ejemplo n.º 4
0
class _Connection(object):
    """ Abstract implementation of a Connection.
    """
    implements(IRobot, IClient)

    INTERFACE_MAP = {}

    def __init__(self, userID, robotID, password, reactor):
        """ Initialize the Connection.

            @param userID:      User ID which will be used to authenticate the
                                connection.
            @type  userID:      str

            @param robotID:     Robot ID which will be used to authenticate the
                                connection.
            @type  robotID:     str

            @param password:    Password which will be used to authenticate the
                                connection.
            @type  password:    str

            @param reactor:     Reference to reactor which is used for this
                                connection.
            @type  reactor:     twisted::reactor
        """
        self._userID = userID
        self._robotID = robotID
        self._password = password
        self._reactor = reactor

        self._rce = None
        self._interfaces = {}

        self.disconnect()

    def __del__(self):
        self.disconnect()

    @property
    def reactor(self):
        """ Reference to twisted::reactor. """
        return self._reactor

    def connect(self, masterUrl, deferred):
        """ Connect to RCE.

            @param masterUrl:   URL of Authentication Handler of Master Manager
            @type  masterUrl:   str

            @param deferred:    Deferred which is called as soon as the
                                connection was successfully established.
            @type  deferred:    twisted.internet.defer.Deferred

            @raise:             ConnectionError, if no connection could be
                                established.
        """
        if self._rce:
            raise ConnectionError('There is already a connection registered.')

        self._rce = RCE(self, self._userID, self._robotID, self._password,
                        self._reactor)

        # Connect
        self._rce.connect(masterUrl, deferred)

    def disconnect(self):
        """ Disconnect from RCE.
        """
        if self._rce:
            self._rce.close()

        self._rce = None

    # Callback Interface objects

    def registerInterface(self, iTag, interface):
        """ Callback for Interface.

            @param iTag:        Tag of interface which should be registered.
            @type  iTag:        str

            @param interface:   Interface instance which should be registered.
            @type  interface:   rce.client.interface.*
        """
        if iTag not in self._interfaces:
            self._interfaces[iTag] = weakref.WeakSet()
        elif interface.UNIQUE:
            raise ValueError('Can not have multiple interfaces with the same '
                             'tag.')

        self._interfaces[iTag].add(interface)

    def unregisterInterface(self, iTag, interface):
        """ Callback for Interfaces.

            @param iTag:        Tag of interface which should be unregistered.
            @type  iTag:        str

            @param interface:   Interface instance which should be
                                unregistered.
            @type  interface:   rce.client.interface.*
        """
        if iTag not in self._interfaces:
            raise ValueError('No Interface registered with tag '
                             "'{0}'.".format(iTag))

        interfaces = self._interfaces[iTag]
        interfaces.discard(interface)

        if not interfaces:
            del self._interfaces[iTag]

    # Callback Client Protocol

    def processReceivedMessage(self, iTag, clsName, msgID, msg):
        try:
            interfaces = self._interfaces[iTag].copy()
        except (KeyError, weakref.ReferenceError):
            interfaces = []

        for interface in interfaces:
            if interface.CALLABLE:
                interface.callback(clsName, msg, msgID)

    processReceivedMessage.__doc__ = \
        IClient.get('processReceivedMessage').getDoc()

    def processInterfaceStatusUpdate(self, iTag, status):
        try:
            interfaces = self._interfaces[iTag].copy()
        except (KeyError, weakref.ReferenceError):
            interfaces = []

        for interface in interfaces:
            interface.setEnabled(status)

    processInterfaceStatusUpdate.__doc__ = \
        IClient.get('processInterfaceStatusUpdate').getDoc()

    # Forwarding

    def sendMessage(self, dest, msgType, msg, msgID):
        if not self._rce:
            raise ConnectionError('No connection to RCE.')

        self._rce.sendMessage(dest, msgType, msg, msgID)

    sendMessage.__doc__ = RCE.sendMessage.__doc__  #@UndefinedVariable

    def createContainer(self,
                        cTag,
                        group='',
                        groupIp='',
                        size=1,
                        cpu=0,
                        memory=0,
                        bandwidth=0,
                        specialFeatures=[]):
        if not self._rce:
            raise ConnectionError('No connection to RCE.')

        # ensure all whitespace characters around group are stripped
        group = group.strip()

        if groupIp and not _IP_V4_REGEX.match(groupIp):
            raise ValueError('Invalid IPv4 address')

        self._rce.createContainer(cTag, group, groupIp, size, cpu, memory,
                                  bandwidth, specialFeatures)

    createContainer.__doc__ = RCE.createContainer.__doc__  #@UndefinedVariable

    def destroyContainer(self, cTag):
        if not self._rce:
            raise ConnectionError('No connection to RCE.')

        self._rce.destroyContainer(cTag)

    destroyContainer.__doc__ = RCE.destroyContainer.__doc__  #@UndefinedVariable

    def addNode(self, cTag, nTag, pkg, exe, args='', name='', namespace=''):
        if not self._rce:
            raise ConnectionError('No connection to RCE.')

        self._rce.addNode(cTag, nTag, pkg, exe, args, name, namespace)

    addNode.__doc__ = RCE.addNode.__doc__  #@UndefinedVariable

    def removeNode(self, cTag, nTag):
        if not self._rce:
            raise ConnectionError('No connection to RCE.')

        self._rce.removeNode(cTag, nTag)

    removeNode.__doc__ = RCE.removeNode.__doc__  #@UndefinedVariable

    def addParameter(self, cTag, name, value):
        if not self._rce:
            raise ConnectionError('No connection to RCE.')

        self._rce.addParameter(cTag, name, value)

    addParameter.__doc__ = RCE.addParameter.__doc__  #@UndefinedVariable

    def removeParameter(self, cTag, name):
        if not self._rce:
            raise ConnectionError('No connection to RCE.')

        self._rce.removeParameter(cTag, name)

    removeParameter.__doc__ = RCE.removeParameter.__doc__  #@UndefinedVariable

    def addInterface(self, eTag, iTag, iType, iCls, addr=''):
        if not self._rce:
            raise ConnectionError('No connection to RCE.')

        iType = self.INTERFACE_MAP.get(iType, iType)
        self._rce.addInterface(eTag, iTag, iType, iCls, addr)

    addInterface.__doc__ = RCE.addInterface.__doc__  #@UndefinedVariable

    def removeInterface(self, eTag, iTag):
        if not self._rce:
            raise ConnectionError('No connection to RCE.')

        self._rce.removeInterface(eTag, iTag)

    removeInterface.__doc__ = RCE.removeInterface.__doc__  #@UndefinedVariable

    def addConnection(self, tagA, tagB):
        if not self._rce:
            raise ConnectionError('No connection to RCE.')

        self._rce.addConnection(tagA, tagB)

    addConnection.__doc__ = RCE.addConnection.__doc__  #@UndefinedVariable

    def removeConnection(self, tagA, tagB):
        if not self._rce:
            raise ConnectionError('No connection to RCE.')

        self._rce.removeConnection(tagA, tagB)

    removeConnection.__doc__ = RCE.removeConnection.__doc__  #@UndefinedVariable