Chromium Code Reviews| Index: runtime/bin/vmservice/server.dart |
| diff --git a/runtime/bin/vmservice/server.dart b/runtime/bin/vmservice/server.dart |
| index 051ea43ff64eed6d97a83ea3b0b961a65516dae4..8fc5a8773a227cdda9fe55289f743b5bfc65b7f8 100644 |
| --- a/runtime/bin/vmservice/server.dart |
| +++ b/runtime/bin/vmservice/server.dart |
| @@ -80,21 +80,27 @@ class HttpRequestClient extends Client { |
| class Server { |
| static const WEBSOCKET_PATH = '/ws'; |
| - String defaultPath = '/index.html'; |
| - final String ip; |
| - int port; |
| + static const SLASH_PATH = '/index.html'; |
|
Anders Johnsen
2014/05/23 06:06:36
Maybe ROOT_REDIRECT_PATH is more descriptive.
Cutch
2014/06/16 22:21:12
Done.
|
| + |
| + final VMService _service; |
| + final String _ip; |
| + final int _port; |
| - final VMService service; |
| HttpServer _server; |
| + var _subscription; |
| + bool get running => _server != null; |
| + bool _displayMessages = false; |
| - Server(this.service, this.ip, this.port); |
| + Server(this._service, this._ip, this._port) { |
| + _displayMessages = (_ip != '127.0.0.1' || _port != 8181); |
| + } |
| void _requestHandler(HttpRequest request) { |
| // Allow cross origin requests. |
| request.response.headers.add('Access-Control-Allow-Origin', '*'); |
| final String path = |
| - request.uri.path == '/' ? defaultPath : request.uri.path; |
| + request.uri.path == '/' ? SLASH_PATH : request.uri.path; |
| var resource = Resource.resources[path]; |
| if (resource != null) { |
| @@ -108,28 +114,64 @@ class Server { |
| if (path == WEBSOCKET_PATH) { |
| WebSocketTransformer.upgrade(request).then((WebSocket webSocket) { |
| - new WebSocketClient(webSocket, service); |
| + new WebSocketClient(webSocket, _service); |
| }); |
| return; |
| } |
| var message = new Message.fromUri(request.uri); |
| - var client = new HttpRequestClient(request, service); |
| + var client = new HttpRequestClient(request, _service); |
| client.onMessage(null, message); |
| } |
| - Future startServer() { |
| - return HttpServer.bind(ip, port).then((s) { |
| - // Only display message when port is automatically selected. |
| - var display_message = (ip != '127.0.0.1' || port != 8181); |
| - // Retrieve port. |
| - port = s.port; |
| + Future startup() { |
| + if (_server != null) { |
|
Anders Johnsen
2014/05/23 06:06:36
_server is not non-null until the bind is complete
Cutch
2014/06/16 22:21:12
This is guarded against in the SIGQUIT handler.
|
| + // Already running. |
| + return new Future.value(this); |
| + } |
| + |
| + // Startup HTTP server. |
| + return HttpServer.bind(_ip, _port).then((s) { |
| _server = s; |
| - _server.listen(_requestHandler); |
| - if (display_message) { |
| + _subscription = _server.listen(_requestHandler); |
| + if (_displayMessages) { |
| + var ip = _server.address.address.toString(); |
| + var port = _server.port.toString(); |
| print('Observatory listening on http://$ip:$port'); |
| } |
| - return s; |
| + // Server is up and running. |
| + return this; |
| + }).catchError((e, st) { |
| + print('Could not start Observatory HTTP server:\n$e\n$st\n'); |
| + return this; |
| }); |
| } |
| + |
| + Future shutdown(bool forced) { |
| + if (_server == null) { |
| + // Not started. |
| + return new Future.value(this); |
| + } |
| + |
| + // Force displaying of status messages if we are forcibly shutdown. |
| + _displayMessages = _displayMessages || forced; |
|
Anders Johnsen
2014/05/23 06:06:36
Do we really want to override the _displayMessages
Cutch
2014/06/16 22:21:12
We need to track the state across multiple calls.
|
| + |
| + // Shutdown HTTP server and subscription. |
| + var ip = _server.address.address.toString(); |
| + var port = _server.port.toString(); |
| + _subscription.cancel(); |
|
Anders Johnsen
2014/05/23 06:06:36
No need to cancel subscription when closing the se
Cutch
2014/06/16 22:21:12
Done.
|
| + _subscription = null; |
| + return _server.close(force: forced).then((_) { |
| + if (_displayMessages) { |
| + print('Observatory no longer listening on http://$ip:$port'); |
| + } |
| + _server = null; |
| + return this; |
| + }).catchError((e, st) { |
| + _server = null; |
| + print('Could not shutdown Observatory HTTP server:\n$e\n$st\n'); |
| + return this; |
| + }); |
| + } |
| + |
| } |