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

Unified Diff: sdk/lib/_internal/pub/lib/src/barback/admin_server.dart

Issue 204673006: Add admin server that supports web socket API. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Revise. Created 6 years, 9 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | sdk/lib/_internal/pub/lib/src/barback/barback_server.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sdk/lib/_internal/pub/lib/src/barback/admin_server.dart
diff --git a/sdk/lib/_internal/pub/lib/src/barback/admin_server.dart b/sdk/lib/_internal/pub/lib/src/barback/admin_server.dart
new file mode 100644
index 0000000000000000000000000000000000000000..f07b7fa3470d6ef1050cb080ec44f9fb45d02848
--- /dev/null
+++ b/sdk/lib/_internal/pub/lib/src/barback/admin_server.dart
@@ -0,0 +1,72 @@
+// 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 pub.barback.admin_server;
+
+import 'dart:async';
+import 'dart:io';
+
+import 'package:stack_trace/stack_trace.dart';
+
+import '../log.dart' as log;
+import 'base_server.dart';
+import 'build_environment.dart';
+import 'web_socket_api.dart';
+
+/// The web admin interface to pub serve.
+// TODO(rnystrom): Currently this just provides access to the Web Socket API.
+// See #16954.
+class AdminServer extends BaseServer {
+ /// All currently open [WebSocket] connections.
+ final _webSockets = new Set<WebSocket>();
+
+ /// Creates a new server and binds it to [port] of [host].
+ static Future<AdminServer> bind(BuildEnvironment environment,
+ String host, int port) {
+ return Chain.track(HttpServer.bind(host, port)).then((server) {
+ log.fine('Bound admin server to $host:$port.');
+ return new AdminServer._(environment, server);
+ });
+ }
+
+ AdminServer._(BuildEnvironment environment, HttpServer server)
+ : super(environment, server);
+
+ /// Closes the server and all Web Socket connections.
+ Future close() {
+ var futures = [super.close()];
+ futures.addAll(_webSockets.map((socket) => socket.close()));
+ return Future.wait(futures);
+ }
+
+ /// Handles an HTTP request.
+ void handleRequest(HttpRequest request) {
+ if (WebSocketTransformer.isUpgradeRequest(request)) {
+ _handleWebSocket(request);
+ return;
+ }
+
+ // TODO(rnystrom): Actually respond to requests once there is an admin
+ // interface. See #16954.
+ logRequest(request, "501 Not Implemented");
+ request.response.headers.contentType =
+ ContentType.parse("text/plain; charset=utf-8");
+
+ request.response.statusCode = 501;
+ request.response.reasonPhrase = "Not Implemented";
+ request.response.write(
+ "Currently this server only accepts Web Socket connections.");
+ request.response.close();
+ }
+
+ /// Creates a web socket for [request] which should be an upgrade request.
+ void _handleWebSocket(HttpRequest request) {
+ Chain.track(WebSocketTransformer.upgrade(request)).then((socket) {
+ _webSockets.add(socket);
+ var api = new WebSocketApi(socket, environment);
+
+ return api.listen().whenComplete(() => _webSockets.remove(api));
+ }).catchError(addError);
+ }
+}
« no previous file with comments | « no previous file | sdk/lib/_internal/pub/lib/src/barback/barback_server.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698