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:async'; | 6 import 'dart:async'; |
7 import 'dart:io'; | 7 import 'dart:io'; |
8 import 'dart:isolate'; | 8 import 'dart:isolate'; |
9 import 'dart:json' as json; | 9 import 'dart:json' as json; |
10 import 'dart:math'; | 10 import 'dart:math'; |
(...skipping 516 matching lines...) Loading... |
527 } | 527 } |
528 | 528 |
529 void init() { | 529 void init() { |
530 _logRequests = false; | 530 _logRequests = false; |
531 _topic = new Topic(); | 531 _topic = new Topic(); |
532 _serverStart = new DateTime.now(); | 532 _serverStart = new DateTime.now(); |
533 _messageCount = 0; | 533 _messageCount = 0; |
534 _messageRate = new Rate(); | 534 _messageRate = new Rate(); |
535 | 535 |
536 // Start a timer for cleanup events. | 536 // Start a timer for cleanup events. |
537 _cleanupTimer = | 537 _cleanupTimer = new Timer.repeating(const Duration(seconds: 10), |
538 new Timer.repeating(10000, (timer) => _topic._handleTimer(timer)); | 538 _topic._handleTimer); |
539 } | 539 } |
540 | 540 |
541 // Start timer for periodic logging. | 541 // Start timer for periodic logging. |
542 void _handleLogging(Timer timer) { | 542 void _handleLogging(Timer timer) { |
543 if (_logging) { | 543 if (_logging) { |
544 print("${_messageRate.rate} messages/s " | 544 print("${_messageRate.rate} messages/s " |
545 "(total $_messageCount messages)"); | 545 "(total $_messageCount messages)"); |
546 } | 546 } |
547 } | 547 } |
548 | 548 |
(...skipping 35 matching lines...) Loading... |
584 (request) => request.path == "/join", _joinHandler); | 584 (request) => request.path == "/join", _joinHandler); |
585 _server.addRequestHandler( | 585 _server.addRequestHandler( |
586 (request) => request.path == "/leave", _leaveHandler); | 586 (request) => request.path == "/leave", _leaveHandler); |
587 _server.addRequestHandler( | 587 _server.addRequestHandler( |
588 (request) => request.path == "/message", _messageHandler); | 588 (request) => request.path == "/message", _messageHandler); |
589 _server.addRequestHandler( | 589 _server.addRequestHandler( |
590 (request) => request.path == "/receive", _receiveHandler); | 590 (request) => request.path == "/receive", _receiveHandler); |
591 try { | 591 try { |
592 _server.listen(_host, _port, backlog: message.backlog); | 592 _server.listen(_host, _port, backlog: message.backlog); |
593 replyTo.send(new ChatServerStatus.started(_server.port), null); | 593 replyTo.send(new ChatServerStatus.started(_server.port), null); |
594 _loggingTimer = new Timer.repeating(1000, _handleLogging); | 594 _loggingTimer = |
| 595 new Timer.repeating(const Duration(seconds: 1), _handleLogging); |
595 } catch (e) { | 596 } catch (e) { |
596 replyTo.send(new ChatServerStatus.error2(e.toString()), null); | 597 replyTo.send(new ChatServerStatus.error2(e.toString()), null); |
597 } | 598 } |
598 } else if (message.isStop) { | 599 } else if (message.isStop) { |
599 replyTo.send(new ChatServerStatus.stopping(), null); | 600 replyTo.send(new ChatServerStatus.stopping(), null); |
600 stop(); | 601 stop(); |
601 replyTo.send(new ChatServerStatus.stopped(), null); | 602 replyTo.send(new ChatServerStatus.stopped(), null); |
602 } | 603 } |
603 } | 604 } |
604 | 605 |
(...skipping 80 matching lines...) Loading... |
685 } | 686 } |
686 } | 687 } |
687 | 688 |
688 int _timeRange; | 689 int _timeRange; |
689 List<int> _buckets; | 690 List<int> _buckets; |
690 int _currentBucket; | 691 int _currentBucket; |
691 int _currentBucketTime; | 692 int _currentBucketTime; |
692 num _bucketTimeRange; | 693 num _bucketTimeRange; |
693 int _sum; | 694 int _sum; |
694 } | 695 } |
OLD | NEW |