Add port-forwarding for remote devices (#27)

This commit is contained in:
Hidenori Matsubayashi
2021-07-28 15:04:09 +09:00
committed by GitHub
parent 09f57d9082
commit 8968bacfa1
+27 -21
View File
@@ -53,6 +53,15 @@ class ELinuxDevice extends Device {
_processUtils = _processUtils =
ProcessUtils(processManager: processManager, logger: logger), ProcessUtils(processManager: processManager, logger: logger),
_operatingSystemUtils = operatingSystemUtils, _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, super(id,
category: desktop ? Category.desktop : Category.mobile, category: desktop ? Category.desktop : Category.mobile,
platformType: PlatformType.custom, platformType: PlatformType.custom,
@@ -70,6 +79,8 @@ class ELinuxDevice extends Device {
final Set<Process> _runningProcesses = <Process>{}; final Set<Process> _runningProcesses = <Process>{};
final ELinuxLogReader _logReader = ELinuxLogReader(); final ELinuxLogReader _logReader = ELinuxLogReader();
int _forwardedHostPort;
@override @override
Future<bool> get isLocalEmulator async => false; Future<bool> get isLocalEmulator async => false;
@@ -151,7 +162,7 @@ class ELinuxDevice extends Device {
final ProtocolDiscovery discovery = ProtocolDiscovery.observatory( final ProtocolDiscovery discovery = ProtocolDiscovery.observatory(
_logReader, _logReader,
portForwarder: null, portForwarder: _config.usesPortForwarding ? portForwarder : null,
hostPort: null, hostPort: null,
devicePort: null, devicePort: null,
logger: _logger, logger: _logger,
@@ -163,6 +174,10 @@ class ELinuxDevice extends Device {
final Uri observatoryUri = await discovery.uri; final Uri observatoryUri = await discovery.uri;
await discovery.cancel(); await discovery.cancel();
if (_config.usesPortForwarding) {
_forwardedHostPort = observatoryUri.port;
}
return LaunchResult.succeeded(observatoryUri: observatoryUri); return LaunchResult.succeeded(observatoryUri: observatoryUri);
} }
@@ -230,6 +245,8 @@ class ELinuxDevice extends Device {
@override @override
Future<bool> stopApp(ELinuxApp app, {String userIdentifier}) async { Future<bool> stopApp(ELinuxApp app, {String userIdentifier}) async {
_maybeUnforwardPort();
bool succeeded = true; bool succeeded = true;
// Walk a copy of _runningProcesses, since the exit handler removes from the // Walk a copy of _runningProcesses, since the exit handler removes from the
// set. // set.
@@ -250,7 +267,7 @@ class ELinuxDevice extends Device {
_logReader; _logReader;
@override @override
DevicePortForwarder get portForwarder => null; final DevicePortForwarder portForwarder;
@override @override
bool isSupported() => true; bool isSupported() => true;
@@ -434,26 +451,15 @@ class ELinuxDevice extends Device {
} }
} }
Future<bool> tryRunDebug( /// Source: [_maybeUnforwardPort] in `custom_device.dart`
{@required String appName, void _maybeUnforwardPort() {
Duration timeout, if (_forwardedHostPort != null) {
Map<String, String> additionalReplacementValues = final ForwardedPort forwardedPort = portForwarder.forwardedPorts.singleWhere((ForwardedPort forwardedPort) {
const <String, String>{}}) async { return forwardedPort.hostPort == _forwardedHostPort;
final List<String> interpolated = interpolateCommand( });
_config.runDebugCommand,
<String, String>{'remotePath': '/tmp/', 'appName': appName},
additionalReplacementValues: additionalReplacementValues);
try { _forwardedHostPort = null;
_logger.printStatus('Launch $appName on ${_config.id}'); portForwarder.unforward(forwardedPort);
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;
} }
} }
} }