Debian Codegraph @

third-party/libhv-v1.3.3/examples/mqtt/mqtt_sub.c

raw
1 /*
2 * mqtt subscribe
3 *
4 * @build make examples
5 * @sub bin/mqtt_sub 127.0.0.1 1883 topic
6 * @pub bin/mqtt_pub 127.0.0.1 1883 topic payload
7 *
8 */
9
10 #include "hv.h"
11 #include "mqtt_client.h"
12
13 /*
14 * @test MQTTS
15 * #define TEST_SSL 1
16 *
17 * @build ./configure --with-mqtt --with-openssl && make clean && make
18 *
19 */
20 #define TEST_SSL 0
21 #define TEST_AUTH 0
22 #define TEST_RECONNECT 1
23
24 /*
25 * workflow:
26 * mqtt_client_new -> mqtt_client_xxx -> mqtt_client_run
27 *
28 * mqtt_client_set_xxx ->
29 * mqtt_client_connect ->
30 * on_connack -> mqtt_client_subscribe ->
31 * on_publish -> handle_message
32 *
33 */
34
35 static void handle_message(mqtt_client_t* cli, mqtt_message_t* msg) {
36 printf("topic: %.*s\n", msg->topic_len, msg->topic);
37 printf("payload: %.*s\n", msg->payload_len, msg->payload);
38 }
39
40 static void on_mqtt(mqtt_client_t* cli, int type) {
41 printf("on_mqtt type=%d\n", type);
42 switch(type) {
43 case MQTT_TYPE_CONNECT:
44 printf("mqtt connected!\n");
45 if (cli->reconn_setting && cli->reconn_setting->cur_retry_cnt > 0) {
46 printf("mqtt reconnect cnt=%d, delay=%d\n", cli->reconn_setting->cur_retry_cnt, cli->reconn_setting->cur_delay);
47 }
48 break;
49 case MQTT_TYPE_DISCONNECT:
50 printf("mqtt disconnected!\n");
51 if (cli->reconn_setting && cli->reconn_setting->cur_retry_cnt > 0) {
52 printf("mqtt reconnect cnt=%d, delay=%d\n", cli->reconn_setting->cur_retry_cnt, cli->reconn_setting->cur_delay);
53 }
54 break;
55 case MQTT_TYPE_CONNACK:
56 printf("mqtt connack!\n");
57 {
58 const char* topic = (const char*)mqtt_client_get_userdata(cli);
59 int mid = mqtt_client_subscribe(cli, topic, 0);
60 printf("mqtt subscribe mid=%d\n", mid);
61 }
62 break;
63 case MQTT_TYPE_SUBACK:
64 printf("mqtt suback mid=%d\n", cli->mid);
65 break;
66 case MQTT_TYPE_PUBLISH:
67 handle_message(cli, &cli->message);
68 default:
69 break;
70 }
71 }
72
73 static int mqtt_subscribe(const char* host, int port, const char* topic) {
74 mqtt_client_t* cli = mqtt_client_new(NULL);
75 if (cli == NULL) return -1;
76 cli->keepalive = 10;
77
78 #if TEST_AUTH
79 mqtt_client_set_auth(cli, "test", "123456");
80 #endif
81
82 mqtt_client_set_userdata(cli, (void*)topic);
83 mqtt_client_set_callback(cli, on_mqtt);
84
85 #if TEST_RECONNECT
86 reconn_setting_t reconn;
87 reconn_setting_init(&reconn);
88 reconn.min_delay = 1000;
89 reconn.max_delay = 10000;
90 reconn.delay_policy = 2;
91 mqtt_client_set_reconnect(cli, &reconn);
92 #endif
93
94 int ssl = 0;
95 #if TEST_SSL
96 ssl = 1;
97 #endif
98 mqtt_client_connect(cli, host, port, ssl);
99 mqtt_client_run(cli);
100 mqtt_client_free(cli);
101 return 0;
102 }
103
104 int main(int argc, char** argv) {
105 if (argc < 4) {
106 printf("Usage: %s host port topic\n", argv[0]);
107 return -10;
108 }
109 const char* host = argv[1];
110 int port = atoi(argv[2]);
111 const char* topic = argv[3];
112
113 return mqtt_subscribe(host, port, topic);
114 }