XMPPJabberRPCModule.m
8.46 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
//
// XMPPJabberRPCModule.m
// XEP-0009
//
// Originally created by Eric Chamberlain on 5/16/10.
//
#import "XMPPJabberRPCModule.h"
#import "XMPP.h"
#import "XMPPIQ+JabberRPC.h"
#import "XMPPIQ+JabberRPCResonse.h"
#import "XMPPLogging.h"
#import "XMPPFramework.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_WARN; // | XMPP_LOG_FLAG_TRACE;
#else
static const int xmppLogLevel = XMPP_LOG_LEVEL_WARN;
#endif
NSString *const XMPPJabberRPCErrorDomain = @"XMPPJabberRPCErrorDomain";
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark -
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@interface RPCID : NSObject
{
NSString *rpcID;
dispatch_source_t timer;
}
@property (nonatomic, readonly) NSString *rpcID;
@property (nonatomic, readonly) dispatch_source_t timer;
- (id)initWithRpcID:(NSString *)rpcID timer:(dispatch_source_t)timer;
- (void)cancelTimer;
@end
@implementation RPCID
@synthesize rpcID;
@synthesize timer;
- (id)initWithRpcID:(NSString *)aRpcID timer:(dispatch_source_t)aTimer
{
if ((self = [super init]))
{
rpcID = [aRpcID copy];
timer = aTimer;
#if !OS_OBJECT_USE_OBJC
dispatch_retain(timer);
#endif
}
return self;
}
- (BOOL)isEqual:(id)anObject
{
return [rpcID isEqual:anObject];
}
- (NSUInteger)hash
{
return [rpcID hash];
}
- (void)cancelTimer
{
if (timer)
{
dispatch_source_cancel(timer);
#if !OS_OBJECT_USE_OBJC
dispatch_release(timer);
#endif
timer = NULL;
}
}
- (void)dealloc
{
[self cancelTimer];
}
@end
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark -
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@implementation XMPPJabberRPCModule
@dynamic defaultTimeout;
- (NSTimeInterval)defaultTimeout
{
__block NSTimeInterval result;
dispatch_block_t block = ^{
result = defaultTimeout;
};
if (dispatch_get_specific(moduleQueueTag))
block();
else
dispatch_sync(moduleQueue, block);
return result;
}
- (void)setDefaultTimeout:(NSTimeInterval)newDefaultTimeout
{
dispatch_block_t block = ^{
XMPPLogTrace();
defaultTimeout = newDefaultTimeout;
};
if (dispatch_get_specific(moduleQueueTag))
block();
else
dispatch_async(moduleQueue, block);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark Module Management
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- (id)init
{
return [self initWithDispatchQueue:NULL];
}
- (id)initWithDispatchQueue:(dispatch_queue_t)queue
{
if ((self = [super initWithDispatchQueue:queue]))
{
XMPPLogTrace();
rpcIDs = [[NSMutableDictionary alloc] initWithCapacity:5];
defaultTimeout = 5.0;
}
return self;
}
- (BOOL)activate:(XMPPStream *)aXmppStream
{
XMPPLogTrace();
if ([super activate:aXmppStream])
{
#ifdef _XMPP_CAPABILITIES_H
[xmppStream autoAddDelegate:self delegateQueue:moduleQueue toModulesOfClass:[XMPPCapabilities class]];
#endif
return YES;
}
return NO;
}
- (void)deactivate
{
XMPPLogTrace();
#ifdef _XMPP_CAPABILITIES_H
[xmppStream removeAutoDelegate:self delegateQueue:moduleQueue fromModulesOfClass:[XMPPCapabilities class]];
#endif
[super deactivate];
}
- (void)dealloc
{
XMPPLogTrace();
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark Send RPC
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- (NSString *)sendRpcIQ:(XMPPIQ *)iq
{
return [self sendRpcIQ:iq withTimeout:[self defaultTimeout]];
}
- (NSString *)sendRpcIQ:(XMPPIQ *)iq withTimeout:(NSTimeInterval)timeout
{
XMPPLogTrace();
NSString *elementID = [iq elementID];
dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, moduleQueue);
dispatch_source_set_event_handler(timer, ^{ @autoreleasepool {
[self timeoutRemoveRpcID:elementID];
}});
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);
RPCID *rpcID = [[RPCID alloc] initWithRpcID:elementID timer:timer];
rpcIDs[elementID] = rpcID;
[xmppStream sendElement:iq];
return elementID;
}
- (void)timeoutRemoveRpcID:(NSString *)elementID
{
XMPPLogTrace();
RPCID *rpcID = rpcIDs[elementID];
if (rpcID)
{
[rpcID cancelTimer];
[rpcIDs removeObjectForKey:elementID];
NSError *error = [NSError errorWithDomain:XMPPJabberRPCErrorDomain
code:1400
userInfo:@{@"error" : @"Request timed out"}];
[multicastDelegate jabberRPC:self elementID:elementID didReceiveError:error];
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark XMPPStream delegate
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- (BOOL)xmppStream:(XMPPStream *)sender didReceiveIQ:(XMPPIQ *)iq
{
XMPPLogTrace();
if ([iq isJabberRPC])
{
if ([iq isResultIQ] || [iq isErrorIQ])
{
// Example:
//
// <iq from="deusty.com" to="robbiehanson@deusty.com/work" id="abc123" type="result"/>
NSString *elementID = [iq elementID];
// check if this is a JabberRPC query
// we could check the query element, but we should be able to do a lookup based on the unique elementID
// because we send an ID, we should get one back
RPCID *rpcID = rpcIDs[elementID];
if (rpcID == nil)
{
return NO;
}
XMPPLogVerbose(@"%@: Received RPC response!", THIS_FILE);
if ([iq isResultIQ])
{
id response;
NSError *error = nil;
//TODO: parse iq and generate response.
response = [iq methodResponse:&error];
if (error == nil) {
[multicastDelegate jabberRPC:self elementID:elementID didReceiveMethodResponse:response];
} else {
[multicastDelegate jabberRPC:self elementID:elementID didReceiveError:error];
}
}
else
{
// TODO: implement error parsing
// not much specified in XEP, only 403 forbidden error
NSXMLElement *errorElement = [iq childErrorElement];
NSError *error = [NSError errorWithDomain:XMPPJabberRPCErrorDomain
code:[errorElement attributeIntValueForName:@"code"]
userInfo:@{
@"error" : [errorElement attributesAsDictionary],
@"condition" : [[errorElement childAtIndex:0] name],
@"iq" : iq}];
[multicastDelegate jabberRPC:self elementID:elementID didReceiveError:error];
}
[rpcID cancelTimer];
[rpcIDs removeObjectForKey:elementID];
#ifdef _XMPP_CAPABILITIES_H
} else if ([iq isSetIQ]) {
// we would receive set when implementing Jabber-RPC server
[multicastDelegate jabberRPC:self didReceiveSetIQ:iq];
#endif
}
// Jabber-RPC doesn't use get iq type
}
return NO;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark XMPPCapabilities delegate
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#ifdef _XMPP_CAPABILITIES_H
/**
* If an XMPPCapabilites instance is used we want to advertise our support for JabberRPC.
**/
- (void)xmppCapabilities:(XMPPCapabilities *)sender collectingMyCapabilities:(NSXMLElement *)query
{
XMPPLogTrace();
// <query xmlns="http://jabber.org/protocol/disco#info">
// ...
// <identity category='automation' type='rpc'/>
// <feature var='jabber:iq:rpc'/>
// ...
// </query>
[query addChild:[XMPPIQ elementRpcIdentity]];
[query addChild:[XMPPIQ elementRpcFeature]];
}
#endif
@end