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'; |
| 84 int port; | |
| 85 | 84 |
| 86 final VMService service; | 85 final VMService _service; |
| 86 final int _port; | |
| 87 bool _displayStatusMessages; | |
| 88 | |
| 87 HttpServer _server; | 89 HttpServer _server; |
| 90 var _subscription; | |
| 88 | 91 |
| 89 Server(this.service, this.port); | 92 bool get running => _server != null; |
| 93 | |
| 94 Server(this._service, int port) : _port = port, | |
| 95 _displayStatusMessages = (port == 0); | |
| 90 | 96 |
| 91 void _requestHandler(HttpRequest request) { | 97 void _requestHandler(HttpRequest request) { |
| 92 // Allow cross origin requests. | 98 // Allow cross origin requests. |
| 93 request.response.headers.add('Access-Control-Allow-Origin', '*'); | 99 request.response.headers.add('Access-Control-Allow-Origin', '*'); |
| 94 | 100 |
| 95 final String path = | 101 final String path = |
| 96 request.uri.path == '/' ? defaultPath : request.uri.path; | 102 request.uri.path == '/' ? SLASH_PATH : request.uri.path; |
| 97 | 103 |
| 98 var resource = Resource.resources[path]; | 104 var resource = Resource.resources[path]; |
| 99 if (resource != null) { | 105 if (resource != null) { |
| 100 // Serving up a static resource (e.g. .css, .html, .png). | 106 // Serving up a static resource (e.g. .css, .html, .png). |
| 101 request.response.headers.contentType = | 107 request.response.headers.contentType = |
| 102 ContentType.parse(resource.mimeType); | 108 ContentType.parse(resource.mimeType); |
| 103 request.response.add(resource.data); | 109 request.response.add(resource.data); |
| 104 request.response.close(); | 110 request.response.close(); |
| 105 return; | 111 return; |
| 106 } | 112 } |
| 107 | 113 |
| 108 if (path == WEBSOCKET_PATH) { | 114 if (path == WEBSOCKET_PATH) { |
| 109 WebSocketTransformer.upgrade(request).then((WebSocket webSocket) { | 115 WebSocketTransformer.upgrade(request).then((WebSocket webSocket) { |
| 110 new WebSocketClient(webSocket, service); | 116 new WebSocketClient(webSocket, _service); |
| 111 }); | 117 }); |
| 112 return; | 118 return; |
| 113 } | 119 } |
| 114 | 120 |
| 115 var message = new Message.fromUri(request.uri); | 121 var message = new Message.fromUri(request.uri); |
| 116 var client = new HttpRequestClient(request, service); | 122 var client = new HttpRequestClient(request, _service); |
| 117 client.onMessage(null, message); | 123 client.onMessage(null, message); |
| 118 } | 124 } |
| 119 | 125 |
| 120 Future startServer() { | 126 Future startup() { |
| 121 return HttpServer.bind(InternetAddress.LOOPBACK_IP_V4, port).then((s) { | 127 return HttpServer.bind(InternetAddress.LOOPBACK_IP_V4, _port).then((s) { |
| 122 // Only display message when port is automatically selected. | |
| 123 var display_message = (port == 0); | |
| 124 // Retrieve port. | |
| 125 port = s.port; | |
| 126 _server = s; | 128 _server = s; |
| 127 _server.listen(_requestHandler); | 129 _subscription = _server.listen(_requestHandler); |
| 128 if (display_message) { | 130 if (_displayStatusMessages) { |
| 129 print('VMService listening on port $port'); | 131 print('VMService listening on port ${_server.port}'); |
| 130 } | 132 } |
| 131 return s; | 133 return s; |
| 132 }); | 134 }); |
| 133 } | 135 } |
| 136 | |
| 137 void shutdown() { | |
| 138 // Force displaying of status messages if we are forcibly shutdown. | |
| 139 _displayStatusMessages = true; | |
|
Ivan Posva
2014/04/14 16:12:58
Shouldn't the fact whether you are forced to shutd
Cutch
2014/04/15 17:42:07
Done.
| |
| 140 // Shutdown HTTP server and subscription. | |
| 141 var closedPort = _server.port; | |
| 142 _server.close(force: true); | |
| 143 _server = null; | |
| 144 _subscription.cancel(); | |
| 145 _subscription = null; | |
| 146 if (_displayStatusMessages) { | |
| 147 print('VMService is no longer listening on port $closedPort'); | |
| 148 } | |
| 149 } | |
| 134 } | 150 } |
| OLD | NEW |