MQTT protocol iOS 1 after porting
Last Update:2015-07-27
Source: Internet
Author: User
<span id="Label3"></p><p style="margin-top:0px; margin-bottom:0px; font-size:11px; font-family:Menlo; color:rgb(255,70,71)"><p style="margin-top:0px; margin-bottom:0px; font-size:11px; font-family:Menlo; color:rgb(255,70,71)">MQTTClient.h</p></p><p style="margin-top:0px; margin-bottom:0px; font-size:11px; font-family:Menlo; color:rgb(255,70,71)"><p style="margin-top:0px; margin-bottom:0px; font-size:11px; font-family:Menlo; color:rgb(255,70,71)"></p></p><pre code_snippet_id="417071" snippet_file_name="blog_20140704_1_3093946" name="code" class="objc">#import <Foundation/Foundation.h> @protocol mqttdelegate <nsobject>/** * @brief Connection Server * * @param [in] N/A * @param [out] N/A * @return void * @note */-(void) didconnect: (nsuinteger) code;/** * @brief disconnected from server * * @param [in] N /a * @param [out] n/a * @return void * @note */-(void) diddisconnect;/** * @brief * * @param [in] N/A * @param [out] N/A * @return void * @note */-(void) didpublish: (nsuinteger) messageid;//fixme:create mosquittomessage class/** * @brief Receive Message * * @param [in] N/A * @param [out] N/A * @return void * @note */-(void) didreceivemessage: (nsstring*) message topic: (NSSTRING*) topic;/** * @brief * * @param [in] N/A * @param [out] N/A * @return void * @note */-(void) didsubscribe: (NSU Integer) messageId grantedqos: (nsarray*) qos;/** * @brief * * @param [in] N/A * @param [out] N/A * @return void * @note */- (void) didunsubscribe: (nsuinteger) messageId; @end @interface mqttclient:nsobject{struct mosquitto *mosq; NSString *host; unsigned short port; NSString *username; NSString *password; unsigned short keepAlive; BOOL cleansession; Id<mqttdelegate> delegate; Nstimer *timer;} @property (readwrite,retain) nsstring *host; @property (readwrite,assign) unsigned short port; @property ( Readwrite,retain) nsstring *username; @property (readwrite,retain) nsstring *password; @property (re Adwrite,assign) unsigned short keepAlive; @property (readwrite,assign) BOOL cleansession; @property (r Eadwrite,assign) id<mqttdelegate> delegate;/** * @brief Singleton mode setting * * @param [in] N/A * @param [out] N/A * @return void * @note */+ (mqttclient *) getmqttinstance;/** * @brief * * @param [in] N/A * @param [out] N/A * @return void * @note */+ (void) initialize;/** * @brief * * @param [in] N/A * @param [out] N/A * @return void * @note */+ (nsstring*) version;/ * * @brieF * * @param [in] N/A * @param [out] N/A * @return void * @note */-(mqttclient *) initwithclientid: (nsstring*) clientId; /** * @brief * * @param [in] N/A * @param [out] N/A * @return void * @note */-(void) setmessageretry: (nsuinteger) seconds ;/** * @brief Connection Server * * @param [in] N/A * @param [out] N/A * @return void * @note */-(void) connect;/** * @brief Connect to Host * * @param [in] N/A * @param [out] N/A * @return void * @note */-(void) connecttohost: (nsstring*) host;/** * @brie F * * @param [in] N/A * @param [out] N/A * @return void * @note */-(void) reconnect;/** * @brief * * @param [in] N/A * @p Aram [out] N/A * @return void * @note */-(void) disconnect;/** * @brief * * @param [in] N/A * @param [out] N/A * @return void * @note */-(void) publishstring: (nsstring *) payload totopic: (nsstring *) topic retain: (BOOL) retain;//-(void) publishmessage/** * @brief * * @param [in] N/A * @param [out] N/A * @return void * @note */-(void) subscribe: (nsstring *) topic;/** * @brief * * @param [in * N/A * @param [out] N/A * @return void * @note */-(void) subscribe: (nsstring *) topic withqos: (nsuinteger) qos;/** * @bri EF * * @param [in] N/A * @param [out] N/A * @return void * @note */-(void) unsubscribe: (nsstring *) Topic;//this is Calle D automatically when Connected-(void) loop: (nstimer *) timer; @end</pre><br><p><p></p></p><p style="margin-top:0px; margin-bottom:0px; font-size:11px; font-family:Menlo; color:rgb(255,70,71)"><p style="margin-top:0px; margin-bottom:0px; font-size:11px; font-family:Menlo; color:rgb(255,70,71)">Mqttclient.m</p></p><p style="margin-top:0px; margin-bottom:0px; font-size:11px; font-family:Menlo; color:rgb(255,70,71)"><p style="margin-top:0px; margin-bottom:0px; font-size:11px; font-family:Menlo; color:rgb(255,70,71)"><br></p></p><p style="margin-top:0px; margin-bottom:0px; font-size:11px; font-family:Menlo; color:rgb(255,70,71)"><p style="margin-top:0px; margin-bottom:0px; font-size:11px; font-family:Menlo; color:rgb(255,70,71)"></p></p><pre code_snippet_id="417071" snippet_file_name="blog_20140704_2_6060332" name="code" class="objc">#import "MQTTClient.h" #import "mosquitto.h" @implementation mqttclient@synthesize host; @synthesize port; @synthesize username, @synthesize password, @synthesize keepAlive, @synthesize cleansession; @synthesize delegate;/** * @brief Setting of the Singleton mode * * @param [in] N/A * @param [out] N/A * @return void * @note */static mqttclient *qttinstance = nil;+ (mqttclient *) getmqttinstance{@synchronized (self) {if (qttinstance==nil) {qttinstance=[[self alloc]init]; }}return qttinstance;} /** * @brief Connection to MQTT * * @param [in] N/A * @param [out] N/A * @return void * @note */static void On_connect (struct Mosquitt o *mosq, void *obj, int rc) {mqttclient* client = (mqttclient *) obj; [[client delegate] didconnect: (nsuinteger) rc];} /** * @brief disconnected from MQTT * * @param [in] N/A * @param [out] N/A * @return void * @note */static void On_disconnect (struct m Osquitto *mosq, void *obj, int rc) {mqttclient* client = (mqttclient *) obj; [[client delegate] diddisconnect];} /** * @brief Publish message * * @param [in] N/A * @param [out] N/A * @return void * @note */static void on_publish (struct mosquitto *mosq, void *obj, int Message_id) {mqttclient* client = (mqttclient *) obj; [[client delegate] didpublish: (nsuinteger) message_id];} /** * @brief Receive Message * * @param [in] N/A * @param [out] N/A * @return void * @note */static void On_message (struc T mosquitto *mosq, void *obj, const struct Mosquitto_message *message) {mqttclient * client = (mqttclient *) obj; NSString *topic = [nsstring stringwithutf8string:message->topic]; NSString *payload = [[[nsstring alloc] initwithbytes:message->payload Length:message->payloadlen encoding:nsutf8stringencoding] autorelease]; Fixme:create Mosquittomessage class instead [[client delegate] didreceivemessage:payload topic:topic];} /** * @brief didsubscribe * * @param [in] N/A * @param [out] N/A * @return void * @note */static voiD on_subscribe (struct mosquitto *mosq, void *obj, int message_id, int qos_count, const INT *GRANTED_QOS) {mqttclient * Client = (mqttclient *) obj; Fixme:implement this [[client delegate] didsubscribe:message_id grantedqos:nil];} /** * @brief * * @param [in] N/A * @param [out] N/A * @return void * @note */static void On_unsubscribe (struct Mosquitto *mosq, void *obj, int message_id) {mqttclient * client = (mqttclient *) obj; [[client delegate] didunsubscribe:message_id];} /** * @brief Initialize is called just before the first object was allocated * * @param [in] N/A * @param [out] N/A * @ret urn void * @note */+ (void) initialize{mosquitto_lib_init ();} /** * @brief Version number information * * @param [in] N/A * @param [out] N/A * @return void * @note */+ (nsstring*) version{int major, min or, revision; Mosquitto_lib_version (&major, &minor, &revision); return [nsstring stringwithformat:@ "%d.%d.%d", major, minor, revision];} /** * @brief Set some parameter status configuration * * @param [in] N/A * @Param [out] N/A * @return void * @note */-(mqttclient *) initwithclientid: (nsstring*) clientid{if (self = [super in It]) {const char* cstrclientid = [clientId cstringusingencoding:nsutf8stringencoding]; [self sethost:nil]; [self setport:1883]; [self setkeepalive:60]; [self setcleansession:yes]; MOSQ = mosquitto_new (cstrclientid, cleansession, self); Mosquitto_connect_callback_set (mosq, on_connect); Mosquitto_disconnect_callback_set (mosq, on_disconnect); Mosquitto_publish_callback_set (mosq, on_publish); Mosquitto_message_callback_set (mosq, on_message); Mosquitto_subscribe_callback_set (mosq, on_subscribe); Mosquitto_unsubscribe_callback_set (mosq, on_unsubscribe); Timer = nil; } return self;} -(void) connect{const Char *cstrhost = [host cstringusingencoding:nsasciistringencoding]; const char *cstrusername = null, *cstrpassword = null; If (username) CstrUserName = [username cstringusingencoding:nsutf8stringencoding]; If (password) Cstrpassword = [password cstringusingencoding:nsutf8stringencoding]; Fixme:check for errors Mosquitto_username_pw_set (mosq, cstrusername, cstrpassword); int ret = Mosquitto_connect (mosq, cstrhost, port, keepAlive); NSLog (@ "mqtt-ret-code =%d", ret); if (ret = = 0) {NSLog (@ "//connection successfully set ud"); } else {NSLog (@ "//connection failed"); }//Setup timer to handle network events//fixme:better-do This-hook into IOS Run Loop Select ()?<p><p></p></p><p><p>or run in seperate thread? Timer = [nstimer scheduledtimerwithtimeinterval:0.01//10ms target:self Selector: @selector (loop:) Userinfo:nil repeats:yes];} -(void) connecttohost: (nsstring*) ahost{[self sethost:ahost]; [self connect];} -(void) reconnect{mosquitto_reconnect (mosq);}-(void) Disconnect{mosquitto_disconnect (mosq);} -(void) loop: (nstimer *) timer{mosquitto_loop (mosq, 1, 1);} Fixme:add QoS parameter?</p></p><p>-(void) publishstring: (nsstring *) payload totopic: (nsstring *) topic retain: (BOOL) retain{const char* cstrtopic = [topi C cstringusingencoding:nsutf8stringencoding]; Const uint8_t* cstrpayload = (const uint8_t*) [payload cstringusingencoding:nsutf8stringencoding]; Mosquitto_publish (mosq, NULL, cstrtopic, [payload length], cstrpayload, 0, retain);} -(void) subscribe: (nsstring *) topic{[self subscribe:topic withqos:0];} -(void) subscribe: (nsstring *) topic withqos: (nsuinteger) qos{const char* cstrtopic = [topic Cstringusingencoding:nsutf 8StringEncoding]; Mosquitto_subscribe (mosq, NULL, cstrtopic, qos);} -(void) unsubscribe: (nsstring *) topic{const char* cstrtopic = [topic cstringusingencoding:nsutf8stringencoding]; Mosquitto_unsubscribe (mosq, NULL, cstrtopic);} -(void) setmessageretry: (nsuinteger) seconds{mosquitto_message_retry_set (mosq, (unsigned int) seconds);} -(void) dealloc{if (mosq) {mosquitto_destroy (mosq); MOSQ = NULL; } if (timer) {[timer invalidate]; Timer = nil; } [super dealloc];} Fixme:how and when to call Mosquitto_lib_cleanup ()? @end</p></pre><br><br><p><p></p></p><br> <p style="font-size:12px;"><p style="font-size:12px;">Copyright Notice: This article blog original Article. blogs, without consent, may not be reproduced.</p></p> <p><p>MQTT protocol iOS 1 after porting</p></p></span>