| Index: runtime/bin/vmservice/server.dart
|
| diff --git a/runtime/bin/vmservice/server.dart b/runtime/bin/vmservice/server.dart
|
| index 4d2587a2defca6c58c4736daee0fcd06853eb562..d50727fc03ab7241ea717070774b32d1f680430b 100644
|
| --- a/runtime/bin/vmservice/server.dart
|
| +++ b/runtime/bin/vmservice/server.dart
|
| @@ -80,14 +80,19 @@ class HttpRequestClient extends Client {
|
|
|
| class Server {
|
| static const WEBSOCKET_PATH = '/ws';
|
| - String observatoryPath = '/index.html';
|
| - final String ip;
|
| - int port;
|
| + static const ROOT_REDIRECT_PATH = '/index.html';
|
| +
|
| + final VMService _service;
|
| + final String _ip;
|
| + final int _port;
|
|
|
| - final VMService service;
|
| HttpServer _server;
|
| + 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);
|
| + }
|
|
|
| bool _shouldServeObservatory(HttpRequest request) {
|
| if (request.headers['Observatory-Version'] != null) {
|
| @@ -117,18 +122,18 @@ class Server {
|
| }
|
|
|
| final String path =
|
| - request.uri.path == '/' ? observatoryPath : request.uri.path;
|
| + request.uri.path == '/' ? ROOT_REDIRECT_PATH : request.uri.path;
|
|
|
| if (path == WEBSOCKET_PATH) {
|
| WebSocketTransformer.upgrade(request).then((WebSocket webSocket) {
|
| - new WebSocketClient(webSocket, service);
|
| + new WebSocketClient(webSocket, _service);
|
| });
|
| return;
|
| }
|
|
|
| var resource = Resource.resources[path];
|
| if (resource == null && _shouldServeObservatory(request)) {
|
| - resource = Resource.resources[observatoryPath];
|
| + resource = Resource.resources[ROOT_REDIRECT_PATH];
|
| assert(resource != null);
|
| }
|
| if (resource != null) {
|
| @@ -140,22 +145,56 @@ class Server {
|
| 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) {
|
| + // 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) {
|
| + 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;
|
| +
|
| + // Shutdown HTTP server and subscription.
|
| + var ip = _server.address.address.toString();
|
| + var port = _server.port.toString();
|
| + 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;
|
| });
|
| }
|
| +
|
| }
|
|
|