adam-markovics commented on code in PR #1432:
URL: https://github.com/apache/nifi-minifi-cpp/pull/1432#discussion_r1019200246


##########
extensions/mqtt/processors/AbstractMQTTProcessor.cpp:
##########
@@ -163,23 +200,261 @@ void AbstractMQTTProcessor::reconnect() {
   }
 
   logger_->log_info("Reconnecting to %s", uri_);
-  int ret = MQTTAsync_connect(client_, &conn_opts);
+  if (MQTTAsync_isConnected(client_)) {
+    logger_->log_debug("Already connected to %s, no need to reconnect", uri_);
+    return;
+  }
+  const int ret = MQTTAsync_connect(client_, &conn_opts);
+  MQTTProperties_free(&connect_props);
   if (ret != MQTTASYNC_SUCCESS) {
-    logger_->log_error("Failed to reconnect to MQTT broker %s (%d)", uri_, 
ret);
+    logger_->log_error("MQTTAsync_connect failed to MQTT broker %s with error 
code [%d]", uri_, ret);
+    return;
   }
+
+  // wait until connection succeeds or fails
+  connect_finished_task.get_future().get();
+}
+
+void AbstractMQTTProcessor::setMqtt5ConnectOptions(MQTTAsync_connectOptions& 
conn_opts, MQTTProperties& connect_props, MQTTProperties& will_props) const {
+  conn_opts.cleanstart = getCleanStart();
+
+  {
+    MQTTProperty property;
+    property.identifier = MQTTPROPERTY_CODE_SESSION_EXPIRY_INTERVAL;
+    property.value.integer4 = 
gsl::narrow<int>(getSessionExpiryInterval().count());
+    MQTTProperties_add(&connect_props, &property);
+  }
+
+  if (!last_will_content_type_.empty()) {
+    MQTTProperty property;
+    property.identifier = MQTTPROPERTY_CODE_CONTENT_TYPE;
+    property.value.data.len = last_will_content_type_.length();
+    property.value.data.data = 
const_cast<char*>(last_will_content_type_.data());
+    MQTTProperties_add(&will_props, &property);
+  }
+
+  conn_opts.willProperties = &will_props;
+
+  setMqtt5ConnectOptionsImpl(connect_props);
+}
+
+void AbstractMQTTProcessor::onTrigger(const 
std::shared_ptr<core::ProcessContext>& context, const 
std::shared_ptr<core::ProcessSession>& session) {
+  // read lock
+  std::shared_lock client_lock{client_mutex_};
+  if (client_ == nullptr) {
+    // we are shutting down
+    return;
+  }
+
+  // reconnect if needed
+  reconnect();
+
+  if (!MQTTAsync_isConnected(client_)) {
+    logger_->log_error("Could not work with MQTT broker because disconnected 
to %s", uri_);
+    yield();
+    return;
+  }
+
+  onTriggerImpl(context, session);
 }
 
 void AbstractMQTTProcessor::freeResources() {
-  if (client_ && MQTTAsync_isConnected(client_)) {
-    MQTTAsync_disconnectOptions disconnect_options = 
MQTTAsync_disconnectOptions_initializer;
-    disconnect_options.context = this;
-    disconnect_options.onSuccess = disconnectionSuccess;
-    disconnect_options.onFailure = disconnectionFailure;
-    disconnect_options.timeout = 
gsl::narrow<int>(std::chrono::milliseconds{connection_timeout_}.count());
-    MQTTAsync_disconnect(client_, &disconnect_options);
+  // write lock
+  std::lock_guard client_lock{client_mutex_};
+
+  if (!client_) {
+    return;
   }
-  if (client_) {
-    MQTTAsync_destroy(&client_);
+
+  disconnect();
+
+  MQTTAsync_destroy(&client_);
+}
+
+void AbstractMQTTProcessor::disconnect() {
+  if (!MQTTAsync_isConnected(client_)) {
+    return;
+  }
+
+  MQTTAsync_disconnectOptions disconnect_options = 
MQTTAsync_disconnectOptions_initializer;
+  std::packaged_task<void(MQTTAsync_successData*, MQTTAsync_successData5*, 
MQTTAsync_failureData*, MQTTAsync_failureData5*)> disconnect_finished_task(
+          [this] (MQTTAsync_successData* success_data, MQTTAsync_successData5* 
success_data_5, MQTTAsync_failureData* failure_data, MQTTAsync_failureData5* 
failure_data_5) {
+            onDisconnectFinished(success_data, success_data_5, failure_data, 
failure_data_5);
+          });
+  disconnect_options.context = &disconnect_finished_task;
+
+  if (mqtt_version_.value() == MqttVersions::V_5_0) {
+    disconnect_options.onSuccess5 = connectionSuccess5;
+    disconnect_options.onFailure5 = connectionFailure5;
+  } else {
+    disconnect_options.onSuccess = connectionSuccess;
+    disconnect_options.onFailure = connectionFailure;
+  }
+
+  disconnect_options.timeout = 
gsl::narrow<int>(std::chrono::milliseconds{connection_timeout_}.count());
+
+  const int ret = MQTTAsync_disconnect(client_, &disconnect_options);
+  if (ret != MQTTASYNC_SUCCESS) {
+    logger_->log_error("MQTTAsync_disconnect failed to MQTT broker %s with 
error code [%d]", uri_, ret);
+    return;
+  }
+
+  // wait until connection succeeds or fails
+  disconnect_finished_task.get_future().get();
+}
+
+void AbstractMQTTProcessor::setBrokerLimits(MQTTAsync_successData5* response) {
+  auto readProperty = [response] (MQTTPropertyCodes property_code, auto& 
out_var) {
+    // defined by Paho MQTT C library
+    static const int failure_code = -9999999;
+
+    const int value = MQTTProperties_getNumericValue(&response->properties, 
property_code);
+    if (value != failure_code) {
+      if constexpr (std::is_same_v<decltype(out_var), 
std::optional<std::chrono::seconds>&>) {
+        out_var = std::chrono::seconds(value);
+      } else {
+        out_var = gsl::narrow<typename 
std::remove_reference<decltype(out_var)>::type::value_type>(value);

Review Comment:
   Yes, we can!



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to