bakaid commented on a change in pull request #713: MINIFICPP-1119 
MINIFICPP-1154 unify win/posix sockets + fix bugs
URL: https://github.com/apache/nifi-minifi-cpp/pull/713#discussion_r393716853
 
 

 ##########
 File path: libminifi/src/io/ClientSocket.cpp
 ##########
 @@ -0,0 +1,625 @@
+/**
+ *
+ * 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 "io/ClientSocket.h"
+#ifndef WIN32
+#include <netinet/tcp.h>
+#include <sys/types.h>
+#include <netinet/in.h>
+#include <ifaddrs.h>
+#include <unistd.h>
+#else
+#include <WS2tcpip.h>
+#pragma comment(lib, "Ws2_32.lib")
+#endif /* !WIN32 */
+
+#include <memory>
+#include <utility>
+#include <vector>
+#include <cerrno>
+#include <string>
+#include <system_error>
+#include <cinttypes>
+#include <Exception.h>
+#include <utils/Deleters.h>
+#include "io/validation.h"
+#include "core/logging/LoggerConfiguration.h"
+#include "utils/GeneralUtils.h"
+
+namespace util = org::apache::nifi::minifi::utils;
+
+namespace {
+
+std::string get_last_err_str() {
+#ifdef WIN32
+  const auto error_code = WSAGetLastError();
+#else
+  const auto error_code = errno;
+#endif /* WIN32 */
+  return std::system_category().message(error_code);
+}
+
+std::string get_last_getaddrinfo_err_str(int getaddrinfo_result) {
+#ifdef WIN32
+  (void)getaddrinfo_result;  // against unused warnings on windows
+  return get_last_err_str();
+#else
+  return gai_strerror(getaddrinfo_result);
+#endif /* WIN32 */
+}
+
+bool valid_sock_fd(org::apache::nifi::minifi::io::SocketDescriptor fd) {
+#ifdef WIN32
+  return fd != INVALID_SOCKET && fd >= 0;
+#else
+  return fd >= 0;
+#endif /* WIN32 */
+}
+
+std::string sockaddr_ntop(const sockaddr* const sa) {
+  std::string result;
+  if (sa->sa_family == AF_INET) {
+    sockaddr_in sa_in{};
+    std::memcpy(reinterpret_cast<void*>(&sa_in), sa, sizeof(sockaddr_in));
+    result.resize(INET_ADDRSTRLEN);
+    if (inet_ntop(AF_INET, &sa_in.sin_addr, &result[0], INET_ADDRSTRLEN) == 
nullptr) {
+      throw minifi::Exception{ minifi::ExceptionType::GENERAL_EXCEPTION, 
get_last_err_str() };
+    }
+  } else if (sa->sa_family == AF_INET6) {
+    sockaddr_in6 sa_in6{};
+    std::memcpy(reinterpret_cast<void*>(&sa_in6), sa, sizeof(sockaddr_in6));
+    result.resize(INET6_ADDRSTRLEN);
+    if (inet_ntop(AF_INET6, &sa_in6.sin6_addr, &result[0], INET6_ADDRSTRLEN) 
== nullptr) {
+      throw minifi::Exception{ minifi::ExceptionType::GENERAL_EXCEPTION, 
get_last_err_str() };
+    }
+  } else {
+    throw minifi::Exception{ minifi::ExceptionType::GENERAL_EXCEPTION, 
"sockaddr_ntop: unknown address family" };
+  }
+  result.resize(strlen(result.c_str()));  // discard remaining null bytes at 
the end
+  return result;
+}
+
+template<typename T, typename Pred, typename Adv>
 
 Review comment:
   Iterating on a linked list is one of the basic CS concepts, one of the first 
things people learn. Almost everyone used it, C developers use it all the time, 
even C++ developers who interface with OS functions should be pretty familiar 
with it. It is easily readable and prevalent in C/C++ codebases.
   
   The point of using templates, at least in my view, is adding generic, 
reusable functionality. This template sits in a cpp file, and is used in a 
single function, so it is not reused, nor it is very much reusable.
   
   I agree that separating logic from control, in the style of the Algorithms 
library can be beneficial, because it can help readbility by enabling the 
reader to focus on the main business logic, and it helps avoiding mistakes in 
implementing the control flows.
   
   In this case however, the extracted control flow is a simple linked list 
iterating loop. To achieve this, a template - with a 2 full line "minimal 
concept emulation", which is an eyesore, in my opinion - is created, multiple 
lambdas are manufactured and then used for calling the template function.
   It is much harder to read than a straightforward loop would be, and since 
the control flow it replaces is a trivial one, it does not help too much on 
that front either.
   
   On the whole, this abstraction feels completely gratuitous, and reminds me 
of the Jurassic Park quote: "Your scientists were so preoccupied with whether 
or not they could, they didn’t stop to think if they should."

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to