def restore_app_connection(self, port=None): """Restores the app after device got reconnected. Instead of creating new instance of the client: - Uses the given port (or find a new available host_port if none is given). - Tries to connect to remote server with selected port. Args: port: If given, this is the host port from which to connect to remote device port. If not provided, find a new available port as host port. Raises: AppRestoreConnectionError: When the app was not able to be started. """ self.host_port = port or utils.get_available_host_port() self._adb.forward( ['tcp:%d' % self.host_port, 'tcp:%d' % self.device_port]) try: self.connect() except: # Failed to connect to app, something went wrong. raise jsonrpc_client_base.AppRestoreConnectionError( self._ad( 'Failed to restore app connection for %s at host port %s, ' 'device port %s'), self.package, self.host_port, self.device_port) # Because the previous connection was lost, update self._proc self._proc = None self._restore_event_client()
def _retry_connect(self): self._adb.forward( ['tcp:%d' % self.host_port, 'tcp:%d' % self.device_port]) start_time = time.time() expiration_time = start_time + _APP_START_WAIT_TIME started = False while time.time() < expiration_time: self.log.debug('Attempting to start %s.', self.app_name) try: self.connect() started = True break except: self.log.debug( '%s is not yet running, retrying', self.app_name, exc_info=True) time.sleep(1) if not started: raise jsonrpc_client_base.AppRestoreConnectionError( '%s failed to connect for %s at host port %s, ' 'device port %s' % (self.app_name, self._adb.serial, self.host_port, self.device_port))