| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library channel.byte_stream; |
| 6 |
| 7 import 'dart:async'; |
| 8 import 'dart:convert'; |
| 9 import 'dart:io'; |
| 10 |
| 11 import 'package:analysis_server/src/channel/channel.dart'; |
| 12 import 'package:analysis_server/src/protocol.dart'; |
| 13 |
| 14 /** |
| 15 * Instances of the class [ByteStreamClientChannel] implement a |
| 16 * [ClientCommunicationChannel] that uses a stream and a sink (typically, |
| 17 * standard input and standard output) to communicate with servers. |
| 18 */ |
| 19 class ByteStreamClientChannel implements ClientCommunicationChannel { |
| 20 final Stream input; |
| 21 final IOSink output; |
| 22 |
| 23 @override |
| 24 Stream<Response> responseStream; |
| 25 |
| 26 @override |
| 27 Stream<Notification> notificationStream; |
| 28 |
| 29 ByteStreamClientChannel(this.input, this.output) { |
| 30 Stream jsonStream = input.transform((new Utf8Codec()).decoder) |
| 31 .transform(new LineSplitter()) |
| 32 .transform(new JsonStreamDecoder()) |
| 33 .where((json) => json is Map) |
| 34 .asBroadcastStream(); |
| 35 responseStream = jsonStream |
| 36 .where((json) => json[Notification.EVENT] == null) |
| 37 .transform(new ResponseConverter()) |
| 38 .asBroadcastStream(); |
| 39 notificationStream = jsonStream |
| 40 .where((json) => json[Notification.EVENT] != null) |
| 41 .transform(new NotificationConverter()) |
| 42 .asBroadcastStream(); |
| 43 } |
| 44 |
| 45 @override |
| 46 Future close() { |
| 47 return output.close(); |
| 48 } |
| 49 |
| 50 @override |
| 51 Future<Response> sendRequest(Request request) { |
| 52 String id = request.id; |
| 53 output.writeln(JSON.encode(request.toJson())); |
| 54 return responseStream.firstWhere((Response response) => response.id == id); |
| 55 } |
| 56 } |
| 57 |
| 58 /** |
| 59 * Instances of the class [ByteStreamServerChannel] implement a |
| 60 * [ServerCommunicationChannel] that uses a stream and a sink (typically, |
| 61 * standard input and standard output) to communicate with clients. |
| 62 */ |
| 63 class ByteStreamServerChannel implements ServerCommunicationChannel { |
| 64 final Stream input; |
| 65 final IOSink output; |
| 66 |
| 67 /** |
| 68 * Completer that will be signalled when the input stream is closed. |
| 69 */ |
| 70 final Completer _closed = new Completer(); |
| 71 |
| 72 ByteStreamServerChannel(this.input, this.output); |
| 73 |
| 74 /** |
| 75 * Future that will be completed when the input stream is closed. |
| 76 */ |
| 77 Future get closed { |
| 78 return _closed.future; |
| 79 } |
| 80 |
| 81 @override |
| 82 void close() { |
| 83 if (!_closed.isCompleted) { |
| 84 _closed.complete(); |
| 85 } |
| 86 } |
| 87 |
| 88 @override |
| 89 void listen(void onRequest(Request request), {Function onError, void |
| 90 onDone()}) { |
| 91 input.transform((new Utf8Codec()).decoder).transform(new LineSplitter() |
| 92 ).listen((String data) => _readRequest(data, onRequest), onError: onErro
r, |
| 93 onDone: () { |
| 94 close(); |
| 95 onDone(); |
| 96 }); |
| 97 } |
| 98 |
| 99 @override |
| 100 void sendNotification(Notification notification) { |
| 101 // Don't send any further notifications after the communication channel is |
| 102 // closed. |
| 103 if (_closed.isCompleted) { |
| 104 return; |
| 105 } |
| 106 output.writeln(JSON.encode(notification.toJson())); |
| 107 } |
| 108 |
| 109 @override |
| 110 void sendResponse(Response response) { |
| 111 // Don't send any further responses after the communication channel is |
| 112 // closed. |
| 113 if (_closed.isCompleted) { |
| 114 return; |
| 115 } |
| 116 output.writeln(JSON.encode(response.toJson())); |
| 117 } |
| 118 |
| 119 /** |
| 120 * Read a request from the given [data] and use the given function to handle |
| 121 * the request. |
| 122 */ |
| 123 void _readRequest(Object data, void onRequest(Request request)) { |
| 124 // Ignore any further requests after the communication channel is closed. |
| 125 if (_closed.isCompleted) { |
| 126 return; |
| 127 } |
| 128 // Parse the string as a JSON descriptor and process the resulting |
| 129 // structure as a request. |
| 130 Request request = new Request.fromString(data); |
| 131 if (request == null) { |
| 132 sendResponse(new Response.invalidRequestFormat()); |
| 133 return; |
| 134 } |
| 135 onRequest(request); |
| 136 } |
| 137 } |
| OLD | NEW |