Commit 73dcca65 authored by Ian Hickson's avatar Ian Hickson Committed by GitHub

Nits for protocol discovery (#10112)

Rather than complain about these in a code review I figured I'd just fix them. :-)
parent 7eef73dd
...@@ -26,7 +26,7 @@ String _homeDirPath; ...@@ -26,7 +26,7 @@ String _homeDirPath;
/// and no stack trace unless the --verbose option is specified. /// and no stack trace unless the --verbose option is specified.
/// For example: network errors /// For example: network errors
void throwToolExit(String message, { int exitCode }) { void throwToolExit(String message, { int exitCode }) {
throw new ToolExit(message, exitCode: exitCode ); throw new ToolExit(message, exitCode: exitCode);
} }
/// Specialized exception for expected situations /// Specialized exception for expected situations
...@@ -34,7 +34,6 @@ void throwToolExit(String message, { int exitCode }) { ...@@ -34,7 +34,6 @@ void throwToolExit(String message, { int exitCode }) {
/// and no stack trace unless the --verbose option is specified. /// and no stack trace unless the --verbose option is specified.
/// For example: network errors /// For example: network errors
class ToolExit implements Exception { class ToolExit implements Exception {
ToolExit(this.message, { this.exitCode }); ToolExit(this.message, { this.exitCode });
final String message; final String message;
......
...@@ -262,8 +262,8 @@ class IOSDevice extends Device { ...@@ -262,8 +262,8 @@ class IOSDevice extends Device {
} }
int installationResult = -1; int installationResult = -1;
Uri localObsUri; Uri localObservatoryUri;
Uri localDiagUri; Uri localDiagnosticUri;
if (!debuggingOptions.debuggingEnabled) { if (!debuggingOptions.debuggingEnabled) {
// If debugging is not enabled, just launch the application and continue. // If debugging is not enabled, just launch the application and continue.
...@@ -281,12 +281,12 @@ class IOSDevice extends Device { ...@@ -281,12 +281,12 @@ class IOSDevice extends Device {
final ProtocolDiscovery diagnosticDiscovery = new ProtocolDiscovery.diagnosticService( final ProtocolDiscovery diagnosticDiscovery = new ProtocolDiscovery.diagnosticService(
getLogReader(app: app), portForwarder: portForwarder, hostPort: debuggingOptions.diagnosticPort); getLogReader(app: app), portForwarder: portForwarder, hostPort: debuggingOptions.diagnosticPort);
final Future<Uri> forwardObsUri = observatoryDiscovery.uri; final Future<Uri> forwardObservatoryUri = observatoryDiscovery.uri;
Future<Uri> forwardDiagUri; Future<Uri> forwardDiagnosticUri;
if (debuggingOptions.buildMode == BuildMode.debug) { if (debuggingOptions.buildMode == BuildMode.debug) {
forwardDiagUri = diagnosticDiscovery.uri; forwardDiagnosticUri = diagnosticDiscovery.uri;
} else { } else {
forwardDiagUri = new Future<Uri>.value(null); forwardDiagnosticUri = new Future<Uri>.value(null);
} }
final Future<int> launch = runCommandAndStreamOutput(launchCommand, trace: true); final Future<int> launch = runCommandAndStreamOutput(launchCommand, trace: true);
...@@ -300,14 +300,14 @@ class IOSDevice extends Device { ...@@ -300,14 +300,14 @@ class IOSDevice extends Device {
} }
printTrace('Application launched on the device. Attempting to forward ports.'); printTrace('Application launched on the device. Attempting to forward ports.');
return await Future.wait(<Future<Uri>>[forwardObsUri, forwardDiagUri]); return await Future.wait(<Future<Uri>>[forwardObservatoryUri, forwardDiagnosticUri]);
}).whenComplete(() { }).whenComplete(() {
observatoryDiscovery.cancel(); observatoryDiscovery.cancel();
diagnosticDiscovery.cancel(); diagnosticDiscovery.cancel();
}); });
localObsUri = uris[0]; localObservatoryUri = uris[0];
localDiagUri = uris[1]; localDiagnosticUri = uris[1];
} }
if (installationResult != 0) { if (installationResult != 0) {
...@@ -318,7 +318,7 @@ class IOSDevice extends Device { ...@@ -318,7 +318,7 @@ class IOSDevice extends Device {
return new LaunchResult.failed(); return new LaunchResult.failed();
} }
return new LaunchResult.succeeded(observatoryUri: localObsUri, diagnosticUri: localDiagUri); return new LaunchResult.succeeded(observatoryUri: localObservatoryUri, diagnosticUri: localDiagnosticUri);
} }
@override @override
......
...@@ -9,71 +9,83 @@ import 'base/port_scanner.dart'; ...@@ -9,71 +9,83 @@ import 'base/port_scanner.dart';
import 'device.dart'; import 'device.dart';
import 'globals.dart'; import 'globals.dart';
/// Discovers a specific service protocol on a device, and forward the service /// Discovers a specific service protocol on a device, and forwards the service
/// protocol device port to the host. /// protocol device port to the host.
class ProtocolDiscovery { class ProtocolDiscovery {
ProtocolDiscovery._( ProtocolDiscovery._(
DeviceLogReader logReader, this.logReader,
String serviceName, { this.serviceName, {
this.portForwarder, this.portForwarder,
this.hostPort, this.hostPort,
this.defaultHostPort, this.defaultHostPort,
}) : _logReader = logReader, _serviceName = serviceName { }) : _prefix = '$serviceName listening on ' {
assert(_logReader != null); assert(logReader != null);
assert(portForwarder == null || defaultHostPort != null); assert(portForwarder == null || defaultHostPort != null);
_deviceLogSubscription = _logReader.logLines.listen(_onLine); _deviceLogSubscription = logReader.logLines.listen(_handleLine);
_timer = new Timer(const Duration(seconds: 60), () {
_stopScrapingLogs();
_completer.completeError(new ToolExit('Timeout while attempting to retrieve URL for $serviceName'));
});
}
factory ProtocolDiscovery.observatory(
DeviceLogReader logReader, {
DevicePortForwarder portForwarder,
int hostPort,
}) {
const String kObservatoryService = 'Observatory';
return new ProtocolDiscovery._(
logReader, kObservatoryService,
portForwarder: portForwarder,
hostPort: hostPort,
defaultHostPort: kDefaultObservatoryPort,
);
} }
factory ProtocolDiscovery.observatory(DeviceLogReader logReader, factory ProtocolDiscovery.diagnosticService(
{DevicePortForwarder portForwarder, int hostPort}) => DeviceLogReader logReader, {
new ProtocolDiscovery._(logReader, _kObservatoryService, DevicePortForwarder portForwarder,
portForwarder: portForwarder, int hostPort,
hostPort: hostPort, }) {
defaultHostPort: kDefaultObservatoryPort); const String kDiagnosticService = 'Diagnostic server';
return new ProtocolDiscovery._(
factory ProtocolDiscovery.diagnosticService(DeviceLogReader logReader, logReader, kDiagnosticService,
{DevicePortForwarder portForwarder, int hostPort}) => portForwarder: portForwarder,
new ProtocolDiscovery._(logReader, _kDiagnosticService, hostPort: hostPort,
portForwarder: portForwarder, defaultHostPort: kDefaultDiagnosticPort,
hostPort: hostPort, );
defaultHostPort: kDefaultDiagnosticPort); }
static const String _kObservatoryService = 'Observatory'; final DeviceLogReader logReader;
static const String _kDiagnosticService = 'Diagnostic server'; final String serviceName;
final DeviceLogReader _logReader;
final String _serviceName;
final DevicePortForwarder portForwarder; final DevicePortForwarder portForwarder;
final int hostPort; final int hostPort;
final int defaultHostPort; final int defaultHostPort;
final String _prefix;
final Completer<Uri> _completer = new Completer<Uri>(); final Completer<Uri> _completer = new Completer<Uri>();
StreamSubscription<String> _deviceLogSubscription; StreamSubscription<String> _deviceLogSubscription;
Timer _timer;
/// The discovered service URI. /// The discovered service URI.
Future<Uri> get uri { Future<Uri> get uri => _completer.future;
return _completer.future
.timeout(const Duration(seconds: 60), onTimeout: () {
throwToolExit('Timeout while attempting to retrieve Uri for $_serviceName');
}).whenComplete(() {
_stopScrapingLogs();
});
}
Future<Null> cancel() => _stopScrapingLogs(); Future<Null> cancel() => _stopScrapingLogs();
Future<Null> _stopScrapingLogs() async { Future<Null> _stopScrapingLogs() async {
_timer?.cancel();
_timer = null;
await _deviceLogSubscription?.cancel(); await _deviceLogSubscription?.cancel();
_deviceLogSubscription = null; _deviceLogSubscription = null;
} }
void _onLine(String line) { void _handleLine(String line) {
Uri uri; Uri uri;
final String prefix = '$_serviceName listening on '; final int index = line.indexOf(_prefix + 'http://');
final int index = line.indexOf(prefix + 'http://');
if (index >= 0) { if (index >= 0) {
try { try {
uri = Uri.parse(line.substring(index + prefix.length)); uri = Uri.parse(line.substring(index + _prefix.length));
} catch (error) { } catch (error) {
_stopScrapingLogs(); _stopScrapingLogs();
_completer.completeError(error); _completer.completeError(error);
...@@ -88,7 +100,7 @@ class ProtocolDiscovery { ...@@ -88,7 +100,7 @@ class ProtocolDiscovery {
} }
Future<Uri> _forwardPort(Uri deviceUri) async { Future<Uri> _forwardPort(Uri deviceUri) async {
printTrace('$_serviceName Uri on device: $deviceUri'); printTrace('$serviceName URL on device: $deviceUri');
Uri hostUri = deviceUri; Uri hostUri = deviceUri;
if (portForwarder != null) { if (portForwarder != null) {
...@@ -97,9 +109,9 @@ class ProtocolDiscovery { ...@@ -97,9 +109,9 @@ class ProtocolDiscovery {
hostPort = await portForwarder hostPort = await portForwarder
.forward(devicePort, hostPort: hostPort) .forward(devicePort, hostPort: hostPort)
.timeout(const Duration(seconds: 60), onTimeout: () { .timeout(const Duration(seconds: 60), onTimeout: () {
throwToolExit('Timeout while atempting to foward device port $devicePort for $_serviceName'); throwToolExit('Timeout while atempting to foward device port $devicePort for $serviceName');
}); });
printTrace('Forwarded host port $hostPort to device port $devicePort for $_serviceName'); printTrace('Forwarded host port $hostPort to device port $devicePort for $serviceName');
hostUri = deviceUri.replace(port: hostPort); hostUri = deviceUri.replace(port: hostPort);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment