XMPPIDTracker.m
7.69 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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#import "XMPPIDTracker.h"
#import "XMPP.h"
#import "XMPPLogging.h"
#if ! __has_feature(objc_arc)
#warning This file must be compiled with ARC. Use -fobjc-arc flag (or convert project to ARC).
#endif
// Log levels: off, error, warn, info, verbose
// Log flags: trace
#if DEBUG
static const int xmppLogLevel = XMPP_LOG_LEVEL_VERBOSE; // | XMPP_LOG_FLAG_TRACE;
#else
static const int xmppLogLevel = XMPP_LOG_LEVEL_WARN;
#endif
#define AssertProperQueue() NSAssert(dispatch_get_specific(queueTag), @"Invoked on incorrect queue")
const NSTimeInterval XMPPIDTrackerTimeoutNone = -1;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark -
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@interface XMPPIDTracker ()
{
void *queueTag;
}
@end
@implementation XMPPIDTracker
- (id)init
{
// You must use initWithDispatchQueue or initWithStream:dispatchQueue:
return nil;
}
- (id)initWithDispatchQueue:(dispatch_queue_t)aQueue
{
return [self initWithStream:nil dispatchQueue:aQueue];
}
- (id)initWithStream:(XMPPStream *)stream dispatchQueue:(dispatch_queue_t)aQueue
{
NSParameterAssert(aQueue != NULL);
if ((self = [super init]))
{
xmppStream = stream;
queue = aQueue;
queueTag = &queueTag;
dispatch_queue_set_specific(queue, queueTag, queueTag, NULL);
#if !OS_OBJECT_USE_OBJC
dispatch_retain(queue);
#endif
dict = [[NSMutableDictionary alloc] init];
}
return self;
}
- (void)dealloc
{
// We don't call [self removeAllIDs] because dealloc might not be invoked on queue
for (id <XMPPTrackingInfo> info in [dict objectEnumerator])
{
[info cancelTimer];
}
[dict removeAllObjects];
#if !OS_OBJECT_USE_OBJC
dispatch_release(queue);
#endif
}
- (void)addID:(NSString *)elementID target:(id)target selector:(SEL)selector timeout:(NSTimeInterval)timeout
{
AssertProperQueue();
XMPPBasicTrackingInfo *trackingInfo;
trackingInfo = [[XMPPBasicTrackingInfo alloc] initWithTarget:target selector:selector timeout:timeout];
[self addID:elementID trackingInfo:trackingInfo];
}
- (void)addElement:(XMPPElement *)element target:(id)target selector:(SEL)selector timeout:(NSTimeInterval)timeout
{
AssertProperQueue();
XMPPBasicTrackingInfo *trackingInfo;
trackingInfo = [[XMPPBasicTrackingInfo alloc] initWithTarget:target selector:selector timeout:timeout];
[self addElement:element trackingInfo:trackingInfo];
}
- (void)addID:(NSString *)elementID
block:(void (^)(id obj, id <XMPPTrackingInfo> info))block
timeout:(NSTimeInterval)timeout
{
AssertProperQueue();
XMPPBasicTrackingInfo *trackingInfo;
trackingInfo = [[XMPPBasicTrackingInfo alloc] initWithBlock:block timeout:timeout];
[self addID:elementID trackingInfo:trackingInfo];
}
- (void)addElement:(XMPPElement *)element
block:(void (^)(id obj, id <XMPPTrackingInfo> info))block
timeout:(NSTimeInterval)timeout
{
AssertProperQueue();
XMPPBasicTrackingInfo *trackingInfo;
trackingInfo = [[XMPPBasicTrackingInfo alloc] initWithBlock:block timeout:timeout];
[self addElement:element trackingInfo:trackingInfo];
}
- (void)addID:(NSString *)elementID trackingInfo:(id <XMPPTrackingInfo>)trackingInfo
{
AssertProperQueue();
dict[elementID] = trackingInfo;
[trackingInfo setElementID:elementID];
[trackingInfo createTimerWithDispatchQueue:queue];
}
- (void)addElement:(XMPPElement *)element trackingInfo:(id <XMPPTrackingInfo>)trackingInfo
{
AssertProperQueue();
if([[element elementID] length] == 0) return;
dict[[element elementID]] = trackingInfo;
[trackingInfo setElementID:[element elementID]];
[trackingInfo setElement:element];
[trackingInfo createTimerWithDispatchQueue:queue];
}
- (BOOL)invokeForID:(NSString *)elementID withObject:(id)obj
{
AssertProperQueue();
if([elementID length] == 0) return NO;
id <XMPPTrackingInfo> info = dict[elementID];
if (info)
{
[info invokeWithObject:obj];
[info cancelTimer];
[dict removeObjectForKey:elementID];
return YES;
}
return NO;
}
- (BOOL)invokeForElement:(XMPPElement *)element withObject:(id)obj
{
AssertProperQueue();
NSString *elementID = [element elementID];
if ([elementID length] == 0) return NO;
id <XMPPTrackingInfo> info = dict[elementID];
if(info)
{
BOOL valid = YES;
if(xmppStream && [element isKindOfClass:[XMPPIQ class]] && [[info element] isKindOfClass:[XMPPIQ class]])
{
XMPPIQ *iq = (XMPPIQ *)element;
if([iq isResultIQ] || [iq isErrorIQ])
{
valid = [xmppStream isValidResponseElement:iq forRequestElement:[info element]];
}
}
if(!valid)
{
XMPPLogError(@"%s: Element with ID %@ cannot be validated.", __FILE__ , [element elementID]);
}
if (valid)
{
[info invokeWithObject:obj];
[info cancelTimer];
[dict removeObjectForKey:[element elementID]];
return YES;
}
}
return NO;
}
- (NSUInteger)numberOfIDs
{
AssertProperQueue();
return [[dict allKeys] count];
}
- (void)removeID:(NSString *)elementID
{
AssertProperQueue();
id <XMPPTrackingInfo> info = dict[elementID];
if (info)
{
[info cancelTimer];
[dict removeObjectForKey:elementID];
}
}
- (void)removeAllIDs
{
AssertProperQueue();
for (id <XMPPTrackingInfo> info in [dict objectEnumerator])
{
[info cancelTimer];
}
[dict removeAllObjects];
}
@end
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark -
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@implementation XMPPBasicTrackingInfo
@synthesize timeout;
@synthesize elementID;
@synthesize element;
- (id)init
{
// Use initWithTarget:selector:timeout: or initWithBlock:timeout:
return nil;
}
- (id)initWithTarget:(id)aTarget selector:(SEL)aSelector timeout:(NSTimeInterval)aTimeout
{
if(target || selector)
{
NSParameterAssert(aTarget);
NSParameterAssert(aSelector);
}
if ((self = [super init]))
{
target = aTarget;
selector = aSelector;
timeout = aTimeout;
}
return self;
}
- (id)initWithBlock:(void (^)(id obj, id <XMPPTrackingInfo> info))aBlock timeout:(NSTimeInterval)aTimeout
{
NSParameterAssert(aBlock);
if ((self = [super init]))
{
block = [aBlock copy];
timeout = aTimeout;
}
return self;
}
- (void)dealloc
{
[self cancelTimer];
target = nil;
selector = NULL;
}
- (void)createTimerWithDispatchQueue:(dispatch_queue_t)queue
{
NSAssert(queue != NULL, @"Method invoked with NULL queue");
NSAssert(timer == NULL, @"Method invoked multiple times");
if (timeout > 0.0)
{
timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
dispatch_source_set_event_handler(timer, ^{ @autoreleasepool {
[self invokeWithObject:nil];
[self cancelTimer];
}});
dispatch_time_t tt = dispatch_time(DISPATCH_TIME_NOW, (timeout * NSEC_PER_SEC));
dispatch_source_set_timer(timer, tt, DISPATCH_TIME_FOREVER, 0);
dispatch_resume(timer);
}
}
- (void)cancelTimer
{
if (timer)
{
dispatch_source_cancel(timer);
#if !OS_OBJECT_USE_OBJC
dispatch_release(timer);
#endif
timer = NULL;
}
}
- (void)invokeWithObject:(id)obj
{
if (block)
{
block(obj, self);
}
else if(target && selector)
{
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
[target performSelector:selector withObject:obj withObject:self];
#pragma clang diagnostic pop
}
}
@end