OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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("chat_server"); | 5 #library("chat_server"); |
6 #import("dart:io"); | 6 #import("dart:io"); |
7 #import("dart:isolate"); | 7 #import("dart:isolate"); |
8 #import("dart:json"); | 8 #import("dart:json"); |
9 | 9 |
| 10 void startChatServer() { |
| 11 var server = new ChatServer(); |
| 12 server.init(); |
| 13 port.receive(server.dispatch); |
| 14 } |
| 15 |
10 class ChatServer extends IsolatedServer { | 16 class ChatServer extends IsolatedServer { |
11 } | 17 } |
12 | 18 |
13 class ServerMain { | 19 class ServerMain { |
14 ServerMain.start(IsolatedServer server, | 20 ServerMain.start(SendPort serverPort, |
15 String hostAddress, | 21 String hostAddress, |
16 int tcpPort, | 22 int tcpPort, |
17 [int listenBacklog = 5]) | 23 [int listenBacklog = 5]) |
18 : _statusPort = new ReceivePort(), | 24 : _statusPort = new ReceivePort(), |
19 _serverPort = null { | 25 _serverPort = serverPort { |
20 server.spawn().then((SendPort port) { | |
21 _serverPort = port; | |
22 _start(hostAddress, tcpPort, listenBacklog); | |
23 }); | |
24 // We can only guess this is the right URL. At least it gives a | 26 // We can only guess this is the right URL. At least it gives a |
25 // hint to the user. | 27 // hint to the user. |
26 print('Server starting http://${hostAddress}:${tcpPort}/'); | 28 print('Server starting http://${hostAddress}:${tcpPort}/'); |
| 29 _start(hostAddress, tcpPort, listenBacklog); |
27 } | 30 } |
28 | 31 |
29 void _start(String hostAddress, int tcpPort, int listenBacklog) { | 32 void _start(String hostAddress, int tcpPort, int listenBacklog) { |
30 // Handle status messages from the server. | 33 // Handle status messages from the server. |
31 _statusPort.receive((var message, SendPort replyTo) { | 34 _statusPort.receive((var message, SendPort replyTo) { |
32 String status = message.message; | 35 String status = message.message; |
33 print("Received status: $status"); | 36 print("Received status: $status"); |
34 }); | 37 }); |
35 | 38 |
36 // Send server start message to the server. | 39 // Send server start message to the server. |
(...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
280 int get port() => _port; | 283 int get port() => _port; |
281 Dynamic get error() => _error; | 284 Dynamic get error() => _error; |
282 | 285 |
283 int _state; | 286 int _state; |
284 String _message; | 287 String _message; |
285 int _port; | 288 int _port; |
286 var _error; | 289 var _error; |
287 } | 290 } |
288 | 291 |
289 | 292 |
290 class IsolatedServer extends Isolate { | 293 class IsolatedServer { |
291 static final String redirectPageHtml = """ | 294 static final String redirectPageHtml = """ |
292 <html> | 295 <html> |
293 <head><title>Welcome to the dart server</title></head> | 296 <head><title>Welcome to the dart server</title></head> |
294 <body><h1>Redirecting to the front page...</h1></body> | 297 <body><h1>Redirecting to the front page...</h1></body> |
295 </html>"""; | 298 </html>"""; |
296 static final String notFoundPageHtml = """ | 299 static final String notFoundPageHtml = """ |
297 <html><head> | 300 <html><head> |
298 <title>404 Not Found</title> | 301 <title>404 Not Found</title> |
299 </head><body> | 302 </head><body> |
300 <h1>Not Found</h1> | 303 <h1>Not Found</h1> |
(...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
513 | 516 |
514 } else { | 517 } else { |
515 _protocolError(request, response); | 518 _protocolError(request, response); |
516 } | 519 } |
517 } else { | 520 } else { |
518 _protocolError(request, response); | 521 _protocolError(request, response); |
519 } | 522 } |
520 }; | 523 }; |
521 } | 524 } |
522 | 525 |
523 void main() { | 526 void init() { |
524 _logRequests = false; | 527 _logRequests = false; |
525 _topic = new Topic(); | 528 _topic = new Topic(); |
526 _serverStart = new Date.now(); | 529 _serverStart = new Date.now(); |
527 _messageCount = 0; | 530 _messageCount = 0; |
528 _messageRate = new Rate(); | 531 _messageRate = new Rate(); |
529 | 532 |
530 // Start a timer for cleanup events. | 533 // Start a timer for cleanup events. |
531 _cleanupTimer = | 534 _cleanupTimer = |
532 new Timer.repeating(10000, (timer) => _topic._handleTimer(timer)); | 535 new Timer.repeating(10000, (timer) => _topic._handleTimer(timer)); |
| 536 } |
533 | 537 |
534 // Start timer for periodic logging. | 538 // Start timer for periodic logging. |
535 void _handleLogging(Timer timer) { | 539 void _handleLogging(Timer timer) { |
536 if (_logging) { | 540 if (_logging) { |
537 print("${_messageRate.rate} messages/s " | 541 print("${_messageRate.rate} messages/s " |
538 "(total $_messageCount messages)"); | 542 "(total $_messageCount messages)"); |
| 543 } |
| 544 } |
| 545 |
| 546 void dispatch(message, replyTo) { |
| 547 if (message.isStart) { |
| 548 _host = message.host; |
| 549 _port = message.port; |
| 550 _logging = message.logging; |
| 551 replyTo.send(new ChatServerStatus.starting(), null); |
| 552 _server = new HttpServer(); |
| 553 _server.defaultRequestHandler = _notFoundHandler; |
| 554 _server.addRequestHandler( |
| 555 (request) => request.path == "/", |
| 556 (HttpRequest request, HttpResponse response) => |
| 557 redirectPageHandler( |
| 558 request, response, "dart_client/index.html")); |
| 559 _server.addRequestHandler( |
| 560 (request) => request.path == "/js_client/index.html", |
| 561 (HttpRequest request, HttpResponse response) => |
| 562 fileHandler(request, response)); |
| 563 _server.addRequestHandler( |
| 564 (request) => request.path == "/js_client/code.js", |
| 565 (HttpRequest request, HttpResponse response) => |
| 566 fileHandler(request, response)); |
| 567 _server.addRequestHandler( |
| 568 (request) => request.path == "/dart_client/index.html", |
| 569 (HttpRequest request, HttpResponse response) => |
| 570 fileHandler(request, response)); |
| 571 _server.addRequestHandler( |
| 572 (request) => request.path == "/out.js", |
| 573 (HttpRequest request, HttpResponse response) => |
| 574 fileHandler(request, response)); |
| 575 _server.addRequestHandler( |
| 576 (request) => request.path == "/favicon.ico", |
| 577 (HttpRequest request, HttpResponse response) => |
| 578 fileHandler(request, response, "static/favicon.ico")); |
| 579 |
| 580 _server.addRequestHandler( |
| 581 (request) => request.path == "/join", _joinHandler); |
| 582 _server.addRequestHandler( |
| 583 (request) => request.path == "/leave", _leaveHandler); |
| 584 _server.addRequestHandler( |
| 585 (request) => request.path == "/message", _messageHandler); |
| 586 _server.addRequestHandler( |
| 587 (request) => request.path == "/receive", _receiveHandler); |
| 588 try { |
| 589 _server.listen(_host, _port, backlog: message.backlog); |
| 590 replyTo.send(new ChatServerStatus.started(_server.port), null); |
| 591 _loggingTimer = new Timer.repeating(1000, _handleLogging); |
| 592 } catch (var e) { |
| 593 replyTo.send(new ChatServerStatus.error(e.toString()), null); |
539 } | 594 } |
| 595 } else if (message.isStop) { |
| 596 replyTo.send(new ChatServerStatus.stopping(), null); |
| 597 stop(); |
| 598 replyTo.send(new ChatServerStatus.stopped(), null); |
540 } | 599 } |
541 | |
542 this.port.receive((var message, SendPort replyTo) { | |
543 if (message.isStart) { | |
544 _host = message.host; | |
545 _port = message.port; | |
546 _logging = message.logging; | |
547 replyTo.send(new ChatServerStatus.starting(), null); | |
548 _server = new HttpServer(); | |
549 _server.defaultRequestHandler = _notFoundHandler; | |
550 _server.addRequestHandler( | |
551 (request) => request.path == "/", | |
552 (HttpRequest request, HttpResponse response) => | |
553 redirectPageHandler( | |
554 request, response, "dart_client/index.html")); | |
555 _server.addRequestHandler( | |
556 (request) => request.path == "/js_client/index.html", | |
557 (HttpRequest request, HttpResponse response) => | |
558 fileHandler(request, response)); | |
559 _server.addRequestHandler( | |
560 (request) => request.path == "/js_client/code.js", | |
561 (HttpRequest request, HttpResponse response) => | |
562 fileHandler(request, response)); | |
563 _server.addRequestHandler( | |
564 (request) => request.path == "/dart_client/index.html", | |
565 (HttpRequest request, HttpResponse response) => | |
566 fileHandler(request, response)); | |
567 _server.addRequestHandler( | |
568 (request) => request.path == "/out.js", | |
569 (HttpRequest request, HttpResponse response) => | |
570 fileHandler(request, response)); | |
571 _server.addRequestHandler( | |
572 (request) => request.path == "/favicon.ico", | |
573 (HttpRequest request, HttpResponse response) => | |
574 fileHandler(request, response, "static/favicon.ico")); | |
575 | |
576 _server.addRequestHandler( | |
577 (request) => request.path == "/join", _joinHandler); | |
578 _server.addRequestHandler( | |
579 (request) => request.path == "/leave", _leaveHandler); | |
580 _server.addRequestHandler( | |
581 (request) => request.path == "/message", _messageHandler); | |
582 _server.addRequestHandler( | |
583 (request) => request.path == "/receive", _receiveHandler); | |
584 try { | |
585 _server.listen(_host, _port, backlog: message.backlog); | |
586 replyTo.send(new ChatServerStatus.started(_server.port), null); | |
587 _loggingTimer = new Timer.repeating(1000, _handleLogging); | |
588 } catch (var e) { | |
589 replyTo.send(new ChatServerStatus.error(e.toString()), null); | |
590 } | |
591 } else if (message.isStop) { | |
592 replyTo.send(new ChatServerStatus.stopping(), null); | |
593 stop(); | |
594 replyTo.send(new ChatServerStatus.stopped(), null); | |
595 } | |
596 }); | |
597 } | 600 } |
598 | 601 |
599 stop() { | 602 stop() { |
600 _server.close(); | 603 _server.close(); |
601 _cleanupTimer.cancel(); | 604 _cleanupTimer.cancel(); |
602 this.port.close(); | 605 port.close(); |
603 } | 606 } |
604 | 607 |
605 String _host; | 608 String _host; |
606 int _port; | 609 int _port; |
607 HttpServer _server; // HTTP server instance. | 610 HttpServer _server; // HTTP server instance. |
608 bool _logRequests; | 611 bool _logRequests; |
609 | 612 |
610 Topic _topic; | 613 Topic _topic; |
611 Timer _cleanupTimer; | 614 Timer _cleanupTimer; |
612 Timer _loggingTimer; | 615 Timer _loggingTimer; |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
679 } | 682 } |
680 } | 683 } |
681 | 684 |
682 int _timeRange; | 685 int _timeRange; |
683 List<int> _buckets; | 686 List<int> _buckets; |
684 int _currentBucket; | 687 int _currentBucket; |
685 int _currentBucketTime; | 688 int _currentBucketTime; |
686 num _bucketTimeRange; | 689 num _bucketTimeRange; |
687 int _sum; | 690 int _sum; |
688 } | 691 } |
OLD | NEW |