Chromium Code Reviews| 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 part of vmservice_io; | 5 part of vmservice_io; |
| 6 | 6 |
| 7 class WebSocketClient extends Client { | 7 class WebSocketClient extends Client { |
| 8 static const int PARSE_ERROR_CODE = 4000; | 8 static const int PARSE_ERROR_CODE = 4000; |
| 9 static const int BINARY_MESSAGE_ERROR_CODE = 4001; | 9 static const int BINARY_MESSAGE_ERROR_CODE = 4001; |
| 10 static const int NOT_MAP_ERROR_CODE = 4002; | 10 static const int NOT_MAP_ERROR_CODE = 4002; |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 73 dynamic toJson() { | 73 dynamic toJson() { |
| 74 Map map = super.toJson(); | 74 Map map = super.toJson(); |
| 75 map['type'] = 'HttpRequestClient'; | 75 map['type'] = 'HttpRequestClient'; |
| 76 map['request'] = '$request'; | 76 map['request'] = '$request'; |
| 77 return map; | 77 return map; |
| 78 } | 78 } |
| 79 } | 79 } |
| 80 | 80 |
| 81 class Server { | 81 class Server { |
| 82 static const WEBSOCKET_PATH = '/ws'; | 82 static const WEBSOCKET_PATH = '/ws'; |
| 83 String defaultPath = '/index.html'; | 83 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.
| |
| 84 final String ip; | |
| 85 int port; | |
| 86 | 84 |
| 87 final VMService service; | 85 final VMService _service; |
| 86 final String _ip; | |
| 87 final int _port; | |
| 88 | |
| 88 HttpServer _server; | 89 HttpServer _server; |
| 90 var _subscription; | |
| 91 bool get running => _server != null; | |
| 92 bool _displayMessages = false; | |
| 89 | 93 |
| 90 Server(this.service, this.ip, this.port); | 94 Server(this._service, this._ip, this._port) { |
| 95 _displayMessages = (_ip != '127.0.0.1' || _port != 8181); | |
| 96 } | |
| 91 | 97 |
| 92 void _requestHandler(HttpRequest request) { | 98 void _requestHandler(HttpRequest request) { |
| 93 // Allow cross origin requests. | 99 // Allow cross origin requests. |
| 94 request.response.headers.add('Access-Control-Allow-Origin', '*'); | 100 request.response.headers.add('Access-Control-Allow-Origin', '*'); |
| 95 | 101 |
| 96 final String path = | 102 final String path = |
| 97 request.uri.path == '/' ? defaultPath : request.uri.path; | 103 request.uri.path == '/' ? SLASH_PATH : request.uri.path; |
| 98 | 104 |
| 99 var resource = Resource.resources[path]; | 105 var resource = Resource.resources[path]; |
| 100 if (resource != null) { | 106 if (resource != null) { |
| 101 // Serving up a static resource (e.g. .css, .html, .png). | 107 // Serving up a static resource (e.g. .css, .html, .png). |
| 102 request.response.headers.contentType = | 108 request.response.headers.contentType = |
| 103 ContentType.parse(resource.mimeType); | 109 ContentType.parse(resource.mimeType); |
| 104 request.response.add(resource.data); | 110 request.response.add(resource.data); |
| 105 request.response.close(); | 111 request.response.close(); |
| 106 return; | 112 return; |
| 107 } | 113 } |
| 108 | 114 |
| 109 if (path == WEBSOCKET_PATH) { | 115 if (path == WEBSOCKET_PATH) { |
| 110 WebSocketTransformer.upgrade(request).then((WebSocket webSocket) { | 116 WebSocketTransformer.upgrade(request).then((WebSocket webSocket) { |
| 111 new WebSocketClient(webSocket, service); | 117 new WebSocketClient(webSocket, _service); |
| 112 }); | 118 }); |
| 113 return; | 119 return; |
| 114 } | 120 } |
| 115 | 121 |
| 116 var message = new Message.fromUri(request.uri); | 122 var message = new Message.fromUri(request.uri); |
| 117 var client = new HttpRequestClient(request, service); | 123 var client = new HttpRequestClient(request, _service); |
| 118 client.onMessage(null, message); | 124 client.onMessage(null, message); |
| 119 } | 125 } |
| 120 | 126 |
| 121 Future startServer() { | 127 Future startup() { |
| 122 return HttpServer.bind(ip, port).then((s) { | 128 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.
| |
| 123 // Only display message when port is automatically selected. | 129 // Already running. |
| 124 var display_message = (ip != '127.0.0.1' || port != 8181); | 130 return new Future.value(this); |
| 125 // Retrieve port. | 131 } |
| 126 port = s.port; | 132 |
| 133 // Startup HTTP server. | |
| 134 return HttpServer.bind(_ip, _port).then((s) { | |
| 127 _server = s; | 135 _server = s; |
| 128 _server.listen(_requestHandler); | 136 _subscription = _server.listen(_requestHandler); |
| 129 if (display_message) { | 137 if (_displayMessages) { |
| 138 var ip = _server.address.address.toString(); | |
| 139 var port = _server.port.toString(); | |
| 130 print('Observatory listening on http://$ip:$port'); | 140 print('Observatory listening on http://$ip:$port'); |
| 131 } | 141 } |
| 132 return s; | 142 // Server is up and running. |
| 143 return this; | |
| 144 }).catchError((e, st) { | |
| 145 print('Could not start Observatory HTTP server:\n$e\n$st\n'); | |
| 146 return this; | |
| 133 }); | 147 }); |
| 134 } | 148 } |
| 149 | |
| 150 Future shutdown(bool forced) { | |
| 151 if (_server == null) { | |
| 152 // Not started. | |
| 153 return new Future.value(this); | |
| 154 } | |
| 155 | |
| 156 // Force displaying of status messages if we are forcibly shutdown. | |
| 157 _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.
| |
| 158 | |
| 159 // Shutdown HTTP server and subscription. | |
| 160 var ip = _server.address.address.toString(); | |
| 161 var port = _server.port.toString(); | |
| 162 _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.
| |
| 163 _subscription = null; | |
| 164 return _server.close(force: forced).then((_) { | |
| 165 if (_displayMessages) { | |
| 166 print('Observatory no longer listening on http://$ip:$port'); | |
| 167 } | |
| 168 _server = null; | |
| 169 return this; | |
| 170 }).catchError((e, st) { | |
| 171 _server = null; | |
| 172 print('Could not shutdown Observatory HTTP server:\n$e\n$st\n'); | |
| 173 return this; | |
| 174 }); | |
| 175 } | |
| 176 | |
| 135 } | 177 } |
| OLD | NEW |