| 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 [ByteStreamServerChannel] 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 ByteStreamServerChannel implements ServerCommunicationChannel { |
| 178 final Stream input; |
| 179 final IOSink output; |
| 180 |
| 181 ByteStreamServerChannel(this.input, this.output); |
| 182 |
| 183 @override |
| 184 void listen(void onRequest(Request request), {Function onError, void |
| 185 onDone()}) { |
| 186 input.transform((new Utf8Codec()).decoder).transform(new LineSplitter() |
| 187 ).listen((String data) => _readRequest(data, onRequest), onError: onErro
r, |
| 188 onDone: onDone); |
| 189 } |
| 190 |
| 191 @override |
| 192 void sendNotification(Notification notification) { |
| 193 output.writeln(JSON.encode(notification.toJson())); |
| 194 } |
| 195 |
| 196 @override |
| 197 void sendResponse(Response response) { |
| 198 output.writeln(JSON.encode(response.toJson())); |
| 199 } |
| 200 |
| 201 /** |
| 202 * Read a request from the given [data] and use the given function to handle |
| 203 * the request. |
| 204 */ |
| 205 void _readRequest(Object data, void onRequest(Request request)) { |
| 206 // Parse the string as a JSON descriptor and process the resulting |
| 207 // structure as a request. |
| 208 Request request = new Request.fromString(data); |
| 209 if (request == null) { |
| 210 sendResponse(new Response.invalidRequestFormat()); |
| 211 return; |
| 212 } |
| 213 onRequest(request); |
| 214 } |
| 215 } |
| 216 |
| 217 /** |
| 173 * Instances of the class [JsonStreamDecoder] convert JSON strings to JSON | 218 * Instances of the class [JsonStreamDecoder] convert JSON strings to JSON |
| 174 * maps. | 219 * maps. |
| 175 */ | 220 */ |
| 176 class JsonStreamDecoder extends Converter<String, Map> { | 221 class JsonStreamDecoder extends Converter<String, Map> { |
| 177 @override | 222 @override |
| 178 Map convert(String text) => JSON.decode(text); | 223 Map convert(String text) => JSON.decode(text); |
| 179 | 224 |
| 180 @override | 225 @override |
| 181 ChunkedConversionSink startChunkedConversion(Sink sink) => | 226 ChunkedConversionSink startChunkedConversion(Sink sink) => |
| 182 new ChannelChunkSink<String, Map>(this, sink); | 227 new ChannelChunkSink<String, Map>(this, sink); |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 242 } | 287 } |
| 243 } | 288 } |
| 244 } | 289 } |
| 245 | 290 |
| 246 @override | 291 @override |
| 247 void close() { | 292 void close() { |
| 248 closed = true; | 293 closed = true; |
| 249 sink.close(); | 294 sink.close(); |
| 250 } | 295 } |
| 251 } | 296 } |
| OLD | NEW |