Beispiel #1
0
class SystemRequestFactory:
    """
    The SystemRequestFactory provides a DSL for building SystemRequest
    messages.
    """
    
    def __init__(self, request_type):
        self.builder = Message(type=Message.SYSTEM_REQUEST)
        self.builder.system_request.type = request_type
    
    def addDevice(self, device):
        """
        Set the Device specified in the message.
        """

        self.builder.system_request.device.id = device.device_id
        self.builder.system_request.device.manufacturer = device.manufacturer
        self.builder.system_request.device.model = device.model
        self.builder.system_request.device.software = device.software
        
        return self
    
    def addDeviceId(self, device_id):
        """
        Set the Device identifier specified in the message.

        This sets all other device fields (manufacturer, model and software
        to "N/A").
        """

        self.builder.system_request.device.id = device_id
        self.builder.system_request.device.manufacturer = "N/A"
        self.builder.system_request.device.model = "N/A"
        self.builder.system_request.device.software = "N/A"
        
        return self
    
    def build(self):
        """
        Serialize the built Message to a String, using the Protocol Buffer
        format.
        """

        return self.builder.SerializeToString()

    def getId(self):
        """
        Get the Identifier assigned to the message.
        """

        return self.builder.id

    @classmethod
    def listDevices(cls):
        """
        Helper method to build a request that the server list all known
        devices.
        """

        builder = SystemRequestFactory(Message.SystemRequest.LIST_DEVICES)

        return builder

    @classmethod
    def listSessions(cls):
        """
        Helper method to build a request that the server list all established
        sessions.
        """

        builder = SystemRequestFactory(Message.SystemRequest.LIST_SESSIONS)

        return builder
        
    @classmethod
    def ping(cls):
        """
        Helper method to build a ping request.
        """

        builder = SystemRequestFactory(Message.SystemRequest.PING)
        
        return builder
    
    def setId(self, message_id):
        """
        Set the identifier of the message.
        """

        self.builder.id = message_id
        
        return self

    def setPassword(self, password):
        """
        Set the password required to establish a session.
        """
        
        if password != None:
            self.builder.system_request.password = password
        
        return self
        
    def setSessionId(self, session):
        """
        Set session identifier, to route a message correctly on the Agent.
        """

        self.builder.system_request.session_id = session

        return self

    @classmethod
    def startSession(cls, device_id):
        """
        Helper method to build a request to start a session with a device.
        """

        builder = SystemRequestFactory(Message.SystemRequest.START_SESSION)

        builder.addDeviceId(device_id)

        return builder

    @classmethod
    def stopSession(cls, session):
        """
        Helper method to build a request to stop an established session.
        """

        builder = SystemRequestFactory(Message.SystemRequest.STOP_SESSION)

        builder.setSessionId(session.session_id)

        return builder

    @classmethod
    def stopSessionId(cls, session_id):
        """
        Helper method to build a request to stop an established session, by
        identifier only.
        """

        builder = SystemRequestFactory(Message.SystemRequest.STOP_SESSION)

        builder.setSessionId(session_id)

        return builder
        
class ReflectionResponseFactory:
    """
    The ReflectionResponseFactory provides a DSL for building ReflectionResponse
    messages.
    """
    
    def __init__(self):
        self.builder = Message(type=Message.REFLECTION_RESPONSE)
        self.builder.reflection_response.status = Message.ReflectionResponse.SUCCESS
    
    def build(self):
        """
        Serialize the built Message to a String, using the Protocol Buffer
        format.
        """

        return self.builder.SerializeToString()
    
    @classmethod
    def error(cls, message):
        """
        Helper method to build an error response.
        """

        builder = ReflectionResponseFactory()
        
        builder.isError()
        builder.setErrorMessage(message)
        
        return builder

    @classmethod
    def fatal(cls, message):
        """
        Helper method to build a fatal error response.
        """

        builder = ReflectionResponseFactory()
        
        builder.isFatal()
        builder.setErrorMessage(message)
        
        return builder
    
    def inReplyTo(self, message):
        """
        Tag the response as a reply to another message, by setting the message
        identifiers to be equal and setting the session to keep the flow
        intact.
        """

        self.builder.id = message.id
        self.builder.reflection_response.session_id = message.reflection_request.session_id
        
        return self
    
    def isError(self):
        """
        Indicate an error in the response status code.
        """

        self.builder.reflection_response.status = Message.ReflectionResponse.ERROR
    
    def isFatal(self):
        """
        Indicate a fatal error in the response status code.
        """

        self.builder.reflection_response.status = Message.ReflectionResponse.FATAL
    
    def isSuccess(self):
        """
        Indicate success in the response status code.
        """

        self.builder.reflection_response.status = Message.ReflectionResponse.SUCCESS

    def setErrorMessage(self, message):
        """
        Set the error message associated with this response.
        """

        self.builder.reflection_response.errormessage = message

        return self
        
Beispiel #3
0
class SystemResponseFactory:
    """
    The SystemResponseFactory provides a DSL for building SystemResponse
    messages.
    """
    def __init__(self, response_type):
        self.builder = Message(type=Message.SYSTEM_RESPONSE)
        self.builder.system_response.type = response_type
        self.builder.system_response.status = Message.SystemResponse.SUCCESS

    def addDevice(self, device):
        """
        Add a Device to the list of devices within the message.
        """

        d = self.builder.system_response.devices.add()
        d.id = device.device_id
        d.manufacturer = device.manufacturer
        d.model = device.model
        d.software = device.software

        return d

    def add_session(self, session):
        """
        Add a Session to the list of Sessions within the message.
        """

        s = self.builder.system_response.sessions.add()
        s.id = session.session_id
        s.device_id = session.device.device_id

    @classmethod
    def bound(cls, device):
        """
        Helper method to build a message to indicate a Device has successfully
        bound to the server.
        """

        builder = SystemResponseFactory(Message.SystemResponse.BOUND)

        builder.addDevice(device)

        return builder

    def build(self):
        """
        Serialize the built Message to a String, using the Protocol Buffer
        format.
        """

        return self.builder.SerializeToString()

    @classmethod
    def error(cls, error_type, message):
        """
        Helper method to build an error message, with a particular type and an
        error message.
        """

        builder = SystemResponseFactory(error_type)

        builder.isError()
        builder.setErrorMessage(message)

        return builder

    def inReplyTo(self, message):
        """
        Tag the response as a reply to another message, by setting the message
        identifiers to be equal.
        """

        self.builder.id = message.id

        return self

    def isError(self):
        """
        Indicate an error in the response status code.
        """

        self.builder.system_response.status = Message.SystemResponse.ERROR

    def isSuccess(self):
        """
        Indicate success in the response status code.
        """

        self.builder.system_response.status = Message.SystemResponse.SUCCESS

    @classmethod
    def listDevices(cls, devices):
        """
        Helper method to build a DEVICE_LIST response, with a list of devices
        built from a collection.
        """

        builder = SystemResponseFactory(Message.SystemResponse.DEVICE_LIST)

        for device in devices:
            builder.addDevice(device)

        return builder

    @classmethod
    def listSessions(cls, sessions):
        """
        Helper method to build a SESSION_LIST response, with a list of sessions
        build from a collection.
        """

        builder = SystemResponseFactory(Message.SystemResponse.SESSION_LIST)

        #for session in sessions:
        #    builder.add_session(session)

        return builder

    def setErrorMessage(self, message):
        """
        Set the error message associated with this response.
        """

        self.builder.system_response.error_message = message

        return self

    @classmethod
    def unbound(cls, device):
        """
        Helper method to build a message to indicate a device has successfully
        unbound from the Server.
        """

        builder = SystemResponseFactory(Message.SystemResponse.UNBOUND)

        builder.addDevice(device)

        return builder
Beispiel #4
0
class ReflectionRequestFactory:
    """
    The ReflectionRequestFactory provides a DSL for building ReflectionRequest
    messages.
    """
    
    def __init__(self, request_type):
        self.builder = Message(type=Message.REFLECTION_REQUEST)
        self.builder.reflection_request.type = request_type
    
    def build(self):
        """
        Serialize the built Message to a String, using the Protocol Buffer
        format.
        """

        return self.builder.SerializeToString()
    
    @classmethod
    def construct(cls, ref):
        """
        Helper method to build a CONSTRUCT request, to build a new object
        instance.
        """

        builder = ReflectionRequestFactory(Message.ReflectionRequest.CONSTRUCT)

        builder.builder.reflection_request.construct.object.reference = ref
        
        return builder

    @classmethod
    def delete(cls, ref):
        """
        Helper method to build a DELETE request, to remove a cached object from
        the ObjectStore.
        """

        builder = ReflectionRequestFactory(Message.ReflectionRequest.DELETE)

        builder.builder.reflection_request.delete.object.reference = ref
        
        return builder

    @classmethod
    def deleteAll(cls):
        """
        Helper method to build a DELETE_ALL request, to clear the ObjectStore.
        """

        builder = ReflectionRequestFactory(Message.ReflectionRequest.DELETE_ALL)
        
        return builder

    def getId(self):
        """
        Get the Identifier assigned to the message.
        """

        return self.builder.id
    
    @classmethod
    def getProperty(cls, ref, property_name):
        """
        Helper method to build a GET_PROPERTY request, to get the value of an
        object's field.
        """

        builder = ReflectionRequestFactory(Message.ReflectionRequest.GET_PROPERTY)

        builder.builder.reflection_request.get_property.object.reference = ref
        builder.builder.reflection_request.get_property.property = property_name
        
        return builder
    
    @classmethod
    def invoke(cls, ref, method_name):
        """
        Helper method to build an INVOKE request, to call a method on an object.
        """

        builder = ReflectionRequestFactory(Message.ReflectionRequest.INVOKE)

        builder.builder.reflection_request.invoke.object.reference = ref
        builder.builder.reflection_request.invoke.method = method_name
        
        return builder

    @classmethod
    def resolve(cls, class_name):
        """
        Helper method to build a RESOLVE request, to get a reference to a class
        given its name.
        """

        builder = ReflectionRequestFactory(Message.ReflectionRequest.RESOLVE)

        builder.builder.reflection_request.resolve.classname = class_name
        
        return builder

    def setArguments(self, arguments):
        """
        Adds an array of Arguments to an INVOKE or a CONSTRUCT request.
        """

        if self.builder.reflection_request.type == Message.ReflectionRequest.INVOKE:
            request = self.builder.reflection_request.invoke
        elif self.builder.reflection_request.type == Message.ReflectionRequest.CONSTRUCT:
            request = self.builder.reflection_request.construct
        else:
            request = None

        if request is not None:
            for argument in arguments:
                request.argument.add().MergeFrom(argument._pb())

        return self
    
    def setId(self, message_id):
        """
        Set the identifier of the message.
        """

        self.builder.id = message_id
        
        return self

    @classmethod
    def setProperty(cls, ref, property_name, value):
        """
        Helper method to build a SET_PROPERTY request, to assign the value of
        an object's field.
        """

        builder = ReflectionRequestFactory(Message.ReflectionRequest.SET_PROPERTY)

        builder.builder.reflection_request.set_property.object.reference = ref
        builder.builder.reflection_request.set_property.property = property_name
        builder.builder.reflection_request.set_property.value.MergeFrom(value._pb())
        
        return builder

    def setSessionId(self, session_id):
        """
        Set session identifier, to route a message correctly on the Agent.
        """

        self.builder.reflection_request.session_id = session_id
        
        return self