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 516 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
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 return new Request.fromJson(result); |
538 return null; | |
539 } | |
540 var id = result[Request.ID]; | |
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) { | 538 } catch (exception) { |
556 return null; | 539 return null; |
557 } | 540 } |
558 } | 541 } |
559 | 542 |
560 /** | 543 /** |
544 * Return a request parsed from the given json, or `null` if the [data] is | |
545 * not a valid json representation of a request. The [data] is expected to | |
546 * have the following format: | |
547 * | |
548 * { | |
549 * 'id': String, | |
550 * 'method': methodName, | |
551 * 'params': { | |
552 * paramter_name: value | |
553 * } | |
554 * 'clientRequestTime': millisecondsSinceEpoch | |
Brian Wilkerson
2015/06/15 15:41:40
nit: It would look nicer if this line were moved b
danrubel
2015/06/15 16:25:17
Done.
| |
555 * } | |
556 * | |
557 * where both the parameters and clientRequestTime are optional. | |
558 * The parameters can contain any number of name/value pairs. | |
559 * The clientRequestTime must be an int representing the time at which | |
560 * the client issued the request (milliseconds since epoch). | |
561 */ | |
562 factory Request.fromJson(result) { | |
Brian Wilkerson
2015/06/15 15:41:40
Parameters should have type annotations. Can we mo
danrubel
2015/06/15 16:25:18
Done.
| |
563 if (result is! Map) { | |
564 return null; | |
565 } | |
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 |