Add reconnection logic

This commit is contained in:
Sergey Lyubka committed 2021-11-10 12:57:02 +00:00
1 parent 90c67e4226
commit 57dd74c180
1 file changed
+35 -21
+35 -21
View File
@@ -3,9 +3,10 @@
//
// Example MQTT client. It performs the following steps:
// 1. Connects to the MQTT server specified by `s_url` variable
// 2. When connected, subscribes to the topic `s_topic`
// 3. Publishes message `hello` to the `s_topic`
// 4. Receives that message back from the subscribed topic and exits
// 2. When connected, subscribes to the topic `s_sub_topic`
// 3. Publishes message `hello` to the `s_pub_topic`
// 4. Receives that message back from the subscribed topic and closes
// 5. Timer-based reconnection logic revives the connection when it is down
//
// To enable SSL/TLS for this client, build it like this:
// make MBEDTLS_DIR=/path/to/your/mbedtls/installation
@@ -13,11 +14,14 @@
#include "mongoose.h"
static const char *s_url = "mqtt://broker.hivemq.com:1883";
static const char *s_topic = "mg/mq-clnt-test";
static const char *s_sub_topic = "mg/+/test";
static const char *s_pub_topic = "mg/clnt/test";
static int s_qos = 1;
static struct mg_connection *s_conn;
static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
if (ev == MG_EV_OPEN) {
LOG(LL_INFO, ("CREATED"));
// c->is_hexdumping = 1;
} else if (ev == MG_EV_ERROR) {
// On error, log error message
@@ -30,37 +34,47 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
}
} else if (ev == MG_EV_MQTT_OPEN) {
// MQTT connect is successful
struct mg_str topic = mg_str(s_topic), data = mg_str("hello");
struct mg_str subt = mg_str(s_sub_topic);
struct mg_str pubt = mg_str(s_pub_topic), data = mg_str("hello");
LOG(LL_INFO, ("CONNECTED to %s", s_url));
mg_mqtt_sub(c, &topic, s_qos);
LOG(LL_INFO, ("SUBSCRIBED to %.*s", (int) topic.len, topic.ptr));
mg_mqtt_pub(c, &topic, &data, s_qos, false);
mg_mqtt_sub(c, &subt, s_qos);
LOG(LL_INFO, ("SUBSCRIBED to %.*s", (int) subt.len, subt.ptr));
mg_mqtt_pub(c, &pubt, &data, s_qos, false);
LOG(LL_INFO, ("PUBSLISHED %.*s -> %.*s", (int) data.len, data.ptr,
(int) topic.len, topic.ptr));
(int) pubt.len, pubt.ptr));
} else if (ev == MG_EV_MQTT_MSG) {
// When we get echo response, print it
struct mg_mqtt_message *mm = (struct mg_mqtt_message *) ev_data;
LOG(LL_INFO, ("RECEIVED %.*s <- %.*s", (int) mm->data.len, mm->data.ptr,
(int) mm->topic.len, mm->topic.ptr));
c->is_closing = 1;
} else if (ev == MG_EV_CLOSE) {
LOG(LL_INFO, ("CLOSED"));
s_conn = NULL; // Mark that we're closed
}
(void) fn_data;
}
if (ev == MG_EV_ERROR || ev == MG_EV_CLOSE || ev == MG_EV_MQTT_MSG) {
LOG(LL_INFO, ("Got event %d, stopping...", ev));
*(bool *) fn_data = true; // Signal that we're done
}
// Timer function - recreate client connection if it is closed
static void timer_fn(void *arg) {
struct mg_mgr *mgr = (struct mg_mgr *) arg;
struct mg_mqtt_opts opts = {.qos = s_qos,
.will_topic = mg_str(s_pub_topic),
.will_message = mg_str("goodbye")};
if (s_conn == NULL) s_conn = mg_mqtt_connect(mgr, s_url, &opts, fn, NULL);
}
int main(void) {
struct mg_mgr mgr;
struct mg_mqtt_opts opts = {.qos = s_qos,
.will_topic = mg_str(s_topic),
.will_message = mg_str("goodbye")};
bool done = false; // Event handler sets this to true
struct mg_timer timer;
int topts = MG_TIMER_REPEAT | MG_TIMER_RUN_NOW;
mg_mgr_init(&mgr); // Initialise event manager
mg_mqtt_connect(&mgr, s_url, &opts, fn, &done); // Create client connection
while (done == false) mg_mgr_poll(&mgr, 1000); // Loop until done
mg_mgr_free(&mgr); // Finished, cleanup
mg_mgr_init(&mgr); // Init event manager
mg_timer_init(&timer, 3000, topts, timer_fn, &mgr); // Init timer
for (;;) mg_mgr_poll(&mgr, 1000); // Event loop, 1s timeout
mg_mgr_free(&mgr); // Finished, cleanup
mg_timer_free(&timer); // Free timer resources
return 0;
}