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


##########
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) {

Review Comment:
   Is it possible to have no flow_file here? If both the `get` and the `create` 
call fails to return a valid flow_file I think we should handle it as an error, 
maybe log the error and return, but yielding is not needed here IMO.



##########
extensions/standard-processors/modbus/Error.h:
##########
@@ -0,0 +1,69 @@
+/**
+ *
+ * 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.
+ */
+
+#pragma once
+
+#include <string>
+#include <system_error>
+#include "magic_enum.hpp"
+
+namespace org::apache::nifi::minifi::modbus {
+
+enum class ModbusExceptionCode : std::underlying_type_t<std::byte> {
+  IllegalFunction = 0x01,
+  IllegalDataAddress = 0x02,
+  IllegalDataValue = 0x03,
+  SlaveDeviceFailure = 0x04,
+  Acknowledge = 0x05,
+  SlaveDeviceBusy = 0x06,
+  NegativeAcknowledge = 0x07,
+  MemoryParityError = 0x08,
+  GatewayPathUnavailable = 0x0a,
+  GatewayTargetDeviceFailedToRespond = 0x0b,
+  InvalidResponse,
+  MessageTooLarge,
+  InvalidTransactionId,
+  IllegalProtocol,
+  InvalidSlaveId
+};
+
+
+struct ModbusErrorCategory final : std::error_category {
+  [[nodiscard]] const char* name() const noexcept override {
+    return "modbus error";
+  }
+
+  [[nodiscard]] std::string message(int ev) const override {
+    const auto modbus_exception_code = static_cast<ModbusExceptionCode>(ev);
+    return 
std::string{magic_enum::enum_name<ModbusExceptionCode>(modbus_exception_code)};
+  }
+};
+
+inline const ModbusErrorCategory& modbus_category() noexcept {
+  static ModbusErrorCategory category;
+  return category;
+};
+
+inline std::error_code make_error_code(ModbusExceptionCode c) {
+  return {static_cast<int>(c), modbus_category()};
+}
+
+}  // namespace org::apache::nifi::minifi::modbus
+
+template <>
+struct 
std::is_error_code_enum<org::apache::nifi::minifi::modbus::ModbusExceptionCode> 
: true_type {};

Review Comment:
   Nitpick: use std::true_type instead of true_type



##########
extensions/standard-processors/modbus/ReadModbusFunctions.h:
##########


Review Comment:
   If a method is not needed to be inlined, I would move the implementation to 
ReadModbusFunctions.cpp



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