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

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: Fixed accidental edit 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
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;
Anders Johnsen 2013/03/07 16:53:49 Not your code, encodeUtf8 or remove '.codeUnits'?
Søren Gjesse 2013/03/08 09:47:46 This builds on the assumption that the redirectPag
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 response.write(_redirectPage);
Anders Johnsen 2013/03/07 16:53:49 writeBytes?
Søren Gjesse 2013/03/08 09:47:46 Absolutely, thanks.
321 response.close(); 321 response.close();
322 } 322 }
323 323
324 // Serve the content of a file. 324 // Serve the content of a file.
325 void fileHandler( 325 void fileHandler(
326 HttpRequest request, HttpResponse response, [String fileName = null]) { 326 HttpRequest request, HttpResponse response, [String fileName = null]) {
327 final int BUFFER_SIZE = 4096; 327 final int BUFFER_SIZE = 4096;
328 if (fileName == null) { 328 if (fileName == null) {
329 fileName = request.uri.path.substring(1); 329 fileName = request.uri.path.substring(1);
330 } 330 }
(...skipping 22 matching lines...) Expand all
353 } 353 }
354 354
355 // Serve the not found page. 355 // Serve the not found page.
356 void _notFoundHandler(HttpRequest request, HttpResponse response) { 356 void _notFoundHandler(HttpRequest request, HttpResponse response) {
357 if (_notFoundPage == null) { 357 if (_notFoundPage == null) {
358 _notFoundPage = notFoundPageHtml.codeUnits; 358 _notFoundPage = notFoundPageHtml.codeUnits;
359 } 359 }
360 response.statusCode = HttpStatus.NOT_FOUND; 360 response.statusCode = HttpStatus.NOT_FOUND;
361 response.headers.set("Content-Type", "text/html; charset=UTF-8"); 361 response.headers.set("Content-Type", "text/html; charset=UTF-8");
362 response.contentLength = _notFoundPage.length; 362 response.contentLength = _notFoundPage.length;
363 response.add(_notFoundPage); 363 response.write(_notFoundPage);
364 response.close(); 364 response.close();
365 } 365 }
366 366
367 // Unexpected protocol data. 367 // Unexpected protocol data.
368 void _protocolError(HttpRequest request, HttpResponse response) { 368 void _protocolError(HttpRequest request, HttpResponse response) {
369 response.statusCode = HttpStatus.INTERNAL_SERVER_ERROR; 369 response.statusCode = HttpStatus.INTERNAL_SERVER_ERROR;
370 response.contentLength = 0; 370 response.contentLength = 0;
371 response.close(); 371 response.close();
372 } 372 }
373 373
374 // Join request: 374 // Join request:
375 // { "request": "join", 375 // { "request": "join",
376 // "handle": <handle> } 376 // "handle": <handle> }
377 void _joinHandler(HttpRequest request, HttpResponse response) { 377 void _joinHandler(HttpRequest request, HttpResponse response) {
378 StringBuffer body = new StringBuffer(); 378 StringBuffer body = new StringBuffer();
379 request.listen( 379 request.listen(
380 (data) => body.add(new String.fromCharCodes(data)), 380 (data) => body.write(new String.fromCharCodes(data)),
381 onDone: () { 381 onDone: () {
382 String data = body.toString(); 382 String data = body.toString();
383 if (data != null) { 383 if (data != null) {
384 var requestData = json.parse(data); 384 var requestData = json.parse(data);
385 if (requestData["request"] == "join") { 385 if (requestData["request"] == "join") {
386 String handle = requestData["handle"]; 386 String handle = requestData["handle"];
387 if (handle != null) { 387 if (handle != null) {
388 // New user joining. 388 // New user joining.
389 User user = _topic._userJoined(handle); 389 User user = _topic._userJoined(handle);
390 390
(...skipping 13 matching lines...) Expand all
404 } 404 }
405 }); 405 });
406 } 406 }
407 407
408 // Leave request: 408 // Leave request:
409 // { "request": "leave", 409 // { "request": "leave",
410 // "sessionId": <sessionId> } 410 // "sessionId": <sessionId> }
411 void _leaveHandler(HttpRequest request, HttpResponse response) { 411 void _leaveHandler(HttpRequest request, HttpResponse response) {
412 StringBuffer body = new StringBuffer(); 412 StringBuffer body = new StringBuffer();
413 request.listen( 413 request.listen(
414 (data) => body.add(new String.fromCharCodes(data)), 414 (data) => body.write(new String.fromCharCodes(data)),
415 onDone: () { 415 onDone: () {
416 String data = body.toString(); 416 String data = body.toString();
417 var requestData = json.parse(data); 417 var requestData = json.parse(data);
418 if (requestData["request"] == "leave") { 418 if (requestData["request"] == "leave") {
419 String sessionId = requestData["sessionId"]; 419 String sessionId = requestData["sessionId"];
420 if (sessionId != null) { 420 if (sessionId != null) {
421 // User leaving. 421 // User leaving.
422 _topic._userLeft(sessionId); 422 _topic._userLeft(sessionId);
423 423
424 // Send response. 424 // Send response.
425 Map responseData = new Map(); 425 Map responseData = new Map();
426 responseData["response"] = "leave"; 426 responseData["response"] = "leave";
427 _sendJSONResponse(response, responseData); 427 _sendJSONResponse(response, responseData);
428 } else { 428 } else {
429 _protocolError(request, response); 429 _protocolError(request, response);
430 } 430 }
431 } else { 431 } else {
432 _protocolError(request, response); 432 _protocolError(request, response);
433 } 433 }
434 }); 434 });
435 } 435 }
436 436
437 // Message request: 437 // Message request:
438 // { "request": "message", 438 // { "request": "message",
439 // "sessionId": <sessionId>, 439 // "sessionId": <sessionId>,
440 // "message": <message> } 440 // "message": <message> }
441 void _messageHandler(HttpRequest request, HttpResponse response) { 441 void _messageHandler(HttpRequest request, HttpResponse response) {
442 StringBuffer body = new StringBuffer(); 442 StringBuffer body = new StringBuffer();
443 request.listen( 443 request.listen(
444 (data) => body.add(new String.fromCharCodes(data)), 444 (data) => body.write(new String.fromCharCodes(data)),
445 onDone: () { 445 onDone: () {
446 String data = body.toString(); 446 String data = body.toString();
447 _messageCount++; 447 _messageCount++;
448 _messageRate.record(1); 448 _messageRate.record(1);
449 var requestData = json.parse(data); 449 var requestData = json.parse(data);
450 if (requestData["request"] == "message") { 450 if (requestData["request"] == "message") {
451 String sessionId = requestData["sessionId"]; 451 String sessionId = requestData["sessionId"];
452 if (sessionId != null) { 452 if (sessionId != null) {
453 // New message from user. 453 // New message from user.
454 bool success = _topic._userMessage(requestData); 454 bool success = _topic._userMessage(requestData);
(...skipping 16 matching lines...) Expand all
471 } 471 }
472 472
473 // Receive request: 473 // Receive request:
474 // { "request": "receive", 474 // { "request": "receive",
475 // "sessionId": <sessionId>, 475 // "sessionId": <sessionId>,
476 // "nextMessage": <nextMessage>, 476 // "nextMessage": <nextMessage>,
477 // "maxMessages": <maxMesssages> } 477 // "maxMessages": <maxMesssages> }
478 void _receiveHandler(HttpRequest request, HttpResponse response) { 478 void _receiveHandler(HttpRequest request, HttpResponse response) {
479 StringBuffer body = new StringBuffer(); 479 StringBuffer body = new StringBuffer();
480 request.listen( 480 request.listen(
481 (data) => body.add(new String.fromCharCodes(data)), 481 (data) => body.write(new String.fromCharCodes(data)),
482 onDone: () { 482 onDone: () {
483 String data = body.toString(); 483 String data = body.toString();
484 var requestData = json.parse(data); 484 var requestData = json.parse(data);
485 if (requestData["request"] == "receive") { 485 if (requestData["request"] == "receive") {
486 String sessionId = requestData["sessionId"]; 486 String sessionId = requestData["sessionId"];
487 int nextMessage = requestData["nextMessage"]; 487 int nextMessage = requestData["nextMessage"];
488 int maxMessages = requestData["maxMessages"]; 488 int maxMessages = requestData["maxMessages"];
489 if (sessionId != null && nextMessage != null) { 489 if (sessionId != null && nextMessage != null) {
490 490
491 void sendResponse(messages) { 491 void sendResponse(messages) {
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after
671 } 671 }
672 } 672 }
673 673
674 int _timeRange; 674 int _timeRange;
675 List<int> _buckets; 675 List<int> _buckets;
676 int _currentBucket; 676 int _currentBucket;
677 int _currentBucketTime; 677 int _currentBucketTime;
678 num _bucketTimeRange; 678 num _bucketTimeRange;
679 int _sum; 679 int _sum;
680 } 680 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698