|
1 | | -import 'utils.dart' as utils; |
2 | | -import 'logger.dart'; |
3 | 1 | import 'dart:convert'; |
4 | 2 |
|
5 | | -const APP_NAME = 'protoo-client'; |
6 | | -var logger = new Logger(APP_NAME); |
| 3 | +import 'Logger.dart'; |
| 4 | +import 'Utils.dart' as utils; |
7 | 5 |
|
8 | | -class Message |
9 | | -{ |
| 6 | +final logger = new Logger('Message'); |
| 7 | + |
| 8 | +class Message { |
10 | 9 | static JsonEncoder encoder = new JsonEncoder(); |
11 | 10 | static JsonDecoder decoder = new JsonDecoder(); |
| 11 | + static Map<String, dynamic>? parse(dynamic raw) { |
| 12 | + var object; |
| 13 | + final message = Map<String, dynamic>(); |
| 14 | + |
| 15 | + try { |
| 16 | + object = decoder.convert(raw); |
| 17 | + } catch (error) { |
| 18 | + logger.error('parse() | invalid JSON: %s' + error.toString()); |
| 19 | + |
| 20 | + return null; |
| 21 | + } |
| 22 | + |
| 23 | + // Request. |
| 24 | + if (object['request'] != null) { |
| 25 | + message['request'] = true; |
12 | 26 |
|
13 | | - static Future<String> parse(raw) |
14 | | - { |
15 | | - var object; |
16 | | - var message; |
17 | | - |
18 | | - try |
19 | | - { |
20 | | - object = decoder.convert(raw); |
21 | | - } |
22 | | - catch (error) |
23 | | - { |
24 | | - logger.failure('parse() | invalid JSON: ' + error); |
25 | | - } |
26 | | - |
27 | | - // if (typeof object !== 'object' || Array.isArray(object)) |
28 | | - // { |
29 | | - // logger.error('parse() | not an object'); |
30 | | - // return; |
31 | | - // } |
32 | | - |
33 | | - // Request. |
34 | | - if (object['request']) |
35 | | - { |
36 | | - message['request'] = true; |
37 | | - |
38 | | - if(!(object['method'] is String)){ |
| 27 | + if (!(object['method'] is String)) { |
39 | 28 | logger.failure('parse() | missing/invalid method field'); |
40 | 29 | } |
41 | 30 |
|
42 | | - if(!(object['id'] is num)){ |
| 31 | + if (!(object['id'] is num)) { |
43 | 32 | logger.failure('parse() | missing/invalid id field'); |
44 | 33 | } |
45 | 34 |
|
46 | | - message['id'] = object['id']; |
47 | | - message['method'] = object['method']; |
48 | | - message['data'] = object['data'] ?? {}; |
49 | | - } |
50 | | - // Response. |
51 | | - else if (object['response']) |
52 | | - { |
53 | | - message['response'] = true; |
54 | | - if(!(object['id'] is num)){ |
| 35 | + message['id'] = object['id']; |
| 36 | + message['method'] = object['method']; |
| 37 | + message['data'] = object['data'] ?? {}; |
| 38 | + } |
| 39 | + // Response. |
| 40 | + else if (object['response'] != null) { |
| 41 | + message['response'] = true; |
| 42 | + if (!(object['id'] is num)) { |
55 | 43 | logger.failure('parse() | missing/invalid id field'); |
56 | 44 | } |
57 | 45 |
|
58 | | - message['id'] = object['id']; |
59 | | - |
60 | | - // Success. |
61 | | - if (object['ok']) |
62 | | - { |
63 | | - message['ok'] = true; |
64 | | - message['data'] = object['data'] ?? {}; |
65 | | - } |
66 | | - // Error. |
67 | | - else |
68 | | - { |
69 | | - message['errorCode'] = object['errorCode']; |
70 | | - message['errorReason'] = object['errorReason']; |
71 | | - } |
72 | | - } |
73 | | - // Notification. |
74 | | - else if (object['notification']) |
75 | | - { |
76 | | - message['notification'] = true; |
77 | | - if(!(object['method'] is String)){ |
| 46 | + message['id'] = object['id']; |
| 47 | + |
| 48 | + // Success. |
| 49 | + if (object['ok']) { |
| 50 | + message['ok'] = true; |
| 51 | + message['data'] = object['data'] ?? {}; |
| 52 | + } |
| 53 | + // Error. |
| 54 | + else { |
| 55 | + message['errorCode'] = object['errorCode']; |
| 56 | + message['errorReason'] = object['errorReason']; |
| 57 | + } |
| 58 | + } |
| 59 | + // Notification. |
| 60 | + else if (object['notification'] != null) { |
| 61 | + message['notification'] = true; |
| 62 | + if (!(object['method'] is String)) { |
78 | 63 | logger.failure('parse() | missing/invalid method field'); |
79 | 64 | } |
80 | 65 |
|
81 | | - message['method'] = object['method']; |
82 | | - message['data'] = object['data'] ?? {}; |
83 | | - }else { |
84 | | - logger.failure('parse() | missing request/response field'); |
85 | | - } |
86 | | - |
87 | | - return message; |
88 | | - } |
89 | | - |
90 | | - static requestFactory(method, data) |
91 | | - { |
92 | | - var requestObj = |
93 | | - { |
94 | | - 'request' : true, |
95 | | - 'id' : utils.randomNumber, |
96 | | - 'method' : method, |
97 | | - 'data' : data ?? {} |
98 | | - }; |
99 | | - |
100 | | - return requestObj; |
101 | | - } |
102 | | - |
103 | | - static successResponseFactory(request, data) |
104 | | - { |
105 | | - var responseObj = |
106 | | - { |
107 | | - 'response' : true, |
108 | | - 'id' : request['id'], |
109 | | - 'ok' : true, |
110 | | - 'data' : data ?? {} |
111 | | - }; |
112 | | - |
113 | | - return responseObj; |
114 | | - } |
115 | | - |
116 | | - static errorResponseFactory(request, errorCode, errorReason) |
117 | | - { |
118 | | - var responseObj = |
119 | | - { |
120 | | - 'response' : true, |
121 | | - 'id' : request['id'], |
122 | | - 'errorCode' : errorCode, |
123 | | - 'errorReason' : errorReason |
124 | | - }; |
125 | | - |
126 | | - return responseObj; |
127 | | - } |
128 | | - |
129 | | - static notificationFactory(method, data) |
130 | | - { |
131 | | - var notificationObj = |
132 | | - { |
133 | | - 'notification' : true, |
134 | | - 'method' : method, |
135 | | - 'data ' : data ?? {}, |
136 | | - }; |
137 | | - |
138 | | - return notificationObj; |
139 | | - } |
| 66 | + message['method'] = object['method']; |
| 67 | + message['data'] = object['data'] ?? {}; |
| 68 | + } |
| 69 | + // Invalid. |
| 70 | + else { |
| 71 | + logger.failure('parse() | missing request/response field'); |
| 72 | + return null; |
| 73 | + } |
| 74 | + |
| 75 | + return message; |
| 76 | + } |
| 77 | + |
| 78 | + static createRequest(method, data) { |
| 79 | + var requestObj = { |
| 80 | + 'request': true, |
| 81 | + 'id': utils.randomNumber, |
| 82 | + 'method': method, |
| 83 | + 'data': data ?? {} |
| 84 | + }; |
| 85 | + return requestObj; |
| 86 | + } |
| 87 | + |
| 88 | + static createSuccessResponse(request, data) { |
| 89 | + var responseObj = { |
| 90 | + 'response': true, |
| 91 | + 'id': request['id'], |
| 92 | + 'ok': true, |
| 93 | + 'data': data ?? {} |
| 94 | + }; |
| 95 | + |
| 96 | + return responseObj; |
| 97 | + } |
| 98 | + |
| 99 | + static createErrorResponse(request, errorCode, errorReason) { |
| 100 | + var responseObj = { |
| 101 | + 'response': true, |
| 102 | + 'id': request['id'], |
| 103 | + 'errorCode': errorCode, |
| 104 | + 'errorReason': errorReason |
| 105 | + }; |
| 106 | + |
| 107 | + return responseObj; |
| 108 | + } |
| 109 | + |
| 110 | + static createNotification(method, data) { |
| 111 | + var notificationObj = { |
| 112 | + 'notification': true, |
| 113 | + 'method': method, |
| 114 | + 'data ': data ?? {}, |
| 115 | + }; |
| 116 | + |
| 117 | + return notificationObj; |
| 118 | + } |
140 | 119 | } |
0 commit comments