| 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; |
| 6 |
| 7 import 'dart:async'; |
| 8 import 'dart:convert'; |
| 9 |
| 10 import 'package:analysis_server/src/protocol.dart'; |
| 11 |
| 12 /** |
| 13 * The abstract class [ClientCommunicationChannel] defines the behavior of |
| 14 * objects that allow a client to send [Request]s to an [AnalysisServer] and to |
| 15 * receive both [Response]s and [Notification]s. |
| 16 */ |
| 17 abstract class ClientCommunicationChannel { |
| 18 /** |
| 19 * The stream of notifications from the server. |
| 20 */ |
| 21 Stream<Notification> notificationStream; |
| 22 |
| 23 /** |
| 24 * The stream of responses from the server. |
| 25 */ |
| 26 Stream<Response> responseStream; |
| 27 |
| 28 /** |
| 29 * Send the given [request] to the server |
| 30 * and return a future with the associated [Response]. |
| 31 */ |
| 32 Future<Response> sendRequest(Request request); |
| 33 |
| 34 /** |
| 35 * Close the channel to the server. Once called, all future communication |
| 36 * with the server via [sendRequest] will silently be ignored. |
| 37 */ |
| 38 Future close(); |
| 39 } |
| 40 |
| 41 /** |
| 42 * The abstract class [ServerCommunicationChannel] defines the behavior of |
| 43 * objects that allow an [AnalysisServer] to receive [Request]s and to return |
| 44 * both [Response]s and [Notification]s. |
| 45 */ |
| 46 abstract class ServerCommunicationChannel { |
| 47 /** |
| 48 * Listen to the channel for requests. If a request is received, invoke the |
| 49 * [onRequest] function. If an error is encountered while trying to read from |
| 50 * the socket, invoke the [onError] function. If the socket is closed by the |
| 51 * client, invoke the [onDone] function. |
| 52 * Only one listener is allowed per channel. |
| 53 */ |
| 54 void listen(void onRequest(Request request), {Function onError, void onDone()}
); |
| 55 |
| 56 /** |
| 57 * Send the given [notification] to the client. |
| 58 */ |
| 59 void sendNotification(Notification notification); |
| 60 |
| 61 /** |
| 62 * Send the given [response] to the client. |
| 63 */ |
| 64 void sendResponse(Response response); |
| 65 |
| 66 /** |
| 67 * Close the communication channel. |
| 68 */ |
| 69 void close(); |
| 70 } |
| 71 |
| 72 /** |
| 73 * Instances of the class [JsonStreamDecoder] convert JSON strings to JSON |
| 74 * maps. |
| 75 */ |
| 76 class JsonStreamDecoder extends Converter<String, Map> { |
| 77 @override |
| 78 Map convert(String text) => JSON.decode(text); |
| 79 |
| 80 @override |
| 81 ChunkedConversionSink startChunkedConversion(Sink sink) => |
| 82 new ChannelChunkSink<String, Map>(this, sink); |
| 83 } |
| 84 |
| 85 /** |
| 86 * Instances of the class [ResponseConverter] convert JSON maps to [Response]s. |
| 87 */ |
| 88 class ResponseConverter extends Converter<Map, Response> { |
| 89 @override |
| 90 Response convert(Map json) => new Response.fromJson(json); |
| 91 |
| 92 @override |
| 93 ChunkedConversionSink startChunkedConversion(Sink sink) => |
| 94 new ChannelChunkSink<Map, Response>(this, sink); |
| 95 } |
| 96 |
| 97 /** |
| 98 * Instances of the class [NotificationConverter] convert JSON maps to |
| 99 * [Notification]s. |
| 100 */ |
| 101 class NotificationConverter extends Converter<Map, Notification> { |
| 102 @override |
| 103 Notification convert(Map json) => new Notification.fromJson(json); |
| 104 |
| 105 @override |
| 106 ChunkedConversionSink startChunkedConversion(Sink sink) => |
| 107 new ChannelChunkSink<Map, Notification>(this, sink); |
| 108 } |
| 109 |
| 110 /** |
| 111 * Instances of the class [ChannelChunkSink] uses a [Converter] to translate |
| 112 * chunks. |
| 113 */ |
| 114 class ChannelChunkSink<S, T> extends ChunkedConversionSink<S> { |
| 115 /** |
| 116 * The converter used to translate chunks. |
| 117 */ |
| 118 final Converter<S, T> converter; |
| 119 |
| 120 /** |
| 121 * The sink to which the converted chunks are added. |
| 122 */ |
| 123 final Sink sink; |
| 124 |
| 125 /** |
| 126 * A flag indicating whether the sink has been closed. |
| 127 */ |
| 128 bool closed = false; |
| 129 |
| 130 /** |
| 131 * Initialize a newly create sink to use the given [converter] to convert |
| 132 * chunks before adding them to the given [sink]. |
| 133 */ |
| 134 ChannelChunkSink(this.converter, this.sink); |
| 135 |
| 136 @override |
| 137 void add(S chunk) { |
| 138 if (!closed) { |
| 139 T convertedChunk = converter.convert(chunk); |
| 140 if (convertedChunk != null) { |
| 141 sink.add(convertedChunk); |
| 142 } |
| 143 } |
| 144 } |
| 145 |
| 146 @override |
| 147 void close() { |
| 148 closed = true; |
| 149 sink.close(); |
| 150 } |
| 151 } |
| OLD | NEW |