コード例 #1
0
    def bindDevice(self, message):
        """
        Invoked when a Device wishes to bind to the Server.

        The Device is added to the collection of available Devices, and a
        request to refresh the list of active sessions is queued for later.
        """

        if message.system_request.HasField('device'):
            device = Devices.addFromProtobuf(message.system_request.device)
            
            if device in Devices:
                self.connection.device = device
                device.connection = self.connection

                def enumerateSessions():
                    """
                    We pass this function to the reactor to call later and grab
                    a list of active Sessions from the Agent.

                    We define this here to capture the outer scope temporarily.
                    """

                    device.enumerateSessions()

                reactor.callLater(1.0, enumerateSessions)
                
                return SystemResponseFactory.bound(device)\
                    .inReplyTo(message)\
                    .build()
            else:
                return SystemResponseFactory\
                    .error(Message.SystemResponse.BOUND, "error binding")\
                    .inReplyTo(message)\
                    .build()
        else:
            return SystemResponseFactory\
                .error(Message.SystemResponse.BOUND, "no device specified")\
                .inReplyTo(message)\
                .build()