| 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 typedef void RequestHandler(HttpRequest request, HttpResponse response); | 10 typedef void RequestHandler(HttpRequest request, HttpResponse response); |
| (...skipping 548 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 559 | 559 |
| 560 void main() { | 560 void main() { |
| 561 _logRequests = false; | 561 _logRequests = false; |
| 562 _topic = new Topic(); | 562 _topic = new Topic(); |
| 563 _serverStart = new Date.now(); | 563 _serverStart = new Date.now(); |
| 564 _messageCount = 0; | 564 _messageCount = 0; |
| 565 _messageRate = new Rate(); | 565 _messageRate = new Rate(); |
| 566 | 566 |
| 567 // Start a timer for cleanup events. | 567 // Start a timer for cleanup events. |
| 568 _cleanupTimer = | 568 _cleanupTimer = |
| 569 new Timer.repeating((timer) => _topic._handleTimer(timer), 10000); | 569 new Timer.repeating(10000, (timer) => _topic._handleTimer(timer)); |
| 570 | 570 |
| 571 // Start timer for periodic logging. | 571 // Start timer for periodic logging. |
| 572 void _handleLogging(Timer timer) { | 572 void _handleLogging(Timer timer) { |
| 573 if (_logging) { | 573 if (_logging) { |
| 574 print((_messageRate.rate).toString() + | 574 print((_messageRate.rate).toString() + |
| 575 " messages/s (total " + | 575 " messages/s (total " + |
| 576 _messageCount + | 576 _messageCount + |
| 577 " messages)"); | 577 " messages)"); |
| 578 } | 578 } |
| 579 } | 579 } |
| 580 | 580 |
| 581 this.port.receive((var message, SendPort replyTo) { | 581 this.port.receive((var message, SendPort replyTo) { |
| 582 if (message.isStart) { | 582 if (message.isStart) { |
| 583 _host = message.host; | 583 _host = message.host; |
| 584 _port = message.port; | 584 _port = message.port; |
| 585 _logging = message.logging; | 585 _logging = message.logging; |
| 586 replyTo.send(new ChatServerStatus.starting(), null); | 586 replyTo.send(new ChatServerStatus.starting(), null); |
| 587 _server = new HttpServer(); | 587 _server = new HttpServer(); |
| 588 try { | 588 try { |
| 589 _server.listen(_host, _port, backlog: message.backlog); | 589 _server.listen(_host, _port, backlog: message.backlog); |
| 590 _server.onRequest = (HttpRequest req, HttpResponse rsp) => | 590 _server.onRequest = (HttpRequest req, HttpResponse rsp) => |
| 591 _requestReceivedHandler(req, rsp); | 591 _requestReceivedHandler(req, rsp); |
| 592 replyTo.send(new ChatServerStatus.started(_server.port), null); | 592 replyTo.send(new ChatServerStatus.started(_server.port), null); |
| 593 _loggingTimer = new Timer.repeating(_handleLogging, 1000); | 593 _loggingTimer = new Timer.repeating(1000, _handleLogging); |
| 594 } catch (var e) { | 594 } catch (var e) { |
| 595 replyTo.send(new ChatServerStatus.error(e.toString()), null); | 595 replyTo.send(new ChatServerStatus.error(e.toString()), null); |
| 596 } | 596 } |
| 597 } else if (message.isStop) { | 597 } else if (message.isStop) { |
| 598 replyTo.send(new ChatServerStatus.stopping(), null); | 598 replyTo.send(new ChatServerStatus.stopping(), null); |
| 599 stop(); | 599 stop(); |
| 600 replyTo.send(new ChatServerStatus.stopped(), null); | 600 replyTo.send(new ChatServerStatus.stopped(), null); |
| 601 } | 601 } |
| 602 }); | 602 }); |
| 603 } | 603 } |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 709 } | 709 } |
| 710 } | 710 } |
| 711 | 711 |
| 712 int _timeRange; | 712 int _timeRange; |
| 713 List<int> _buckets; | 713 List<int> _buckets; |
| 714 int _currentBucket; | 714 int _currentBucket; |
| 715 int _currentBucketTime; | 715 int _currentBucketTime; |
| 716 num _bucketTimeRange; | 716 num _bucketTimeRange; |
| 717 int _sum; | 717 int _sum; |
| 718 } | 718 } |
| OLD | NEW |