/
XMPPModule.m
191 lines (157 loc) · 3.99 KB
/
XMPPModule.m
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
#import "XMPPModule.h"
#import "XMPPStream.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
#if DEBUG
static const int xmppLogLevel = XMPP_LOG_LEVEL_WARN;
#else
static const int xmppLogLevel = XMPP_LOG_LEVEL_WARN;
#endif
@implementation XMPPModule
/**
* Standard init method.
**/
- (id)init
{
return [self initWithDispatchQueue:NULL];
}
/**
* Designated initializer.
**/
- (id)initWithDispatchQueue:(dispatch_queue_t)queue
{
if ((self = [super init]))
{
if (queue)
{
moduleQueue = queue;
dispatch_retain(moduleQueue);
}
else
{
const char *moduleQueueName = [[self moduleName] UTF8String];
moduleQueue = dispatch_queue_create(moduleQueueName, NULL);
}
multicastDelegate = [[GCDMulticastDelegate alloc] init];
}
return self;
}
- (void)dealloc
{
dispatch_release(moduleQueue);
}
/**
* The activate method is the point at which the module gets plugged into the xmpp stream.
* Subclasses may override this method to perform any custom actions,
* but must invoke [super activate:aXmppStream] at some point within their implementation.
**/
- (BOOL)activate:(XMPPStream *)aXmppStream
{
__block BOOL result = YES;
dispatch_block_t block = ^{
if (xmppStream != nil)
{
result = NO;
}
else
{
xmppStream = aXmppStream;
[xmppStream addDelegate:self delegateQueue:moduleQueue];
[xmppStream registerModule:self];
}
};
if (dispatch_get_current_queue() == moduleQueue)
block();
else
dispatch_sync(moduleQueue, block);
return result;
}
/**
* The deactivate method unplugs a module from the xmpp stream.
* When this method returns, no further delegate methods on this module will be dispatched.
* However, there may be delegate methods that have already been dispatched.
* If this is the case, the module will be properly retained until the delegate methods have completed.
* If your custom module requires that delegate methods are not run after the deactivate method has been run,
* then simply check the xmppStream variable in your delegate methods.
**/
- (void)deactivate
{
dispatch_block_t block = ^{
if (xmppStream)
{
[xmppStream removeDelegate:self delegateQueue:moduleQueue];
[xmppStream unregisterModule:self];
xmppStream = nil;
}
};
if (dispatch_get_current_queue() == moduleQueue)
block();
else
dispatch_sync(moduleQueue, block);
}
- (dispatch_queue_t)moduleQueue
{
return moduleQueue;
}
- (XMPPStream *)xmppStream
{
if (dispatch_get_current_queue() == moduleQueue)
{
return xmppStream;
}
else
{
__block XMPPStream *result;
dispatch_sync(moduleQueue, ^{
result = xmppStream;
});
return result;
}
}
- (void)addDelegate:(id)delegate delegateQueue:(dispatch_queue_t)delegateQueue
{
// Asynchronous operation (if outside xmppQueue)
dispatch_block_t block = ^{
[multicastDelegate addDelegate:delegate delegateQueue:delegateQueue];
};
if (dispatch_get_current_queue() == moduleQueue)
block();
else
dispatch_async(moduleQueue, block);
}
- (void)removeDelegate:(id)delegate delegateQueue:(dispatch_queue_t)delegateQueue
{
// Synchronous operation
//
// Delegate removal MUST always be synchronous.
dispatch_block_t block = ^{
[multicastDelegate removeDelegate:delegate delegateQueue:delegateQueue];
};
if (dispatch_get_current_queue() == moduleQueue)
block();
else
dispatch_sync(moduleQueue, block);
}
- (void)removeDelegate:(id)delegate
{
// Synchronous operation
//
// Delegate remove MUST always be synchronous.
dispatch_block_t block = ^{
[multicastDelegate removeDelegate:delegate];
};
if (dispatch_get_current_queue() == moduleQueue)
block();
else
dispatch_sync(moduleQueue, block);
}
- (NSString *)moduleName
{
// Override me (if needed) to provide a customized module name.
// This name is used as the name of the dispatch_queue which could aid in debugging.
return NSStringFromClass([self class]);
}
@end