 Chromium Code Reviews
 Chromium Code Reviews Issue 137853026:
  Basic communication protocol for server  (Closed) 
  Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
    
  
    Issue 137853026:
  Basic communication protocol for server  (Closed) 
  Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart| Index: pkg/analysis_server/lib/src/channel.dart | 
| diff --git a/pkg/analysis_server/lib/src/channel.dart b/pkg/analysis_server/lib/src/channel.dart | 
| new file mode 100644 | 
| index 0000000000000000000000000000000000000000..4ccf12ee18bba7151e74c5c7ede4713a6c0b2b15 | 
| --- /dev/null | 
| +++ b/pkg/analysis_server/lib/src/channel.dart | 
| @@ -0,0 +1,84 @@ | 
| +// Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 
| +// for details. All rights reserved. Use of this source code is governed by a | 
| +// BSD-style license that can be found in the LICENSE file. | 
| + | 
| +library channel; | 
| + | 
| +import 'dart:async'; | 
| +import 'dart:convert'; | 
| +import 'dart:io'; | 
| + | 
| +import 'protocol.dart'; | 
| + | 
| +/** | 
| + * The abstract class [CommunicationChannel] defines the behavior of objects | 
| + * that allow an [AnalysisServer] to receive [Request]s and to return both | 
| + * [Response]s and [Notification]s. | 
| + */ | 
| +abstract class CommunicationChannel { | 
| + /** | 
| + * Listen to the channel for requests. If a request is received, invoke the | 
| + * [onRequest] function. If an error is encountered while trying to read from | 
| + * the socket, invoke the [onError] function. If the socket is closed by the | 
| + * client, invoke the [onDone] function. | 
| + */ | 
| + 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
 | 
| + | 
| + /** | 
| + * Send the given [Response] or [Notification] to the client. | 
| + */ | 
| + 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.
 | 
| +} | 
| + | 
| +/** | 
| + * Instances of the class [WebSocketChannel] implement a [CommunicationChannel] | 
| + * that uses a [WebSocket] to communicate with clients. | 
| + */ | 
| +class WebSocketChannel implements CommunicationChannel { | 
| + /** | 
| + * The socket being wrapped. | 
| + */ | 
| + final WebSocket socket; | 
| + | 
| + /** | 
| + * Initialize a newly create [WebSocket] wrapper to wrap the given [socket]. | 
| + */ | 
| + WebSocketChannel(this.socket); | 
| + | 
| + @override | 
| + void listen(void onRequest(Request request), {void onError(), void onDone()}) { | 
| + socket.listen((data) => _readRequest(data, onRequest), onError: onError, onDone: onDone); | 
| + } | 
| + | 
| + @override | 
| + void send(Object value) { | 
| + if (value is Response) { | 
| + JsonEncoder encoder = const JsonEncoder(null); | 
| + socket.add(encoder.convert(value.toJson())); | 
| + } else if (value is Notification) { | 
| + JsonEncoder encoder = const JsonEncoder(null); | 
| + socket.add(encoder.convert(value.toJson())); | 
| + } | 
| + } | 
| + | 
| + /** | 
| + * Read a request from the given [data] and use the given function to handle | 
| + * the request. | 
| + */ | 
| + void _readRequest(Object data, void onRequest(Request request)) { | 
| + if (data is List<int>) { | 
| + send(new Response.invalidRequestFormat()); | 
| + return; | 
| + } | 
| + if (data is String) { | 
| + // Parse the string as a JSON descriptor and process the resulting | 
| + // structure as a request. | 
| + Request request = new Request.fromString(data); | 
| + if (request == null) { | 
| + send(new Response.invalidRequestFormat()); | 
| + return; | 
| + } | 
| + onRequest(request); | 
| + } | 
| + } | 
| +} |