Linux下學習用C語言實現MQTT(三)(異步函數)

客戶端庫支持兩種操作模式,稱爲同步模式和異步模式。如果你的應用程序調用了MQTTClient_setCallbacks(),則客戶端將會進入異步模式,否則會以同步模式運行。

同步模式下,客戶端應用程序運行在單個線程上。信息的發佈使用MQTTClient_publish()和MQTTClient_publishMessage()函數。爲了確定Qos1和Qos2(詳見Quality of service)的信息已經成功傳遞,應用程序必須調用MQTTClient_waitForCompletion()這個函數。一個展示同步發佈的例子可查看Synchronous publication example。同步模式下接收信息使用 MQTTClient_receive()函數,客戶端應用程序必須相對頻繁地調用 MQTTClient_receive()或MQTTClient_yield(),以便允許(執行)確認和保持與服務器的網絡連接的 MQTT "ping"這些操作。

異步模式下,客戶端應用程序運行在若干個線程上。如同同步模式,(異步模式中)主程序調用客戶端庫中的函數來發布和訂閱,而在在後臺進行握手和維持網絡連接這些操作。客戶端應用程序通過調用 MQTTClient_setCallbacks()( (詳情請見MQTTClient_messageArrived(), MQTTClient_connectionLost()和 MQTTClient_deliveryComplete()))來使用庫進行回調註冊而獲得狀態通知和消息接收。然而, 這個 API 不是線程安全的-不可能從多個線程調用它而不進行同步。你可以使用MQTTAsync API來完成此操作。

接下使用MQTTAsync系列函數來實現publish和subscribe:
publish端:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "MQTTAsync.h"
#include <unistd.h>

#define ADDRESS     "tcp://localhost:1883"
#define CLIENTID    "ExampleClientPub"
#define TOPIC       "MQTT Examples"
#define PAYLOAD     "Hello World!"
#define QOS         1
#define TIMEOUT     10000L

volatile MQTTAsync_token deliveredtoken;

int finished = 0;

void connlost(void *context, char *cause)
{
	MQTTAsync client = (MQTTAsync)context;
	MQTTAsync_connectOptions conn_opts = MQTTAsync_connectOptions_initializer;
	int rc;

	printf("\nConnection lost\n");
	printf("     cause: %s\n", cause);

	printf("Reconnecting\n");
	conn_opts.keepAliveInterval = 20;
	conn_opts.cleansession = 1;
	if ((rc = MQTTAsync_connect(client, &conn_opts)) != MQTTASYNC_SUCCESS)
	{
		printf("Failed to start connect, return code %d\n", rc);
 		finished = 1;
	}
}


void onDisconnect(void* context, MQTTAsync_successData* response)
{
	printf("Successful disconnection\n");
	finished = 1;
}


void onSend(void* context, MQTTAsync_successData* response)
{
	MQTTAsync client = (MQTTAsync)context;
	MQTTAsync_disconnectOptions opts = MQTTAsync_disconnectOptions_initializer;
	int rc;

	printf("Message with token value %d delivery confirmed\n", response->token);

	opts.onSuccess = onDisconnect;
	opts.context = client;

	if ((rc = MQTTAsync_disconnect(client, &opts)) != MQTTASYNC_SUCCESS)
	{
		printf("Failed to start sendMessage, return code %d\n", rc);
		exit(EXIT_FAILURE);
	}
}


void onConnectFailure(void* context, MQTTAsync_failureData* response)
{
	printf("Connect failed, rc %d\n", response ? response->code : 0);
	finished = 1;
}


void onConnect(void* context, MQTTAsync_successData* response)
{
	MQTTAsync client = (MQTTAsync)context;
	MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
	MQTTAsync_message pubmsg = MQTTAsync_message_initializer;
	int rc;

	printf("Successful connection\n");
	
	opts.onSuccess = onSend;
	opts.context = client;

	pubmsg.payload = PAYLOAD;
	pubmsg.payloadlen = (int)strlen(PAYLOAD);
	pubmsg.qos = QOS;
	pubmsg.retained = 0;
	deliveredtoken = 0;

	if ((rc = MQTTAsync_sendMessage(client, TOPIC, &pubmsg, &opts)) != MQTTASYNC_SUCCESS)
	{
		printf("Failed to start sendMessage, return code %d\n", rc);
		exit(EXIT_FAILURE);
	}
}


int main(int argc, char* argv[])
{
	MQTTAsync client;
	MQTTAsync_connectOptions conn_opts = MQTTAsync_connectOptions_initializer;
	int rc;

	MQTTAsync_create(&client, ADDRESS, CLIENTID, MQTTCLIENT_PERSISTENCE_NONE, NULL);

	MQTTAsync_setCallbacks(client, NULL, connlost, NULL, NULL);

	conn_opts.keepAliveInterval = 20;
	conn_opts.cleansession = 1;
	conn_opts.onSuccess = onConnect;
	conn_opts.onFailure = onConnectFailure;
	conn_opts.context = client;
	if ((rc = MQTTAsync_connect(client, &conn_opts)) != MQTTASYNC_SUCCESS)
	{
		printf("Failed to start connect, return code %d\n", rc);
		exit(EXIT_FAILURE);
	}

	printf("Waiting for publication of %s\n"
         "on topic %s for client with ClientID: %s\n",
         PAYLOAD, TOPIC, CLIENTID);
	while (!finished)
		#if defined(WIN32)
			Sleep(100);
		#else
			usleep(10000L);
		#endif

	MQTTAsync_destroy(&client);
 	return rc;
}
  

subscribe端:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "MQTTAsync.h"
#include <unistd.h>


#define ADDRESS     "tcp://localhost:1883"
#define CLIENTID    "ExampleClientSub"
#define TOPIC       "MQTT Examples"
#define PAYLOAD     "Hello World!"
#define QOS         1
#define TIMEOUT     10000L

volatile MQTTAsync_token deliveredtoken;

int disc_finished = 0;
int subscribed = 0;
int finished = 0;

void connlost(void *context, char *cause)
{
	MQTTAsync client = (MQTTAsync)context;
	MQTTAsync_connectOptions conn_opts = MQTTAsync_connectOptions_initializer;
	int rc;

	printf("\nConnection lost\n");
	if (cause)
		printf("     cause: %s\n", cause);

	printf("Reconnecting\n");
	conn_opts.keepAliveInterval = 20;
	conn_opts.cleansession = 1;
	if ((rc = MQTTAsync_connect(client, &conn_opts)) != MQTTASYNC_SUCCESS)
	{
		printf("Failed to start connect, return code %d\n", rc);
		finished = 1;
	}
}


int msgarrvd(void *context, char *topicName, int topicLen, MQTTAsync_message *message)
{
    int i;
    char* payloadptr;

    printf("Message arrived\n");
    printf("     topic: %s\n", topicName);
    printf("   message: ");

    payloadptr = message->payload;
    for(i=0; i<message->payloadlen; i++)
    {
        putchar(*payloadptr++);
    }
    putchar('\n');
    MQTTAsync_freeMessage(&message);
    MQTTAsync_free(topicName);
    return 1;
}


void onDisconnect(void* context, MQTTAsync_successData* response)
{
	printf("Successful disconnection\n");
	disc_finished = 1;
}


void onSubscribe(void* context, MQTTAsync_successData* response)
{
	printf("Subscribe succeeded\n");
	subscribed = 1;
}

void onSubscribeFailure(void* context, MQTTAsync_failureData* response)
{
	printf("Subscribe failed, rc %d\n", response ? response->code : 0);
	finished = 1;
}


void onConnectFailure(void* context, MQTTAsync_failureData* response)
{
	printf("Connect failed, rc %d\n", response ? response->code : 0);
	finished = 1;
}


void onConnect(void* context, MQTTAsync_successData* response)
{
	MQTTAsync client = (MQTTAsync)context;
	MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
	int rc;

	printf("Successful connection\n");

	printf("Subscribing to topic %s\nfor client %s using QoS%d\n\n"
           "Press Q<Enter> to quit\n\n", TOPIC, CLIENTID, QOS);
	opts.onSuccess = onSubscribe;
	opts.onFailure = onSubscribeFailure;
	opts.context = client;

	deliveredtoken = 0;

	if ((rc = MQTTAsync_subscribe(client, TOPIC, QOS, &opts)) != MQTTASYNC_SUCCESS)
	{
		printf("Failed to start subscribe, return code %d\n", rc);
		exit(EXIT_FAILURE);
	}
}


int main(int argc, char* argv[])
{
	MQTTAsync client;
	MQTTAsync_connectOptions conn_opts = MQTTAsync_connectOptions_initializer;
	MQTTAsync_disconnectOptions disc_opts = MQTTAsync_disconnectOptions_initializer;
	int rc;
	int ch;

	MQTTAsync_create(&client, ADDRESS, CLIENTID, MQTTCLIENT_PERSISTENCE_NONE, NULL);

	MQTTAsync_setCallbacks(client, client, connlost, msgarrvd, NULL);

	conn_opts.keepAliveInterval = 20;
	conn_opts.cleansession = 1;
	conn_opts.onSuccess = onConnect;
	conn_opts.onFailure = onConnectFailure;
	conn_opts.context = client;
	if ((rc = MQTTAsync_connect(client, &conn_opts)) != MQTTASYNC_SUCCESS)
	{
		printf("Failed to start connect, return code %d\n", rc);
		exit(EXIT_FAILURE);
	}

	while	(!subscribed)
		#if defined(WIN32)
			Sleep(100);
		#else
			usleep(10000L);
		#endif

	if (finished)
		goto exit;

	do 
	{
		ch = getchar();
	} while (ch!='Q' && ch != 'q');

	disc_opts.onSuccess = onDisconnect;
	if ((rc = MQTTAsync_disconnect(client, &disc_opts)) != MQTTASYNC_SUCCESS)
	{
		printf("Failed to start disconnect, return code %d\n", rc);
		exit(EXIT_FAILURE);
	}
 	while	(!disc_finished)
		#if defined(WIN32)
			Sleep(100);
		#else
			usleep(10000L);
		#endif

exit:
	MQTTAsync_destroy(&client);
 	return rc;
}

編譯同樣要加上頭文件,動態庫,運行時可能也需要導入環境變量。

zhanghang@Ubuntu-14:~/MQTT/paho.mqtt.c/build/output/samples$ ./MQTTAsync_publish 
Waiting for publication of Hello World!
on topic MQTT Examples for client with ClientID: ExampleClientPub
Successful connection
Message with token value 1 delivery confirmed
Successful disconnection
zhanghang@Ubuntu-14:~/MQTT/paho.mqtt.c/build/output/samples$ ./MQTTAsync_subscribe 
Successful connection
Subscribing to topic MQTT Examples
for client ExampleClientSub using QoS1

Press Q<Enter> to quit

Subscribe succeeded
Message arrived
     topic: MQTT Examples
   message: Hello World!

客戶端成功推送話題和訂閱話題。
使用mosquitto分別測試publish和subscribe成功

zhanghang@Ubuntu-14:~/MQTT/paho.mqtt.c/build/output/samples$ ./MQTTAsync_publish 
Waiting for publication of Hello World!
on topic MQTT Examples for client with ClientID: ExampleClientPub
Successful connection
Message with token value 1 delivery confirmed
Successful disconnection
zhanghang@Ubuntu-14:~/MQTT/paho.mqtt.c/build/output/samples$ mosquitto_sub  -h 127.0.0.1 -p 1883 -v -t "MQTT Examples"
MQTT Examples Hello World!
zhanghang@Ubuntu-14:~/MQTT/paho.mqtt.c/build/output/samples$ mosquitto_pub  -h 127.0.0.1 -p 1883 -t "MQTT Examples" -m "Hello World"
zhanghang@Ubuntu-14:~/MQTT/paho.mqtt.c/build/output/samples$ ./MQTTAsync_subscribe 
Successful connection
Subscribing to topic MQTT Examples
for client ExampleClientSub using QoS1

Press Q<Enter> to quit

Subscribe succeeded
Message arrived
     topic: MQTT Examples
   message: Hello World

使用mqtt.fx應用程序測試軟件成功
使用mqtt.fx訂閱話題:

zhanghang@Ubuntu-14:~/MQTT/paho.mqtt.c/build/output/samples$ ./MQTTAsync_publish 
Waiting for publication of Hello World!
on topic MQTT Examples for client with ClientID: ExampleClientPub
Successful connection
Message with token value 1 delivery confirmed
Successful disconnection

在這裏插入圖片描述
使用mqtt.fx推送話題:
在這裏插入圖片描述

zhanghang@Ubuntu-14:~/MQTT/paho.mqtt.c/build/output/samples$ ./MQTTAsync_subscribe 
Successful connection
Subscribing to topic MQTT Examples
for client ExampleClientSub using QoS1

Press Q<Enter> to quit

Subscribe succeeded
Message arrived
     topic: MQTT Examples
   message: hello
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章