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;
/// and no stack trace unless the --verbose option is specified.
/// For example: network errors
void throwToolExit(String message, { int exitCode }) {
throw new ToolExit(message, exitCode: exitCode );
throw new ToolExit(message, exitCode: exitCode);
}
/// Specialized exception for expected situations
......@@ -34,7 +34,6 @@ void throwToolExit(String message, { int exitCode }) {
/// and no stack trace unless the --verbose option is specified.
/// For example: network errors
class ToolExit implements Exception {
ToolExit(this.message, { this.exitCode });
final String message;
......
......@@ -262,8 +262,8 @@ class IOSDevice extends Device {
}
int installationResult = -1;
Uri localObsUri;
Uri localDiagUri;
Uri localObservatoryUri;
Uri localDiagnosticUri;
if (!debuggingOptions.debuggingEnabled) {
// If debugging is not enabled, just launch the application and continue.
......@@ -281,12 +281,12 @@ class IOSDevice extends Device {
final ProtocolDiscovery diagnosticDiscovery = new ProtocolDiscovery.diagnosticService(
getLogReader(app: app), portForwarder: portForwarder, hostPort: debuggingOptions.diagnosticPort);
final Future<Uri> forwardObsUri = observatoryDiscovery.uri;
Future<Uri> forwardDiagUri;
final Future<Uri> forwardObservatoryUri = observatoryDiscovery.uri;
Future<Uri> forwardDiagnosticUri;
if (debuggingOptions.buildMode == BuildMode.debug) {
forwardDiagUri = diagnosticDiscovery.uri;
forwardDiagnosticUri = diagnosticDiscovery.uri;
} else {
forwardDiagUri = new Future<Uri>.value(null);
forwardDiagnosticUri = new Future<Uri>.value(null);
}
final Future<int> launch = runCommandAndStreamOutput(launchCommand, trace: true);
......@@ -300,14 +300,14 @@ class IOSDevice extends Device {
}
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(() {
observatoryDiscovery.cancel();
diagnosticDiscovery.cancel();
});
localObsUri = uris[0];
localDiagUri = uris[1];
localObservatoryUri = uris[0];
localDiagnosticUri = uris[1];
}
if (installationResult != 0) {
......@@ -318,7 +318,7 @@ class IOSDevice extends Device {
return new LaunchResult.failed();
}
return new LaunchResult.succeeded(observatoryUri: localObsUri, diagnosticUri: localDiagUri);
return new LaunchResult.succeeded(observatoryUri: localObservatoryUri, diagnosticUri: localDiagnosticUri);
}
@override
......
......@@ -9,71 +9,83 @@ import 'base/port_scanner.dart';
import 'device.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.
class ProtocolDiscovery {
ProtocolDiscovery._(
DeviceLogReader logReader,
String serviceName, {
this.logReader,
this.serviceName, {
this.portForwarder,
this.hostPort,
this.defaultHostPort,
}) : _logReader = logReader, _serviceName = serviceName {
assert(_logReader != null);
}) : _prefix = '$serviceName listening on ' {
assert(logReader != 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,
{DevicePortForwarder portForwarder, int hostPort}) =>
new ProtocolDiscovery._(logReader, _kObservatoryService,
portForwarder: portForwarder,
hostPort: hostPort,
defaultHostPort: kDefaultObservatoryPort);
factory ProtocolDiscovery.diagnosticService(DeviceLogReader logReader,
{DevicePortForwarder portForwarder, int hostPort}) =>
new ProtocolDiscovery._(logReader, _kDiagnosticService,
portForwarder: portForwarder,
hostPort: hostPort,
defaultHostPort: kDefaultDiagnosticPort);
static const String _kObservatoryService = 'Observatory';
static const String _kDiagnosticService = 'Diagnostic server';
final DeviceLogReader _logReader;
final String _serviceName;
factory ProtocolDiscovery.diagnosticService(
DeviceLogReader logReader, {
DevicePortForwarder portForwarder,
int hostPort,
}) {
const String kDiagnosticService = 'Diagnostic server';
return new ProtocolDiscovery._(
logReader, kDiagnosticService,
portForwarder: portForwarder,
hostPort: hostPort,
defaultHostPort: kDefaultDiagnosticPort,
);
}
final DeviceLogReader logReader;
final String serviceName;
final DevicePortForwarder portForwarder;
final int hostPort;
final int defaultHostPort;
final String _prefix;
final Completer<Uri> _completer = new Completer<Uri>();
StreamSubscription<String> _deviceLogSubscription;
Timer _timer;
/// The discovered service URI.
Future<Uri> get uri {
return _completer.future
.timeout(const Duration(seconds: 60), onTimeout: () {
throwToolExit('Timeout while attempting to retrieve Uri for $_serviceName');
}).whenComplete(() {
_stopScrapingLogs();
});
}
Future<Uri> get uri => _completer.future;
Future<Null> cancel() => _stopScrapingLogs();
Future<Null> _stopScrapingLogs() async {
_timer?.cancel();
_timer = null;
await _deviceLogSubscription?.cancel();
_deviceLogSubscription = null;
}
void _onLine(String line) {
void _handleLine(String line) {
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) {
try {
uri = Uri.parse(line.substring(index + prefix.length));
uri = Uri.parse(line.substring(index + _prefix.length));
} catch (error) {
_stopScrapingLogs();
_completer.completeError(error);
......@@ -88,7 +100,7 @@ class ProtocolDiscovery {
}
Future<Uri> _forwardPort(Uri deviceUri) async {
printTrace('$_serviceName Uri on device: $deviceUri');
printTrace('$serviceName URL on device: $deviceUri');
Uri hostUri = deviceUri;
if (portForwarder != null) {
......@@ -97,9 +109,9 @@ class ProtocolDiscovery {
hostPort = await portForwarder
.forward(devicePort, hostPort: hostPort)
.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);
}
......
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