OLD | NEW |
---|---|
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 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 | 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 dart.pkg.isolate.sample.httpserver; | 5 library dart.pkg.isolate.sample.httpserver; |
6 | 6 |
7 import "dart:io"; | 7 import "dart:io"; |
8 import "dart:async"; | 8 import "dart:async"; |
9 import "dart:isolate"; | 9 import "dart:isolate"; |
10 import "package:isolate/isolaterunner.dart"; | 10 import "package:isolate/isolaterunner.dart"; |
11 import "package:isolate/runner.dart"; | 11 import "package:isolate/runner.dart"; |
12 import "package:isolate/ports.dart"; | 12 import "package:isolate/ports.dart"; |
13 | 13 |
14 typedef Future RemoteStop(); | 14 typedef Future RemoteStop(); |
15 | 15 |
16 Future<RemoteStop> runHttpServer( | 16 Future<RemoteStop> runHttpServer( |
17 Runner runner, ServerSocket socket, HttpListener listener) { | 17 Runner runner, ServerSocket socket, HttpListener listener) { |
18 return runner.run(_startHttpServer, new List(2)..[0] = socket.reference | 18 return runner.run(_startHttpServer, new List(2) |
19 ..[1] = listener) | 19 ..[0] = socket.reference |
20 .then((SendPort stopPort) => () => _sendStop(stopPort)); | 20 ..[1] = listener).then((SendPort stopPort) => () => _sendStop(stopPort)); |
Lasse Reichstein Nielsen
2015/02/26 10:59:13
No to this change. It is not more readable.
| |
21 } | 21 } |
22 | 22 |
23 Future _sendStop(SendPort stopPort) { | 23 Future _sendStop(SendPort stopPort) { |
24 return singleResponseFuture(stopPort.send); | 24 return singleResponseFuture(stopPort.send); |
25 } | 25 } |
26 | 26 |
27 Future<SendPort> _startHttpServer(List args) { | 27 Future<SendPort> _startHttpServer(List args) { |
28 ServerSocketReference ref = args[0]; | 28 ServerSocketReference ref = args[0]; |
29 HttpListener listener = args[1]; | 29 HttpListener listener = args[1]; |
30 return ref.create().then((socket) { | 30 return ref.create().then((socket) { |
(...skipping 30 matching lines...) Expand all Loading... | |
61 | 61 |
62 EchoHttpListener(this._counter); | 62 EchoHttpListener(this._counter); |
63 | 63 |
64 start(HttpServer server) { | 64 start(HttpServer server) { |
65 print("Starting isolate $_id"); | 65 print("Starting isolate $_id"); |
66 _subscription = server.listen((HttpRequest request) { | 66 _subscription = server.listen((HttpRequest request) { |
67 request.response.addStream(request).then((_) { | 67 request.response.addStream(request).then((_) { |
68 _counter.send(null); | 68 _counter.send(null); |
69 print("Request to $_id"); | 69 print("Request to $_id"); |
70 request.response.write("#$_id\n"); | 70 request.response.write("#$_id\n"); |
71 var t0 = new DateTime.now().add(new Duration(seconds:2)); | 71 var t0 = new DateTime.now().add(new Duration(seconds: 2)); |
72 while (new DateTime.now().isBefore(t0)); | 72 while (new DateTime.now().isBefore(t0)); |
73 print("Response from $_id"); | 73 print("Response from $_id"); |
74 request.response.close(); | 74 request.response.close(); |
75 }); | 75 }); |
76 }); | 76 }); |
77 } | 77 } |
78 | 78 |
79 stop() { | 79 stop() { |
80 print("Stopping isolate $_id"); | 80 print("Stopping isolate $_id"); |
81 _subscription.cancel(); | 81 _subscription.cancel(); |
82 _subscription = null; | 82 _subscription = null; |
83 } | 83 } |
84 } | 84 } |
85 | 85 |
86 main(args) { | 86 main(args) { |
87 int port = 0; | 87 int port = 0; |
88 if (args.length > 0) { | 88 if (args.length > 0) { |
89 port = int.parse(args[0]); | 89 port = int.parse(args[0]); |
90 } | 90 } |
91 RawReceivePort counter = new RawReceivePort(); | 91 RawReceivePort counter = new RawReceivePort(); |
92 HttpListener listener = new EchoHttpListener(counter.sendPort); | 92 HttpListener listener = new EchoHttpListener(counter.sendPort); |
93 ServerSocket | 93 ServerSocket.bind(InternetAddress.ANY_IP_V6, port).then( |
94 .bind(InternetAddress.ANY_IP_V6, port) | 94 (ServerSocket socket) { |
95 .then((ServerSocket socket) { | 95 port = socket.port; |
96 port = socket.port; | 96 return Future |
97 return Future.wait(new Iterable.generate(5, (_) => IsolateRunner.spawn()), | 97 .wait(new Iterable.generate(5, (_) => IsolateRunner.spawn()), |
98 cleanUp: (isolate) { isolate.close(); }) | 98 cleanUp: (isolate) { |
99 .then((List<IsolateRunner> isolates) { | 99 isolate.close(); |
100 return Future.wait(isolates.map((IsolateRunner isolate) { | 100 }).then((List<IsolateRunner> isolates) { |
101 return runHttpServer(isolate, socket, listener); | 101 return Future.wait(isolates.map((IsolateRunner isolate) { |
102 }), cleanUp: (server) { server.stop(); }); | 102 return runHttpServer(isolate, socket, listener); |
103 }) | 103 }), cleanUp: (server) { |
104 .then((stoppers) { | 104 server.stop(); |
105 socket.close(); | 105 }); |
106 int count = 25; | 106 }).then((stoppers) { |
107 counter.handler = (_) { | 107 socket.close(); |
108 count--; | 108 int count = 25; |
109 if (count == 0) { | 109 counter.handler = (_) { |
110 stoppers.forEach((f) => f()); | 110 count--; |
111 counter.close(); | 111 if (count == 0) { |
112 } | 112 stoppers.forEach((f) => f()); |
113 }; | 113 counter.close(); |
114 print("Server listening on port $port for 25 requests"); | 114 } |
115 print("Test with:"); | 115 }; |
116 print(" ab -c10 -n 25 http://localhost:$port/"); | 116 print("Server listening on port $port for 25 requests"); |
117 }); | 117 print("Test with:"); |
118 print(" ab -c10 -n 25 http://localhost:$port/"); | |
119 }); | |
Lasse Reichstein Nielsen
2015/02/26 10:59:13
Also no to this change.
Putting the ".then" after
| |
118 }); | 120 }); |
119 } | 121 } |
OLD | NEW |