forked from ably/ably-flutter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAblyFlutterWriter.m
More file actions
294 lines (259 loc) · 12.5 KB
/
AblyFlutterWriter.m
File metadata and controls
294 lines (259 loc) · 12.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
@import Ably;
#import "AblyFlutterWriter.h"
#import "AblyFlutterMessage.h"
#import "AblyFlutterReader.h"
#import "AblyPlatformConstants.h"
NS_ASSUME_NONNULL_BEGIN
typedef NSDictionary * (^AblyCodecEncoder)(id toEncode);
NS_ASSUME_NONNULL_END
@implementation AblyFlutterWriter
+ (UInt8) getType:(id)value{
if([value isKindOfClass:[AblyFlutterMessage class]]){
return ablyMessageCodecType;
}else if([value isKindOfClass:[ARTErrorInfo class]]){
return errorInfoCodecType;
}else if([value isKindOfClass:[ARTConnectionStateChange class]]){
return connectionStateChangeCodecType;
}else if([value isKindOfClass:[ARTChannelStateChange class]]){
return channelStateChangeCodecType;
}else if([value isKindOfClass:[ARTMessage class]]){
return messageCodecType;
}else if([value isKindOfClass:[ARTPresenceMessage class]]){
return presenceMessageCodecType;
}else if([value isKindOfClass:[ARTTokenParams class]]){
return tokenParamsCodecType;
}else if([value isKindOfClass:[ARTPaginatedResult class]]){
return paginatedResultCodecType;
}
return 0;
}
+ (AblyCodecEncoder) getEncoder:(const NSString*)type {
NSDictionary<NSString *, AblyCodecEncoder>* _handlers = @{
[NSString stringWithFormat:@"%d", ablyMessageCodecType]: encodeAblyMessage,
[NSString stringWithFormat:@"%d", errorInfoCodecType]: encodeErrorInfo,
[NSString stringWithFormat:@"%d", connectionStateChangeCodecType]: encodeConnectionStateChange,
[NSString stringWithFormat:@"%d", channelStateChangeCodecType]: encodeChannelStateChange,
[NSString stringWithFormat:@"%d", messageCodecType]: encodeChannelMessage,
[NSString stringWithFormat:@"%d", presenceMessageCodecType]: encodePresenceMessage,
[NSString stringWithFormat:@"%d", tokenParamsCodecType]: encodeTokenParams,
[NSString stringWithFormat:@"%d", paginatedResultCodecType]: encodePaginatedResult,
};
return [_handlers objectForKey:[NSString stringWithFormat:@"%@", type]];
}
- (void)writeValue:(id)value {
UInt8 const type = [AblyFlutterWriter getType:value];
if(type != 0){
[self writeByte: type];
AblyCodecEncoder encoder = [AblyFlutterWriter getEncoder: [NSString stringWithFormat:@"%d", type]];
[self writeValue: encoder(value)];
return;
}
[super writeValue:value];
}
#define WRITE_VALUE(DICTIONARY, JSON_KEY, VALUE) { \
if (VALUE) { \
[DICTIONARY setObject:VALUE forKey:JSON_KEY]; \
} \
}
static AblyCodecEncoder encodeAblyMessage = ^NSMutableDictionary*(AblyFlutterMessage *const message) {
NSMutableDictionary<NSString *, NSObject *> *dictionary = [[NSMutableDictionary alloc] init];
WRITE_VALUE(dictionary, TxAblyMessage_registrationHandle, [message handle]);
id value = [message message];
UInt8 type = [AblyFlutterWriter getType:value];
if(type != 0){
AblyCodecEncoder encoder = [AblyFlutterWriter getEncoder: [NSString stringWithFormat:@"%d", type]];
value = encoder(value);
WRITE_VALUE(dictionary, TxAblyMessage_type, [NSNumber numberWithInt:type]);
}
WRITE_VALUE(dictionary, TxAblyMessage_message, value);
return dictionary;
};
static AblyCodecEncoder encodeErrorInfo = ^NSMutableDictionary*(ARTErrorInfo *const e) {
NSMutableDictionary<NSString *, NSObject *> *dictionary = [[NSMutableDictionary alloc] init];
WRITE_VALUE(dictionary, TxErrorInfo_message, [e message]);
WRITE_VALUE(dictionary, TxErrorInfo_statusCode, [e statusCode]?@([e statusCode]):nil);
// code, href, requestId and cause - not available in ably-cocoa
// track @ https://github.com/ably/ably-flutter/issues/14
return dictionary;
};
+(NSString *) encodeConnectionState: (ARTRealtimeConnectionState) state {
switch (state) {
case ARTRealtimeInitialized:
return TxEnumConstants_initialized;
case ARTRealtimeConnecting:
return TxEnumConstants_connecting;
case ARTRealtimeConnected:
return TxEnumConstants_connected;
case ARTRealtimeDisconnected:
return TxEnumConstants_disconnected;
case ARTRealtimeSuspended:
return TxEnumConstants_suspended;
case ARTRealtimeClosing:
return TxEnumConstants_closing;
case ARTRealtimeClosed:
return TxEnumConstants_closed;
case ARTRealtimeFailed:
return TxEnumConstants_failed;
}
};
+(NSString *) encodeConnectionEvent: (ARTRealtimeConnectionEvent) event {
switch(event) {
case ARTRealtimeConnectionEventInitialized:
return TxEnumConstants_initialized;
case ARTRealtimeConnectionEventConnecting:
return TxEnumConstants_connecting;
case ARTRealtimeConnectionEventConnected:
return TxEnumConstants_connected;
case ARTRealtimeConnectionEventDisconnected:
return TxEnumConstants_disconnected;
case ARTRealtimeConnectionEventSuspended:
return TxEnumConstants_suspended;
case ARTRealtimeConnectionEventClosing:
return TxEnumConstants_closing;
case ARTRealtimeConnectionEventClosed:
return TxEnumConstants_closed;
case ARTRealtimeConnectionEventFailed:
return TxEnumConstants_failed;
case ARTRealtimeConnectionEventUpdate:
return TxEnumConstants_update;
}
}
+(NSString *) encodeChannelState: (ARTRealtimeChannelState) event {
switch(event) {
case ARTRealtimeChannelInitialized:
return TxEnumConstants_initialized;
case ARTRealtimeChannelAttaching:
return TxEnumConstants_attaching;
case ARTRealtimeChannelAttached:
return TxEnumConstants_attached;
case ARTRealtimeChannelDetaching:
return TxEnumConstants_detaching;
case ARTRealtimeChannelDetached:
return TxEnumConstants_detached;
case ARTRealtimeChannelSuspended:
return TxEnumConstants_suspended;
case ARTRealtimeChannelFailed:
return TxEnumConstants_failed;
}
}
+(NSString *) encodeChannelEvent: (ARTChannelEvent) event {
switch(event) {
case ARTChannelEventInitialized:
return TxEnumConstants_initialized;
case ARTChannelEventAttaching:
return TxEnumConstants_attaching;
case ARTChannelEventAttached:
return TxEnumConstants_attached;
case ARTChannelEventDetaching:
return TxEnumConstants_detaching;
case ARTChannelEventDetached:
return TxEnumConstants_detached;
case ARTChannelEventSuspended:
return TxEnumConstants_suspended;
case ARTChannelEventFailed:
return TxEnumConstants_failed;
case ARTChannelEventUpdate:
return TxEnumConstants_update;
}
}
static AblyCodecEncoder encodeConnectionStateChange = ^NSMutableDictionary*(ARTConnectionStateChange *const stateChange) {
NSMutableDictionary<NSString *, NSObject *> *dictionary = [[NSMutableDictionary alloc] init];
WRITE_VALUE(dictionary,
TxConnectionStateChange_current,
[AblyFlutterWriter encodeConnectionState: [stateChange current]]);
WRITE_VALUE(dictionary,
TxConnectionStateChange_previous,
[AblyFlutterWriter encodeConnectionState: [stateChange previous]]);
WRITE_VALUE(dictionary,
TxConnectionStateChange_event,
[AblyFlutterWriter encodeConnectionEvent: [stateChange event]]);
WRITE_VALUE(dictionary, TxConnectionStateChange_retryIn, [stateChange retryIn]?@((int)([stateChange retryIn] * 1000)):nil);
WRITE_VALUE(dictionary, TxConnectionStateChange_reason, encodeErrorInfo([stateChange reason]));
return dictionary;
};
static AblyCodecEncoder encodeChannelStateChange = ^NSMutableDictionary*(ARTChannelStateChange *const stateChange) {
NSMutableDictionary<NSString *, NSObject *> *dictionary = [[NSMutableDictionary alloc] init];
WRITE_VALUE(dictionary,
TxChannelStateChange_current,
[AblyFlutterWriter encodeChannelState: [stateChange current]]);
WRITE_VALUE(dictionary,
TxChannelStateChange_previous,
[AblyFlutterWriter encodeChannelState: [stateChange previous]]);
WRITE_VALUE(dictionary,
TxChannelStateChange_event,
[AblyFlutterWriter encodeChannelEvent: [stateChange event]]);
WRITE_VALUE(dictionary, TxChannelStateChange_resumed, [stateChange resumed]?@([stateChange resumed]):nil);
WRITE_VALUE(dictionary, TxChannelStateChange_reason, encodeErrorInfo([stateChange reason]));
return dictionary;
};
static AblyCodecEncoder encodeChannelMessage = ^NSMutableDictionary*(ARTMessage *const message) {
NSMutableDictionary<NSString *, NSObject *> *dictionary = [[NSMutableDictionary alloc] init];
WRITE_VALUE(dictionary, TxMessage_id, [message id]);
WRITE_VALUE(dictionary, TxMessage_name, [message name]);
WRITE_VALUE(dictionary, TxMessage_clientId, [message clientId]);
WRITE_VALUE(dictionary, TxMessage_connectionId, [message connectionId]);
WRITE_VALUE(dictionary, TxMessage_encoding, [message encoding]);
WRITE_VALUE(dictionary, TxMessage_extras, [message extras]);
WRITE_VALUE(dictionary, TxMessage_data, (NSObject *)[message data]);
WRITE_VALUE(dictionary, TxMessage_timestamp,
[message timestamp]?@((long)([[message timestamp] timeIntervalSince1970]*1000)):nil);
return dictionary;
};
+(NSString *) encodePresenceAction: (ARTPresenceAction) action {
switch(action) {
case ARTPresenceAbsent:
return TxEnumConstants_absent;
case ARTPresencePresent:
return TxEnumConstants_present;
case ARTPresenceEnter:
return TxEnumConstants_enter;
case ARTPresenceLeave:
return TxEnumConstants_leave;
case ARTPresenceUpdate:
return TxEnumConstants_update;
}
}
static AblyCodecEncoder encodePresenceMessage = ^NSMutableDictionary*(ARTPresenceMessage *const message) {
NSMutableDictionary<NSString *, NSObject *> *dictionary = [[NSMutableDictionary alloc] init];
WRITE_VALUE(dictionary, TxPresenceMessage_id, [message id]);
WRITE_VALUE(dictionary,
TxPresenceMessage_action,
[AblyFlutterWriter encodePresenceAction: [message action]]);
WRITE_VALUE(dictionary, TxPresenceMessage_clientId, [message clientId]);
WRITE_VALUE(dictionary, TxPresenceMessage_data, (NSObject *)[message data]);
WRITE_VALUE(dictionary, TxPresenceMessage_connectionId, [message connectionId]);
WRITE_VALUE(dictionary, TxPresenceMessage_encoding, [message encoding]);
WRITE_VALUE(dictionary, TxPresenceMessage_extras, [message extras]);
WRITE_VALUE(dictionary, TxPresenceMessage_timestamp,
[message timestamp]?@((long)([[message timestamp] timeIntervalSince1970]*1000)):nil);
return dictionary;
};
static AblyCodecEncoder encodeTokenParams = ^NSMutableDictionary*(ARTTokenParams *const params) {
NSMutableDictionary<NSString *, NSObject *> *dictionary = [[NSMutableDictionary alloc] init];
WRITE_VALUE(dictionary, TxTokenParams_ttl, [params ttl]);
WRITE_VALUE(dictionary, TxTokenParams_nonce, [params nonce]);
WRITE_VALUE(dictionary, TxTokenParams_clientId, [params clientId]);
WRITE_VALUE(dictionary, TxTokenParams_timestamp,
[params timestamp]?@((long)([[params timestamp] timeIntervalSince1970]*1000)):nil);
WRITE_VALUE(dictionary, TxTokenParams_capability, [params capability]);
return dictionary;
};
static AblyCodecEncoder encodePaginatedResult = ^NSMutableDictionary*(ARTPaginatedResult *const result) {
NSMutableDictionary<NSString *, NSObject *> *dictionary = [[NSMutableDictionary alloc] init];
NSArray* items = [result items];
if([items count] > 0){
UInt8 type = [AblyFlutterWriter getType:items[0]];
if(type != 0){
AblyCodecEncoder encoder = [AblyFlutterWriter getEncoder: [NSString stringWithFormat:@"%d", type]];
NSMutableArray *result = [NSMutableArray arrayWithCapacity:[items count]];
[items enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
[result addObject: encoder(obj)];
}];
WRITE_VALUE(dictionary, TxPaginatedResult_type, [NSNumber numberWithInt:type]);
WRITE_VALUE(dictionary, TxPaginatedResult_items, result);
}
}
WRITE_VALUE(dictionary, TxPaginatedResult_hasNext, @([result hasNext]));
return dictionary;
};
@end