protocol_discovery.dart 2.7 KB
Newer Older
1 2 3 4 5 6
// 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';

7
import 'base/io.dart';
8
import 'device.dart';
9
import 'globals.dart';
10

11
/// Discovers a specific service protocol on a device, and forwards the service
12
/// protocol device port to the host.
13
class ProtocolDiscovery {
14
  ProtocolDiscovery._(
15 16
    this.logReader,
    this.serviceName, {
17 18
    this.portForwarder,
    this.hostPort,
19
    this.ipv6,
20
  }) : assert(logReader != null) {
21 22 23 24 25 26 27
    _deviceLogSubscription = logReader.logLines.listen(_handleLine);
  }

  factory ProtocolDiscovery.observatory(
    DeviceLogReader logReader, {
    DevicePortForwarder portForwarder,
    int hostPort,
28
    bool ipv6 = false,
29 30
  }) {
    const String kObservatoryService = 'Observatory';
31
    return ProtocolDiscovery._(
32 33
      logReader,
      kObservatoryService,
34 35
      portForwarder: portForwarder,
      hostPort: hostPort,
36
      ipv6: ipv6,
37
    );
38 39
  }

40 41
  final DeviceLogReader logReader;
  final String serviceName;
42
  final DevicePortForwarder portForwarder;
43
  final int hostPort;
44
  final bool ipv6;
45

46
  final Completer<Uri> _completer = Completer<Uri>();
Devon Carew's avatar
Devon Carew committed
47

48
  StreamSubscription<String> _deviceLogSubscription;
49

50
  /// The discovered service URI.
51
  Future<Uri> get uri => _completer.future;
52

53
  Future<void> cancel() => _stopScrapingLogs();
54

55
  Future<void> _stopScrapingLogs() async {
56 57
    await _deviceLogSubscription?.cancel();
    _deviceLogSubscription = null;
Devon Carew's avatar
Devon Carew committed
58 59
  }

60
  void _handleLine(String line) {
61
    Uri uri;
62
    final RegExp r = RegExp('${RegExp.escape(serviceName)} listening on ((http|\/\/)[a-zA-Z0-9:/=_\\-\.\\[\\]]+)');
63 64 65
    final Match match = r.firstMatch(line);

    if (match != null) {
66
      try {
67
        uri = Uri.parse(match[1]);
68
      } catch (error, stackTrace) {
69
        _stopScrapingLogs();
70
        _completer.completeError(error, stackTrace);
71 72
      }
    }
73 74 75 76 77 78

    if (uri != null) {
      assert(!_completer.isCompleted);
      _stopScrapingLogs();
      _completer.complete(_forwardPort(uri));
    }
79

80 81
  }

82
  Future<Uri> _forwardPort(Uri deviceUri) async {
83
    printTrace('$serviceName URL on device: $deviceUri');
84 85 86
    Uri hostUri = deviceUri;

    if (portForwarder != null) {
87 88 89 90
      final int actualDevicePort = deviceUri.port;
      final int actualHostPort = await portForwarder.forward(actualDevicePort, hostPort: hostPort);
      printTrace('Forwarded host port $actualHostPort to device port $actualDevicePort for $serviceName');
      hostUri = deviceUri.replace(port: actualHostPort);
91
    }
Devon Carew's avatar
Devon Carew committed
92

93
    assert(InternetAddress(hostUri.host).isLoopback);
94
    if (ipv6) {
95
      hostUri = hostUri.replace(host: InternetAddress.loopbackIPv6.host);
96 97
    }

98
    return hostUri;
99 100
  }
}