Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(52)

Side by Side Diff: pkg/analysis_server/lib/src/channel/byte_stream_channel.dart

Issue 872633002: Use 'print' to send responses/notifications to the client. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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.byte_stream; 5 library channel.byte_stream;
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 229 matching lines...) Expand 10 before | Expand all | Expand 10 after
240 ServerCommunicationChannel.FromJson.start(); 240 ServerCommunicationChannel.FromJson.start();
241 Request request = new Request.fromString(data); 241 Request request = new Request.fromString(data);
242 ServerCommunicationChannel.FromJson.stop(); 242 ServerCommunicationChannel.FromJson.stop();
243 if (request == null) { 243 if (request == null) {
244 sendResponse(new Response.invalidRequestFormat()); 244 sendResponse(new Response.invalidRequestFormat());
245 return; 245 return;
246 } 246 }
247 onRequest(request); 247 onRequest(request);
248 } 248 }
249 } 249 }
250
251 /**
252 * Instances of the class [PrintServerChannel] implement a
253 * [ServerCommunicationChannel] that uses standard input and [print]
254 * to communicate with clients.
255 */
256 class PrintServerChannel implements ServerCommunicationChannel {
257 /**
258 * The instrumentation service that is to be used by this analysis server.
259 */
260 final InstrumentationService instrumentationService;
261
262 /**
263 * Completer that will be signalled when the input stream is closed.
264 */
265 final Completer _closed = new Completer();
266
267 /**
268 * True if [close] has been called.
269 */
270 bool _closeRequested = false;
271
272 PrintServerChannel(this.instrumentationService);
273
274 /**
275 * Future that will be completed when the input stream is closed.
276 */
277 Future get closed {
278 return _closed.future;
279 }
280
281 @override
282 void close() {
283 if (!_closeRequested) {
284 _closeRequested = true;
285 assert(!_closed.isCompleted);
286 _closed.complete();
287 }
288 }
289
290 @override
291 void listen(void onRequest(Request request), {Function onError, void
292 onDone()}) {
293 stdin.transform(
294 (new Utf8Codec()).decoder).transform(
295 new LineSplitter()).listen(
296 (String data) => _readRequest(data, onRequest),
297 onError: onError,
298 onDone: () {
299 close();
300 onDone();
301 });
302 }
303
304 @override
305 void sendNotification(Notification notification) {
306 // Don't send any further notifications after the communication channel is
307 // closed.
308 if (_closeRequested) {
309 return;
310 }
311 ServerCommunicationChannel.ToJson.start();
312 String jsonEncoding = JSON.encode(notification.toJson());
313 ServerCommunicationChannel.ToJson.stop();
314 print(jsonEncoding);
315 instrumentationService.logNotification(jsonEncoding);
316 }
317
318 @override
319 void sendResponse(Response response) {
320 // Don't send any further responses after the communication channel is
321 // closed.
322 if (_closeRequested) {
323 return;
324 }
325 ServerCommunicationChannel.ToJson.start();
326 String jsonEncoding = JSON.encode(response.toJson());
327 ServerCommunicationChannel.ToJson.stop();
328 print(jsonEncoding);
329 instrumentationService.logResponse(jsonEncoding);
330 }
331
332 /**
333 * Read a request from the given [data] and use the given function to handle
334 * the request.
335 */
336 void _readRequest(Object data, void onRequest(Request request)) {
337 // Ignore any further requests after the communication channel is closed.
338 if (_closed.isCompleted) {
339 return;
340 }
341 instrumentationService.logRequest(data);
342 // Parse the string as a JSON descriptor and process the resulting
343 // structure as a request.
344 ServerCommunicationChannel.FromJson.start();
345 Request request = new Request.fromString(data);
346 ServerCommunicationChannel.FromJson.stop();
347 if (request == null) {
348 sendResponse(new Response.invalidRequestFormat());
349 return;
350 }
351 onRequest(request);
352 }
353 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698