coolers/mosquitto.c

changeset 35
f3c5ae78b746
parent 28
32ed1ea4d0b6
child 40
dafbbd5e9922
--- a/coolers/mosquitto.c	Mon May 12 19:45:11 2014 +0200
+++ b/coolers/mosquitto.c	Tue May 13 16:33:47 2014 +0200
@@ -100,7 +100,7 @@
 {
     char                *id = NULL, *state = NULL;
     char                buf[1024];
-    int                 rc, keepalive = 60;
+    int                 try, rc, keepalive = 60;
     unsigned int        max_inflight = 20;
     char                err[1024];
     w1_therm		*tmp1, *old1;
@@ -169,13 +169,21 @@
     mosquitto_disconnect_callback_set(mymosq, my_disconnect_callback);
     mosquitto_publish_callback_set(mymosq, my_publish_callback);
 
-    if ((rc = mosquitto_connect(mymosq, Config.mosq_host, Config.mosq_port, keepalive))) {
-	if (rc == MOSQ_ERR_ERRNO) {
-	    strerror_r(errno, err, 1024);
-	    syslog(LOG_NOTICE, "mosquitto_connect: error: %s", err);
-	} else {
-	    syslog(LOG_NOTICE, "mosquitto_connect: unable to connect (%d)", rc);
-	}
+    try = 10; rc = -1;
+    while (try && rc) {
+    	if ((rc = mosquitto_connect(mymosq, Config.mosq_host, Config.mosq_port, keepalive))) {
+	    if (rc == MOSQ_ERR_ERRNO) {
+	    	strerror_r(errno, err, 1024);
+	    	syslog(LOG_NOTICE, "mosquitto_connect: error: %s, try=%d", err, 11-try);
+	    } else {
+	    	syslog(LOG_NOTICE, "mosquitto_connect: unable to connect (%d)", rc);
+	    }
+	    sleep(2);
+	    try--;
+    	}
+    }
+    if (rc) {
+	syslog(LOG_NOTICE, "mosquitto_connect: too many tries, giving up");
 	mosquitto_lib_cleanup();
 	return rc;
     }

mercurial