OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 protocol; | 5 library protocol; |
6 | 6 |
7 import 'dart:collection'; | 7 import 'dart:collection'; |
8 import 'dart:convert'; | 8 import 'dart:convert'; |
9 | 9 |
10 part 'generated_protocol.dart'; | 10 part 'generated_protocol.dart'; |
(...skipping 500 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
511 Request(this.id, this.method, | 511 Request(this.id, this.method, |
512 [Map<String, Object> params, this.clientRequestTime]) | 512 [Map<String, Object> params, this.clientRequestTime]) |
513 : _params = params != null ? params : new HashMap<String, Object>(); | 513 : _params = params != null ? params : new HashMap<String, Object>(); |
514 | 514 |
515 /** | 515 /** |
516 * Return a request parsed from the given [data], or `null` if the [data] is | 516 * Return a request parsed from the given [data], or `null` if the [data] is |
517 * not a valid json representation of a request. The [data] is expected to | 517 * not a valid json representation of a request. The [data] is expected to |
518 * have the following format: | 518 * have the following format: |
519 * | 519 * |
520 * { | 520 * { |
| 521 * 'clientRequestTime': millisecondsSinceEpoch |
521 * 'id': String, | 522 * 'id': String, |
522 * 'method': methodName, | 523 * 'method': methodName, |
523 * 'params': { | 524 * 'params': { |
524 * paramter_name: value | 525 * paramter_name: value |
525 * } | 526 * } |
526 * 'clientRequestTime': millisecondsSinceEpoch | |
527 * } | 527 * } |
528 * | 528 * |
529 * where both the parameters and clientRequestTime are optional. | 529 * where both the parameters and clientRequestTime are optional. |
530 * The parameters can contain any number of name/value pairs. | 530 * The parameters can contain any number of name/value pairs. |
531 * The clientRequestTime must be an int representing the time at which | 531 * The clientRequestTime must be an int representing the time at which |
532 * the client issued the request (milliseconds since epoch). | 532 * the client issued the request (milliseconds since epoch). |
533 */ | 533 */ |
534 factory Request.fromString(String data) { | 534 factory Request.fromString(String data) { |
535 try { | 535 try { |
536 var result = JSON.decode(data); | 536 var result = JSON.decode(data); |
537 if (result is! Map) { | 537 if (result is Map) { |
538 return null; | 538 return new Request.fromJson(result); |
539 } | 539 } |
540 var id = result[Request.ID]; | 540 return null; |
541 var method = result[Request.METHOD]; | |
542 if (id is! String || method is! String) { | |
543 return null; | |
544 } | |
545 var time = result[Request.CLIENT_REQUEST_TIME]; | |
546 if (time != null && time is! int) { | |
547 return null; | |
548 } | |
549 var params = result[Request.PARAMS]; | |
550 if (params is Map || params == null) { | |
551 return new Request(id, method, params, time); | |
552 } else { | |
553 return null; | |
554 } | |
555 } catch (exception) { | 541 } catch (exception) { |
556 return null; | 542 return null; |
557 } | 543 } |
558 } | 544 } |
559 | 545 |
560 /** | 546 /** |
| 547 * Return a request parsed from the given json, or `null` if the [data] is |
| 548 * not a valid json representation of a request. The [data] is expected to |
| 549 * have the following format: |
| 550 * |
| 551 * { |
| 552 * 'clientRequestTime': millisecondsSinceEpoch |
| 553 * 'id': String, |
| 554 * 'method': methodName, |
| 555 * 'params': { |
| 556 * paramter_name: value |
| 557 * } |
| 558 * } |
| 559 * |
| 560 * where both the parameters and clientRequestTime are optional. |
| 561 * The parameters can contain any number of name/value pairs. |
| 562 * The clientRequestTime must be an int representing the time at which |
| 563 * the client issued the request (milliseconds since epoch). |
| 564 */ |
| 565 factory Request.fromJson(Map<String, dynamic> result) { |
| 566 var id = result[Request.ID]; |
| 567 var method = result[Request.METHOD]; |
| 568 if (id is! String || method is! String) { |
| 569 return null; |
| 570 } |
| 571 var time = result[Request.CLIENT_REQUEST_TIME]; |
| 572 if (time != null && time is! int) { |
| 573 return null; |
| 574 } |
| 575 var params = result[Request.PARAMS]; |
| 576 if (params is Map || params == null) { |
| 577 return new Request(id, method, params, time); |
| 578 } else { |
| 579 return null; |
| 580 } |
| 581 } |
| 582 |
| 583 /** |
561 * Return a table representing the structure of the Json object that will be | 584 * Return a table representing the structure of the Json object that will be |
562 * sent to the client to represent this response. | 585 * sent to the client to represent this response. |
563 */ | 586 */ |
564 Map<String, Object> toJson() { | 587 Map<String, Object> toJson() { |
565 Map<String, Object> jsonObject = new HashMap<String, Object>(); | 588 Map<String, Object> jsonObject = new HashMap<String, Object>(); |
566 jsonObject[ID] = id; | 589 jsonObject[ID] = id; |
567 jsonObject[METHOD] = method; | 590 jsonObject[METHOD] = method; |
568 if (_params.isNotEmpty) { | 591 if (_params.isNotEmpty) { |
569 jsonObject[PARAMS] = _params; | 592 jsonObject[PARAMS] = _params; |
570 } | 593 } |
(...skipping 340 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
911 hash = 0x1fffffff & (hash + ((0x03ffffff & hash) << 3)); | 934 hash = 0x1fffffff & (hash + ((0x03ffffff & hash) << 3)); |
912 hash = hash ^ (hash >> 11); | 935 hash = hash ^ (hash >> 11); |
913 return 0x1fffffff & (hash + ((0x00003fff & hash) << 15)); | 936 return 0x1fffffff & (hash + ((0x00003fff & hash) << 15)); |
914 } | 937 } |
915 | 938 |
916 static int hash2(a, b) => finish(combine(combine(0, a), b)); | 939 static int hash2(a, b) => finish(combine(combine(0, a), b)); |
917 | 940 |
918 static int hash4(a, b, c, d) => | 941 static int hash4(a, b, c, d) => |
919 finish(combine(combine(combine(combine(0, a), b), c), d)); | 942 finish(combine(combine(combine(combine(0, a), b), c), d)); |
920 } | 943 } |
OLD | NEW |