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 /// Service protocol is the protocol that a client like the observatory | |
8 /// could use to access the services provided by the Dart VM for | |
9 /// debugging and inspecting Dart programs. This class encapsulates the | |
10 /// version number and Uri for accessing this service. | |
11 class ServiceProtocolInfo { | |
12 /// The major version of the protocol. If the running Dart environment does | |
13 /// not support the service protocol, this is 0. | |
14 final int majorVersion = _getServiceMajorVersion(); | |
15 /// The minor version of the protocol. If the running Dart environment does | |
16 /// not support the service protocol, this is 0. | |
17 final int minorVersion = _getServiceMinorVersion(); | |
18 /// The Uri to access the service. If the web server is not running, this | |
19 /// will be null. | |
20 final Uri serverUri; | |
21 | |
22 ServiceProtocolInfo(this.serverUri); | |
23 | |
24 String toString() { | |
25 if (serverUri != null) { | |
26 return 'Dart VM Service Protocol v$majorVersion.$minorVersion ' | |
27 'listening on $serverUri'; | |
28 } else { | |
29 return 'Dart VM Service Protocol v$majorVersion.$minorVersion'; | |
30 } | |
31 } | |
32 } | |
33 | |
34 /// Access information about the service protocol and control the web server | |
35 /// that provides access to the services provided by the Dart VM for | |
36 /// debugging and inspecting Dart programs. | |
37 class Service { | |
38 /// Get information about the service protocol (version number and | |
39 /// Uri to access the service). | |
40 static Future<ServiceProtocolInfo> getInfo() async { | |
41 // Port to receive response from service isolate. | |
42 final RawReceivePort receivePort = new RawReceivePort(); | |
43 final Completer<Uri> uriCompleter = new Completer<Uri>(); | |
44 receivePort.handler = (Uri uri) => uriCompleter.complete(uri); | |
45 // Request the information from the service isolate. | |
46 _getServerInfo(receivePort.sendPort); | |
47 // Await the response from the service isolate. | |
48 Uri uri = await uriCompleter.future; | |
49 // Close the port. | |
50 receivePort.close(); | |
51 return new ServiceProtocolInfo(uri); | |
52 } | |
53 | |
54 /// Control the web server that the service protocol is accessed through. | |
55 /// The [enable] argument must be a boolean and is used as a toggle to | |
56 /// enable(true) or disable(false) the web server servicing requests. | |
57 static Future<ServiceProtocolInfo> controlWebServer( | |
58 {bool enable: false}) async { | |
59 if (enable is! bool) { | |
60 throw new ArgumentError.value(enable, | |
61 'enable', | |
62 'Must be a bool'); | |
63 } | |
64 // Port to receive response from service isolate. | |
65 final RawReceivePort receivePort = new RawReceivePort(); | |
66 final Completer<Uri> uriCompleter = new Completer<Uri>(); | |
67 receivePort.handler = (Uri uri) => uriCompleter.complete(uri); | |
68 // Request the information from the service isolate. | |
69 _webServerControl(receivePort.sendPort, enable); | |
70 // Await the response from the service isolate. | |
71 Uri uri = await uriCompleter.future; | |
72 // Close the port. | |
73 receivePort.close(); | |
74 return new ServiceProtocolInfo(uri); | |
75 } | |
76 } | |
77 | |
78 /// [sp] will receive a Uri or null. | |
79 external void _getServerInfo(SendPort sp); | |
80 | |
81 /// [sp] will receive a Uri or null. | |
82 external void _webServerControl(SendPort sp, bool enable); | |
83 | |
84 /// Returns the major version of the service protocol. | |
85 external int _getServiceMajorVersion(); | |
86 | |
87 /// Returns the minor version of the service protocol. | |
88 external int _getServiceMinorVersion(); | |
89 | |
OLD | NEW |