| OLD | NEW |
| (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 /// An [HttpRequest] handler setup. Gets called when with the server, and | |
| 40 /// is told when to stop listening. | |
| 41 /// | |
| 42 /// These callbacks allow the listener to set up handlers for HTTP requests. | |
| 43 /// The object should be sendable to an equivalent isolate. | |
| 44 abstract class HttpListener { | |
| 45 Future start(HttpServer server); | |
| 46 Future stop(); | |
| 47 } | |
| 48 | |
| 49 /// An [HttpListener] that sets itself up as an echo server. | |
| 50 /// | |
| 51 /// Returns the message content plus an ID describing the isolate that | |
| 52 /// handled the request. | |
| 53 class EchoHttpListener implements HttpListener { | |
| 54 StreamSubscription _subscription; | |
| 55 static int _id = new Object().hashCode; | |
| 56 SendPort _counter; | |
| 57 | |
| 58 EchoHttpListener(this._counter); | |
| 59 | |
| 60 start(HttpServer server) { | |
| 61 print("Starting isolate $_id"); | |
| 62 _subscription = server.listen((HttpRequest request) { | |
| 63 request.response.addStream(request).then((_) { | |
| 64 _counter.send(null); | |
| 65 print("Request to $_id"); | |
| 66 request.response.write("#$_id\n"); | |
| 67 var t0 = new DateTime.now().add(new Duration(seconds: 2)); | |
| 68 while (new DateTime.now().isBefore(t0)); | |
| 69 print("Response from $_id"); | |
| 70 request.response.close(); | |
| 71 }); | |
| 72 }); | |
| 73 } | |
| 74 | |
| 75 stop() { | |
| 76 print("Stopping isolate $_id"); | |
| 77 _subscription.cancel(); | |
| 78 _subscription = null; | |
| 79 } | |
| 80 } | |
| 81 | |
| 82 main(args) { | |
| 83 int port = 0; | |
| 84 if (args.length > 0) { | |
| 85 port = int.parse(args[0]); | |
| 86 } | |
| 87 RawReceivePort counter = new RawReceivePort(); | |
| 88 HttpListener listener = new EchoHttpListener(counter.sendPort); | |
| 89 ServerSocket | |
| 90 .bind(InternetAddress.ANY_IP_V6, port) | |
| 91 .then((ServerSocket socket) { | |
| 92 port = socket.port; | |
| 93 return Future.wait(new Iterable.generate(5, (_) => IsolateRunner.spawn()), | |
| 94 cleanUp: (isolate) { isolate.close(); }) | |
| 95 .then((List<IsolateRunner> isolates) { | |
| 96 return Future.wait(isolates.map((IsolateRunner isolate) { | |
| 97 return runHttpServer(isolate, socket, listener); | |
| 98 }), cleanUp: (server) { server.stop(); }); | |
| 99 }) | |
| 100 .then((stoppers) { | |
| 101 socket.close(); | |
| 102 int count = 25; | |
| 103 counter.handler = (_) { | |
| 104 count--; | |
| 105 if (count == 0) { | |
| 106 stoppers.forEach((f) => f()); | |
| 107 counter.close(); | |
| 108 } | |
| 109 }; | |
| 110 print("Server listening on port $port for 25 requests"); | |
| 111 print("Test with:"); | |
| 112 print(" ab -c10 -n 25 http://localhost:$port/"); | |
| 113 }); | |
| 114 }); | |
| 115 } | |
| OLD | NEW |