martinzink commented on code in PR #1779: URL: https://github.com/apache/nifi-minifi-cpp/pull/1779#discussion_r1598602084
########## 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: Makes sense, changed it in https://github.com/apache/nifi-minifi-cpp/pull/1779/commits/a08b46b37dd61817923349d802ab351fdf9b79a5#diff-9f8b9763f59e4bfc0ab6bfb37c88dbd4a441b99abc14682bc954309aa0c59551R85 -- 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]
