| 153 | } |
| 154 | |
| 155 | bool AbstractMQTTProcessor::reconnect() { |
| 156 | if (!client_) |
| 157 | return false; |
| 158 | if (MQTTClient_isConnected(client_)) |
| 159 | return true; |
| 160 | MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer; |
| 161 | conn_opts.keepAliveInterval = keepAliveInterval_; |
| 162 | conn_opts.cleansession = cleanSession_; |
| 163 | if (!userName_.empty()) { |
| 164 | conn_opts.username = userName_.c_str(); |
| 165 | conn_opts.password = passWord_.c_str(); |
| 166 | } |
| 167 | if (sslEnabled_) { |
| 168 | conn_opts.ssl = &sslopts_; |
| 169 | } |
| 170 | int ret = MQTTClient_connect(client_, &conn_opts); |
| 171 | if (ret != MQTTCLIENT_SUCCESS) { |
| 172 | logger_->log_error("Failed to connect to MQTT broker %s (%d)", uri_, ret); |
| 173 | return false; |
| 174 | } |
| 175 | if (isSubscriber_) { |
| 176 | ret = MQTTClient_subscribe(client_, topic_.c_str(), qos_); |
| 177 | if(ret != MQTTCLIENT_SUCCESS) { |
| 178 | logger_->log_error("Failed to subscribe to MQTT topic %s (%d)", topic_, ret); |
| 179 | return false; |
| 180 | } |
| 181 | logger_->log_debug("Successfully subscribed to MQTT topic: %s", topic_); |
| 182 | } |
| 183 | return true; |
| 184 | } |
| 185 | |
| 186 | } /* namespace processors */ |
| 187 | } /* namespace minifi */ |
no test coverage detected