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

Side by Side Diff: example/http-server.dart

Issue 928663003: Add IsolateRunner as a helper around Isolate. (Closed) Base URL: https://github.com/dart-lang/isolate.git@master
Patch Set: Add .status. Created 5 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 unified diff | Download patch
« no previous file with comments | « README.md ('k') | example/runner-pool.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2015, 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 dart.pkg.isolate.sample.httpserver;
6
7 import "dart:io";
8 import "dart:async";
9 import "dart:isolate";
10 import "package:isolate/isolaterunner.dart";
11 import "package:isolate/runner.dart";
12 import "package:isolate/ports.dart";
13
14 typedef Future RemoteStop();
15
16 Future<RemoteStop> runHttpServer(
17 Runner runner, ServerSocket socket, HttpListener listener) {
18 return runner.run(_startHttpServer, new List(2)..[0] = socket.reference
19 ..[1] = listener)
20 .then((SendPort stopPort) => () => _sendStop(stopPort));
21 }
22
23 Future _sendStop(SendPort stopPort) {
24 return singleResponseFuture(stopPort.send);
25 }
26
27 Future<SendPort> _startHttpServer(List args) {
28 ServerSocketReference ref = args[0];
29 HttpListener listener = args[1];
30 return ref.create().then((socket) {
31 return listener.start(new HttpServer.listenOn(socket));
32 }).then((_) {
33 return singleCallbackPort((SendPort resultPort) {
34 sendFutureResult(new Future.sync(listener.stop), resultPort);
35 });
36 });
37 }
38
39 /**
40 * An [HttpRequest] handler setup. Gets called when with the server, and
41 * is told when to stop listening.
42 *
43 * These callbacks allow the listener to set up handlers for HTTP requests.
44 * The object should be sendable to an equivalent isolate.
45 */
46 abstract class HttpListener {
47 Future start(HttpServer server);
48 Future stop();
49 }
50
51 /**
52 * An [HttpListener] that sets itself up as an echo server.
53 *
54 * Returns the message content plus an ID describing the isolate that
55 * handled the request.
56 */
57 class EchoHttpListener implements HttpListener {
58 StreamSubscription _subscription;
59 static int _id = new Object().hashCode;
60 SendPort _counter;
61
62 EchoHttpListener(this._counter);
63
64 start(HttpServer server) {
65 print("Starting isolate $_id");
66 _subscription = server.listen((HttpRequest request) {
67 request.response.addStream(request).then((_) {
68 _counter.send(null);
69 print("Request to $_id");
70 request.response.write("#$_id\n");
71 var t0 = new DateTime.now().add(new Duration(seconds:2));
72 while (new DateTime.now().isBefore(t0));
73 print("Response from $_id");
74 request.response.close();
75 });
76 });
77 }
78
79 stop() {
80 print("Stopping isolate $_id");
81 _subscription.cancel();
82 _subscription = null;
83 }
84 }
85
86 main(args) {
87 int port = 0;
88 if (args.length > 0) {
89 port = int.parse(args[0]);
90 }
91 RawReceivePort counter = new RawReceivePort();
92 HttpListener listener = new EchoHttpListener(counter.sendPort);
93 ServerSocket
94 .bind(InternetAddress.ANY_IP_V6, port)
95 .then((ServerSocket socket) {
96 port = socket.port;
97 return Future.wait(new Iterable.generate(5, (_) => IsolateRunner.spawn()),
98 cleanUp: (isolate) { isolate.close(); })
99 .then((List<IsolateRunner> isolates) {
100 return Future.wait(isolates.map((IsolateRunner isolate) {
101 return runHttpServer(isolate, socket, listener);
102 }), cleanUp: (server) { server.stop(); });
103 })
104 .then((stoppers) {
105 socket.close();
106 int count = 25;
107 counter.handler = (_) {
108 count--;
109 if (count == 0) {
110 stoppers.forEach((f) => f());
111 counter.close();
112 }
113 };
114 print("Server listening on port $port for 25 requests");
115 print("Test with:");
116 print(" ab -c10 -n 25 http://localhost:$port/");
117 });
118 });
119 }
OLDNEW
« no previous file with comments | « README.md ('k') | example/runner-pool.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698