| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 library channel.web_socket; | 5 library channel.web_socket; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'dart:convert'; | 8 import 'dart:convert'; |
| 9 import 'dart:io'; | 9 import 'dart:io'; |
| 10 | 10 |
| 11 import 'package:analysis_server/src/channel/channel.dart'; | 11 import 'package:analysis_server/src/channel/channel.dart'; |
| 12 import 'package:analysis_server/src/protocol.dart'; | 12 import 'package:analysis_server/src/protocol.dart'; |
| 13 import 'package:analyzer/instrumentation/instrumentation.dart'; |
| 13 | 14 |
| 14 | 15 |
| 15 /** | 16 /** |
| 16 * Instances of the class [WebSocketClientChannel] implement a | 17 * Instances of the class [WebSocketClientChannel] implement a |
| 17 * [ClientCommunicationChannel] that uses a [WebSocket] to communicate with | 18 * [ClientCommunicationChannel] that uses a [WebSocket] to communicate with |
| 18 * servers. | 19 * servers. |
| 19 */ | 20 */ |
| 20 class WebSocketClientChannel implements ClientCommunicationChannel { | 21 class WebSocketClientChannel implements ClientCommunicationChannel { |
| 21 /** | 22 /** |
| 22 * The socket being wrapped. | 23 * The socket being wrapped. |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 65 * [ServerCommunicationChannel] that uses a [WebSocket] to communicate with | 66 * [ServerCommunicationChannel] that uses a [WebSocket] to communicate with |
| 66 * clients. | 67 * clients. |
| 67 */ | 68 */ |
| 68 class WebSocketServerChannel implements ServerCommunicationChannel { | 69 class WebSocketServerChannel implements ServerCommunicationChannel { |
| 69 /** | 70 /** |
| 70 * The socket being wrapped. | 71 * The socket being wrapped. |
| 71 */ | 72 */ |
| 72 final WebSocket socket; | 73 final WebSocket socket; |
| 73 | 74 |
| 74 /** | 75 /** |
| 76 * The instrumentation server that is to be used by this analysis server. |
| 77 */ |
| 78 final InstrumentationServer instrumentationServer; |
| 79 |
| 80 /** |
| 75 * Initialize a newly create [WebSocket] wrapper to wrap the given [socket]. | 81 * Initialize a newly create [WebSocket] wrapper to wrap the given [socket]. |
| 76 */ | 82 */ |
| 77 WebSocketServerChannel(this.socket); | 83 WebSocketServerChannel(this.socket, this.instrumentationServer); |
| 78 | 84 |
| 79 @override | 85 @override |
| 80 void close() { | 86 void close() { |
| 81 socket.close(WebSocketStatus.NORMAL_CLOSURE); | 87 socket.close(WebSocketStatus.NORMAL_CLOSURE); |
| 82 } | 88 } |
| 83 | 89 |
| 84 @override | 90 @override |
| 85 void listen(void onRequest(Request request), {void onError(), void | 91 void listen(void onRequest(Request request), {void onError(), void |
| 86 onDone()}) { | 92 onDone()}) { |
| 87 socket.listen( | 93 socket.listen( |
| 88 (data) => readRequest(data, onRequest), | 94 (data) => readRequest(data, onRequest), |
| 89 onError: onError, | 95 onError: onError, |
| 90 onDone: onDone); | 96 onDone: onDone); |
| 91 } | 97 } |
| 92 | 98 |
| 93 /** | 99 /** |
| 94 * Read a request from the given [data] and use the given function to handle | 100 * Read a request from the given [data] and use the given function to handle |
| 95 * the request. | 101 * the request. |
| 96 */ | 102 */ |
| 97 void readRequest(Object data, void onRequest(Request request)) { | 103 void readRequest(Object data, void onRequest(Request request)) { |
| 98 if (data is String) { | 104 if (data is String) { |
| 105 instrumentationServer.log(data); |
| 99 // Parse the string as a JSON descriptor and process the resulting | 106 // Parse the string as a JSON descriptor and process the resulting |
| 100 // structure as a request. | 107 // structure as a request. |
| 101 ServerCommunicationChannel.FromJson.start(); | 108 ServerCommunicationChannel.FromJson.start(); |
| 102 Request request = new Request.fromString(data); | 109 Request request = new Request.fromString(data); |
| 103 ServerCommunicationChannel.FromJson.stop(); | 110 ServerCommunicationChannel.FromJson.stop(); |
| 104 if (request == null) { | 111 if (request == null) { |
| 105 sendResponse(new Response.invalidRequestFormat()); | 112 sendResponse(new Response.invalidRequestFormat()); |
| 106 return; | 113 return; |
| 107 } | 114 } |
| 108 onRequest(request); | 115 onRequest(request); |
| 109 } else if (data is List<int>) { | 116 } else if (data is List<int>) { |
| 110 // TODO(brianwilkerson) Implement a more efficient protocol. | 117 // TODO(brianwilkerson) Implement a more efficient protocol. |
| 111 sendResponse(new Response.invalidRequestFormat()); | 118 sendResponse(new Response.invalidRequestFormat()); |
| 112 } else { | 119 } else { |
| 113 sendResponse(new Response.invalidRequestFormat()); | 120 sendResponse(new Response.invalidRequestFormat()); |
| 114 } | 121 } |
| 115 } | 122 } |
| 116 | 123 |
| 117 @override | 124 @override |
| 118 void sendNotification(Notification notification) { | 125 void sendNotification(Notification notification) { |
| 119 ServerCommunicationChannel.ToJson.start(); | 126 ServerCommunicationChannel.ToJson.start(); |
| 120 String jsonEncoding = JSON.encode(notification.toJson()); | 127 String jsonEncoding = JSON.encode(notification.toJson()); |
| 121 ServerCommunicationChannel.ToJson.stop(); | 128 ServerCommunicationChannel.ToJson.stop(); |
| 122 socket.add(jsonEncoding); | 129 socket.add(jsonEncoding); |
| 130 instrumentationServer.log(jsonEncoding); |
| 123 } | 131 } |
| 124 | 132 |
| 125 @override | 133 @override |
| 126 void sendResponse(Response response) { | 134 void sendResponse(Response response) { |
| 127 ServerCommunicationChannel.ToJson.start(); | 135 ServerCommunicationChannel.ToJson.start(); |
| 128 String jsonEncoding = JSON.encode(response.toJson()); | 136 String jsonEncoding = JSON.encode(response.toJson()); |
| 129 ServerCommunicationChannel.ToJson.stop(); | 137 ServerCommunicationChannel.ToJson.stop(); |
| 130 socket.add(jsonEncoding); | 138 socket.add(jsonEncoding); |
| 139 instrumentationServer.log(jsonEncoding); |
| 131 } | 140 } |
| 132 } | 141 } |
| OLD | NEW |