Skip to content

Commit

Permalink
added reconnect handling if con. refused at startup
Browse files Browse the repository at this point in the history
This should allow startups where the mqtt server will be available later
  • Loading branch information
mbehr1 committed Aug 5, 2018
1 parent 01eb73b commit c2b8fbd
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions src/mqtt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,28 @@ MqttClient::MqttClient(struct json_object *option) : _enabled(false)
res = mosquitto_connect(_mcs, _host.c_str(), _port, _keepalive);
if (res != MOSQ_ERR_SUCCESS)
{
print(log_alert, "mosquitto_connect failed. res=%d (%s)! Stopped!", "mqtt",
res, strerror(errno));
_enabled = false;
switch (res)
{
case MOSQ_ERR_CONN_REFUSED: // mqtt might accept us later only.
print(log_warning, "mosquitto_connect failed. res=%d (%s)! Trying anyhow.", "mqtt",
res, strerror(errno));
break;
case MOSQ_ERR_ERRNO:
if (errno == 111) // con refused:
{
print(log_warning, "mosquitto_connect failed. res=%d (%d %s)! Trying anyhow.", "mqtt",
res, errno, strerror(errno));
} else {
print(log_alert, "mosquitto_connect failed. res=%d (%d %s)! Stopped!", "mqtt",
res, errno, strerror(errno));
_enabled = false;
}
break;
default:
print(log_alert, "mosquitto_connect failed. res=%d! Stopped!", "mqtt", res);
_enabled = false;
break;
}
}
}
}
Expand Down

0 comments on commit c2b8fbd

Please sign in to comment.