You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@nifi.apache.org by "martinzink (via GitHub)" <gi...@apache.org> on 2023/06/30 06:44:16 UTC

[GitHub] [nifi-minifi-cpp] martinzink commented on a diff in pull request #1595: MINIFICPP-2137 Rewrite MiNiFi Controller to use asio

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


##########
controller/Controller.cpp:
##########
@@ -20,195 +20,286 @@
 
 #include "io/BufferStream.h"
 #include "c2/C2Payload.h"
+#include "io/AsioStream.h"
+#include "asio/ssl/context.hpp"
+#include "asio/ssl/stream.hpp"
+#include "asio/connect.hpp"
+#include "core/logging/Logger.h"
+#include "utils/net/AsioSocketUtils.h"
 
 namespace org::apache::nifi::minifi::controller {
 
-bool sendSingleCommand(std::unique_ptr<io::Socket> socket, uint8_t op, const std::string& value) {
-  if (socket->initialize() < 0) {
+namespace {
+
+class ClientConnection {
+ public:
+  explicit ClientConnection(const ControllerSocketData& socket_data) {
+    if (socket_data.ssl_context_service) {
+      connectTcpSocketOverSsl(socket_data);
+    } else {
+      connectTcpSocket(socket_data);
+    }
+  }
+
+  [[nodiscard]] io::BaseStream* getStream() const {
+    return stream_.get();
+  }
+
+ private:
+  void connectTcpSocketOverSsl(const ControllerSocketData& socket_data) {
+    auto ssl_context = utils::net::getSslContext(*socket_data.ssl_context_service);
+    asio::ssl::stream<asio::ip::tcp::socket> socket(io_context_, ssl_context);
+
+    asio::ip::tcp::resolver resolver(io_context_);
+    asio::error_code err;
+    asio::ip::tcp::resolver::results_type endpoints = resolver.resolve(socket_data.host, std::to_string(socket_data.port), err);
+    if (err) {
+      logger_->log_error("Resolving host '%s' on port '%s' failed with the following message: '%s'", socket_data.host, std::to_string(socket_data.port), err.message());
+      return;
+    }
+
+    asio::connect(socket.lowest_layer(), endpoints, err);
+    if (err) {
+      logger_->log_error("Connecting to host '%s' on port '%s' failed with the following message: '%s'", socket_data.host, std::to_string(socket_data.port), err.message());
+      return;
+    }
+    socket.handshake(asio::ssl::stream_base::client, err);
+    if (err) {
+      logger_->log_error("SSL handshake failed while connecting to host '%s' on port '%s' with the following message: '%s'", socket_data.host, std::to_string(socket_data.port), err.message());
+      return;
+    }
+    stream_ = std::make_unique<io::AsioStream<asio::ssl::stream<asio::ip::tcp::socket>>>(std::move(socket));
+  }
+
+  void connectTcpSocket(const ControllerSocketData& socket_data) {
+    asio::ip::tcp::socket socket(io_context_);
+
+    asio::ip::tcp::resolver resolver(io_context_);
+    asio::error_code err;
+    asio::ip::tcp::resolver::results_type endpoints = resolver.resolve(socket_data.host, std::to_string(socket_data.port));

Review Comment:
   I think it might be better to use the async variant, since this might take a long time before it fails, and there is no way to set timeout for the synchronous variant.
   
   The resolve part is also sync in PutUDP we should reconsider that one aswell



-- 
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-unsubscribe@nifi.apache.org

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