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


##########
extensions/mqtt/processors/PublishMQTT.cpp:
##########
@@ -34,76 +34,267 @@ void PublishMQTT::initialize() {
   setSupportedRelationships(relationships());
 }
 
-void PublishMQTT::onSchedule(const std::shared_ptr<core::ProcessContext> 
&context, const std::shared_ptr<core::ProcessSessionFactory> &factory) {
+void PublishMQTT::readProperties(const std::shared_ptr<core::ProcessContext>& 
context) {
+  if (!context->getProperty(Topic).has_value()) {
+    logger_->log_error("PublishMQTT: could not get Topic");
+  }
+
   if (const auto retain_opt = context->getProperty<bool>(Retain)) {
     retain_ = *retain_opt;
   }
   logger_->log_debug("PublishMQTT: Retain [%d]", retain_);
 
-  AbstractMQTTProcessor::onSchedule(context, factory);
+  if (const auto message_expiry_interval = 
context->getProperty<core::TimePeriodValue>(MessageExpiryInterval)) {
+    message_expiry_interval_ = 
std::chrono::duration_cast<std::chrono::seconds>(message_expiry_interval->getMilliseconds());
+    logger_->log_debug("PublishMQTT: MessageExpiryInterval [%" PRId64 "] s", 
int64_t{message_expiry_interval_->count()});
+  }
+
+  in_flight_message_counter_.setMqttVersion(mqtt_version_);
+  in_flight_message_counter_.setQoS(qos_);
 }
 
-void PublishMQTT::onTrigger(const std::shared_ptr<core::ProcessContext>& 
/*context*/, const std::shared_ptr<core::ProcessSession> &session) {
-  // reconnect if needed
-  reconnect();
+void PublishMQTT::onTriggerImpl(const std::shared_ptr<core::ProcessContext>& 
context, const std::shared_ptr<core::ProcessSession> &session) {
+  std::shared_ptr<core::FlowFile> flow_file = session->get();
 
-  if (!MQTTAsync_isConnected(client_)) {
-    logger_->log_error("Could not publish to MQTT broker because disconnected 
to %s", uri_);
-    yield();
+  if (!flow_file) {
     return;
   }
 
-  std::shared_ptr<core::FlowFile> flowFile = session->get();
+  // broker's Receive Maximum can change after reconnect
+  in_flight_message_counter_.setMax(broker_receive_maximum_.value_or(65535));
 
-  if (!flowFile) {
-    return;
-  }
-
-  PublishMQTT::ReadCallback callback(this, flowFile->getSize(), max_seg_size_, 
topic_, client_, gsl::narrow<int>(qos_), retain_);
-  session->read(flowFile, std::ref(callback));
-  if (callback.status_ < 0) {
-    logger_->log_error("Failed to send flow to MQTT topic %s", topic_);
-    session->transfer(flowFile, Failure);
+  const auto topic = getTopic(context, flow_file);
+  PublishMQTT::ReadCallback callback(this, flow_file, topic, 
getContentType(context, flow_file));
+  session->read(flow_file, std::ref(callback));
+  if (!callback.getSuccessStatus()) {
+    logger_->log_error("Failed to send flow file [%s] to MQTT topic '%s' on 
broker %s", flow_file->getUUIDStr(), topic, uri_);
+    session->transfer(flow_file, Failure);
   } else {
-    logger_->log_debug("Sent flow with length %d to MQTT topic %s", 
callback.read_size_, topic_);
-    session->transfer(flowFile, Success);
+    logger_->log_debug("Sent flow file [%s] with length %d to MQTT topic '%s' 
on broker %s", flow_file->getUUIDStr(), callback.getReadSize(), topic, uri_);
+    session->transfer(flow_file, Success);
   }
 }
 
 int64_t PublishMQTT::ReadCallback::operator()(const 
std::shared_ptr<io::InputStream>& stream) {
-  if (flow_size_ < max_seg_size_)
-    max_seg_size_ = flow_size_;
-  gsl_Expects(max_seg_size_ < 
gsl::narrow<uint64_t>(std::numeric_limits<int>::max()));
-  std::vector<std::byte> buffer(max_seg_size_);
+  if (flow_file_->getSize() > 268'435'455) {
+    processor_->logger_->log_error("Sending message failed because MQTT limit 
maximum packet size [268'435'455] is exceeded by FlowFile of [%" PRIu64 "]", 
flow_file_->getSize());
+    success_status_ = false;
+    return -1;
+  }
+
+  if (processor_->maximum_packet_size_.has_value() && flow_file_->getSize() > 
*(processor_->maximum_packet_size_)) {
+    processor_->logger_->log_error("Sending message failed because 
broker-requested maximum packet size [%" PRIu32 "] is exceeded by FlowFile of 
[%" PRIu64 "]",
+                                   *processor_->maximum_packet_size_, 
flow_file_->getSize());
+    success_status_ = false;
+    return -1;
+  }
+
+  std::vector<std::byte> buffer(flow_file_->getSize());
   read_size_ = 0;
-  status_ = 0;
-  while (read_size_ < flow_size_) {
-    // MQTTClient_message::payloadlen is int, so we can't handle 2GB+
-    const auto readRet = stream->read(buffer);
+  success_status_ = true;
+  while (read_size_ < flow_file_->getSize()) {

Review Comment:
   Done.



-- 
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: issues-unsubscr...@nifi.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to