John McCutchan | 9cb7001 | 2016-03-04 15:01:26 -0800 | [diff] [blame] | 1 | // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | import 'dart:async'; |
| 6 | |
Jonah Williams | e3cb2c3 | 2019-11-13 16:02:46 -0800 | [diff] [blame] | 7 | import 'package:meta/meta.dart'; |
| 8 | |
Todd Volkert | e792c6b | 2017-11-21 20:12:21 -0800 | [diff] [blame] | 9 | import 'base/io.dart'; |
John McCutchan | 9cb7001 | 2016-03-04 15:01:26 -0800 | [diff] [blame] | 10 | import 'device.dart'; |
Dan Rubel | 93e662a | 2016-12-06 09:19:12 -0800 | [diff] [blame] | 11 | import 'globals.dart'; |
John McCutchan | 9cb7001 | 2016-03-04 15:01:26 -0800 | [diff] [blame] | 12 | |
Ian Hickson | 73dcca6 | 2017-05-16 11:38:07 -0700 | [diff] [blame] | 13 | /// Discovers a specific service protocol on a device, and forwards the service |
Todd Volkert | 10decc7 | 2017-05-16 08:25:51 -0700 | [diff] [blame] | 14 | /// protocol device port to the host. |
Devon Carew | 40c0d6e | 2016-05-12 18:15:23 -0700 | [diff] [blame] | 15 | class ProtocolDiscovery { |
Todd Volkert | 10decc7 | 2017-05-16 08:25:51 -0700 | [diff] [blame] | 16 | ProtocolDiscovery._( |
Ian Hickson | 73dcca6 | 2017-05-16 11:38:07 -0700 | [diff] [blame] | 17 | this.logReader, |
| 18 | this.serviceName, { |
Todd Volkert | 10decc7 | 2017-05-16 08:25:51 -0700 | [diff] [blame] | 19 | this.portForwarder, |
Emmanuel Garcia | 6d77996 | 2019-11-19 15:11:41 -0800 | [diff] [blame] | 20 | this.throttleDuration, |
Todd Volkert | 10decc7 | 2017-05-16 08:25:51 -0700 | [diff] [blame] | 21 | this.hostPort, |
Jonah Williams | e3cb2c3 | 2019-11-13 16:02:46 -0800 | [diff] [blame] | 22 | this.devicePort, |
Todd Volkert | e792c6b | 2017-11-21 20:12:21 -0800 | [diff] [blame] | 23 | this.ipv6, |
Emmanuel Garcia | 6d77996 | 2019-11-19 15:11:41 -0800 | [diff] [blame] | 24 | }) : assert(logReader != null) |
| 25 | { |
| 26 | _deviceLogSubscription = logReader.logLines.listen( |
| 27 | _handleLine, |
| 28 | onDone: _stopScrapingLogs, |
| 29 | ); |
| 30 | _uriStreamController = StreamController<Uri>.broadcast(); |
Devon Carew | b0dca79 | 2016-04-27 14:43:42 -0700 | [diff] [blame] | 31 | } |
| 32 | |
Ian Hickson | 73dcca6 | 2017-05-16 11:38:07 -0700 | [diff] [blame] | 33 | factory ProtocolDiscovery.observatory( |
| 34 | DeviceLogReader logReader, { |
| 35 | DevicePortForwarder portForwarder, |
Emmanuel Garcia | 6d77996 | 2019-11-19 15:11:41 -0800 | [diff] [blame] | 36 | Duration throttleDuration = const Duration(milliseconds: 200), |
Jonah Williams | e3cb2c3 | 2019-11-13 16:02:46 -0800 | [diff] [blame] | 37 | @required int hostPort, |
| 38 | @required int devicePort, |
| 39 | @required bool ipv6, |
Ian Hickson | 73dcca6 | 2017-05-16 11:38:07 -0700 | [diff] [blame] | 40 | }) { |
| 41 | const String kObservatoryService = 'Observatory'; |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 42 | return ProtocolDiscovery._( |
Matteo Crippa | 8c02b8f | 2018-08-04 21:06:56 +0200 | [diff] [blame] | 43 | logReader, |
| 44 | kObservatoryService, |
Ian Hickson | 73dcca6 | 2017-05-16 11:38:07 -0700 | [diff] [blame] | 45 | portForwarder: portForwarder, |
Emmanuel Garcia | 6d77996 | 2019-11-19 15:11:41 -0800 | [diff] [blame] | 46 | throttleDuration: throttleDuration, |
Ian Hickson | 73dcca6 | 2017-05-16 11:38:07 -0700 | [diff] [blame] | 47 | hostPort: hostPort, |
Jonah Williams | e3cb2c3 | 2019-11-13 16:02:46 -0800 | [diff] [blame] | 48 | devicePort: devicePort, |
Todd Volkert | e792c6b | 2017-11-21 20:12:21 -0800 | [diff] [blame] | 49 | ipv6: ipv6, |
Ian Hickson | 73dcca6 | 2017-05-16 11:38:07 -0700 | [diff] [blame] | 50 | ); |
| 51 | } |
Dan Rubel | 93e662a | 2016-12-06 09:19:12 -0800 | [diff] [blame] | 52 | |
Ian Hickson | 73dcca6 | 2017-05-16 11:38:07 -0700 | [diff] [blame] | 53 | final DeviceLogReader logReader; |
| 54 | final String serviceName; |
Dan Rubel | 93e662a | 2016-12-06 09:19:12 -0800 | [diff] [blame] | 55 | final DevicePortForwarder portForwarder; |
Todd Volkert | 10decc7 | 2017-05-16 08:25:51 -0700 | [diff] [blame] | 56 | final int hostPort; |
Jonah Williams | e3cb2c3 | 2019-11-13 16:02:46 -0800 | [diff] [blame] | 57 | final int devicePort; |
Todd Volkert | e792c6b | 2017-11-21 20:12:21 -0800 | [diff] [blame] | 58 | final bool ipv6; |
Ian Hickson | 73dcca6 | 2017-05-16 11:38:07 -0700 | [diff] [blame] | 59 | |
Emmanuel Garcia | 6d77996 | 2019-11-19 15:11:41 -0800 | [diff] [blame] | 60 | /// The time to wait before forwarding a new observatory URIs from [logReader]. |
| 61 | final Duration throttleDuration; |
Devon Carew | b0dca79 | 2016-04-27 14:43:42 -0700 | [diff] [blame] | 62 | |
Todd Volkert | 10decc7 | 2017-05-16 08:25:51 -0700 | [diff] [blame] | 63 | StreamSubscription<String> _deviceLogSubscription; |
Emmanuel Garcia | 6d77996 | 2019-11-19 15:11:41 -0800 | [diff] [blame] | 64 | StreamController<Uri> _uriStreamController; |
John McCutchan | 9cb7001 | 2016-03-04 15:01:26 -0800 | [diff] [blame] | 65 | |
Todd Volkert | 10decc7 | 2017-05-16 08:25:51 -0700 | [diff] [blame] | 66 | /// The discovered service URI. |
Emmanuel Garcia | 6d77996 | 2019-11-19 15:11:41 -0800 | [diff] [blame] | 67 | /// Use [uris] instead. |
| 68 | // TODO(egarciad): replace `uri` for `uris`. |
| 69 | Future<Uri> get uri { |
| 70 | return uris.first; |
| 71 | } |
| 72 | |
| 73 | /// The discovered service URIs. |
Christopher Fujino | ed482c3 | 2019-10-09 16:30:27 -0700 | [diff] [blame] | 74 | /// |
Emmanuel Garcia | 6d77996 | 2019-11-19 15:11:41 -0800 | [diff] [blame] | 75 | /// When a new observatory URI is available in [logReader], |
| 76 | /// the URIs are forwarded at most once every [throttleDuration]. |
| 77 | /// |
| 78 | /// Port forwarding is only attempted when this is invoked, |
| 79 | /// for each observatory URI in the stream. |
| 80 | Stream<Uri> get uris { |
| 81 | return _uriStreamController.stream |
| 82 | .transform(_throttle<Uri>( |
| 83 | waitDuration: throttleDuration, |
| 84 | )) |
| 85 | .asyncMap<Uri>(_forwardPort); |
Christopher Fujino | ed482c3 | 2019-10-09 16:30:27 -0700 | [diff] [blame] | 86 | } |
John McCutchan | 9cb7001 | 2016-03-04 15:01:26 -0800 | [diff] [blame] | 87 | |
Alexandre Ardhuin | 2d3ff10 | 2018-10-05 07:54:56 +0200 | [diff] [blame] | 88 | Future<void> cancel() => _stopScrapingLogs(); |
Todd Volkert | 10decc7 | 2017-05-16 08:25:51 -0700 | [diff] [blame] | 89 | |
Alexandre Ardhuin | 2d3ff10 | 2018-10-05 07:54:56 +0200 | [diff] [blame] | 90 | Future<void> _stopScrapingLogs() async { |
Emmanuel Garcia | 6d77996 | 2019-11-19 15:11:41 -0800 | [diff] [blame] | 91 | await _uriStreamController?.close(); |
Todd Volkert | 10decc7 | 2017-05-16 08:25:51 -0700 | [diff] [blame] | 92 | await _deviceLogSubscription?.cancel(); |
| 93 | _deviceLogSubscription = null; |
Devon Carew | b0dca79 | 2016-04-27 14:43:42 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Emmanuel Garcia | 6d77996 | 2019-11-19 15:11:41 -0800 | [diff] [blame] | 96 | Match _getPatternMatch(String line) { |
| 97 | final RegExp r = RegExp('${RegExp.escape(serviceName)} listening on ((http|\/\/)[a-zA-Z0-9:/=_\\-\.\\[\\]]+)'); |
| 98 | return r.firstMatch(line); |
| 99 | } |
| 100 | |
| 101 | Uri _getObservatoryUri(String line) { |
| 102 | final Match match = _getPatternMatch(line); |
| 103 | if (match != null) { |
| 104 | return Uri.parse(match[1]); |
| 105 | } |
| 106 | return null; |
| 107 | } |
| 108 | |
Ian Hickson | 73dcca6 | 2017-05-16 11:38:07 -0700 | [diff] [blame] | 109 | void _handleLine(String line) { |
Dan Rubel | a9584e1 | 2016-11-30 20:29:04 -0500 | [diff] [blame] | 110 | Uri uri; |
Emmanuel Garcia | 6d77996 | 2019-11-19 15:11:41 -0800 | [diff] [blame] | 111 | try { |
| 112 | uri = _getObservatoryUri(line); |
| 113 | } on FormatException catch(error, stackTrace) { |
| 114 | _uriStreamController.addError(error, stackTrace); |
John McCutchan | 9cb7001 | 2016-03-04 15:01:26 -0800 | [diff] [blame] | 115 | } |
Jonah Williams | e3cb2c3 | 2019-11-13 16:02:46 -0800 | [diff] [blame] | 116 | if (uri == null) { |
| 117 | return; |
Jonah Williams | 985da83 | 2019-11-13 14:35:07 -0800 | [diff] [blame] | 118 | } |
Emmanuel Garcia | 6d77996 | 2019-11-19 15:11:41 -0800 | [diff] [blame] | 119 | if (devicePort != null && uri.port != devicePort) { |
Jonah Williams | e3cb2c3 | 2019-11-13 16:02:46 -0800 | [diff] [blame] | 120 | printTrace('skipping potential observatory $uri due to device port mismatch'); |
| 121 | return; |
| 122 | } |
Emmanuel Garcia | 6d77996 | 2019-11-19 15:11:41 -0800 | [diff] [blame] | 123 | _uriStreamController.add(uri); |
John McCutchan | 9cb7001 | 2016-03-04 15:01:26 -0800 | [diff] [blame] | 124 | } |
| 125 | |
Todd Volkert | 10decc7 | 2017-05-16 08:25:51 -0700 | [diff] [blame] | 126 | Future<Uri> _forwardPort(Uri deviceUri) async { |
Ian Hickson | 73dcca6 | 2017-05-16 11:38:07 -0700 | [diff] [blame] | 127 | printTrace('$serviceName URL on device: $deviceUri'); |
Todd Volkert | 10decc7 | 2017-05-16 08:25:51 -0700 | [diff] [blame] | 128 | Uri hostUri = deviceUri; |
Devon Carew | b0dca79 | 2016-04-27 14:43:42 -0700 | [diff] [blame] | 129 | |
Todd Volkert | 10decc7 | 2017-05-16 08:25:51 -0700 | [diff] [blame] | 130 | if (portForwarder != null) { |
Ian Hickson | 35ad2a7 | 2018-06-27 16:44:28 -0700 | [diff] [blame] | 131 | final int actualDevicePort = deviceUri.port; |
| 132 | final int actualHostPort = await portForwarder.forward(actualDevicePort, hostPort: hostPort); |
| 133 | printTrace('Forwarded host port $actualHostPort to device port $actualDevicePort for $serviceName'); |
| 134 | hostUri = deviceUri.replace(port: actualHostPort); |
Todd Volkert | 10decc7 | 2017-05-16 08:25:51 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 137 | assert(InternetAddress(hostUri.host).isLoopback); |
Todd Volkert | e792c6b | 2017-11-21 20:12:21 -0800 | [diff] [blame] | 138 | if (ipv6) { |
Ian Hickson | 35ad2a7 | 2018-06-27 16:44:28 -0700 | [diff] [blame] | 139 | hostUri = hostUri.replace(host: InternetAddress.loopbackIPv6.host); |
Todd Volkert | e792c6b | 2017-11-21 20:12:21 -0800 | [diff] [blame] | 140 | } |
Todd Volkert | 10decc7 | 2017-05-16 08:25:51 -0700 | [diff] [blame] | 141 | return hostUri; |
John McCutchan | 9cb7001 | 2016-03-04 15:01:26 -0800 | [diff] [blame] | 142 | } |
| 143 | } |
Emmanuel Garcia | 6d77996 | 2019-11-19 15:11:41 -0800 | [diff] [blame] | 144 | |
| 145 | /// This transformer will produce an event at most once every [waitDuration]. |
| 146 | /// |
| 147 | /// For example, consider a `waitDuration` of `10ms`, and list of event names |
| 148 | /// and arrival times: `a (0ms), b (5ms), c (11ms), d (21ms)`. |
| 149 | /// The events `c` and `d` will be produced as a result. |
| 150 | StreamTransformer<S, S> _throttle<S>({ |
| 151 | @required Duration waitDuration, |
| 152 | }) { |
| 153 | assert(waitDuration != null); |
| 154 | |
| 155 | S latestLine; |
| 156 | int lastExecution; |
| 157 | Future<void> throttleFuture; |
| 158 | |
| 159 | return StreamTransformer<S, S> |
| 160 | .fromHandlers( |
| 161 | handleData: (S value, EventSink<S> sink) { |
| 162 | latestLine = value; |
| 163 | |
| 164 | final int currentTime = DateTime.now().millisecondsSinceEpoch; |
| 165 | lastExecution ??= currentTime; |
| 166 | final int remainingTime = currentTime - lastExecution; |
| 167 | final int nextExecutionTime = remainingTime > waitDuration.inMilliseconds |
| 168 | ? 0 |
| 169 | : waitDuration.inMilliseconds - remainingTime; |
| 170 | |
| 171 | throttleFuture ??= Future<void> |
| 172 | .delayed(Duration(milliseconds: nextExecutionTime)) |
| 173 | .whenComplete(() { |
| 174 | sink.add(latestLine); |
| 175 | throttleFuture = null; |
| 176 | lastExecution = DateTime.now().millisecondsSinceEpoch; |
| 177 | }); |
| 178 | } |
| 179 | ); |
| 180 | } |