Forráskód Böngészése

Add ProtocolMapper class

- add Protocol class to hold OS specific information
- add mapper for mapping protocol type to OS specific type
- add test coverage for ProtocolMapper class
Patrick-Christopher Mattulat 2 éve
szülő
commit
256c8da50a

+ 2 - 0
CMakeLists.txt

@@ -181,6 +181,7 @@ set(SOURCE_FILES_IO
 
 set(SOURCE_FILES_NETWORK
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls_std/network/core/ProtocolFamilyMapper.cpp
+        ${CMAKE_CURRENT_SOURCE_DIR}/source/ls_std/network/core/ProtocolMapper.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls_std/network/socket/Socket.cpp)
 
 set(SOURCE_FILES_TIME
@@ -261,6 +262,7 @@ if (${LS_STD_BUILD_WITH_TESTS})
 
     set(TEST_FILES_NETWORK
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/network/core/ProtocolFamilyMapperTest.cpp
+            ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/network/core/ProtocolMapperTest.cpp
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/network/socket/SocketTest.cpp)
 
     set(TEST_FILES_SERIALIZATION

+ 3 - 1
include/ls_std/ls_std_network.hpp

@@ -3,16 +3,18 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2022-11-16
- * Changed:         2022-11-16
+ * Changed:         2022-11-17
  *
  * */
 
 #ifndef LS_STD_LS_STD_NETWORK_HPP
 #define LS_STD_LS_STD_NETWORK_HPP
 
+#include <ls_std/network/core/Protocol.hpp>
 #include <ls_std/network/core/ProtocolFamily.hpp>
 #include <ls_std/network/core/ProtocolFamilyMapper.hpp>
 #include <ls_std/network/core/ProtocolFamilyType.hpp>
+#include <ls_std/network/core/ProtocolMapper.hpp>
 #include <ls_std/network/core/ProtocolType.hpp>
 #include <ls_std/network/socket/Socket.hpp>
 #include <ls_std/network/socket/SocketAddress.hpp>

+ 21 - 0
include/ls_std/network/core/Protocol.hpp

@@ -0,0 +1,21 @@
+/*
+ * Author:          Patrick-Christopher Mattulat
+ * Company:         Lynar Studios
+ * E-Mail:          webmaster@lynarstudios.com
+ * Created:         2022-11-17
+ * Changed:         2022-11-17
+ *
+ * */
+
+#ifndef LS_STD_PROTOCOL_HPP
+#define LS_STD_PROTOCOL_HPP
+
+namespace ls::std::network
+{
+  struct Protocol
+  {
+    int unixProtocol{};
+  };
+}
+
+#endif

+ 37 - 0
include/ls_std/network/core/ProtocolMapper.hpp

@@ -0,0 +1,37 @@
+/*
+ * Author:          Patrick-Christopher Mattulat
+ * Company:         Lynar Studios
+ * E-Mail:          webmaster@lynarstudios.com
+ * Created:         2022-11-17
+ * Changed:         2022-11-17
+ *
+ * */
+
+#ifndef LS_STD_PROTOCOL_MAPPER_HPP
+#define LS_STD_PROTOCOL_MAPPER_HPP
+
+#include <ls_std/os/dynamic_goal.hpp>
+#include <ls_std/core/Class.hpp>
+#include "Protocol.hpp"
+#include "ProtocolType.hpp"
+
+namespace ls::std::network
+{
+  class LS_STD_DYNAMIC_GOAL ProtocolMapper : public ls::std::core::Class
+  {
+    public:
+
+      ProtocolMapper();
+      ~ProtocolMapper() override = default;
+
+      [[nodiscard]] static ls::std::network::Protocol from(const ls::std::network::ProtocolType& _protocolType);
+
+    private:
+
+      #if defined(unix) || defined(__APPLE__)
+      [[nodiscard]] static ls::std::network::Protocol _toUnixProtocol(const ls::std::network::ProtocolType& _protocolType);
+      #endif
+  };
+}
+
+#endif

+ 45 - 0
source/ls_std/network/core/ProtocolMapper.cpp

@@ -0,0 +1,45 @@
+/*
+ * Author:          Patrick-Christopher Mattulat
+ * Company:         Lynar Studios
+ * E-Mail:          webmaster@lynarstudios.com
+ * Created:         2022-11-17
+ * Changed:         2022-11-17
+ *
+ * */
+
+#include <ls_std/network/core/ProtocolMapper.hpp>
+#if defined(unix) || defined(__APPLE__)
+#include <sys/socket.h>
+#endif
+#include <ls_std/core/exception/IllegalArgumentException.hpp>
+
+ls::std::network::ProtocolMapper::ProtocolMapper() : ls::std::core::Class("ProtocolMapper")
+{}
+
+ls::std::network::Protocol ls::std::network::ProtocolMapper::from(const ls::std::network::ProtocolType &_protocolType)
+{
+  #if defined(unix) || defined(__APPLE__)
+  return ls::std::network::ProtocolMapper::_toUnixProtocol(_protocolType);
+  #endif
+}
+
+#if defined(unix) || defined(__APPLE__)
+ls::std::network::Protocol ls::std::network::ProtocolMapper::_toUnixProtocol(const ls::std::network::ProtocolType &_protocolType)
+{
+  ls::std::network::Protocol protocol{};
+
+  switch (_protocolType)
+  {
+    case ls::std::network::ProtocolType::PROTOCOL_TYPE_TCP:
+    {
+      protocol.unixProtocol = SOCK_STREAM;
+    } break;
+    case ls::std::network::ProtocolType::PROTOCOL_TYPE_NOT_INITIALIZED:
+    {
+      throw ls::std::core::IllegalArgumentException{};
+    }
+  }
+
+  return protocol;
+}
+#endif

+ 63 - 0
test/cases/network/core/ProtocolMapperTest.cpp

@@ -0,0 +1,63 @@
+/*
+ * Author:          Patrick-Christopher Mattulat
+ * Company:         Lynar Studios
+ * E-Mail:          webmaster@lynarstudios.com
+ * Created:         2020-11-17
+ * Changed:         2022-11-17
+ *
+ * */
+
+#include <gtest/gtest.h>
+#include <ls_std/ls_std_network.hpp>
+#if defined(unix) || defined(__APPLE__)
+#include <sys/socket.h>
+#endif
+#include <ls_std/ls_std_core.hpp>
+
+using namespace ls::std::network;
+using namespace ls::std::core;
+
+namespace
+{
+  class ProtocolMapperTest : public ::testing::Test
+  {
+    protected:
+
+      ProtocolMapperTest() = default;
+      ~ProtocolMapperTest() override = default;
+
+      void SetUp() override
+      {}
+
+      void TearDown() override
+      {}
+  };
+
+  TEST_F(ProtocolMapperTest, getClassName)
+  {
+    ASSERT_STREQ("ProtocolMapper", ProtocolMapper{}.getClassName().c_str());
+  }
+
+  TEST_F(ProtocolMapperTest, from)
+  {
+    Protocol protocol = ProtocolMapper::from(ProtocolType::PROTOCOL_TYPE_TCP);
+
+    #if defined(unix) || defined(__APPLE__)
+    ASSERT_EQ(SOCK_STREAM, protocol.unixProtocol);
+    #endif
+  }
+
+  TEST_F(ProtocolMapperTest, from_invalid_protocol_type)
+  {
+    EXPECT_THROW({
+                   try
+                   {
+                     Protocol protocol = ProtocolMapper::from(ProtocolType::PROTOCOL_TYPE_NOT_INITIALIZED);
+                   }
+                   catch (const IllegalArgumentException &_exception)
+                   {
+                     throw;
+                   }
+                 }, IllegalArgumentException);
+  }
+}