Chromium Code Reviews| 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 import 'dart:io'; | |
| 10 | |
| 11 import 'protocol.dart'; | |
| 12 | |
| 13 /** | |
| 14 * The abstract class [CommunicationChannel] defines the behavior of objects | |
| 15 * that allow an [AnalysisServer] to receive [Request]s and to return both | |
| 16 * [Response]s and [Notification]s. | |
| 17 */ | |
| 18 abstract class CommunicationChannel { | |
| 19 /** | |
| 20 * Listen to the channel for requests. If a request is received, invoke the | |
| 21 * [onRequest] function. If an error is encountered while trying to read from | |
| 22 * the socket, invoke the [onError] function. If the socket is closed by the | |
| 23 * client, invoke the [onDone] function. | |
| 24 */ | |
| 25 void listen(void onRequest(Request request), {void onError(), void onDone()}); | |
|
devoncarew
2014/01/20 22:35:01
My guess is that you want this to be a Stream. So
Brian Wilkerson
2014/01/21 03:21:20
I don't understand why that would be better. Let's
| |
| 26 | |
| 27 /** | |
| 28 * Send the given [Response] or [Notification] to the client. | |
| 29 */ | |
| 30 void send(Object value); | |
|
devoncarew
2014/01/20 22:35:01
Perhaps have separate methods for the two types? Y
Brian Wilkerson
2014/01/21 03:21:20
I like that! Done.
| |
| 31 } | |
| 32 | |
| 33 /** | |
| 34 * Instances of the class [WebSocketChannel] implement a [CommunicationChannel] | |
| 35 * that uses a [WebSocket] to communicate with clients. | |
| 36 */ | |
| 37 class WebSocketChannel implements CommunicationChannel { | |
| 38 /** | |
| 39 * The socket being wrapped. | |
| 40 */ | |
| 41 final WebSocket socket; | |
| 42 | |
| 43 /** | |
| 44 * Initialize a newly create [WebSocket] wrapper to wrap the given [socket]. | |
| 45 */ | |
| 46 WebSocketChannel(this.socket); | |
| 47 | |
| 48 @override | |
| 49 void listen(void onRequest(Request request), {void onError(), void onDone()}) { | |
| 50 socket.listen((data) => _readRequest(data, onRequest), onError: onError, onD one: onDone); | |
| 51 } | |
| 52 | |
| 53 @override | |
| 54 void send(Object value) { | |
| 55 if (value is Response) { | |
| 56 JsonEncoder encoder = const JsonEncoder(null); | |
| 57 socket.add(encoder.convert(value.toJson())); | |
| 58 } else if (value is Notification) { | |
| 59 JsonEncoder encoder = const JsonEncoder(null); | |
| 60 socket.add(encoder.convert(value.toJson())); | |
| 61 } | |
| 62 } | |
| 63 | |
| 64 /** | |
| 65 * Read a request from the given [data] and use the given function to handle | |
| 66 * the request. | |
| 67 */ | |
| 68 void _readRequest(Object data, void onRequest(Request request)) { | |
| 69 if (data is List<int>) { | |
| 70 send(new Response.invalidRequestFormat()); | |
| 71 return; | |
| 72 } | |
| 73 if (data is String) { | |
| 74 // Parse the string as a JSON descriptor and process the resulting | |
| 75 // structure as a request. | |
| 76 Request request = new Request.fromString(data); | |
| 77 if (request == null) { | |
| 78 send(new Response.invalidRequestFormat()); | |
| 79 return; | |
| 80 } | |
| 81 onRequest(request); | |
| 82 } | |
| 83 } | |
| 84 } | |
| OLD | NEW |