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 final bool silentObservatory = |
| 8 const bool.fromEnvironment('SILENT_OBSERVATORY'); |
| 9 |
| 10 void serverPrint(String s) { |
| 11 if (silentObservatory) { |
| 12 // We've been requested to be silent. |
| 13 return; |
| 14 } |
| 15 print(s); |
| 16 } |
| 17 |
7 class WebSocketClient extends Client { | 18 class WebSocketClient extends Client { |
8 static const int PARSE_ERROR_CODE = 4000; | 19 static const int PARSE_ERROR_CODE = 4000; |
9 static const int BINARY_MESSAGE_ERROR_CODE = 4001; | 20 static const int BINARY_MESSAGE_ERROR_CODE = 4001; |
10 static const int NOT_MAP_ERROR_CODE = 4002; | 21 static const int NOT_MAP_ERROR_CODE = 4002; |
11 static const int ID_ERROR_CODE = 4003; | 22 static const int ID_ERROR_CODE = 4003; |
12 final WebSocket socket; | 23 final WebSocket socket; |
13 | 24 |
14 WebSocketClient(this.socket, VMService service) : super(service) { | 25 WebSocketClient(this.socket, VMService service) : super(service) { |
15 socket.listen((message) => onWebSocketMessage(message)); | 26 socket.listen((message) => onWebSocketMessage(message)); |
16 socket.done.then((_) => close()); | 27 socket.done.then((_) => close()); |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
53 try { | 64 try { |
54 if (result is String || result is Uint8List) { | 65 if (result is String || result is Uint8List) { |
55 socket.add(result); // String or binary message. | 66 socket.add(result); // String or binary message. |
56 } else { | 67 } else { |
57 // String message as external Uint8List. | 68 // String message as external Uint8List. |
58 assert(result is List); | 69 assert(result is List); |
59 Uint8List cstring = result[0]; | 70 Uint8List cstring = result[0]; |
60 socket.addUtf8Text(cstring); | 71 socket.addUtf8Text(cstring); |
61 } | 72 } |
62 } catch (e, st) { | 73 } catch (e, st) { |
63 print("Ignoring error posting over WebSocket."); | 74 serverPrint("Ignoring error posting over WebSocket."); |
64 print(e); | 75 serverPrint(e); |
65 print(st); | 76 serverPrint(st); |
66 } | 77 } |
67 } | 78 } |
68 | 79 |
69 dynamic toJson() { | 80 dynamic toJson() { |
70 Map map = super.toJson(); | 81 Map map = super.toJson(); |
71 map['type'] = 'WebSocketClient'; | 82 map['type'] = 'WebSocketClient'; |
72 map['socket'] = '$socket'; | 83 map['socket'] = '$socket'; |
73 return map; | 84 return map; |
74 } | 85 } |
75 } | 86 } |
(...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
285 request.response.add(asset.data); | 296 request.response.add(asset.data); |
286 request.response.close(); | 297 request.response.close(); |
287 return; | 298 return; |
288 } | 299 } |
289 // HTTP based service request. | 300 // HTTP based service request. |
290 try { | 301 try { |
291 var client = new HttpRequestClient(request, _service); | 302 var client = new HttpRequestClient(request, _service); |
292 var message = new Message.fromUri(client, request.uri); | 303 var message = new Message.fromUri(client, request.uri); |
293 client.onMessage(null, message); | 304 client.onMessage(null, message); |
294 } catch (e) { | 305 } catch (e) { |
295 print('Unexpected error processing HTTP request uri: ' | 306 serverPrint('Unexpected error processing HTTP request uri: ' |
296 '${request.uri}\n$e\n'); | 307 '${request.uri}\n$e\n'); |
297 rethrow; | 308 rethrow; |
298 } | 309 } |
299 } | 310 } |
300 | 311 |
301 Future startup() async { | 312 Future startup() async { |
302 if (_server != null) { | 313 if (_server != null) { |
303 // Already running. | 314 // Already running. |
304 return this; | 315 return this; |
305 } | 316 } |
306 | 317 |
307 // Startup HTTP server. | 318 // Startup HTTP server. |
308 try { | 319 try { |
309 var address; | 320 var address; |
310 if (Platform.isFuchsia) { | 321 if (Platform.isFuchsia) { |
311 address = InternetAddress.ANY_IP_V6; | 322 address = InternetAddress.ANY_IP_V6; |
312 } else { | 323 } else { |
313 var addresses = await InternetAddress.lookup(_ip); | 324 var addresses = await InternetAddress.lookup(_ip); |
314 // Prefer IPv4 addresses. | 325 // Prefer IPv4 addresses. |
315 for (var i = 0; i < addresses.length; i++) { | 326 for (var i = 0; i < addresses.length; i++) { |
316 address = addresses[i]; | 327 address = addresses[i]; |
317 if (address.type == InternetAddressType.IP_V4) break; | 328 if (address.type == InternetAddressType.IP_V4) break; |
318 } | 329 } |
319 } | 330 } |
320 _server = await HttpServer.bind(address, _port); | 331 _server = await HttpServer.bind(address, _port); |
321 _server.listen(_requestHandler, cancelOnError: true); | 332 _server.listen(_requestHandler, cancelOnError: true); |
322 print('Observatory listening on $serverAddress'); | 333 serverPrint('Observatory listening on $serverAddress'); |
323 // Server is up and running. | 334 // Server is up and running. |
324 _notifyServerState(serverAddress.toString()); | 335 _notifyServerState(serverAddress.toString()); |
325 onServerAddressChange('$serverAddress'); | 336 onServerAddressChange('$serverAddress'); |
326 return this; | 337 return this; |
327 } catch (e, st) { | 338 } catch (e, st) { |
328 print('Could not start Observatory HTTP server:\n$e\n$st\n'); | 339 serverPrint('Could not start Observatory HTTP server:\n$e\n$st\n'); |
329 _notifyServerState(""); | 340 _notifyServerState(""); |
330 onServerAddressChange(null); | 341 onServerAddressChange(null); |
331 return this; | 342 return this; |
332 } | 343 } |
333 } | 344 } |
334 | 345 |
335 Future cleanup(bool force) { | 346 Future cleanup(bool force) { |
336 if (_server == null) { | 347 if (_server == null) { |
337 return new Future.value(null); | 348 return new Future.value(null); |
338 } | 349 } |
339 return _server.close(force: force); | 350 return _server.close(force: force); |
340 } | 351 } |
341 | 352 |
342 Future shutdown(bool forced) { | 353 Future shutdown(bool forced) { |
343 if (_server == null) { | 354 if (_server == null) { |
344 // Not started. | 355 // Not started. |
345 return new Future.value(this); | 356 return new Future.value(this); |
346 } | 357 } |
347 | 358 |
348 // Shutdown HTTP server and subscription. | 359 // Shutdown HTTP server and subscription. |
349 Uri oldServerAddress = serverAddress; | 360 Uri oldServerAddress = serverAddress; |
350 return cleanup(forced).then((_) { | 361 return cleanup(forced).then((_) { |
351 print('Observatory no longer listening on $oldServerAddress'); | 362 serverPrint('Observatory no longer listening on $oldServerAddress'); |
352 _server = null; | 363 _server = null; |
353 _notifyServerState(""); | 364 _notifyServerState(""); |
354 onServerAddressChange(null); | 365 onServerAddressChange(null); |
355 return this; | 366 return this; |
356 }).catchError((e, st) { | 367 }).catchError((e, st) { |
357 _server = null; | 368 _server = null; |
358 print('Could not shutdown Observatory HTTP server:\n$e\n$st\n'); | 369 serverPrint('Could not shutdown Observatory HTTP server:\n$e\n$st\n'); |
359 _notifyServerState(""); | 370 _notifyServerState(""); |
360 onServerAddressChange(null); | 371 onServerAddressChange(null); |
361 return this; | 372 return this; |
362 }); | 373 }); |
363 } | 374 } |
364 | 375 |
365 } | 376 } |
366 | 377 |
367 void _notifyServerState(String uri) | 378 void _notifyServerState(String uri) |
368 native "VMServiceIO_NotifyServerState"; | 379 native "VMServiceIO_NotifyServerState"; |
OLD | NEW |