def unbindDevice(self, message): """ Invoked when a Device wishes to unbind from the Server. All handles on the device a freed. We allow Sessions to live for now, but they will get DeviceGoneAway when they try to make another request. """ if message.system_request.HasField("device"): device = Devices.removeFromProtobuf(message.system_request.device) self.connection.device = None device.connection = None if device not in Devices: return SystemResponseFactory.unbound(device).inReplyTo(message).build() else: return ( SystemResponseFactory.error(Message.SystemResponse.UNBOUND, "error unbinding") .inReplyTo(message) .build() ) else: raise Exception("UNBIND_DEVICE request does not specify a device")
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) self.__logger.info("accepted connection from " + device.device_id) 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() )
def startSession(self, message): """ Invoked when a Console wishes to establish a Session with a bound Device. """ if message.system_request.HasField("device"): device = Devices.getFromProtobuf(message.system_request.device) if device is not None: return device.startSession(self.connection, message) else: return ( SystemResponseFactory.error(Message.SystemResponse.SESSION_ID, "unknown device") .inReplyTo(message) .build() ) else: return ( SystemResponseFactory.error(Message.SystemResponse.SESSION_ID, "no device").inReplyTo(message).build() )