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

Side by Side Diff: samples/chat/chat_server_lib.dart

Issue 11783009: Big merge from experimental to bleeding edge. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 11 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « samples/calculator/tape.dart ('k') | samples/chat/chat_stress_client.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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:io'; 7 import 'dart:io';
7 import 'dart:isolate'; 8 import 'dart:isolate';
8 import 'dart:json'; 9 import 'dart:json' as json;
9 import 'dart:math'; 10 import 'dart:math';
10 11
11 void startChatServer() { 12 void startChatServer() {
12 var server = new ChatServer(); 13 var server = new ChatServer();
13 server.init(); 14 server.init();
14 port.receive(server.dispatch); 15 port.receive(server.dispatch);
15 } 16 }
16 17
17 class ChatServer extends IsolatedServer { 18 class ChatServer extends IsolatedServer {
18 } 19 }
(...skipping 282 matching lines...) Expand 10 before | Expand all | Expand 10 after
301 static const String notFoundPageHtml = """ 302 static const String notFoundPageHtml = """
302 <html><head> 303 <html><head>
303 <title>404 Not Found</title> 304 <title>404 Not Found</title>
304 </head><body> 305 </head><body>
305 <h1>Not Found</h1> 306 <h1>Not Found</h1>
306 <p>The requested URL was not found on this server.</p> 307 <p>The requested URL was not found on this server.</p>
307 </body></html>"""; 308 </body></html>""";
308 309
309 void _sendJSONResponse(HttpResponse response, Map responseData) { 310 void _sendJSONResponse(HttpResponse response, Map responseData) {
310 response.headers.set("Content-Type", "application/json; charset=UTF-8"); 311 response.headers.set("Content-Type", "application/json; charset=UTF-8");
311 response.outputStream.writeString(JSON.stringify(responseData)); 312 response.outputStream.writeString(json.stringify(responseData));
312 response.outputStream.close(); 313 response.outputStream.close();
313 } 314 }
314 315
315 void redirectPageHandler(HttpRequest request, 316 void redirectPageHandler(HttpRequest request,
316 HttpResponse response, 317 HttpResponse response,
317 String redirectPath) { 318 String redirectPath) {
318 if (_redirectPage == null) { 319 if (_redirectPage == null) {
319 _redirectPage = redirectPageHtml.charCodes; 320 _redirectPage = redirectPageHtml.charCodes;
320 } 321 }
321 response.statusCode = HttpStatus.FOUND; 322 response.statusCode = HttpStatus.FOUND;
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
379 // Join request: 380 // Join request:
380 // { "request": "join", 381 // { "request": "join",
381 // "handle": <handle> } 382 // "handle": <handle> }
382 void _joinHandler(HttpRequest request, HttpResponse response) { 383 void _joinHandler(HttpRequest request, HttpResponse response) {
383 StringBuffer body = new StringBuffer(); 384 StringBuffer body = new StringBuffer();
384 StringInputStream input = new StringInputStream(request.inputStream); 385 StringInputStream input = new StringInputStream(request.inputStream);
385 input.onData = () => body.add(input.read()); 386 input.onData = () => body.add(input.read());
386 input.onClosed = () { 387 input.onClosed = () {
387 String data = body.toString(); 388 String data = body.toString();
388 if (data != null) { 389 if (data != null) {
389 var requestData = JSON.parse(data); 390 var requestData = json.parse(data);
390 if (requestData["request"] == "join") { 391 if (requestData["request"] == "join") {
391 String handle = requestData["handle"]; 392 String handle = requestData["handle"];
392 if (handle != null) { 393 if (handle != null) {
393 // New user joining. 394 // New user joining.
394 User user = _topic._userJoined(handle); 395 User user = _topic._userJoined(handle);
395 396
396 // Send response. 397 // Send response.
397 Map responseData = new Map(); 398 Map responseData = new Map();
398 responseData["response"] = "join"; 399 responseData["response"] = "join";
399 responseData["sessionId"] = user.sessionId; 400 responseData["sessionId"] = user.sessionId;
(...skipping 12 matching lines...) Expand all
412 413
413 // Leave request: 414 // Leave request:
414 // { "request": "leave", 415 // { "request": "leave",
415 // "sessionId": <sessionId> } 416 // "sessionId": <sessionId> }
416 void _leaveHandler(HttpRequest request, HttpResponse response) { 417 void _leaveHandler(HttpRequest request, HttpResponse response) {
417 StringBuffer body = new StringBuffer(); 418 StringBuffer body = new StringBuffer();
418 StringInputStream input = new StringInputStream(request.inputStream); 419 StringInputStream input = new StringInputStream(request.inputStream);
419 input.onData = () => body.add(input.read()); 420 input.onData = () => body.add(input.read());
420 input.onClosed = () { 421 input.onClosed = () {
421 String data = body.toString(); 422 String data = body.toString();
422 var requestData = JSON.parse(data); 423 var requestData = json.parse(data);
423 if (requestData["request"] == "leave") { 424 if (requestData["request"] == "leave") {
424 String sessionId = requestData["sessionId"]; 425 String sessionId = requestData["sessionId"];
425 if (sessionId != null) { 426 if (sessionId != null) {
426 // User leaving. 427 // User leaving.
427 _topic._userLeft(sessionId); 428 _topic._userLeft(sessionId);
428 429
429 // Send response. 430 // Send response.
430 Map responseData = new Map(); 431 Map responseData = new Map();
431 responseData["response"] = "leave"; 432 responseData["response"] = "leave";
432 _sendJSONResponse(response, responseData); 433 _sendJSONResponse(response, responseData);
(...skipping 11 matching lines...) Expand all
444 // "sessionId": <sessionId>, 445 // "sessionId": <sessionId>,
445 // "message": <message> } 446 // "message": <message> }
446 void _messageHandler(HttpRequest request, HttpResponse response) { 447 void _messageHandler(HttpRequest request, HttpResponse response) {
447 StringBuffer body = new StringBuffer(); 448 StringBuffer body = new StringBuffer();
448 StringInputStream input = new StringInputStream(request.inputStream); 449 StringInputStream input = new StringInputStream(request.inputStream);
449 input.onData = () => body.add(input.read()); 450 input.onData = () => body.add(input.read());
450 input.onClosed = () { 451 input.onClosed = () {
451 String data = body.toString(); 452 String data = body.toString();
452 _messageCount++; 453 _messageCount++;
453 _messageRate.record(1); 454 _messageRate.record(1);
454 var requestData = JSON.parse(data); 455 var requestData = json.parse(data);
455 if (requestData["request"] == "message") { 456 if (requestData["request"] == "message") {
456 String sessionId = requestData["sessionId"]; 457 String sessionId = requestData["sessionId"];
457 if (sessionId != null) { 458 if (sessionId != null) {
458 // New message from user. 459 // New message from user.
459 bool success = _topic._userMessage(requestData); 460 bool success = _topic._userMessage(requestData);
460 461
461 // Send response. 462 // Send response.
462 if (success) { 463 if (success) {
463 Map responseData = new Map(); 464 Map responseData = new Map();
464 responseData["response"] = "message"; 465 responseData["response"] = "message";
(...skipping 14 matching lines...) Expand all
479 // { "request": "receive", 480 // { "request": "receive",
480 // "sessionId": <sessionId>, 481 // "sessionId": <sessionId>,
481 // "nextMessage": <nextMessage>, 482 // "nextMessage": <nextMessage>,
482 // "maxMessages": <maxMesssages> } 483 // "maxMessages": <maxMesssages> }
483 void _receiveHandler(HttpRequest request, HttpResponse response) { 484 void _receiveHandler(HttpRequest request, HttpResponse response) {
484 StringBuffer body = new StringBuffer(); 485 StringBuffer body = new StringBuffer();
485 StringInputStream input = new StringInputStream(request.inputStream); 486 StringInputStream input = new StringInputStream(request.inputStream);
486 input.onData = () => body.add(input.read()); 487 input.onData = () => body.add(input.read());
487 input.onClosed = () { 488 input.onClosed = () {
488 String data = body.toString(); 489 String data = body.toString();
489 var requestData = JSON.parse(data); 490 var requestData = json.parse(data);
490 if (requestData["request"] == "receive") { 491 if (requestData["request"] == "receive") {
491 String sessionId = requestData["sessionId"]; 492 String sessionId = requestData["sessionId"];
492 int nextMessage = requestData["nextMessage"]; 493 int nextMessage = requestData["nextMessage"];
493 int maxMessages = requestData["maxMessages"]; 494 int maxMessages = requestData["maxMessages"];
494 if (sessionId != null && nextMessage != null) { 495 if (sessionId != null && nextMessage != null) {
495 496
496 void sendResponse(messages) { 497 void sendResponse(messages) {
497 // Send response. 498 // Send response.
498 Map responseData = new Map(); 499 Map responseData = new Map();
499 responseData["response"] = "receive"; 500 responseData["response"] = "receive";
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after
631 // range is split over a number of buckets where each bucket collects 632 // range is split over a number of buckets where each bucket collects
632 // the number of events happening in that time sub-range. The first 633 // the number of events happening in that time sub-range. The first
633 // constructor arument specifies the time range in milliseconds. The 634 // constructor arument specifies the time range in milliseconds. The
634 // buckets are in the list _buckets organized at a circular buffer 635 // buckets are in the list _buckets organized at a circular buffer
635 // with _currentBucket marking the bucket where an event was last 636 // with _currentBucket marking the bucket where an event was last
636 // recorded. A current sum of the content of all buckets except the 637 // recorded. A current sum of the content of all buckets except the
637 // one pointed a by _currentBucket is kept in _sum. 638 // one pointed a by _currentBucket is kept in _sum.
638 class Rate { 639 class Rate {
639 Rate([int timeRange = 1000, int buckets = 10]) 640 Rate([int timeRange = 1000, int buckets = 10])
640 : _timeRange = timeRange, 641 : _timeRange = timeRange,
641 _buckets = new List(buckets + 1), // Current bucket is not in the sum. 642 _buckets = new List.fixedLength(buckets + 1), // Current bucket is not in the sum.
642 _currentBucket = 0, 643 _currentBucket = 0,
643 _currentBucketTime = new Date.now().millisecondsSinceEpoch, 644 _currentBucketTime = new Date.now().millisecondsSinceEpoch,
644 _sum = 0 { 645 _sum = 0 {
645 _bucketTimeRange = (_timeRange / buckets).toInt(); 646 _bucketTimeRange = (_timeRange / buckets).toInt();
646 for (int i = 0; i < _buckets.length; i++) { 647 for (int i = 0; i < _buckets.length; i++) {
647 _buckets[i] = 0; 648 _buckets[i] = 0;
648 } 649 }
649 } 650 }
650 651
651 // Record the specified number of events. 652 // Record the specified number of events.
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
684 } 685 }
685 } 686 }
686 687
687 int _timeRange; 688 int _timeRange;
688 List<int> _buckets; 689 List<int> _buckets;
689 int _currentBucket; 690 int _currentBucket;
690 int _currentBucketTime; 691 int _currentBucketTime;
691 num _bucketTimeRange; 692 num _bucketTimeRange;
692 int _sum; 693 int _sum;
693 } 694 }
OLDNEW
« no previous file with comments | « samples/calculator/tape.dart ('k') | samples/chat/chat_stress_client.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698