-
Notifications
You must be signed in to change notification settings - Fork 27.7k
/
protocol_discovery.dart
105 lines (89 loc) · 2.94 KB
/
protocol_discovery.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:async';
import 'base/common.dart';
import 'base/io.dart';
import 'base/port_scanner.dart';
import 'device.dart';
import 'globals.dart';
/// Discovers a specific service protocol on a device, and forwards the service
/// protocol device port to the host.
class ProtocolDiscovery {
ProtocolDiscovery._(
this.logReader,
this.serviceName, {
this.portForwarder,
this.hostPort,
this.defaultHostPort,
this.ipv6,
}) : assert(logReader != null),
assert(portForwarder == null || defaultHostPort != null),
_prefix = '$serviceName listening on ' {
_deviceLogSubscription = logReader.logLines.listen(_handleLine);
}
factory ProtocolDiscovery.observatory(
DeviceLogReader logReader, {
DevicePortForwarder portForwarder,
int hostPort,
bool ipv6: false,
}) {
const String kObservatoryService = 'Observatory';
return new ProtocolDiscovery._(
logReader, kObservatoryService,
portForwarder: portForwarder,
hostPort: hostPort,
defaultHostPort: kDefaultObservatoryPort,
ipv6: ipv6,
);
}
final DeviceLogReader logReader;
final String serviceName;
final DevicePortForwarder portForwarder;
final int hostPort;
final int defaultHostPort;
final bool ipv6;
final String _prefix;
final Completer<Uri> _completer = new Completer<Uri>();
StreamSubscription<String> _deviceLogSubscription;
/// The discovered service URI.
Future<Uri> get uri => _completer.future;
Future<Null> cancel() => _stopScrapingLogs();
Future<Null> _stopScrapingLogs() async {
await _deviceLogSubscription?.cancel();
_deviceLogSubscription = null;
}
void _handleLine(String line) {
Uri uri;
final int index = line.indexOf(_prefix + 'http://');
if (index >= 0) {
try {
uri = Uri.parse(line.substring(index + _prefix.length));
} catch (error) {
_stopScrapingLogs();
_completer.completeError(error);
}
}
if (uri != null) {
assert(!_completer.isCompleted);
_stopScrapingLogs();
_completer.complete(_forwardPort(uri));
}
}
Future<Uri> _forwardPort(Uri deviceUri) async {
printTrace('$serviceName URL on device: $deviceUri');
Uri hostUri = deviceUri;
if (portForwarder != null) {
final int devicePort = deviceUri.port;
int hostPort = this.hostPort ?? await portScanner.findPreferredPort(defaultHostPort);
hostPort = await portForwarder.forward(devicePort, hostPort: hostPort);
printTrace('Forwarded host port $hostPort to device port $devicePort for $serviceName');
hostUri = deviceUri.replace(port: hostPort);
}
assert(new InternetAddress(hostUri.host).isLoopback);
if (ipv6) {
hostUri = hostUri.replace(host: InternetAddress.LOOPBACK_IP_V6.host);
}
return hostUri;
}
}