def claim(self, session_id): """Claim device Tries to claim MPM device and provides a human readable session_id. The caller must remember this token, and call reclaim() on regular intervals in order not to lose the claim. Will return a token on success, or raise an Exception on failure. """ self._state.lock.acquire() if self._state.claim_status.value: self.log.warning("Someone tried to claim this device again") self._last_error = "Someone tried to claim this device again" self._state.lock.release() raise RuntimeError("Double-claim") self.log.debug("Claiming from: %s, Session ID: %s", self.client_host, session_id) self._state.claim_token.value = bytes( ''.join(choice(ascii_letters + digits) for _ in range(TOKEN_LEN)), 'ascii') self._state.claim_status.value = True self.periph_manager.claimed = True self.periph_manager.claim() self._state.lock.release() self.session_id = session_id + " ({})".format(self.client_host) self._reset_timer() self.log.debug("giving token: %s to host: %s", self._state.claim_token.value, self.client_host) if self.client_host in net.get_local_ip_addrs(): self.periph_manager.set_connection_type("local") else: self.periph_manager.set_connection_type("remote") return self._state.claim_token.value
def get_device_info(self): """ get device information This is as safe method which can be called without a claim on the device """ info = self.periph_manager.get_device_info() if self.client_host in net.get_local_ip_addrs(): info["connection"] = "local" else: info["connection"] = "remote" return info
def get_device_info(self): """ get device information This is as safe method which can be called without a claim on the device """ info = self.periph_manager.get_device_info() info["mpm_version"] = "{}.{}".format(*MPM_COMPAT_NUM) if self.client_host in net.get_local_ip_addrs(): info["connection"] = "local" else: info["connection"] = "remote" return info
def claim(self, session_id): """Claim device Tries to claim MPM device and provides a human readable session_id. The caller must remember this token, and call reclaim() on regular intervals in order not to lose the claim. Will return a token on success, or raise an Exception on failure. """ self._state.lock.acquire() if self._state.claim_status.value: error_msg = \ "Someone tried to claim this device again (From: {})".format( self.client_host) self.log.warning(error_msg) self._last_error = error_msg self._state.lock.release() raise RuntimeError("Double-claim") self.log.debug( "Claiming from: %s, Session ID: %s", self.client_host, session_id ) self._state.claim_token.value = bytes(''.join( choice(ascii_letters + digits) for _ in range(TOKEN_LEN) ), 'ascii') self._state.claim_status.value = True self.periph_manager.claimed = True self.periph_manager.claim() if self.periph_manager.clear_rpc_method_registry_on_unclaim: self._init_rpc_calls(self.periph_manager) self._state.lock.release() self.session_id = session_id + " ({})".format(self.client_host) self._reset_timer() self.log.debug( "giving token: %s to host: %s", self._state.claim_token.value, self.client_host ) if self.client_host in net.get_local_ip_addrs(): self.periph_manager.set_connection_type("local") else: self.periph_manager.set_connection_type("remote") return self._state.claim_token.value
def _is_connection_local(client_hostname): return client_hostname in net.get_local_ip_addrs()