| 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; | 5 library channel; |
| 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 |
| (...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 163 } else if (data is List<int>) { | 163 } else if (data is List<int>) { |
| 164 // TODO(brianwilkerson) Implement a more efficient protocol. | 164 // TODO(brianwilkerson) Implement a more efficient protocol. |
| 165 sendResponse(new Response.invalidRequestFormat()); | 165 sendResponse(new Response.invalidRequestFormat()); |
| 166 } else { | 166 } else { |
| 167 sendResponse(new Response.invalidRequestFormat()); | 167 sendResponse(new Response.invalidRequestFormat()); |
| 168 } | 168 } |
| 169 } | 169 } |
| 170 } | 170 } |
| 171 | 171 |
| 172 /** | 172 /** |
| 173 * Instances of the class [ByteStreamClientChannel] implement a |
| 174 * [ClientCommunicationChannel] that uses a stream and a sink (typically, |
| 175 * standard input and standard output) to communicate with servers. |
| 176 */ |
| 177 class ByteStreamClientChannel implements ClientCommunicationChannel { |
| 178 final Stream input; |
| 179 final IOSink output; |
| 180 |
| 181 @override |
| 182 Stream<Response> responseStream; |
| 183 |
| 184 @override |
| 185 Stream<Notification> notificationStream; |
| 186 |
| 187 ByteStreamClientChannel(this.input, this.output) { |
| 188 Stream jsonStream = input.transform((new Utf8Codec()).decoder) |
| 189 .transform(new LineSplitter()) |
| 190 .transform(new JsonStreamDecoder()) |
| 191 .where((json) => json is Map) |
| 192 .asBroadcastStream(); |
| 193 responseStream = jsonStream |
| 194 .where((json) => json[Notification.EVENT] == null) |
| 195 .transform(new ResponseConverter()) |
| 196 .asBroadcastStream(); |
| 197 notificationStream = jsonStream |
| 198 .where((json) => json[Notification.EVENT] != null) |
| 199 .transform(new NotificationConverter()) |
| 200 .asBroadcastStream(); |
| 201 } |
| 202 |
| 203 @override |
| 204 Future close() { |
| 205 // TODO: implement close |
| 206 } |
| 207 |
| 208 @override |
| 209 Future<Response> sendRequest(Request request) { |
| 210 String id = request.id; |
| 211 output.writeln(JSON.encode(request.toJson())); |
| 212 return responseStream.firstWhere((Response response) => response.id == id); |
| 213 } |
| 214 } |
| 215 |
| 216 /** |
| 173 * Instances of the class [ByteStreamServerChannel] implement a | 217 * Instances of the class [ByteStreamServerChannel] implement a |
| 174 * [ClientCommunicationChannel] that uses a stream and a sink (typically, | 218 * [ServerCommunicationChannel] that uses a stream and a sink (typically, |
| 175 * standard input and standard output) to communicate with servers. | 219 * standard input and standard output) to communicate with clients. |
| 176 */ | 220 */ |
| 177 class ByteStreamServerChannel implements ServerCommunicationChannel { | 221 class ByteStreamServerChannel implements ServerCommunicationChannel { |
| 178 final Stream input; | 222 final Stream input; |
| 179 final IOSink output; | 223 final IOSink output; |
| 180 | 224 |
| 181 /** | 225 /** |
| 182 * Completer that will be signalled when the input stream is closed. | 226 * Completer that will be signalled when the input stream is closed. |
| 183 */ | 227 */ |
| 184 final Completer _closed = new Completer(); | 228 final Completer _closed = new Completer(); |
| 185 | 229 |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 302 } | 346 } |
| 303 } | 347 } |
| 304 } | 348 } |
| 305 | 349 |
| 306 @override | 350 @override |
| 307 void close() { | 351 void close() { |
| 308 closed = true; | 352 closed = true; |
| 309 sink.close(); | 353 sink.close(); |
| 310 } | 354 } |
| 311 } | 355 } |
| OLD | NEW |