| 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'; |
| (...skipping 21 matching lines...) Expand all Loading... |
| 32 if (server != null) { | 32 if (server != null) { |
| 33 return; | 33 return; |
| 34 } | 34 } |
| 35 // Lazily create service. | 35 // Lazily create service. |
| 36 var service = new VMService(); | 36 var service = new VMService(); |
| 37 // Lazily create server. | 37 // Lazily create server. |
| 38 server = new Server(service, _ip, _port); | 38 server = new Server(service, _ip, _port); |
| 39 } | 39 } |
| 40 | 40 |
| 41 Future cleanupCallback() async { | 41 Future cleanupCallback() async { |
| 42 shutdownLoaders(); |
| 42 // Cancel the sigquit subscription. | 43 // Cancel the sigquit subscription. |
| 43 if (_signalSubscription != null) { | 44 if (_signalSubscription != null) { |
| 44 await _signalSubscription.cancel(); | 45 await _signalSubscription.cancel(); |
| 45 _signalSubscription = null; | 46 _signalSubscription = null; |
| 46 } | 47 } |
| 47 if (server != null) { | 48 if (server != null) { |
| 48 try { | 49 try { |
| 49 await server.cleanup(true); | 50 await server.cleanup(true); |
| 50 } catch (e, st) { | 51 } catch (e, st) { |
| 51 print("Error in vm-service shutdown: $e\n$st\n"); | 52 print("Error in vm-service shutdown: $e\n$st\n"); |
| 52 } | 53 } |
| 53 } | 54 } |
| 55 if (_registerSignalHandlerTimer != null) { |
| 56 _registerSignalHandlerTimer.cancel(); |
| 57 _registerSignalHandlerTimer = null; |
| 58 } |
| 54 // Call out to embedder's shutdown callback. | 59 // Call out to embedder's shutdown callback. |
| 55 _shutdown(); | 60 _shutdown(); |
| 56 } | 61 } |
| 57 | 62 |
| 58 _clearFuture(_) { | 63 _clearFuture(_) { |
| 59 serverFuture = null; | 64 serverFuture = null; |
| 60 } | 65 } |
| 61 | 66 |
| 62 _onSignal(ProcessSignal signal) { | 67 _onSignal(ProcessSignal signal) { |
| 63 if (serverFuture != null) { | 68 if (serverFuture != null) { |
| 64 // Still waiting. | 69 // Still waiting. |
| 65 return; | 70 return; |
| 66 } | 71 } |
| 67 _lazyServerBoot(); | 72 _lazyServerBoot(); |
| 68 // Toggle HTTP server. | 73 // Toggle HTTP server. |
| 69 if (server.running) { | 74 if (server.running) { |
| 70 serverFuture = server.shutdown(true).then(_clearFuture); | 75 serverFuture = server.shutdown(true).then(_clearFuture); |
| 71 } else { | 76 } else { |
| 72 serverFuture = server.startup().then(_clearFuture); | 77 serverFuture = server.startup().then(_clearFuture); |
| 73 } | 78 } |
| 74 } | 79 } |
| 75 | 80 |
| 81 Timer _registerSignalHandlerTimer; |
| 82 |
| 76 _registerSignalHandler() { | 83 _registerSignalHandler() { |
| 84 _registerSignalHandlerTimer = null; |
| 77 if (_signalWatch == null) { | 85 if (_signalWatch == null) { |
| 78 // Cannot register for signals. | 86 // Cannot register for signals. |
| 79 return; | 87 return; |
| 80 } | 88 } |
| 81 if (_isWindows) { | 89 if (_isWindows) { |
| 82 // Cannot register for signals on Windows. | 90 // Cannot register for signals on Windows. |
| 83 return; | 91 return; |
| 84 } | 92 } |
| 85 _signalSubscription = _signalWatch(ProcessSignal.SIGQUIT).listen(_onSignal); | 93 _signalSubscription = _signalWatch(ProcessSignal.SIGQUIT).listen(_onSignal); |
| 86 } | 94 } |
| 87 | 95 |
| 88 main() { | 96 main() { |
| 89 // Set embedder hooks. | 97 // Set embedder hooks. |
| 90 VMServiceEmbedderHooks.cleanup = cleanupCallback; | 98 VMServiceEmbedderHooks.cleanup = cleanupCallback; |
| 99 // Always instantiate the vmservice object so that the exit message |
| 100 // can be delivered and waiting loaders can be cancelled. |
| 101 var service = new VMService(); |
| 91 if (_autoStart) { | 102 if (_autoStart) { |
| 92 _lazyServerBoot(); | 103 _lazyServerBoot(); |
| 93 server.startup(); | 104 server.startup(); |
| 94 // It's just here to push an event on the event loop so that we invoke the | 105 // It's just here to push an event on the event loop so that we invoke the |
| 95 // scheduled microtasks. | 106 // scheduled microtasks. |
| 96 Timer.run(() {}); | 107 Timer.run(() {}); |
| 97 } | 108 } |
| 98 // TODO(johnmccutchan): Fixup service isolate shutdown in the general case. | |
| 99 // See ServiceIsolate::KillServiceIsolate and ServiceIsolate::Shutdown. | |
| 100 scriptLoadPort.handler = _processLoadRequest; | 109 scriptLoadPort.handler = _processLoadRequest; |
| 101 // Register signal handler after a small delay to avoid stalling main | 110 // Register signal handler after a small delay to avoid stalling main |
| 102 // isolate startup. | 111 // isolate startup. |
| 103 new Timer(shortDelay, _registerSignalHandler); | 112 _registerSignalHandlerTimer = new Timer(shortDelay, _registerSignalHandler); |
| 104 return scriptLoadPort; | 113 return scriptLoadPort; |
| 105 } | 114 } |
| 106 | 115 |
| 107 _shutdown() native "VMServiceIO_Shutdown"; | 116 _shutdown() native "VMServiceIO_Shutdown"; |
| OLD | NEW |