martinzink commented on code in PR #1779:
URL: https://github.com/apache/nifi-minifi-cpp/pull/1779#discussion_r1633200516


##########
extensions/standard-processors/modbus/FetchModbusTcp.cpp:
##########
@@ -0,0 +1,259 @@
+/**
+* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "FetchModbusTcp.h"
+
+#include <utils/net/ConnectionHandler.h>
+
+#include <asio/read.hpp>
+#include <range/v3/view/drop.hpp>
+
+#include "core/Resource.h"
+
+#include "core/ProcessSession.h"
+#include "modbus/Error.h"
+#include "modbus/ReadModbusFunctions.h"
+#include "utils/net/AsioCoro.h"
+#include "utils/net/AsioSocketUtils.h"
+
+using namespace std::literals::chrono_literals;
+
+namespace org::apache::nifi::minifi::modbus {
+
+
+void FetchModbusTcp::onSchedule(core::ProcessContext& context, 
core::ProcessSessionFactory&) {
+  const auto record_set_writer_name = context.getProperty(RecordSetWriter);
+  record_set_writer_ = 
std::dynamic_pointer_cast<core::RecordSetWriter>(context.getControllerService(record_set_writer_name.value_or("")));
+  if (!record_set_writer_)
+    throw Exception{ExceptionType::PROCESS_SCHEDULE_EXCEPTION, "Invalid or 
missing RecordSetWriter"};
+
+    // if the required properties are missing or empty even before evaluating 
the EL expression, then we can throw in onSchedule, before we waste any flow 
files
+  if (context.getProperty(Hostname).value_or(std::string{}).empty()) {
+    throw Exception{ExceptionType::PROCESS_SCHEDULE_EXCEPTION, "missing 
hostname"};
+  }
+  if (context.getProperty(Port).value_or(std::string{}).empty()) {
+    throw Exception{ExceptionType::PROCESS_SCHEDULE_EXCEPTION, "missing port"};
+  }
+  if (const auto idle_connection_expiration = 
context.getProperty<core::TimePeriodValue>(IdleConnectionExpiration); 
idle_connection_expiration && idle_connection_expiration->getMilliseconds() > 
0ms)
+    idle_connection_expiration_ = 
idle_connection_expiration->getMilliseconds();
+  else
+    idle_connection_expiration_.reset();
+
+  if (const auto timeout = 
context.getProperty<core::TimePeriodValue>(Timeout); timeout && 
timeout->getMilliseconds() > 0ms)
+    timeout_duration_ = timeout->getMilliseconds();
+  else
+    timeout_duration_ = 15s;
+
+  if (context.getProperty<bool>(ConnectionPerFlowFile).value_or(false))
+    connections_.reset();
+  else
+    connections_.emplace();
+
+  ssl_context_.reset();
+  if (const auto context_name = context.getProperty(SSLContextService); 
context_name && !IsNullOrEmpty(*context_name)) {
+    if (auto controller_service = context.getControllerService(*context_name)) 
{
+      if (const auto ssl_context_service = 
std::dynamic_pointer_cast<minifi::controllers::SSLContextService>(context.getControllerService(*context_name)))
 {
+        ssl_context_ = utils::net::getSslContext(*ssl_context_service);
+      } else {
+        throw Exception(PROCESS_SCHEDULE_EXCEPTION, *context_name + " is not 
an SSL Context Service");
+      }
+    } else {
+      throw Exception(PROCESS_SCHEDULE_EXCEPTION, "Invalid controller service: 
" + *context_name);
+    }
+  }
+
+  readDynamicPropertyKeys(context);
+}
+
+void FetchModbusTcp::onTrigger(core::ProcessContext& context, 
core::ProcessSession& session) {
+  const auto flow_file = getFlowFile(session);
+  if (!flow_file) {
+    logger_->log_error("No flowfile to work on");
+    return;
+  }
+
+  removeExpiredConnections();
+
+  auto hostname = context.getProperty(Hostname, 
flow_file.get()).value_or(std::string{});
+  auto port = context.getProperty(Port, 
flow_file.get()).value_or(std::string{});
+
+  if (hostname.empty() || port.empty()) {
+    logger_->log_error("[{}] invalid target endpoint: hostname: {}, port: {}", 
flow_file->getUUIDStr(),
+        hostname.empty() ? "(empty)" : hostname.c_str(),
+        port.empty() ? "(empty)" : port.c_str());
+    session.transfer(flow_file, Failure);
+    return;
+  }
+
+  auto connection_id = utils::net::ConnectionId(std::move(hostname), 
std::move(port));
+  std::shared_ptr<utils::net::ConnectionHandlerBase> handler;
+  if (!connections_ || !connections_->contains(connection_id)) {
+    if (ssl_context_)
+      handler = 
std::make_shared<utils::net::ConnectionHandler<utils::net::SslSocket>>(connection_id,
 timeout_duration_, logger_, max_size_of_socket_send_buffer_, &*ssl_context_);
+    else
+      handler = 
std::make_shared<utils::net::ConnectionHandler<utils::net::TcpSocket>>(connection_id,
 timeout_duration_, logger_, max_size_of_socket_send_buffer_, nullptr);
+    if (connections_)
+      (*connections_)[connection_id] = handler;
+  } else {
+    handler = (*connections_)[connection_id];
+  }
+
+  gsl_Expects(handler);
+
+  processFlowFile(handler, context, session, flow_file);
+}
+
+void FetchModbusTcp::initialize() {
+  setSupportedProperties(Properties);
+  setSupportedRelationships(Relationships);
+}
+
+void FetchModbusTcp::readDynamicPropertyKeys(const core::ProcessContext& 
context) {
+  dynamic_property_keys_.clear();
+  const std::vector<std::string> dynamic_prop_keys = 
context.getDynamicPropertyKeys();
+  for (const auto& key : dynamic_prop_keys) {
+    
dynamic_property_keys_.emplace_back(core::PropertyDefinitionBuilder<>::createProperty(key).withDescription("auto
 generated").supportsExpressionLanguage(true).build());
+  }
+}
+
+std::shared_ptr<core::FlowFile> 
FetchModbusTcp::getFlowFile(core::ProcessSession& session) const {
+  if (hasIncomingConnections()) {
+    return session.get();
+  }
+  return session.create();
+}
+
+std::unordered_map<std::string, std::unique_ptr<ReadModbusFunction>> 
FetchModbusTcp::getAddressMap(core::ProcessContext& context, const 
core::FlowFile& flow_file) {
+  std::unordered_map<std::string, std::unique_ptr<ReadModbusFunction>> 
address_map{};
+  const auto unit_id_str = context.getProperty(UnitIdentifier, 
&flow_file).value_or("0");
+  const uint8_t unit_id = 
utils::string::parse<uint8_t>(unit_id_str).value_or(1);
+  for (const auto& dynamic_property : dynamic_property_keys_) {
+    if (std::string dynamic_property_value{}; 
context.getDynamicProperty(dynamic_property, dynamic_property_value, 
&flow_file)) {
+      if (auto modbus_func = ReadModbusFunction::parse(++transaction_id_, 
unit_id, dynamic_property_value); modbus_func)
+        address_map.emplace(dynamic_property.getName(), 
std::move(modbus_func));
+    }
+  }
+  return address_map;
+}
+
+void FetchModbusTcp::removeExpiredConnections() {
+  if (connections_) {
+    std::erase_if(*connections_, [this](auto& item) -> bool {
+      const auto& connection_handler = item.second;
+      return (!connection_handler || (idle_connection_expiration_ && 
!connection_handler->hasBeenUsedIn(*idle_connection_expiration_)));
+    });
+  }
+}
+
+void FetchModbusTcp::processFlowFile(const 
std::shared_ptr<utils::net::ConnectionHandlerBase>& connection_handler,
+    core::ProcessContext& context,
+    core::ProcessSession& session,
+    const std::shared_ptr<core::FlowFile>& flow_file) {
+  std::unordered_map<std::string, std::string> result_map{};
+  const auto address_map = getAddressMap(context, *flow_file);
+  if (address_map.empty()) {
+    logger_->log_warn("There are no registers to query");
+    session.transfer(flow_file, Failure);
+    return;
+  }
+
+  if (auto result = readModbus(connection_handler, address_map); !result) {
+    connection_handler->reset();
+    logger_->log_error("{}", result.error().message());
+    session.transfer(flow_file, Failure);
+  } else {
+    core::RecordSet record_set;
+    record_set.push_back(std::move(*result));
+    record_set_writer_->write(record_set, flow_file, session);
+    session.transfer(flow_file, Success);
+  }
+}
+
+nonstd::expected<core::Record, std::error_code> FetchModbusTcp::readModbus(
+    const std::shared_ptr<utils::net::ConnectionHandlerBase>& 
connection_handler,
+    const std::unordered_map<std::string, 
std::unique_ptr<ReadModbusFunction>>& address_map) {
+  nonstd::expected<core::Record, std::error_code> result;
+  io_context_.restart();
+  asio::co_spawn(io_context_,
+    sendRequestsAndReadResponses(*connection_handler, address_map),
+    [&result](const std::exception_ptr& exception_ptr, auto res) {
+      if (exception_ptr) {
+        result = nonstd::make_unexpected(ModbusExceptionCode::InvalidResponse);
+      } else {
+        result = std::move(res);
+      }
+    });
+  io_context_.run();
+  return result;
+}
+
+auto 
FetchModbusTcp::sendRequestsAndReadResponses(utils::net::ConnectionHandlerBase& 
connection_handler,
+    const std::unordered_map<std::string, 
std::unique_ptr<ReadModbusFunction>>& address_map) -> 
asio::awaitable<nonstd::expected<core::Record, std::error_code>> {
+  core::Record result;
+  for (const auto& [variable, read_modbus_fn] : address_map) {
+    auto response = co_await sendRequestAndReadResponse(connection_handler, 
*read_modbus_fn);
+    if (!response) {
+      co_return nonstd::make_unexpected(response.error());
+    }
+    result.emplace(variable, std::move(*response));
+  }
+  co_return result;
+}
+
+
+auto 
FetchModbusTcp::sendRequestAndReadResponse(utils::net::ConnectionHandlerBase& 
connection_handler,
+    const ReadModbusFunction& read_modbus_function) -> 
asio::awaitable<nonstd::expected<core::RecordField, std::error_code>> {
+  std::string result;
+  if (auto connection_error = co_await 
connection_handler.setupUsableSocket(io_context_))  // NOLINT
+    co_return nonstd::make_unexpected(connection_error);

Review Comment:
   Added some explanation in 
https://github.com/apache/nifi-minifi-cpp/pull/1779/commits/329f1bf27782636e5365ffb38beb1d0557808ba3#diff-9f8b9763f59e4bfc0ab6bfb37c88dbd4a441b99abc14682bc954309aa0c59551R228



-- 
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