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

Side by Side Diff: sdk/lib/_internal/pub/lib/src/barback/web_socket_api.dart

Issue 309503005: Convert json_rpc.Server to take a Stream and StreamSink. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: code review Created 6 years, 6 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
« no previous file with comments | « pkg/json_rpc_2/test/server/utils.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 pub.barback.web_socket_api; 5 library pub.barback.web_socket_api;
6 6
7 import 'dart:async'; 7 import 'dart:async';
8 import 'dart:io'; 8 import 'dart:io';
9 9
10 import 'package:http_parser/http_parser.dart'; 10 import 'package:http_parser/http_parser.dart';
11 import 'package:path/path.dart' as path; 11 import 'package:path/path.dart' as path;
12 import 'package:json_rpc_2/json_rpc_2.dart' as json_rpc; 12 import 'package:json_rpc_2/json_rpc_2.dart' as json_rpc;
13 13
14 import '../exit_codes.dart' as exit_codes; 14 import '../exit_codes.dart' as exit_codes;
15 import '../io.dart'; 15 import '../io.dart';
16 import '../log.dart' as log; 16 import '../log.dart' as log;
17 import '../utils.dart'; 17 import '../utils.dart';
18 import 'asset_environment.dart'; 18 import 'asset_environment.dart';
19 19
20 /// Implements the [WebSocket] API for communicating with a running pub serve 20 /// Implements the [WebSocket] API for communicating with a running pub serve
21 /// process, mainly for use by the Editor. 21 /// process, mainly for use by the Editor.
22 /// 22 ///
23 /// This is a [JSON-RPC 2.0](http://www.jsonrpc.org/specification) server. Its 23 /// This is a [JSON-RPC 2.0](http://www.jsonrpc.org/specification) server. Its
24 /// methods are described in the method-level documentation below. 24 /// methods are described in the method-level documentation below.
25 class WebSocketApi { 25 class WebSocketApi {
26 final CompatibleWebSocket _socket;
27 final AssetEnvironment _environment; 26 final AssetEnvironment _environment;
28 final _server = new json_rpc.Server(); 27 final json_rpc.Server _server;
29 28
30 /// Whether the application should exit when this connection closes. 29 /// Whether the application should exit when this connection closes.
31 bool _exitOnClose = false; 30 bool _exitOnClose = false;
32 31
33 WebSocketApi(this._socket, this._environment) { 32 WebSocketApi(CompatibleWebSocket socket, this._environment)
33 : _server = new json_rpc.Server(socket) {
34 _server.registerMethod("urlToAssetId", _urlToAssetId); 34 _server.registerMethod("urlToAssetId", _urlToAssetId);
35 _server.registerMethod("pathToUrls", _pathToUrls); 35 _server.registerMethod("pathToUrls", _pathToUrls);
36 _server.registerMethod("serveDirectory", _serveDirectory); 36 _server.registerMethod("serveDirectory", _serveDirectory);
37 _server.registerMethod("unserveDirectory", _unserveDirectory); 37 _server.registerMethod("unserveDirectory", _unserveDirectory);
38 38
39 /// Tells the server to exit as soon as this WebSocket connection is closed. 39 /// Tells the server to exit as soon as this WebSocket connection is closed.
40 /// 40 ///
41 /// This takes no arguments and returns no results. It can safely be called 41 /// This takes no arguments and returns no results. It can safely be called
42 /// as a JSON-RPC notification. 42 /// as a JSON-RPC notification.
43 _server.registerMethod("exitOnClose", () { 43 _server.registerMethod("exitOnClose", () {
44 _exitOnClose = true; 44 _exitOnClose = true;
45 }); 45 });
46 } 46 }
47 47
48 /// Listens on the socket. 48 /// Listens on the socket.
49 /// 49 ///
50 /// Returns a future that completes when the socket has closed. It will 50 /// Returns a future that completes when the socket has closed. It will
51 /// complete with an error if the socket had an error, otherwise it will 51 /// complete with an error if the socket had an error, otherwise it will
52 /// complete to `null`. 52 /// complete to `null`.
53 Future listen() { 53 Future listen() {
54 return _socket.listen((request) { 54 _server.listen().then((_) {
55 _server.parseRequest(request).then((response) {
56 if (response != null) _socket.add(response);
57 });
58 }, cancelOnError: true).asFuture().then((_) {
59 if (!_exitOnClose) return; 55 if (!_exitOnClose) return;
60 log.message("WebSocket connection closed, terminating."); 56 log.message("WebSocket connection closed, terminating.");
61 flushThenExit(exit_codes.SUCCESS); 57 flushThenExit(exit_codes.SUCCESS);
62 }); 58 });
63 } 59 }
64 60
65 /// Given a URL to an asset that is served by pub, returns the ID of the 61 /// Given a URL to an asset that is served by pub, returns the ID of the
66 /// asset that would be accessed by that URL. 62 /// asset that would be accessed by that URL.
67 /// 63 ///
68 /// The method name is "urlToAssetId" and it takes a "url" parameter for the 64 /// The method name is "urlToAssetId" and it takes a "url" parameter for the
(...skipping 236 matching lines...) Expand 10 before | Expand all | Expand 10 after
305 301
306 302
307 /// The pub-specific JSON RPC error codes. 303 /// The pub-specific JSON RPC error codes.
308 class _Error { 304 class _Error {
309 /// The specified directory is not being served. 305 /// The specified directory is not being served.
310 static const NOT_SERVED = 1; 306 static const NOT_SERVED = 1;
311 307
312 /// The specified directory overlaps one or more ones already being served. 308 /// The specified directory overlaps one or more ones already being served.
313 static const OVERLAPPING = 2; 309 static const OVERLAPPING = 2;
314 } 310 }
OLDNEW
« no previous file with comments | « pkg/json_rpc_2/test/server/utils.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698