OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library vmservice_io; | 5 library vmservice_io; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:collection'; | 8 import 'dart:collection'; |
9 import 'dart:convert'; | 9 import 'dart:convert'; |
10 import 'dart:io'; | 10 import 'dart:io'; |
11 import 'dart:isolate'; | 11 import 'dart:isolate'; |
12 import 'dart:_vmservice'; | 12 import 'dart:_vmservice'; |
13 | 13 |
14 part 'loader.dart'; | 14 part 'loader.dart'; |
15 part 'server.dart'; | 15 part 'server.dart'; |
16 | 16 |
17 // The TCP ip/port that the HTTP server listens on. | 17 // The TCP ip/port that the HTTP server listens on. |
18 int _port; | 18 int _port; |
19 String _ip; | 19 String _ip; |
20 // Should the HTTP server auto start? | 20 // Should the HTTP server auto start? |
21 bool _autoStart; | 21 bool _autoStart; |
22 | 22 |
23 bool _isWindows = false; | 23 bool _isWindows = false; |
24 | |
25 var _signalWatch; | 24 var _signalWatch; |
26 var _signalSubscription; | 25 var _signalSubscription; |
27 | 26 |
28 // HTTP server. | 27 // HTTP server. |
29 Server server; | 28 Server server; |
30 Future<Server> serverFuture; | 29 Future<Server> serverFuture; |
31 HashMap<String, Asset> _assets; | 30 |
32 HashMap<String, Asset> get assets { | 31 _lazyServerBoot() { |
33 if (_assets == null) { | 32 if (server != null) { |
| 33 return; |
| 34 } |
| 35 // Lazily create service. |
| 36 var service = new VMService(); |
| 37 // Lazily create server. |
| 38 server = new Server(service, _ip, _port); |
| 39 } |
| 40 |
| 41 Future cleanupCallback() async { |
| 42 // Cancel the sigquit subscription. |
| 43 if (_signalSubscription != null) { |
| 44 await _signalSubscription.cancel(); |
| 45 _signalSubscription = null; |
| 46 } |
| 47 if (server != null) { |
34 try { | 48 try { |
35 _assets = Asset.request(); | 49 await server.cleanup(true); |
36 } catch (e) { | 50 } catch (e, st) { |
37 print('Could not load Observatory assets: $e'); | 51 print("Error in vm-service shutdown: $e\n$st\n"); |
38 } | 52 } |
39 } | 53 } |
40 return _assets; | 54 // Call out to embedder's shutdown callback. |
41 } | 55 _shutdown(); |
42 | |
43 _onShutdown() { | |
44 if (server != null) { | |
45 server.close(true).catchError((e, st) { | |
46 print("Error in vm-service shutdown: $e\n$st\n"); | |
47 }); | |
48 } | |
49 if (_signalSubscription != null) { | |
50 _signalSubscription.cancel(); | |
51 _signalSubscription = null; | |
52 } | |
53 } | |
54 | |
55 _bootServer() { | |
56 // Lazily create service. | |
57 var service = new VMService(); | |
58 service.onShutdown = _onShutdown; | |
59 // Lazily create server. | |
60 server = new Server(service, _ip, _port); | |
61 } | 56 } |
62 | 57 |
63 _clearFuture(_) { | 58 _clearFuture(_) { |
64 serverFuture = null; | 59 serverFuture = null; |
65 } | 60 } |
66 | 61 |
67 _onSignal(ProcessSignal signal) { | 62 _onSignal(ProcessSignal signal) { |
68 if (serverFuture != null) { | 63 if (serverFuture != null) { |
69 // Still waiting. | 64 // Still waiting. |
70 return; | 65 return; |
71 } | 66 } |
72 if (server == null) { | 67 _lazyServerBoot(); |
73 _bootServer(); | |
74 } | |
75 // Toggle HTTP server. | 68 // Toggle HTTP server. |
76 if (server.running) { | 69 if (server.running) { |
77 serverFuture = server.shutdown(true).then(_clearFuture); | 70 serverFuture = server.shutdown(true).then(_clearFuture); |
78 } else { | 71 } else { |
79 serverFuture = server.startup().then(_clearFuture); | 72 serverFuture = server.startup().then(_clearFuture); |
80 } | 73 } |
81 } | 74 } |
82 | 75 |
83 _registerSignalHandler() { | 76 _registerSignalHandler() { |
| 77 if (_signalWatch == null) { |
| 78 // Cannot register for signals. |
| 79 return; |
| 80 } |
84 if (_isWindows) { | 81 if (_isWindows) { |
85 // Cannot register for signals on Windows. | 82 // Cannot register for signals on Windows. |
86 return; | 83 return; |
87 } | 84 } |
88 _signalSubscription = _signalWatch(ProcessSignal.SIGQUIT).listen(_onSignal); | 85 _signalSubscription = _signalWatch(ProcessSignal.SIGQUIT).listen(_onSignal); |
89 } | 86 } |
90 | 87 |
91 const _shortDelay = const Duration(milliseconds: 10); | |
92 | |
93 main() { | 88 main() { |
| 89 // Set embedder hooks. |
| 90 VMServiceEmbedderHooks.cleanup = cleanupCallback; |
94 if (_autoStart) { | 91 if (_autoStart) { |
95 _bootServer(); | 92 _lazyServerBoot(); |
96 server.startup(); | 93 server.startup(); |
97 // It's just here to push an event on the event loop so that we invoke the | 94 // It's just here to push an event on the event loop so that we invoke the |
98 // scheduled microtasks. | 95 // scheduled microtasks. |
99 Timer.run(() {}); | 96 Timer.run(() {}); |
100 } | 97 } |
101 // TODO(johnmccutchan, turnidge) Creating a VMService object here causes | 98 // TODO(johnmccutchan): Fixup service isolate shutdown in the general case. |
102 // strange behavior from the legacy debug protocol and coverage tool. | 99 // See ServiceIsolate::KillServiceIsolate and ServiceIsolate::Shutdown. |
103 // Enable this code, and remove the call to Isolate::KillIsolate() from | |
104 // service_isolate.cc when the strange behavior is solved. | |
105 // See: https://github.com/dart-lang/sdk/issues/23977 | |
106 // else { | |
107 // var service = new VMService(); | |
108 // service.onShutdown = _onShutdown; | |
109 // } | |
110 scriptLoadPort.handler = _processLoadRequest; | 100 scriptLoadPort.handler = _processLoadRequest; |
111 // Register signal handler after a small delay to avoid stalling main | 101 // Register signal handler after a small delay to avoid stalling main |
112 // isolate startup. | 102 // isolate startup. |
113 new Timer(_shortDelay, _registerSignalHandler); | 103 new Timer(shortDelay, _registerSignalHandler); |
114 return scriptLoadPort; | 104 return scriptLoadPort; |
115 } | 105 } |
| 106 |
| 107 _shutdown() native "VMServiceIO_Shutdown"; |
OLD | NEW |