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

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

Issue 12504006: Make IOSink implement StringSink (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Addressed second round of review comments Created 7 years, 9 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 | « runtime/bin/socket_patch.dart ('k') | samples/tests/samples/chat/chat_server_test.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) 2013, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2013, 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 285 matching lines...) Expand 10 before | Expand all | Expand 10 after
296 static const String notFoundPageHtml = """ 296 static const String notFoundPageHtml = """
297 <html><head> 297 <html><head>
298 <title>404 Not Found</title> 298 <title>404 Not Found</title>
299 </head><body> 299 </head><body>
300 <h1>Not Found</h1> 300 <h1>Not Found</h1>
301 <p>The requested URL was not found on this server.</p> 301 <p>The requested URL was not found on this server.</p>
302 </body></html>"""; 302 </body></html>""";
303 303
304 void _sendJSONResponse(HttpResponse response, Map responseData) { 304 void _sendJSONResponse(HttpResponse response, Map responseData) {
305 response.headers.set("Content-Type", "application/json; charset=UTF-8"); 305 response.headers.set("Content-Type", "application/json; charset=UTF-8");
306 response.addString(json.stringify(responseData)); 306 response.write(json.stringify(responseData));
307 response.close(); 307 response.close();
308 } 308 }
309 309
310 void redirectPageHandler(HttpRequest request, 310 void redirectPageHandler(HttpRequest request,
311 HttpResponse response, 311 HttpResponse response,
312 String redirectPath) { 312 String redirectPath) {
313 if (_redirectPage == null) { 313 if (_redirectPage == null) {
314 _redirectPage = redirectPageHtml.codeUnits; 314 _redirectPage = redirectPageHtml.codeUnits;
315 } 315 }
316 response.statusCode = HttpStatus.FOUND; 316 response.statusCode = HttpStatus.FOUND;
317 response.headers.set( 317 response.headers.set(
318 "Location", "http://$_host:$_port/${redirectPath}"); 318 "Location", "http://$_host:$_port/${redirectPath}");
319 response.contentLength = _redirectPage.length; 319 response.contentLength = _redirectPage.length;
320 response.add(_redirectPage); 320 print(_redirectPage.length);
321 response.writeBytes(_redirectPage);
321 response.close(); 322 response.close();
322 } 323 }
323 324
324 // Serve the content of a file. 325 // Serve the content of a file.
325 void fileHandler( 326 void fileHandler(
326 HttpRequest request, HttpResponse response, [String fileName = null]) { 327 HttpRequest request, HttpResponse response, [String fileName = null]) {
327 final int BUFFER_SIZE = 4096; 328 final int BUFFER_SIZE = 4096;
328 if (fileName == null) { 329 if (fileName == null) {
329 fileName = request.uri.path.substring(1); 330 fileName = request.uri.path.substring(1);
330 } 331 }
(...skipping 22 matching lines...) Expand all
353 } 354 }
354 355
355 // Serve the not found page. 356 // Serve the not found page.
356 void _notFoundHandler(HttpRequest request, HttpResponse response) { 357 void _notFoundHandler(HttpRequest request, HttpResponse response) {
357 if (_notFoundPage == null) { 358 if (_notFoundPage == null) {
358 _notFoundPage = notFoundPageHtml.codeUnits; 359 _notFoundPage = notFoundPageHtml.codeUnits;
359 } 360 }
360 response.statusCode = HttpStatus.NOT_FOUND; 361 response.statusCode = HttpStatus.NOT_FOUND;
361 response.headers.set("Content-Type", "text/html; charset=UTF-8"); 362 response.headers.set("Content-Type", "text/html; charset=UTF-8");
362 response.contentLength = _notFoundPage.length; 363 response.contentLength = _notFoundPage.length;
363 response.add(_notFoundPage); 364 response.writeBytes(_notFoundPage);
364 response.close(); 365 response.close();
365 } 366 }
366 367
367 // Unexpected protocol data. 368 // Unexpected protocol data.
368 void _protocolError(HttpRequest request, HttpResponse response) { 369 void _protocolError(HttpRequest request, HttpResponse response) {
369 response.statusCode = HttpStatus.INTERNAL_SERVER_ERROR; 370 response.statusCode = HttpStatus.INTERNAL_SERVER_ERROR;
370 response.contentLength = 0; 371 response.contentLength = 0;
371 response.close(); 372 response.close();
372 } 373 }
373 374
374 // Join request: 375 // Join request:
375 // { "request": "join", 376 // { "request": "join",
376 // "handle": <handle> } 377 // "handle": <handle> }
377 void _joinHandler(HttpRequest request, HttpResponse response) { 378 void _joinHandler(HttpRequest request, HttpResponse response) {
378 StringBuffer body = new StringBuffer(); 379 StringBuffer body = new StringBuffer();
379 request.listen( 380 request.listen(
380 (data) => body.add(new String.fromCharCodes(data)), 381 (data) => body.write(new String.fromCharCodes(data)),
381 onDone: () { 382 onDone: () {
382 String data = body.toString(); 383 String data = body.toString();
383 if (data != null) { 384 if (data != null) {
384 var requestData = json.parse(data); 385 var requestData = json.parse(data);
385 if (requestData["request"] == "join") { 386 if (requestData["request"] == "join") {
386 String handle = requestData["handle"]; 387 String handle = requestData["handle"];
387 if (handle != null) { 388 if (handle != null) {
388 // New user joining. 389 // New user joining.
389 User user = _topic._userJoined(handle); 390 User user = _topic._userJoined(handle);
390 391
(...skipping 13 matching lines...) Expand all
404 } 405 }
405 }); 406 });
406 } 407 }
407 408
408 // Leave request: 409 // Leave request:
409 // { "request": "leave", 410 // { "request": "leave",
410 // "sessionId": <sessionId> } 411 // "sessionId": <sessionId> }
411 void _leaveHandler(HttpRequest request, HttpResponse response) { 412 void _leaveHandler(HttpRequest request, HttpResponse response) {
412 StringBuffer body = new StringBuffer(); 413 StringBuffer body = new StringBuffer();
413 request.listen( 414 request.listen(
414 (data) => body.add(new String.fromCharCodes(data)), 415 (data) => body.write(new String.fromCharCodes(data)),
415 onDone: () { 416 onDone: () {
416 String data = body.toString(); 417 String data = body.toString();
417 var requestData = json.parse(data); 418 var requestData = json.parse(data);
418 if (requestData["request"] == "leave") { 419 if (requestData["request"] == "leave") {
419 String sessionId = requestData["sessionId"]; 420 String sessionId = requestData["sessionId"];
420 if (sessionId != null) { 421 if (sessionId != null) {
421 // User leaving. 422 // User leaving.
422 _topic._userLeft(sessionId); 423 _topic._userLeft(sessionId);
423 424
424 // Send response. 425 // Send response.
425 Map responseData = new Map(); 426 Map responseData = new Map();
426 responseData["response"] = "leave"; 427 responseData["response"] = "leave";
427 _sendJSONResponse(response, responseData); 428 _sendJSONResponse(response, responseData);
428 } else { 429 } else {
429 _protocolError(request, response); 430 _protocolError(request, response);
430 } 431 }
431 } else { 432 } else {
432 _protocolError(request, response); 433 _protocolError(request, response);
433 } 434 }
434 }); 435 });
435 } 436 }
436 437
437 // Message request: 438 // Message request:
438 // { "request": "message", 439 // { "request": "message",
439 // "sessionId": <sessionId>, 440 // "sessionId": <sessionId>,
440 // "message": <message> } 441 // "message": <message> }
441 void _messageHandler(HttpRequest request, HttpResponse response) { 442 void _messageHandler(HttpRequest request, HttpResponse response) {
442 StringBuffer body = new StringBuffer(); 443 StringBuffer body = new StringBuffer();
443 request.listen( 444 request.listen(
444 (data) => body.add(new String.fromCharCodes(data)), 445 (data) => body.write(new String.fromCharCodes(data)),
445 onDone: () { 446 onDone: () {
446 String data = body.toString(); 447 String data = body.toString();
447 _messageCount++; 448 _messageCount++;
448 _messageRate.record(1); 449 _messageRate.record(1);
449 var requestData = json.parse(data); 450 var requestData = json.parse(data);
450 if (requestData["request"] == "message") { 451 if (requestData["request"] == "message") {
451 String sessionId = requestData["sessionId"]; 452 String sessionId = requestData["sessionId"];
452 if (sessionId != null) { 453 if (sessionId != null) {
453 // New message from user. 454 // New message from user.
454 bool success = _topic._userMessage(requestData); 455 bool success = _topic._userMessage(requestData);
(...skipping 16 matching lines...) Expand all
471 } 472 }
472 473
473 // Receive request: 474 // Receive request:
474 // { "request": "receive", 475 // { "request": "receive",
475 // "sessionId": <sessionId>, 476 // "sessionId": <sessionId>,
476 // "nextMessage": <nextMessage>, 477 // "nextMessage": <nextMessage>,
477 // "maxMessages": <maxMesssages> } 478 // "maxMessages": <maxMesssages> }
478 void _receiveHandler(HttpRequest request, HttpResponse response) { 479 void _receiveHandler(HttpRequest request, HttpResponse response) {
479 StringBuffer body = new StringBuffer(); 480 StringBuffer body = new StringBuffer();
480 request.listen( 481 request.listen(
481 (data) => body.add(new String.fromCharCodes(data)), 482 (data) => body.write(new String.fromCharCodes(data)),
482 onDone: () { 483 onDone: () {
483 String data = body.toString(); 484 String data = body.toString();
484 var requestData = json.parse(data); 485 var requestData = json.parse(data);
485 if (requestData["request"] == "receive") { 486 if (requestData["request"] == "receive") {
486 String sessionId = requestData["sessionId"]; 487 String sessionId = requestData["sessionId"];
487 int nextMessage = requestData["nextMessage"]; 488 int nextMessage = requestData["nextMessage"];
488 int maxMessages = requestData["maxMessages"]; 489 int maxMessages = requestData["maxMessages"];
489 if (sessionId != null && nextMessage != null) { 490 if (sessionId != null && nextMessage != null) {
490 491
491 void sendResponse(messages) { 492 void sendResponse(messages) {
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after
671 } 672 }
672 } 673 }
673 674
674 int _timeRange; 675 int _timeRange;
675 List<int> _buckets; 676 List<int> _buckets;
676 int _currentBucket; 677 int _currentBucket;
677 int _currentBucketTime; 678 int _currentBucketTime;
678 num _bucketTimeRange; 679 num _bucketTimeRange;
679 int _sum; 680 int _sum;
680 } 681 }
OLDNEW
« no previous file with comments | « runtime/bin/socket_patch.dart ('k') | samples/tests/samples/chat/chat_server_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698