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

Unified Diff: samples/chat/chat_server_lib.dart

Issue 27215002: Very simple version of Isolates. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Address comments. Created 7 years, 2 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 | « samples/chat/chat_server.dart ('k') | samples/chat/chat_stress_client.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: samples/chat/chat_server_lib.dart
diff --git a/samples/chat/chat_server_lib.dart b/samples/chat/chat_server_lib.dart
index 86f720b11b8185e0953bdbbb314d8d954d0d42e5..f3fa26d63af82028d35b266501e453540fb5fbc6 100644
--- a/samples/chat/chat_server_lib.dart
+++ b/samples/chat/chat_server_lib.dart
@@ -6,13 +6,20 @@ library chat_server;
import 'dart:async';
import 'dart:io';
import 'dart:isolate';
-import "dart:convert";
+import 'dart:convert';
import 'dart:math';
-void startChatServer() {
+Future<SendPort> spawnChatServer() {
+ var response = new ReceivePort();
+ return Isolate.spawn(startChatServer, response.sendPort)
+ .then((remoteIsolate) => response.first)
+ .whenComplete(() { response.close(); }); // Make sure the port is closed.
+}
+
+void startChatServer(SendPort replyTo) {
var server = new ChatServer();
server.init();
- port.receive(server.dispatch);
+ replyTo.send(server.dispatchSendPort);
}
class ChatServer extends IsolatedServer {
@@ -32,19 +39,19 @@ class ServerMain {
void _start(String hostAddress, int tcpPort) {
// Handle status messages from the server.
- _statusPort.receive((var message, SendPort replyTo) {
+ _statusPort.listen((var message) {
String status = message.message;
print("Received status: $status");
});
// Send server start message to the server.
var command = new ChatServerCommand.start(hostAddress, tcpPort);
- _serverPort.send(command, _statusPort.toSendPort());
+ _serverPort.send([command, _statusPort.sendPort]);
}
void shutdown() {
// Send server stop message to the server.
- _serverPort.send(new ChatServerCommand.stop(), _statusPort.toSendPort());
+ _serverPort.send([new ChatServerCommand.stop(), _statusPort.sendPort]);
_statusPort.close();
}
@@ -301,6 +308,10 @@ class IsolatedServer {
<p>The requested URL was not found on this server.</p>
</body></html>""";
+ IsolatedServer() : _dispatchReceivePort = new ReceivePort() {
+ _dispatchReceivePort.listen(dispatch);
+ }
+
void _sendJSONResponse(HttpResponse response, Map responseData) {
response.headers.set("Content-Type", "application/json; charset=UTF-8");
response.write(JSON.encode(responseData));
@@ -540,12 +551,16 @@ class IsolatedServer {
}
}
- void dispatch(message, replyTo) {
- if (message.isStart) {
- _host = message.host;
- _port = message.port;
- _logging = message.logging;
- replyTo.send(new ChatServerStatus.starting(), null);
+ SendPort get dispatchSendPort => _dispatchReceivePort.sendPort;
+
+ void dispatch(message) {
+ var command = message[0];
+ var replyTo = message[1];
+ if (command.isStart) {
+ _host = command.host;
+ _port = command.port;
+ _logging = command.logging;
+ replyTo.send(new ChatServerStatus.starting());
var handlers = {};
void addRequestHandler(String path, Function handler) {
handlers[path] = handler;
@@ -574,27 +589,28 @@ class IsolatedServer {
_notFoundHandler(request, request.response);
}
});
- replyTo.send(new ChatServerStatus.started(_server.port), null);
+ replyTo.send(new ChatServerStatus.started(_server.port));
_loggingTimer =
new Timer.periodic(const Duration(seconds: 1), _handleLogging);
})
.catchError((e) {
- replyTo.send(new ChatServerStatus.error2(e.toString()), null);
+ replyTo.send(new ChatServerStatus.error2(e.toString()));
});
- } else if (message.isStop) {
- replyTo.send(new ChatServerStatus.stopping(), null);
+ } else if (command.isStop) {
+ replyTo.send(new ChatServerStatus.stopping());
stop();
- replyTo.send(new ChatServerStatus.stopped(), null);
+ replyTo.send(new ChatServerStatus.stopped());
}
}
stop() {
_server.close();
_cleanupTimer.cancel();
- port.close();
+ _dispatchReceivePort.close();
}
String _host;
+ ReceivePort _dispatchReceivePort;
int _port;
HttpServer _server; // HTTP server instance.
bool _logRequests;
« no previous file with comments | « samples/chat/chat_server.dart ('k') | samples/chat/chat_stress_client.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698