| 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:convert'; | 8 import 'dart:convert'; |
| 9 import 'dart:io'; | 9 import 'dart:io'; |
| 10 import 'dart:isolate'; | 10 import 'dart:isolate'; |
| 11 import 'dart:mirrors'; | |
| 12 import 'dart:vmservice'; | 11 import 'dart:vmservice'; |
| 13 | 12 |
| 14 part 'resources.dart'; | 13 part 'resources.dart'; |
| 15 part 'server.dart'; | 14 part 'server.dart'; |
| 16 | 15 |
| 17 // The TCP ip/port that the HTTP server listens on. | 16 // The TCP ip/port that the HTTP server listens on. |
| 18 int _port; | 17 int _port; |
| 19 String _ip; | 18 String _ip; |
| 20 // Should the HTTP server auto start? | 19 // Should the HTTP server auto start? |
| 21 bool _autoStart; | 20 bool _autoStart; |
| 22 | 21 |
| 23 bool _isWindows = false; | 22 bool _isWindows = false; |
| 24 | 23 |
| 25 // HTTP servr. | 24 // HTTP servr. |
| 26 Server server; | 25 Server server; |
| 27 Future<Server> serverFuture; | 26 Future<Server> serverFuture; |
| 28 | 27 |
| 29 // The VM service instance. | 28 void _bootServer() { |
| 30 VMService service; | 29 // Load resources. |
| 30 _triggerResourceLoad(); |
| 31 // Lazily create service. |
| 32 var service = new VMService(); |
| 33 // Lazily create server. |
| 34 server = new Server(service, _ip, _port); |
| 35 } |
| 36 |
| 37 void _clearFuture(_) { |
| 38 serverFuture = null; |
| 39 } |
| 31 | 40 |
| 32 void _onSignal(ProcessSignal signal) { | 41 void _onSignal(ProcessSignal signal) { |
| 33 if (serverFuture != null) { | 42 if (serverFuture != null) { |
| 34 // Still waiting. | 43 // Still waiting. |
| 35 return; | 44 return; |
| 36 } | 45 } |
| 46 if (server == null) { |
| 47 _bootServer(); |
| 48 } |
| 37 // Toggle HTTP server. | 49 // Toggle HTTP server. |
| 38 if (server.running) { | 50 if (server.running) { |
| 39 serverFuture = server.shutdown(true).then((_) { | 51 serverFuture = server.shutdown(true).then(_clearFuture); |
| 40 serverFuture = null; | |
| 41 }); | |
| 42 } else { | 52 } else { |
| 43 serverFuture = server.startup().then((_) { | 53 serverFuture = server.startup().then(_clearFuture); |
| 44 serverFuture = null; | |
| 45 }); | |
| 46 } | 54 } |
| 47 } | 55 } |
| 48 | 56 |
| 49 void registerSignalHandler() { | 57 void _registerSignalHandler(Stream signalWatch(ProcessSignal signal)) { |
| 50 if (_isWindows) { | 58 if (_isWindows) { |
| 51 // Cannot register for signals on Windows. | 59 // Cannot register for signals on Windows. |
| 52 return; | 60 return; |
| 53 } | 61 } |
| 54 bool useSIGQUIT = true; | 62 signalWatch(ProcessSignal.SIGQUIT).listen(_onSignal); |
| 55 // Listen for SIGQUIT. | |
| 56 if (useSIGQUIT) { | |
| 57 var io = currentMirrorSystem().findLibrary(const Symbol('dart.io')); | |
| 58 var c = MirrorSystem.getSymbol('_ProcessUtils', io); | |
| 59 var m = MirrorSystem.getSymbol('_watchSignalInternal', io); | |
| 60 var processUtils = io.declarations[c]; | |
| 61 processUtils.invoke(m, [ProcessSignal.SIGQUIT]).reflectee.listen(_onSignal); | |
| 62 } else { | |
| 63 ProcessSignal.SIGUSR1.watch().listen(_onSignal); | |
| 64 } | |
| 65 } | 63 } |
| 66 | 64 |
| 67 | 65 main(Stream signalWatch(ProcessSignal signal)) { |
| 68 main() { | |
| 69 // Get VMService. | |
| 70 service = new VMService(); | |
| 71 // Start HTTP server. | |
| 72 server = new Server(service, _ip, _port); | |
| 73 if (_autoStart) { | 66 if (_autoStart) { |
| 67 _bootServer(); |
| 74 server.startup(); | 68 server.startup(); |
| 75 } | 69 } |
| 76 | 70 _registerSignalHandler(signalWatch); |
| 77 registerSignalHandler(); | |
| 78 } | 71 } |
| OLD | NEW |