XMPPModule.m
5.01 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
#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
@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;
#if !OS_OBJECT_USE_OBJC
dispatch_retain(moduleQueue);
#endif
}
else
{
const char *moduleQueueName = [[self moduleName] UTF8String];
moduleQueue = dispatch_queue_create(moduleQueueName, NULL);
}
moduleQueueTag = &moduleQueueTag;
dispatch_queue_set_specific(moduleQueue, moduleQueueTag, moduleQueueTag, NULL);
multicastDelegate = [[GCDMulticastDelegate alloc] init];
}
return self;
}
- (void)dealloc
{
#if !OS_OBJECT_USE_OBJC
dispatch_release(moduleQueue);
#endif
}
/**
* The activate method is the point at which the module gets plugged into the xmpp stream.
*
* It is recommended that subclasses override didActivate, instead of this method,
* to perform any custom actions upon activation.
**/
- (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];
[self didActivate];
}
};
if (dispatch_get_specific(moduleQueueTag))
block();
else
dispatch_sync(moduleQueue, block);
return result;
}
/**
* It is recommended that subclasses override this method (instead of activate:)
* to perform tasks after the module has been activated.
*
* This method is only invoked if the module is successfully activated.
* This method is always invoked on the moduleQueue.
**/
- (void)didActivate
{
// Override me to do custom work after the module is activated
}
/**
* 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.
*
* It is recommended that subclasses override didDeactivate, instead of this method,
* to perform any custom actions upon deactivation.
**/
- (void)deactivate
{
dispatch_block_t block = ^{
if (xmppStream)
{
[self willDeactivate];
[xmppStream removeDelegate:self delegateQueue:moduleQueue];
[xmppStream unregisterModule:self];
xmppStream = nil;
}
};
if (dispatch_get_specific(moduleQueueTag))
block();
else
dispatch_sync(moduleQueue, block);
}
/**
* It is recommended that subclasses override this method (instead of deactivate:)
* to perform tasks after the module has been deactivated.
*
* This method is only invoked if the module is transitioning from activated to deactivated.
* This method is always invoked on the moduleQueue.
**/
- (void)willDeactivate
{
// Override me to do custom work after the module is deactivated
}
- (dispatch_queue_t)moduleQueue
{
return moduleQueue;
}
- (void *)moduleQueueTag
{
return moduleQueueTag;
}
- (XMPPStream *)xmppStream
{
if (dispatch_get_specific(moduleQueueTag))
{
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_specific(moduleQueueTag))
block();
else
dispatch_async(moduleQueue, block);
}
- (void)removeDelegate:(id)delegate delegateQueue:(dispatch_queue_t)delegateQueue synchronously:(BOOL)synchronously
{
dispatch_block_t block = ^{
[multicastDelegate removeDelegate:delegate delegateQueue:delegateQueue];
};
if (dispatch_get_specific(moduleQueueTag))
block();
else if (synchronously)
dispatch_sync(moduleQueue, block);
else
dispatch_async(moduleQueue, block);
}
- (void)removeDelegate:(id)delegate delegateQueue:(dispatch_queue_t)delegateQueue
{
// Synchronous operation (common-case default)
[self removeDelegate:delegate delegateQueue:delegateQueue synchronously:YES];
}
- (void)removeDelegate:(id)delegate
{
// Synchronous operation (common-case default)
[self removeDelegate:delegate delegateQueue:NULL synchronously:YES];
}
- (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