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