OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 part of dart.developer; |
| 6 |
| 7 /// Information about the service protocol. |
| 8 class ServiceProtocolInfo { |
| 9 /// The major version of the protocol. If the running Dart environment does |
| 10 /// not support the service protocol, this is 0. |
| 11 final int majorVersion = _getServiceMajorVersion(); |
| 12 /// The minor version of the protocol. If the running Dart environment does |
| 13 /// not support the service protocol, this is 0. |
| 14 final int minorVersion = _getServiceMinorVersion(); |
| 15 /// The Uri to access the service. If the web server is not running, this |
| 16 /// will be null. |
| 17 final Uri serverUri; |
| 18 |
| 19 ServiceProtocolInfo(this.serverUri); |
| 20 |
| 21 String toString() { |
| 22 if (serverUri != null) { |
| 23 return 'Dart VM Service Protocol v$majorVersion.$minorVersion ' |
| 24 'listening on $serverUri'; |
| 25 } else { |
| 26 return 'Dart VM Service Protocol v$majorVersion.$minorVersion'; |
| 27 } |
| 28 } |
| 29 } |
| 30 |
| 31 /// Access information about the service protocol and control the web server. |
| 32 class Service { |
| 33 /// Get information about the service protocol. |
| 34 static Future<ServiceProtocolInfo> getInfo() async { |
| 35 // Port to receive response from service isolate. |
| 36 final RawReceivePort receivePort = new RawReceivePort(); |
| 37 final Completer<Uri> uriCompleter = new Completer<Uri>(); |
| 38 receivePort.handler = (Uri uri) => uriCompleter.complete(uri); |
| 39 // Request the information from the service isolate. |
| 40 _getServerInfo(receivePort.sendPort); |
| 41 // Await the response from the service isolate. |
| 42 Uri uri = await uriCompleter.future; |
| 43 // Close the port. |
| 44 receivePort.close(); |
| 45 return new ServiceProtocolInfo(uri); |
| 46 } |
| 47 |
| 48 /// Control the web server that the service protocol is accessed through. |
| 49 static Future<ServiceProtocolInfo> controlWebServer( |
| 50 {bool enable: false}) async { |
| 51 if (enable is! bool) { |
| 52 throw new ArgumentError.value(enable, |
| 53 'enable', |
| 54 'Must be a bool'); |
| 55 } |
| 56 // Port to receive response from service isolate. |
| 57 final RawReceivePort receivePort = new RawReceivePort(); |
| 58 final Completer<Uri> uriCompleter = new Completer<Uri>(); |
| 59 receivePort.handler = (Uri uri) => uriCompleter.complete(uri); |
| 60 // Request the information from the service isolate. |
| 61 _webServerControl(receivePort.sendPort, enable); |
| 62 // Await the response from the service isolate. |
| 63 Uri uri = await uriCompleter.future; |
| 64 // Close the port. |
| 65 receivePort.close(); |
| 66 return new ServiceProtocolInfo(uri); |
| 67 } |
| 68 } |
| 69 |
| 70 /// [sp] will receive a Uri or null. |
| 71 external void _getServerInfo(SendPort sp); |
| 72 |
| 73 /// [sp] will receive a Uri or null. |
| 74 external void _webServerControl(SendPort sp, bool enable); |
| 75 |
| 76 /// Returns the major version of the service protocol. |
| 77 external int _getServiceMajorVersion(); |
| 78 |
| 79 /// Returns the minor version of the service protocol. |
| 80 external int _getServiceMinorVersion(); |
| 81 |
OLD | NEW |