commit | author | age
|
ea8e18
|
1 |
// |
L |
2 |
// MQTTSessionSynchron.h |
|
3 |
// MQTTClient.framework |
|
4 |
// |
|
5 |
|
|
6 |
/** |
|
7 |
Synchronous API |
|
8 |
|
|
9 |
@author Christoph Krey krey.christoph@gmail.com |
|
10 |
@copyright Copyright © 2013-2016, Christoph Krey |
|
11 |
|
|
12 |
*/ |
|
13 |
|
|
14 |
|
|
15 |
#import <Foundation/Foundation.h> |
|
16 |
#import "MQTTSession.h" |
|
17 |
|
|
18 |
@interface MQTTSession(Synchron) |
|
19 |
|
|
20 |
/** connects to the specified MQTT server synchronously |
|
21 |
|
|
22 |
@param timeout defines the maximum time to wait. Defaults to 0 for no timeout. |
|
23 |
|
|
24 |
@return true if the connection was established |
|
25 |
|
|
26 |
@code |
|
27 |
#import "MQTTClient.h" |
|
28 |
|
|
29 |
MQTTSession *session = [[MQTTSession alloc] init]; |
|
30 |
|
|
31 |
[session connectAndWaitTimeout:30]; |
|
32 |
@endcode |
|
33 |
|
|
34 |
*/ |
|
35 |
- (BOOL)connectAndWaitTimeout:(NSTimeInterval)timeout; |
|
36 |
|
|
37 |
|
|
38 |
/** connects to the specified MQTT server synchronously |
|
39 |
|
|
40 |
@param host see connectAndWaitToHost:port:usingSSL:timeout: for details |
|
41 |
@param port see connectAndWaitToHost:port:usingSSL:timeout: for details |
|
42 |
@param usingSSL see connectAndWaitToHost:port:usingSSL:timeout: for details |
|
43 |
|
|
44 |
@return true if the connection was established |
|
45 |
|
|
46 |
@code |
|
47 |
#import "MQTTClient.h" |
|
48 |
|
|
49 |
MQTTSession *session = [[MQTTSession alloc] init]; |
|
50 |
|
|
51 |
[session connectAndWaitToHost:@"192.168.0.1" port:1883 usingSSL:NO]; |
|
52 |
@endcode |
|
53 |
|
|
54 |
@deprecated as not all connection parameters are supported, use connectAndWaitTimeout |
|
55 |
|
|
56 |
*/ |
|
57 |
- (BOOL)connectAndWaitToHost:(NSString *)host |
|
58 |
port:(UInt32)port |
|
59 |
usingSSL:(BOOL)usingSSL; |
|
60 |
|
|
61 |
/** connects to the specified MQTT server synchronously |
|
62 |
|
|
63 |
@param host specifies the hostname or ip address to connect to. Defaults to @"localhost". |
|
64 |
@param port spefifies the port to connect to |
|
65 |
@param usingSSL specifies whether to use SSL or not |
|
66 |
@param timeout defines the maximum time to wait |
|
67 |
|
|
68 |
@return true if the connection was established |
|
69 |
|
|
70 |
@code |
|
71 |
#import "MQTTClient.h" |
|
72 |
|
|
73 |
MQTTSession *session = [[MQTTSession alloc] init]; |
|
74 |
|
|
75 |
[session connectAndWaitToHost:@"192.168.0.1" port:1883 usingSSL:NO]; |
|
76 |
@endcode |
|
77 |
@deprecated as not all connection parameters are supported, use connectAndWaitTimeout |
|
78 |
|
|
79 |
*/ |
|
80 |
|
|
81 |
- (BOOL)connectAndWaitToHost:(NSString *)host |
|
82 |
port:(UInt32)port |
|
83 |
usingSSL:(BOOL)usingSSL |
|
84 |
timeout:(NSTimeInterval)timeout; |
|
85 |
|
|
86 |
/** subscribes to a topic at a specific QoS level synchronously |
|
87 |
|
|
88 |
@param topic the Topic Filter to subscribe to. |
|
89 |
|
|
90 |
@param qosLevel specifies the QoS Level of the subscription. |
|
91 |
qosLevel can be 0, 1, or 2. |
|
92 |
|
|
93 |
@return TRUE if successfully subscribed |
|
94 |
|
|
95 |
@code |
|
96 |
#import "MQTTClient.h" |
|
97 |
|
|
98 |
MQTTSession *session = [[MQTTSession alloc] init]; |
|
99 |
|
|
100 |
[session connectAndWaitTimeout:30]; |
|
101 |
|
|
102 |
[session subscribeAndWaitToTopic:@"example/#" atLevel:2]; |
|
103 |
|
|
104 |
@endcode |
|
105 |
|
|
106 |
*/ |
|
107 |
- (BOOL)subscribeAndWaitToTopic:(NSString *)topic |
|
108 |
atLevel:(MQTTQosLevel)qosLevel; |
|
109 |
|
|
110 |
/** subscribes to a topic at a specific QoS level synchronously |
|
111 |
|
|
112 |
@param topic the Topic Filter to subscribe to. |
|
113 |
|
|
114 |
@param qosLevel specifies the QoS Level of the subscription. |
|
115 |
qosLevel can be 0, 1, or 2. |
|
116 |
@param timeout defines the maximum time to wait |
|
117 |
|
|
118 |
@return TRUE if successfully subscribed |
|
119 |
|
|
120 |
@code |
|
121 |
#import "MQTTClient.h" |
|
122 |
|
|
123 |
MQTTSession *session = [[MQTTSession alloc] init]; |
|
124 |
|
|
125 |
[session connectAndWaitTimeout:30]; |
|
126 |
|
|
127 |
[session subscribeAndWaitToTopic:@"example/#" atLevel:2 timeout:10]; |
|
128 |
|
|
129 |
@endcode |
|
130 |
|
|
131 |
*/ |
|
132 |
- (BOOL)subscribeAndWaitToTopic:(NSString *)topic |
|
133 |
atLevel:(MQTTQosLevel)qosLevel |
|
134 |
timeout:(NSTimeInterval)timeout; |
|
135 |
|
|
136 |
/** subscribes a number of topics |
|
137 |
|
|
138 |
@param topics an NSDictionary<NSString *, NSNumber *> containing the Topic Filters to subscribe to as keys and |
|
139 |
the corresponding QoS as NSNumber values |
|
140 |
|
|
141 |
@return the Message Identifier of the SUBSCRIBE message. |
|
142 |
|
|
143 |
@note returns immediately. To check results, register as an MQTTSessionDelegate and watch for events. |
|
144 |
|
|
145 |
@code |
|
146 |
#import "MQTTClient.h" |
|
147 |
|
|
148 |
MQTTSession *session = [[MQTTSession alloc] init]; |
|
149 |
|
|
150 |
[session connectAndWaitTimeout:30]; |
|
151 |
|
|
152 |
[session subscribeAndWaitToTopics:@{ |
|
153 |
@"example/#": @(0), |
|
154 |
@"example/status": @(2), |
|
155 |
@"other/#": @(1) |
|
156 |
}]; |
|
157 |
|
|
158 |
@endcode |
|
159 |
*/ |
|
160 |
- (BOOL)subscribeAndWaitToTopics:(NSDictionary<NSString *, NSNumber *> *)topics; |
|
161 |
|
|
162 |
/** subscribes a number of topics |
|
163 |
|
|
164 |
@param topics an NSDictionary<NSString *, NSNumber *> containing the Topic Filters to subscribe to as keys and |
|
165 |
the corresponding QoS as NSNumber values |
|
166 |
@param timeout defines the maximum time to wait |
|
167 |
|
|
168 |
@return TRUE if the subscribe was succesfull |
|
169 |
|
|
170 |
@code |
|
171 |
#import "MQTTClient.h" |
|
172 |
|
|
173 |
MQTTSession *session = [[MQTTSession alloc] init]; |
|
174 |
|
|
175 |
[session connectAndWaitTimeout:30]; |
|
176 |
|
|
177 |
[session subscribeAndWaitToTopics:@{ |
|
178 |
@"example/#": @(0), |
|
179 |
@"example/status": @(2), |
|
180 |
@"other/#": @(1) |
|
181 |
} |
|
182 |
timeout:10]; |
|
183 |
|
|
184 |
@endcode |
|
185 |
*/ |
|
186 |
- (BOOL)subscribeAndWaitToTopics:(NSDictionary<NSString *, NSNumber *> *)topics |
|
187 |
timeout:(NSTimeInterval)timeout; |
|
188 |
|
|
189 |
|
|
190 |
/** unsubscribes from a topic synchronously |
|
191 |
|
|
192 |
@param topic the Topic Filter to unsubscribe from. |
|
193 |
|
|
194 |
@return TRUE if sucessfully unsubscribed |
|
195 |
|
|
196 |
@code |
|
197 |
#import "MQTTClient.h" |
|
198 |
|
|
199 |
MQTTSession *session = [[MQTTSession alloc] init]; |
|
200 |
|
|
201 |
[session connectAndWaitTimeout:30]; |
|
202 |
|
|
203 |
... |
|
204 |
|
|
205 |
[session unsubscribeAndWaitTopic:@"example/#"]; |
|
206 |
|
|
207 |
@endcode |
|
208 |
*/ |
|
209 |
- (BOOL)unsubscribeAndWaitTopic:(NSString *)topic; |
|
210 |
|
|
211 |
/** unsubscribes from a topic synchronously |
|
212 |
|
|
213 |
@param topic the Topic Filter to unsubscribe from. |
|
214 |
@param timeout defines the maximum time to wait |
|
215 |
|
|
216 |
@return TRUE if sucessfully unsubscribed |
|
217 |
|
|
218 |
@code |
|
219 |
#import "MQTTClient.h" |
|
220 |
|
|
221 |
MQTTSession *session = [[MQTTSession alloc] init]; |
|
222 |
|
|
223 |
[session connectAndWaitTimeout:30]; |
|
224 |
|
|
225 |
... |
|
226 |
|
|
227 |
[session unsubscribeAndWaitTopic:@"example/#" timeout:10]; |
|
228 |
|
|
229 |
@endcode |
|
230 |
*/ |
|
231 |
- (BOOL)unsubscribeAndWaitTopic:(NSString *)topic |
|
232 |
timeout:(NSTimeInterval)timeout; |
|
233 |
|
|
234 |
|
|
235 |
/** unsubscribes from a number of topics synchronously |
|
236 |
|
|
237 |
@param topics an NSArray<NSString *> of topics to unsubscribe from |
|
238 |
|
|
239 |
@return TRUE if the unsubscribe was successful |
|
240 |
|
|
241 |
@code |
|
242 |
#import "MQTTClient.h" |
|
243 |
|
|
244 |
MQTTSession *session = [[MQTTSession alloc] init]; |
|
245 |
|
|
246 |
[session connectAndWaitTimeout:30]; |
|
247 |
|
|
248 |
... |
|
249 |
|
|
250 |
[session unsubscribeAndWaitTopics:@[ |
|
251 |
@"example/#", |
|
252 |
@"example/status", |
|
253 |
@"other/#" |
|
254 |
]]; |
|
255 |
|
|
256 |
@endcode |
|
257 |
|
|
258 |
*/ |
|
259 |
- (BOOL)unsubscribeAndWaitTopics:(NSArray<NSString *> *)topics; |
|
260 |
|
|
261 |
/** unsubscribes from a number of topics synchronously |
|
262 |
|
|
263 |
@param topics an NSArray<NSString *> of topics to unsubscribe from |
|
264 |
@param timeout defines the maximum time to wait |
|
265 |
|
|
266 |
@return TRUE if the unsubscribe was successful |
|
267 |
|
|
268 |
@code |
|
269 |
#import "MQTTClient.h" |
|
270 |
|
|
271 |
MQTTSession *session = [[MQTTSession alloc] init]; |
|
272 |
|
|
273 |
[session connectAndWaitTimeout:30]; |
|
274 |
|
|
275 |
... |
|
276 |
|
|
277 |
[session unsubscribeAndWaitTopics:@[ |
|
278 |
@"example/#", |
|
279 |
@"example/status", |
|
280 |
@"other/#" |
|
281 |
] |
|
282 |
timeout:10]; |
|
283 |
|
|
284 |
@endcode |
|
285 |
|
|
286 |
*/ |
|
287 |
- (BOOL)unsubscribeAndWaitTopics:(NSArray<NSString *> *)topics |
|
288 |
timeout:(NSTimeInterval)timeout; |
|
289 |
|
|
290 |
|
|
291 |
/** publishes synchronously data |
|
292 |
|
|
293 |
@param data see publishAndWaitData:onTopic:retain:qos:timeout: for details |
|
294 |
@param topic see publishAndWaitData:onTopic:retain:qos:timeout: for details |
|
295 |
@param retainFlag see publishAndWaitData:onTopic:retain:qos:timeout: for details |
|
296 |
@param qos see publishAndWaitData:onTopic:retain:qos:timeout: for details |
|
297 |
@returns TRUE if the publish was successful |
|
298 |
|
|
299 |
@code |
|
300 |
#import "MQTTClient.h" |
|
301 |
|
|
302 |
MQTTSession *session = [[MQTTSession alloc] init]; |
|
303 |
|
|
304 |
[session connectAndWaitTimeout:30]; |
|
305 |
|
|
306 |
[session publishAndWaitData:[@"Sample Data" dataUsingEncoding:NSUTF8StringEncoding] |
|
307 |
topic:@"example/data" |
|
308 |
retain:YES |
|
309 |
qos:1]; |
|
310 |
@endcode |
|
311 |
|
|
312 |
*/ |
|
313 |
- (BOOL)publishAndWaitData:(NSData *)data |
|
314 |
onTopic:(NSString *)topic |
|
315 |
retain:(BOOL)retainFlag |
|
316 |
qos:(MQTTQosLevel)qos; |
|
317 |
|
|
318 |
/** publishes synchronously data on a given topic at a specified QoS level and retain flag |
|
319 |
|
|
320 |
@param data the data to be sent. length may range from 0 to 268,435,455 - 4 - _lengthof-topic_ bytes. Defaults to length 0. |
|
321 |
@param topic the Topic to identify the data |
|
322 |
@param retainFlag if YES, data is stored on the MQTT broker until overwritten by the next publish with retainFlag = YES |
|
323 |
@param qos specifies the Quality of Service for the publish |
|
324 |
qos can be 0, 1, or 2. |
|
325 |
@param timeout defines the maximum time to wait |
|
326 |
@returns TRUE if the publish was successful |
|
327 |
|
|
328 |
@code |
|
329 |
#import "MQTTClient.h" |
|
330 |
|
|
331 |
MQTTSession *session = [[MQTTSession alloc] init]; |
|
332 |
|
|
333 |
[session connectAndWaitTimeout:30]; |
|
334 |
|
|
335 |
[session publishAndWaitData:[@"Sample Data" dataUsingEncoding:NSUTF8StringEncoding] |
|
336 |
topic:@"example/data" |
|
337 |
retain:YES |
|
338 |
qos:1 |
|
339 |
timeout:10]; |
|
340 |
@endcode |
|
341 |
|
|
342 |
*/ |
|
343 |
- (BOOL)publishAndWaitData:(NSData *)data |
|
344 |
onTopic:(NSString *)topic |
|
345 |
retain:(BOOL)retainFlag |
|
346 |
qos:(MQTTQosLevel)qos |
|
347 |
timeout:(NSTimeInterval)timeout; |
|
348 |
|
|
349 |
/** closes an MQTTSession gracefully synchronously |
|
350 |
If the connection was successfully established before, a DISCONNECT is sent. |
|
351 |
|
|
352 |
@code |
|
353 |
#import "MQTTClient.h" |
|
354 |
|
|
355 |
MQTTSession *session = [[MQTTSession alloc] init]; |
|
356 |
|
|
357 |
[session connectAndWaitTimeout:30]; |
|
358 |
|
|
359 |
... |
|
360 |
|
|
361 |
[session closeAndWait]; |
|
362 |
|
|
363 |
@endcode |
|
364 |
|
|
365 |
*/ |
|
366 |
- (void)closeAndWait; |
|
367 |
|
|
368 |
/** closes an MQTTSession gracefully synchronously |
|
369 |
@param timeout defines the maximum time to wait |
|
370 |
|
|
371 |
If the connection was successfully established before, a DISCONNECT is sent. |
|
372 |
|
|
373 |
@code |
|
374 |
#import "MQTTClient.h" |
|
375 |
|
|
376 |
MQTTSession *session = [[MQTTSession alloc] init]; |
|
377 |
|
|
378 |
[session connectAndWaitTimeout:30]; |
|
379 |
|
|
380 |
... |
|
381 |
|
|
382 |
[session closeAndWait:10]; |
|
383 |
|
|
384 |
@endcode |
|
385 |
|
|
386 |
*/ |
|
387 |
- (void)closeAndWait:(NSTimeInterval)timeout; |
|
388 |
|
|
389 |
@end |