From 8968bacfa18078d87ef85c360f94b0c128d859e0 Mon Sep 17 00:00:00 2001 From: Hidenori Matsubayashi Date: Wed, 28 Jul 2021 15:04:09 +0900 Subject: [PATCH] Add port-forwarding for remote devices (#27) --- lib/elinux_device.dart | 48 ++++++++++++++++++++++++------------------ 1 file changed, 27 insertions(+), 21 deletions(-) diff --git a/lib/elinux_device.dart b/lib/elinux_device.dart index 81eb266..259013c 100644 --- a/lib/elinux_device.dart +++ b/lib/elinux_device.dart @@ -53,6 +53,15 @@ class ELinuxDevice extends Device { _processUtils = ProcessUtils(processManager: processManager, logger: logger), _operatingSystemUtils = operatingSystemUtils, + portForwarder = config != null && config.usesPortForwarding + ? CustomDevicePortForwarder( + deviceName: config.label, + forwardPortCommand: config.forwardPortCommand, + forwardPortSuccessRegex: config.forwardPortSuccessRegex, + processManager: processManager, + logger: logger, + ) + : const NoOpDevicePortForwarder(), super(id, category: desktop ? Category.desktop : Category.mobile, platformType: PlatformType.custom, @@ -70,6 +79,8 @@ class ELinuxDevice extends Device { final Set _runningProcesses = {}; final ELinuxLogReader _logReader = ELinuxLogReader(); + int _forwardedHostPort; + @override Future get isLocalEmulator async => false; @@ -151,7 +162,7 @@ class ELinuxDevice extends Device { final ProtocolDiscovery discovery = ProtocolDiscovery.observatory( _logReader, - portForwarder: null, + portForwarder: _config.usesPortForwarding ? portForwarder : null, hostPort: null, devicePort: null, logger: _logger, @@ -163,6 +174,10 @@ class ELinuxDevice extends Device { final Uri observatoryUri = await discovery.uri; await discovery.cancel(); + if (_config.usesPortForwarding) { + _forwardedHostPort = observatoryUri.port; + } + return LaunchResult.succeeded(observatoryUri: observatoryUri); } @@ -230,6 +245,8 @@ class ELinuxDevice extends Device { @override Future stopApp(ELinuxApp app, {String userIdentifier}) async { + _maybeUnforwardPort(); + bool succeeded = true; // Walk a copy of _runningProcesses, since the exit handler removes from the // set. @@ -250,7 +267,7 @@ class ELinuxDevice extends Device { _logReader; @override - DevicePortForwarder get portForwarder => null; + final DevicePortForwarder portForwarder; @override bool isSupported() => true; @@ -434,26 +451,15 @@ class ELinuxDevice extends Device { } } - Future tryRunDebug( - {@required String appName, - Duration timeout, - Map additionalReplacementValues = - const {}}) async { - final List interpolated = interpolateCommand( - _config.runDebugCommand, - {'remotePath': '/tmp/', 'appName': appName}, - additionalReplacementValues: additionalReplacementValues); + /// Source: [_maybeUnforwardPort] in `custom_device.dart` + void _maybeUnforwardPort() { + if (_forwardedHostPort != null) { + final ForwardedPort forwardedPort = portForwarder.forwardedPorts.singleWhere((ForwardedPort forwardedPort) { + return forwardedPort.hostPort == _forwardedHostPort; + }); - try { - _logger.printStatus('Launch $appName on ${_config.id}'); - await _processUtils.run(interpolated, - throwOnError: true, timeout: timeout); - _logger.printStatus('Running $appName...'); - return true; - } on ProcessException catch (e) { - _logger.printError( - 'Error executing runDebug command for custom device $id: $e'); - return false; + _forwardedHostPort = null; + portForwarder.unforward(forwardedPort); } } }