Sniff service protocol port from device

This commit is contained in:
John McCutchan 2016-03-15 10:00:08 -07:00
parent 882f849ccd
commit 83e6ee36a0
5 changed files with 37 additions and 7 deletions

View file

@ -17,6 +17,7 @@ import '../build_configuration.dart';
import '../device.dart';
import '../flx.dart' as flx;
import '../globals.dart';
import '../service_protocol.dart';
import '../toolchain.dart';
import 'adb.dart';
import 'android.dart';
@ -185,12 +186,12 @@ class AndroidDevice extends Device {
return true;
}
Future<Null> _forwardObservatoryPort(int port) async {
bool portWasZero = port == 0;
Future<Null> _forwardObservatoryPort(int devicePort, int port) async {
bool portWasZero = (port == null) || (port == 0);
try {
// Set up port forwarding for observatory.
port = await portForwarder.forward(observatoryDefaultPort,
port = await portForwarder.forward(devicePort,
hostPort: port);
if (portWasZero)
printStatus('Observatory listening on http://127.0.0.1:$port');
@ -214,13 +215,18 @@ class AndroidDevice extends Device {
return false;
}
await _forwardObservatoryPort(debugPort);
if (clearLogs)
this.clearLogs();
runCheckedSync(adbCommandForDevice(<String>['push', bundlePath, _deviceBundlePath]));
ServiceProtocolDiscovery serviceProtocolDiscovery =
new ServiceProtocolDiscovery(logReader);
// We take this future here but do not wait for completion until *after*
// we start the bundle.
Future<int> serviceProtocolPort = serviceProtocolDiscovery.nextPort();
List<String> cmd = adbCommandForDevice(<String>[
'shell', 'am', 'start',
'-a', 'android.intent.action.RUN',
@ -243,6 +249,13 @@ class AndroidDevice extends Device {
printError(result.trim());
return false;
}
// Wait for the service protocol port here. This will complete once
// the device has printed "Observatory is listening on..."
int devicePort = await serviceProtocolPort;
printTrace('service protocol port = $devicePort');
await _forwardObservatoryPort(devicePort, debugPort);
return true;
}

View file

@ -199,7 +199,6 @@ class IOSDevice extends Device {
return false;
}
printTrace('Installation successful.');
return true;
}

View file

@ -16,6 +16,7 @@ import '../build_configuration.dart';
import '../device.dart';
import '../flx.dart' as flx;
import '../globals.dart';
import '../service_protocol.dart';
import '../toolchain.dart';
import 'mac.dart';
@ -462,6 +463,13 @@ class IOSSimulator extends Device {
if (!(await _setupUpdatedApplicationBundle(app, toolchain)))
return false;
ServiceProtocolDiscovery serviceProtocolDiscovery =
new ServiceProtocolDiscovery(logReader);
// We take this future here but do not wait for completion until *after*
// we start the application.
Future<int> serviceProtocolPort = serviceProtocolDiscovery.nextPort();
// Prepare launch arguments.
List<String> args = <String>[
"--flx=${path.absolute(path.join('build', 'app.flx'))}",
@ -486,7 +494,12 @@ class IOSSimulator extends Device {
return false;
}
// Wait for the service protocol port here. This will complete once
// the device has printed "Observatory is listening on..."
int devicePort = await serviceProtocolPort;
printTrace('service protocol port = $devicePort');
printTrace('Successfully started ${app.name} on $id.');
printStatus('Observatory listening on http://127.0.0.1:$devicePort');
return true;
}

View file

@ -29,7 +29,7 @@ class ServiceProtocolDiscovery {
void _onLine(String line) {
int portNumber = 0;
if (line.startsWith('Observatory listening on http://')) {
if (line.contains('Observatory listening on http://')) {
try {
RegExp portExp = new RegExp(r"\d+.\d+.\d+.\d+:(\d+)");
String port = portExp.firstMatch(line).group(1);

View file

@ -40,6 +40,11 @@ void main() {
const Duration(milliseconds: 100), onTimeout: () => 77);
// Expect the timeout port.
expect(port, 77);
// Get next port future.
nextPort = discoverer.nextPort();
logReader.addLine(
'I/flutter : Observatory listening on http://127.0.0.1:52584');
expect(await nextPort, 52584);
});
});
}