From 33f27bebcd11052a80ba31a5343377a656d57679 Mon Sep 17 00:00:00 2001 From: Greg Medding Date: Thu, 11 Jul 2024 18:46:57 -0700 Subject: [PATCH 01/10] Removing unused utility stubs These utilities were not needed for the 1.0.0 release of up-cpp, so they're being removed from the release branch. See #214 #215 #216 #217 --- include/up-cpp/utils/CyclicQueue.h | 61 ------------ include/up-cpp/utils/IpAddress.h | 124 ------------------------ include/up-cpp/utils/ThreadPool.h | 55 ----------- include/up-cpp/utils/base64.h | 49 ---------- src/utils/IpAddress.cpp | 12 --- src/utils/ThreadPool.cpp | 12 --- src/utils/base64.cpp | 12 --- test/CMakeLists.txt | 4 - test/coverage/utils/CyclicQueueTest.cpp | 38 -------- test/coverage/utils/IpAddressTest.cpp | 38 -------- test/coverage/utils/ThreadPoolTest.cpp | 38 -------- test/coverage/utils/base64Test.cpp | 38 -------- 12 files changed, 481 deletions(-) delete mode 100644 include/up-cpp/utils/CyclicQueue.h delete mode 100644 include/up-cpp/utils/IpAddress.h delete mode 100644 include/up-cpp/utils/ThreadPool.h delete mode 100644 include/up-cpp/utils/base64.h delete mode 100644 src/utils/IpAddress.cpp delete mode 100644 src/utils/ThreadPool.cpp delete mode 100644 src/utils/base64.cpp delete mode 100644 test/coverage/utils/CyclicQueueTest.cpp delete mode 100644 test/coverage/utils/IpAddressTest.cpp delete mode 100644 test/coverage/utils/ThreadPoolTest.cpp delete mode 100644 test/coverage/utils/base64Test.cpp diff --git a/include/up-cpp/utils/CyclicQueue.h b/include/up-cpp/utils/CyclicQueue.h deleted file mode 100644 index 2fc8ee13f..000000000 --- a/include/up-cpp/utils/CyclicQueue.h +++ /dev/null @@ -1,61 +0,0 @@ -// SPDX-FileCopyrightText: 2024 Contributors to the Eclipse Foundation -// -// See the NOTICE file(s) distributed with this work for additional -// information regarding copyright ownership. -// -// This program and the accompanying materials are made available under the -// terms of the Apache License Version 2.0 which is available at -// https://www.apache.org/licenses/LICENSE-2.0 -// -// SPDX-License-Identifier: Apache-2.0 - -#ifndef UP_CPP_UTILS_CYCLICQUEUE_H -#define UP_CPP_UTILS_CYCLICQUEUE_H - -#include -#include -#include - -namespace uprotocol::utils { - -/// @brief Queue that enforces a maximum size by evicting the oldest entry to -/// make room for new ones. -template -class CyclicQueue final { -public: - explicit CyclicQueue(const size_t max_size); - - CyclicQueue(const CyclicQueue&) = delete; - CyclicQueue& operator=(const CyclicQueue&) = delete; - - virtual ~CyclicQueue() = default; - - void push(T&& data) noexcept; - void push(const T& data) noexcept; - - bool isFull() const noexcept; - bool isEmpty() const noexcept; - - // Blocking pop() - bool pop(T& popped_value) noexcept; - // Non-blocking pop() - bool tryPop(T& popped_value) noexcept; - // Time-limited blocking pop()s - bool tryPopFor(T& popped_value, std::chrono::milliseconds limit) noexcept; - bool tryPopUntil(T& popped_value, - std::chrono::system_clock::time_point when) noexcept; - - size_t size() const noexcept; - - void clear() noexcept; - -private: - size_t queueMaxSize_; - mutable std::mutex mutex_; - std::condition_variable conditionVariable_; - std::queue queue_; -}; - -} // namespace uprotocol::utils - -#endif // UP_CPP_UTILS_CYCLICQUEUE_H diff --git a/include/up-cpp/utils/IpAddress.h b/include/up-cpp/utils/IpAddress.h deleted file mode 100644 index b8ed8c735..000000000 --- a/include/up-cpp/utils/IpAddress.h +++ /dev/null @@ -1,124 +0,0 @@ -// SPDX-FileCopyrightText: 2024 Contributors to the Eclipse Foundation -// -// See the NOTICE file(s) distributed with this work for additional -// information regarding copyright ownership. -// -// This program and the accompanying materials are made available under the -// terms of the Apache License Version 2.0 which is available at -// https://www.apache.org/licenses/LICENSE-2.0 -// -// SPDX-License-Identifier: Apache-2.0 - -#ifndef UP_CPP_UTILS_IPADDRESS_H -#define UP_CPP_UTILS_IPADDRESS_H - -#include -#include -#include -#include -#include - -namespace uprotocol::utils { - -/// @brief Utility for converting IP addresses between representations and for -/// normalizing string representations. -/// -/// IP addresses can be represented as either human-readable strings or binary -/// numbers, and may be either IPv4 or IPv6. Additionally, each address family -/// has multiple allowable string representations. For example these IPv6 -/// addresses are all the same: -/// -/// * `2001::C0FF:EE01` -/// * `2001:0000:0000:0000:0000:0000:c0ff:ee01` -/// * `2001:0000:0000:0000::0000:192.255.238.1` -/// -/// This class provides three utilities: -/// -/// 1. Given a string, it will detect if it contains an IP address and which -/// family (IPv4 or IPv6) it contains. -/// 2. Given a string IP address, it will convert the address to its binary -/// representation. -/// 3. Given a binary representation of an IP address, it will convert it -/// to a string. -/// 4. In all cases, it will produce a consistent, normalized string -/// representation of an IP address. -/// -/// Point #4 is particularly important in any scenario where a direct string -/// match is used on an address, such as in the Zenoh URI <-> Topic mapping -/// (https://github.com/eclipse-uprotocol/up-spec/blob/main/up-l1/zenoh.adoc). -struct IpAddress { - /// @brief Describes the type / family of an IP address - enum class Type { - /// @brief For IPv4 family addresses - IpV4, - /// @brief For IPv6 family addresses - IpV6, - /// @brief Used when constructed from a string that is not an IP - Invalid - }; - - /// @brief Constructs an IP address from a string representation of - /// an address. - explicit IpAddress(std::string_view const ip_string); - - /// @brief Constructs an IP address from a binary representation of - /// an address. - IpAddress(std::vector const& ip_bytes, Type type); - - /// @brief Get the type of this IP address. - [[nodiscard]] Type getType() const; - - /// @brief Gets the nomalized string representation of this IP address. - [[nodiscard]] const std::string& getString() const; - - /// @brief Gets the binary representation of this IP address. - [[nodiscard]] const std::vector& getBytes() const; - - /// @brief Gets the binary representation of this IP address, wrapped in a - /// string-like container to better interface with protobuf. - /// - /// Protobuf uses std::string as a generic byte container, so this can be - /// useful for embedding compact, binary representations of IP addresses - /// into a protobuf message. - [[nodiscard]] std::string getBytesString() const; - - /// @brief Number of bytes in IPv4 address. - static constexpr uint8_t ip_v4_address_bytes = 4; - - /// @brief Number of bytes in IPv6 address. - static constexpr uint8_t ip_v6_address_bytes = 16; - -private: - /// @brief Updates the state of this instance from the value of the - /// ipString_ field. - void fromString(); - - /// @brief Updates the state of this instance from the value of the - /// ipBytes_ field. - void fromBytes(); - - /// @brief Type of the IP addess contained in this instance. - Type type_{Type::Invalid}; - - /// @brief IP address in byte format. - std::vector ipBytes_{}; - - /// @brief IP address in string format. - std::string ipString_{}; - - /// @name String container compatibility checks - /// @{ - using Bytes = typename std::decay_t::value_type; - using StrBytes = typename std::string_view::value_type; - using StrBytesPtr = typename std::string_view::const_pointer; - - static_assert( - sizeof(StrBytes) == sizeof(Bytes), - "Mismatch in size between string_view value type and ipBytes_ " - "value type invalidates reinterpret_cast used in constructor."); - /// @} -}; - -} // namespace uprotocol::utils - -#endif // UP_CPP_UTILS_IPADDRESS_H diff --git a/include/up-cpp/utils/ThreadPool.h b/include/up-cpp/utils/ThreadPool.h deleted file mode 100644 index d1d6cde46..000000000 --- a/include/up-cpp/utils/ThreadPool.h +++ /dev/null @@ -1,55 +0,0 @@ -// SPDX-FileCopyrightText: 2024 Contributors to the Eclipse Foundation -// -// See the NOTICE file(s) distributed with this work for additional -// information regarding copyright ownership. -// -// This program and the accompanying materials are made available under the -// terms of the Apache License Version 2.0 which is available at -// https://www.apache.org/licenses/LICENSE-2.0 -// -// SPDX-License-Identifier: Apache-2.0 - -#ifndef UP_CPP_UTILS_THREADPOOL_H -#define UP_CPP_UTILS_THREADPOOL_H - -#include - -#include -#include -#include -#include -#include -#include -#include - -namespace uprotocol::utils { - -class ThreadPool { -public: - ThreadPool(const ThreadPool&) = delete; - ThreadPool(ThreadPool&&) = delete; - - ThreadPool& operator=(const ThreadPool&) = delete; - ThreadPool& operator=(ThreadPool&&) = delete; - - ThreadPool(const size_t max_queue_size, const size_t max_num_of_threads, - std::chrono::milliseconds task_timeout); - - ~ThreadPool(); - - // Submit a function to be executed asynchronously by the pool - template - auto submit(F&& f, Args&&... args) -> std::future; - -private: - CyclicQueue> queue_; - bool terminate_; - size_t maxNumOfThreads_; - std::atomic numOfThreads_; - std::vector> threads_; - std::mutex mutex_; - const std::chrono::milliseconds timeout_; -}; -} // namespace uprotocol::utils - -#endif // UP_CPP_UTILS_THREADPOOL_H diff --git a/include/up-cpp/utils/base64.h b/include/up-cpp/utils/base64.h deleted file mode 100644 index 88453460e..000000000 --- a/include/up-cpp/utils/base64.h +++ /dev/null @@ -1,49 +0,0 @@ -// SPDX-FileCopyrightText: 2024 Contributors to the Eclipse Foundation -// -// See the NOTICE file(s) distributed with this work for additional -// information regarding copyright ownership. -// -// This program and the accompanying materials are made available under the -// terms of the Apache License Version 2.0 which is available at -// https://www.apache.org/licenses/LICENSE-2.0 -// -// SPDX-License-Identifier: Apache-2.0 - -#ifndef UP_CPP_UTILS_BASE64_H -#define UP_CPP_UTILS_BASE64_H - -#include -#include -#include -#include - -/// @brief Utilities for encoding / decoding data in Base 64 format. -namespace uprotocol::utils::Base64 { - -/// @brief Encode a string of bytes to base64. -std::string encode(std::string_view); - -/// @brief Encode a vector of bytes to base64. -std::string encode(const std::vector&); - -/// @brief Decode a base64 string to a string of bytes. -std::string decodeAsString(std::string_view); - -/// @brief Decode a base64 string to a vector of bytes. -std::vector decodeAsBytes(std::string_view); - -/// @brief Given a string of bytes, calculate the length needed for a -/// base64 encoding of that string. -size_t encodedLen(std::string_view); - -/// @brief Given a vector of bytes, calculate the length needed for a -/// base64 encoding of that vector. -size_t encodedLen(std::vector); - -/// @brief Given a base64 encoded string, calculate the length of the -/// decoded data. -size_t decodedLen(std::string_view); - -} // namespace uprotocol::utils::Base64 - -#endif // UP_CPP_UTILS_BASE64_H diff --git a/src/utils/IpAddress.cpp b/src/utils/IpAddress.cpp deleted file mode 100644 index 8c222e49a..000000000 --- a/src/utils/IpAddress.cpp +++ /dev/null @@ -1,12 +0,0 @@ -// SPDX-FileCopyrightText: 2024 Contributors to the Eclipse Foundation -// -// See the NOTICE file(s) distributed with this work for additional -// information regarding copyright ownership. -// -// This program and the accompanying materials are made available under the -// terms of the Apache License Version 2.0 which is available at -// https://www.apache.org/licenses/LICENSE-2.0 -// -// SPDX-License-Identifier: Apache-2.0 - -#include "up-cpp/utils/IpAddress.h" diff --git a/src/utils/ThreadPool.cpp b/src/utils/ThreadPool.cpp deleted file mode 100644 index 8733d7e32..000000000 --- a/src/utils/ThreadPool.cpp +++ /dev/null @@ -1,12 +0,0 @@ -// SPDX-FileCopyrightText: 2024 Contributors to the Eclipse Foundation -// -// See the NOTICE file(s) distributed with this work for additional -// information regarding copyright ownership. -// -// This program and the accompanying materials are made available under the -// terms of the Apache License Version 2.0 which is available at -// https://www.apache.org/licenses/LICENSE-2.0 -// -// SPDX-License-Identifier: Apache-2.0 - -#include "up-cpp/utils/ThreadPool.h" diff --git a/src/utils/base64.cpp b/src/utils/base64.cpp deleted file mode 100644 index 474163955..000000000 --- a/src/utils/base64.cpp +++ /dev/null @@ -1,12 +0,0 @@ -// SPDX-FileCopyrightText: 2024 Contributors to the Eclipse Foundation -// -// See the NOTICE file(s) distributed with this work for additional -// information regarding copyright ownership. -// -// This program and the accompanying materials are made available under the -// terms of the Apache License Version 2.0 which is available at -// https://www.apache.org/licenses/LICENSE-2.0 -// -// SPDX-License-Identifier: Apache-2.0 - -#include "up-cpp/utils/base64.h" diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 3c9a6f573..e689f2ab6 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -50,11 +50,7 @@ endfunction() ########################### COVERAGE ########################################## # Utils add_coverage_test("ExpectedTest" coverage/utils/ExpectedTest.cpp) -add_coverage_test("base64Test" coverage/utils/base64Test.cpp) -add_coverage_test("IpAddressTest" coverage/utils/IpAddressTest.cpp) add_coverage_test("CallbackConnectionTest" coverage/utils/CallbackConnectionTest.cpp) -add_coverage_test("CyclicQueueTest" coverage/utils/CyclicQueueTest.cpp) -add_coverage_test("ThreadPoolTest" coverage/utils/ThreadPoolTest.cpp) # Validators add_coverage_test("UuidValidatorTest" coverage/datamodel/UuidValidatorTest.cpp) diff --git a/test/coverage/utils/CyclicQueueTest.cpp b/test/coverage/utils/CyclicQueueTest.cpp deleted file mode 100644 index 8160e8b33..000000000 --- a/test/coverage/utils/CyclicQueueTest.cpp +++ /dev/null @@ -1,38 +0,0 @@ -// SPDX-FileCopyrightText: 2024 Contributors to the Eclipse Foundation -// -// See the NOTICE file(s) distributed with this work for additional -// information regarding copyright ownership. -// -// This program and the accompanying materials are made available under the -// terms of the Apache License Version 2.0 which is available at -// https://www.apache.org/licenses/LICENSE-2.0 -// -// SPDX-License-Identifier: Apache-2.0 - -#include -#include - -namespace { - -class TestFixture : public testing::Test { -protected: - // Run once per TEST_F. - // Used to set up clean environments per test. - void SetUp() override {} - void TearDown() override {} - - // Run once per execution of the test application. - // Used for setup of all tests. Has access to this instance. - TestFixture() = default; - ~TestFixture() = default; - - // Run once per execution of the test application. - // Used only for global setup outside of tests. - static void SetUpTestSuite() {} - static void TearDownTestSuite() {} -}; - -// TODO replace -TEST_F(TestFixture, SomeTestName) {} - -} // namespace diff --git a/test/coverage/utils/IpAddressTest.cpp b/test/coverage/utils/IpAddressTest.cpp deleted file mode 100644 index b243bd071..000000000 --- a/test/coverage/utils/IpAddressTest.cpp +++ /dev/null @@ -1,38 +0,0 @@ -// SPDX-FileCopyrightText: 2024 Contributors to the Eclipse Foundation -// -// See the NOTICE file(s) distributed with this work for additional -// information regarding copyright ownership. -// -// This program and the accompanying materials are made available under the -// terms of the Apache License Version 2.0 which is available at -// https://www.apache.org/licenses/LICENSE-2.0 -// -// SPDX-License-Identifier: Apache-2.0 - -#include -#include - -namespace { - -class TestFixture : public testing::Test { -protected: - // Run once per TEST_F. - // Used to set up clean environments per test. - void SetUp() override {} - void TearDown() override {} - - // Run once per execution of the test application. - // Used for setup of all tests. Has access to this instance. - TestFixture() = default; - ~TestFixture() = default; - - // Run once per execution of the test application. - // Used only for global setup outside of tests. - static void SetUpTestSuite() {} - static void TearDownTestSuite() {} -}; - -// TODO replace -TEST_F(TestFixture, SomeTestName) {} - -} // namespace diff --git a/test/coverage/utils/ThreadPoolTest.cpp b/test/coverage/utils/ThreadPoolTest.cpp deleted file mode 100644 index 22b1dfd6f..000000000 --- a/test/coverage/utils/ThreadPoolTest.cpp +++ /dev/null @@ -1,38 +0,0 @@ -// SPDX-FileCopyrightText: 2024 Contributors to the Eclipse Foundation -// -// See the NOTICE file(s) distributed with this work for additional -// information regarding copyright ownership. -// -// This program and the accompanying materials are made available under the -// terms of the Apache License Version 2.0 which is available at -// https://www.apache.org/licenses/LICENSE-2.0 -// -// SPDX-License-Identifier: Apache-2.0 - -#include -#include - -namespace { - -class TestFixture : public testing::Test { -protected: - // Run once per TEST_F. - // Used to set up clean environments per test. - void SetUp() override {} - void TearDown() override {} - - // Run once per execution of the test application. - // Used for setup of all tests. Has access to this instance. - TestFixture() = default; - ~TestFixture() = default; - - // Run once per execution of the test application. - // Used only for global setup outside of tests. - static void SetUpTestSuite() {} - static void TearDownTestSuite() {} -}; - -// TODO replace -TEST_F(TestFixture, SomeTestName) {} - -} // namespace diff --git a/test/coverage/utils/base64Test.cpp b/test/coverage/utils/base64Test.cpp deleted file mode 100644 index 34fec4db0..000000000 --- a/test/coverage/utils/base64Test.cpp +++ /dev/null @@ -1,38 +0,0 @@ -// SPDX-FileCopyrightText: 2024 Contributors to the Eclipse Foundation -// -// See the NOTICE file(s) distributed with this work for additional -// information regarding copyright ownership. -// -// This program and the accompanying materials are made available under the -// terms of the Apache License Version 2.0 which is available at -// https://www.apache.org/licenses/LICENSE-2.0 -// -// SPDX-License-Identifier: Apache-2.0 - -#include -#include - -namespace { - -class TestFixture : public testing::Test { -protected: - // Run once per TEST_F. - // Used to set up clean environments per test. - void SetUp() override {} - void TearDown() override {} - - // Run once per execution of the test application. - // Used for setup of all tests. Has access to this instance. - TestFixture() = default; - ~TestFixture() = default; - - // Run once per execution of the test application. - // Used only for global setup outside of tests. - static void SetUpTestSuite() {} - static void TearDownTestSuite() {} -}; - -// TODO replace -TEST_F(TestFixture, SomeTestName) {} - -} // namespace From 4870481548ff0694a627acf48438d568b6c2bde5 Mon Sep 17 00:00:00 2001 From: Greg Medding Date: Thu, 11 Jul 2024 18:53:41 -0700 Subject: [PATCH 02/10] Pointing CI jobs at the release version of up-spec Updating up-spec version to the official alpha.2 release 1.6.0 Also removing the last reference to my fork of up-conan-recipes --- .github/workflows/ci.yml | 2 +- .github/workflows/codeql.yml | 2 +- .github/workflows/coverage.yml | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 0fe1704ca..c7f2402dc 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -39,7 +39,7 @@ jobs: - name: Build up-core-api conan package shell: bash run: | - conan create --version 1.5.8 up-conan-recipes/up-core-api/developer + conan create --version 1.6.0 up-conan-recipes/up-core-api/release - name: Build up-cpp with tests shell: bash diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml index 38a659b11..d1fd48f19 100644 --- a/.github/workflows/codeql.yml +++ b/.github/workflows/codeql.yml @@ -69,7 +69,7 @@ jobs: name: Build up-core-api conan package shell: bash run: | - conan create --version 1.5.8 up-conan-recipes/up-core-api/developer + conan create --version 1.6.0 up-conan-recipes/up-core-api/release - if: matrix.build-mode == 'manual' name: Build up-cpp with tests diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml index d4cfa94a3..6ac1d7dfb 100644 --- a/.github/workflows/coverage.yml +++ b/.github/workflows/coverage.yml @@ -37,12 +37,12 @@ jobs: uses: actions/checkout@v4 with: path: up-conan-recipes - repository: gregmedd/up-conan-recipes + repository: eclipse-uprotocol/up-conan-recipes - name: Build up-core-api conan package shell: bash run: | - conan create --version 1.5.8 up-conan-recipes/up-core-api/developer + conan create --version 1.6.0 up-conan-recipes/up-core-api/release - name: Build up-cpp with tests shell: bash From d67dcf6150209c12508bc9c0d871a86c16a553b3 Mon Sep 17 00:00:00 2001 From: Greg Medding Date: Fri, 12 Jul 2024 10:11:37 -0700 Subject: [PATCH 03/10] Updating build versions and dependencies Conan dependencies are locked in to specific versions for the release. The up-cpp package version will be 1.0.0 for alpha.2. --- CMakeLists.txt | 2 +- conanfile.txt | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 442352175..31e6097d2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,7 +10,7 @@ # SPDX-License-Identifier: Apache-2.0 cmake_minimum_required(VERSION 3.10.0) -project(up-cpp VERSION 0.1.5 LANGUAGES CXX DESCRIPTION "C++ API for uProtocol") +project(up-cpp VERSION 1.0.0 LANGUAGES CXX DESCRIPTION "C++ API for uProtocol") find_package(protobuf REQUIRED) find_package(spdlog REQUIRED) diff --git a/conanfile.txt b/conanfile.txt index a437df1d5..edc726b8c 100644 --- a/conanfile.txt +++ b/conanfile.txt @@ -1,8 +1,7 @@ [requires] -up-core-api/1.5.8 +up-core-api/1.6.0 spdlog/1.13.0 -# Should result in using the one from up-core-api -protobuf/[>3.21.11] +protobuf/3.21.12 [test_requires] gtest/1.14.0 From 3cafc9973bd2c768df7f08b8a16a7f6fbab6c914 Mon Sep 17 00:00:00 2001 From: Greg Medding Date: Fri, 12 Jul 2024 10:17:23 -0700 Subject: [PATCH 04/10] Updating version numbers in README.md Referencing release package for up-core-api and changing guidance on how to consume up-cpp with a `conanfile.txt`. --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 38918725e..222ad7c53 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ implementation, such as [up-transport-zenoh-cpp][zenoh-transport-repo]. Using the recipes found in [up-conan-recipes][conan-recipe-repo], build these Conan packages: -1. [up-core-api][spec-repo]: `conan create --version 1.5.8 --build=missing up-core-api/developer` +1. [up-core-api][spec-repo]: `conan create --version 1.6.0 --build=missing up-core-api/release` **NOTE:** all `conan` commands in this document use Conan 2.x syntax. Please adjust accordingly when using Conan 1.x. @@ -34,7 +34,7 @@ To add up-cpp to your conan build dependencies, place following in your ``` [requires] -up-cpp/0.2.0 +up-cpp/[~1.0] [generators] CMakeDeps @@ -96,4 +96,4 @@ Give a ⭐️ if this project helped you! [conan-recipe-repo]: https://github.com/eclipse-uprotocol/up-conan-recipes [spec-repo]: https://github.com/eclipse-uprotocol/up-spec [conan-abi-docs]: https://docs.conan.io/en/1.60/howtos/manage_gcc_abi.html -[conan-txt-reference]: https://docs.conan.io/2/reference/conanfile_txt.html \ No newline at end of file +[conan-txt-reference]: https://docs.conan.io/2/reference/conanfile_txt.html From dfc90b3b827854d292c4ffe88b7af0860d94c5f1 Mon Sep 17 00:00:00 2001 From: Greg Medding Date: Fri, 12 Jul 2024 10:41:40 -0700 Subject: [PATCH 05/10] Removing spdlog Nothing in the alpha.2 release code is using spdlog, so there is no need to include it as a depenency in the 1.0.0 release. --- CMakeLists.txt | 8 +------- NOTICE.adoc | 1 - conanfile.txt | 1 - test/CMakeLists.txt | 1 - 4 files changed, 1 insertion(+), 10 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 31e6097d2..6c21980eb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,7 +13,6 @@ cmake_minimum_required(VERSION 3.10.0) project(up-cpp VERSION 1.0.0 LANGUAGES CXX DESCRIPTION "C++ API for uProtocol") find_package(protobuf REQUIRED) -find_package(spdlog REQUIRED) find_package(up-core-api REQUIRED) message("* Adding build types...") @@ -36,9 +35,6 @@ if(CMAKE_BUILD_TYPE STREQUAL Coverage) endif() message("* Current Compiler Flags: ${CMAKE_CXX_FLAGS}") -# TODO NEEDED? -#add_definitions(-DSPDLOG_FMT_EXTERNAL) - # This is the root CMakeLists.txt file; We can set project wide settings here # TODO: Is this needed? if(${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR}) @@ -60,8 +56,7 @@ target_include_directories(${PROJECT_NAME} $ $ ${up-core-api_INCLUDE_DIR} - ${protobuf_INCLUDE_DIR} - ${spdlog_INCLUDE_DIR}) + ${protobuf_INCLUDE_DIR}) set_property(TARGET ${PROJECT_NAME} PROPERTY POSITION_INDEPENDENT_CODE ON) @@ -79,7 +74,6 @@ target_link_libraries(${PROJECT_NAME} PRIVATE up-core-api::up-core-api protobuf::libprotobuf - spdlog::spdlog gcov) enable_testing() diff --git a/NOTICE.adoc b/NOTICE.adoc index be66c02c4..a17e0df82 100644 --- a/NOTICE.adoc +++ b/NOTICE.adoc @@ -33,5 +33,4 @@ The following are libraries used by this project: * https://github.com/openssl/openssl * https://github.com/topics/libuuid * https://github.com/Tencent/rapidjson -* https://github.com/gabime/spdlog * https://github.com/cgreen-devs/cgreen diff --git a/conanfile.txt b/conanfile.txt index edc726b8c..eff1c7636 100644 --- a/conanfile.txt +++ b/conanfile.txt @@ -1,6 +1,5 @@ [requires] up-core-api/1.6.0 -spdlog/1.13.0 protobuf/3.21.12 [test_requires] diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index e689f2ab6..43e910031 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -30,7 +30,6 @@ function(add_coverage_test Name) PUBLIC up-core-api::up-core-api up-cpp::up-cpp - spdlog::spdlog protobuf::protobuf PRIVATE GTest::gtest_main From 7b526daa07550357a0e819ff4f879a0b5fb06c96 Mon Sep 17 00:00:00 2001 From: Greg Medding Date: Fri, 12 Jul 2024 11:49:39 -0700 Subject: [PATCH 06/10] Dropping CONTRIBUTORS file based on feedback In eclipse-uprotocol/up-transport-zenoh-cpp#48 we were asked to remove this file. The list of contributors can be determined from commit history in the repo. --- CONTRIBUTORS.adoc | 14 -------------- 1 file changed, 14 deletions(-) delete mode 100644 CONTRIBUTORS.adoc diff --git a/CONTRIBUTORS.adoc b/CONTRIBUTORS.adoc deleted file mode 100644 index 4945e4e73..000000000 --- a/CONTRIBUTORS.adoc +++ /dev/null @@ -1,14 +0,0 @@ -= Eclipse uProtocol Contributors - -These are the initial contributors to Eclipse uProtocol C++ SDK - -|=== -| GitHub username | Name - -|https://github.com/MelamudMichael[MelamudMichael] | Michael Melamud -|https://github.com/uzimeirov[uzimeirov] | Uzi Meirov -|https://github.com/adienzel[adienzel] | Adi -|https://github.com/giorgiozoppi[giorgiozoppi] | Giorgio Zoppi -|https://github.com/stevenhartley[stevenhartley] | Steven Hartley -|https://github.com/gregmedd[gregmedd] | Greg Medding -|=== From 9deeb2167b7499b696acd92cf0386567d0be2fdc Mon Sep 17 00:00:00 2001 From: Greg Medding Date: Fri, 12 Jul 2024 11:51:47 -0700 Subject: [PATCH 07/10] Updating README for clarity and using new build steps Build steps are based on capabilities from new(er) versions of cmake, simplifying the process of generating builds. Also adding a CI badge to the top of the README --- README.md | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 222ad7c53..cc790eca3 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,7 @@ # uProtocol C++ Interface Library (up-cpp) +[![Build & Test](https://github.com/eclipse-uprotocol/up-cpp/actions/workflows/ci.yml/badge.svg?branch=v1.0_up-v1.6.0)](https://github.com/eclipse-uprotocol/up-cpp/actions/workflows/ci.yml) + ## Welcome! This library provides the primary interface to uProtocol for uEntity @@ -56,30 +58,38 @@ up-cpp, If you are making a project that uses up-cpp, follow the steps in the ### With Conan for dependencies -Clone the up-cpp repo to a folder named: up-cpp +From the root of this repo, run: -``` -cd up-cpp -conan install . -cd build -cmake ../ -DCMAKE_TOOLCHAIN_FILE=Release/generators/conan_toolchain.cmake -DCMAKE_BUILD_TYPE=Release +```bash +conan install --build=missing . +cmake --preset conan-release +cd build/Release cmake --build . -- -j ``` Once the build completes, tests can be run with `ctest`. -### Generate UT Coverage +Debug builds can be generated by changing those steps to: -To get code coverage, perform the steps above, but use the following cmake line instead +```bash +conan install --build=missing --settings=build_type=Debug . +cmake --preset conan-debug +cd build/Debug +cmake --build . -- -j ``` -cmake ../ -DCMAKE_TOOLCHAIN_FILE=Release/generators/conan_toolchain.cmake -DCMAKE_BUILD_TYPE=Coverage + +### Generate UT Coverage + +To get code coverage, perform the steps above, but replace `cmake --preset...` with +``` +cd build/Release +cmake ../../ -DCMAKE_TOOLCHAIN_FILE=generators/conan_toolchain.cmake -DCMAKE_BUILD_TYPE=Coverage ``` Once the tests complete, the Unit Test Coverage report can be generated from the base up-cpp folder with: ./Coverage.sh ``` ./coverage.sh ``` - ### With dependencies installed as system libraries **TODO** Verify steps for pure cmake build without Conan. From be60dd10327b4e120ca9efcb6ffe58eee9445924 Mon Sep 17 00:00:00 2001 From: Greg Medding Date: Fri, 12 Jul 2024 10:55:14 -0700 Subject: [PATCH 08/10] Updating library list in NOTICE.adoc The list was out of date and did not represent the actual usage of libraries in up-cpp. --- NOTICE.adoc | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/NOTICE.adoc b/NOTICE.adoc index a17e0df82..c79c2643c 100644 --- a/NOTICE.adoc +++ b/NOTICE.adoc @@ -27,10 +27,5 @@ SPDX-License-Identifier: Apache-2.0 The following are libraries used by this project: -* http://cloudevents.io -* https://github.com/google/googletest * http://protobuf.dev -* https://github.com/openssl/openssl -* https://github.com/topics/libuuid -* https://github.com/Tencent/rapidjson -* https://github.com/cgreen-devs/cgreen +* https://github.com/google/googletest From a9b92a56fe20d0c7ad880cbf1d22def8077bb740 Mon Sep 17 00:00:00 2001 From: Greg Medding Date: Fri, 12 Jul 2024 12:16:36 -0700 Subject: [PATCH 09/10] Match CI process to revised documentation We are using `cmake --preset conan-release` now --- .github/workflows/ci.yml | 40 +++++++++++++++++----------------- .github/workflows/codeql.yml | 6 ++--- .github/workflows/coverage.yml | 20 ++++++++--------- 3 files changed, 33 insertions(+), 33 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ca4cd7415..ad602491a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -45,9 +45,9 @@ jobs: shell: bash run: | cd up-cpp - conan install . --build=missing - cd build - cmake -S .. -DCMAKE_TOOLCHAIN_FILE=Release/generators/conan_toolchain.cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_EXPORT_COMPILE_COMMANDS=yes + conan install --build=missing . + cmake --preset conan-release -DCMAKE_EXPORT_COMPILE_COMMANDS=yes + cd build/Release cmake --build . -- -j - name: Save conan cache to archive @@ -59,13 +59,13 @@ jobs: uses: actions/upload-artifact@v4 with: name: build-artifacts - path: up-cpp/build + path: up-cpp/build/Release - name: Upload compile commands uses: actions/upload-artifact@v4 with: name: compile-commands - path: up-cpp/build/compile_commands.json + path: up-cpp/build/Release/compile_commands.json - name: Upload conan cache for linting uses: actions/upload-artifact@v4 @@ -83,12 +83,12 @@ jobs: uses: actions/download-artifact@v4 with: name: build-artifacts - path: up-cpp/build + path: up-cpp/build/Release - name: Run all tests shell: bash run: | - cd up-cpp/build + cd up-cpp/build/Release chmod +x bin/* ctest @@ -97,7 +97,7 @@ jobs: if: success() || failure() with: name: test-results - path: 'up-cpp/build/test/results/*.xml' + path: 'up-cpp/build/Release/test/results/*.xml' # NOTE: Run dynamic analysis in unit tests memcheck: @@ -110,7 +110,7 @@ jobs: uses: actions/download-artifact@v4 with: name: build-artifacts - path: up-cpp/build + path: up-cpp/build/Release - name: Install Valgrind run: | @@ -120,7 +120,7 @@ jobs: - name: Run Valgrind Memcheck continue-on-error: true run: | - cd up-cpp/build + cd up-cpp/build/Release chmod +x bin/* mkdir -p valgrind_logs : > valgrind_logs/valgrind_memcheck_summary.log @@ -166,7 +166,7 @@ jobs: if: success() || failure() with: name: valgrind-memcheck-log - path: up-cpp/build/valgrind_logs/valgrind_complete_memcheck_log.log + path: up-cpp/build/Release/valgrind_logs/valgrind_complete_memcheck_log.log threadcheck: name: Run Valgrind ThreadCheck @@ -178,7 +178,7 @@ jobs: uses: actions/download-artifact@v4 with: name: build-artifacts - path: up-cpp/build + path: up-cpp/build/Release - name: Install Valgrind run: | @@ -188,7 +188,7 @@ jobs: - name: Run Valgrind ThreadCheck continue-on-error: true run: | - cd up-cpp/build + cd up-cpp/build/Release chmod +x bin/* mkdir -p valgrind_logs : > valgrind_logs/valgrind_threadcheck_summary.log @@ -235,7 +235,7 @@ jobs: if: success() || failure() with: name: valgrind-threadcheck-log - path: up-cpp/build/valgrind_logs/valgrind_complete_threadcheck_log.log + path: up-cpp/build/Release/valgrind_logs/valgrind_complete_threadcheck_log.log helgrind: name: Run Valgrind Helgrind @@ -247,7 +247,7 @@ jobs: uses: actions/download-artifact@v4 with: name: build-artifacts - path: up-cpp/build + path: up-cpp/build/Release - name: Install Valgrind run: | @@ -257,7 +257,7 @@ jobs: - name: Run Valgrind Helgrind continue-on-error: true run: | - cd up-cpp/build + cd up-cpp/build/Release chmod +x bin/* mkdir -p valgrind_logs : > valgrind_logs/valgrind_helgrind_summary.log @@ -304,7 +304,7 @@ jobs: if: success() || failure() with: name: valgrind-helgrind-log - path: up-cpp/build/valgrind_logs/valgrind_complete_helgrind_log.log + path: up-cpp/build/Release/valgrind_logs/valgrind_complete_helgrind_log.log dhat: name: Run Valgrind DHAT @@ -316,7 +316,7 @@ jobs: uses: actions/download-artifact@v4 with: name: build-artifacts - path: up-cpp/build + path: up-cpp/build/Release - name: Install Valgrind run: | @@ -326,7 +326,7 @@ jobs: - name: Run Valgrind DHAT continue-on-error: true run: | - cd up-cpp/build + cd up-cpp/build/Release chmod +x bin/* mkdir -p valgrind_logs : > valgrind_logs/valgrind_dhat_summary.log @@ -373,7 +373,7 @@ jobs: if: success() || failure() with: name: valgrind-dhat-log - path: up-cpp/build/valgrind_logs/valgrind_complete_dhat_log.log + path: up-cpp/build/Release/valgrind_logs/valgrind_complete_dhat_log.log lint: name: Lint C++ sources diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml index 89445bf07..d4e57bfad 100644 --- a/.github/workflows/codeql.yml +++ b/.github/workflows/codeql.yml @@ -76,9 +76,9 @@ jobs: shell: bash run: | cd up-cpp - conan install . --build=missing - cd build - cmake -S .. -DCMAKE_TOOLCHAIN_FILE=Release/generators/conan_toolchain.cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_EXPORT_COMPILE_COMMANDS=yes + conan install --build=missing . + cmake --preset conan-release -DCMAKE_EXPORT_COMPILE_COMMANDS=yes + cd build/Release cmake --build . -- -j - name: Perform CodeQL Analysis diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml index 28a661d3e..50a738d0a 100644 --- a/.github/workflows/coverage.yml +++ b/.github/workflows/coverage.yml @@ -8,7 +8,7 @@ on: jobs: test: - name: Generate Test Coverage eport + name: Generate Test Coverage Report runs-on: ubuntu-latest steps: @@ -48,26 +48,26 @@ jobs: shell: bash run: | cd up-cpp - conan install . --build=missing - cd build - cmake -S .. -DCMAKE_TOOLCHAIN_FILE=Release/generators/conan_toolchain.cmake -DCMAKE_BUILD_TYPE=Coverage + conan install --build=missing . + cd build/Release + cmake -S ../../ -DCMAKE_TOOLCHAIN_FILE=generators/conan_toolchain.cmake -DCMAKE_BUILD_TYPE=Coverage cmake --build . -- -j - name: Run all tests shell: bash run: | - cd up-cpp/build + cd up-cpp/build/Release chmod +x bin/* ctest || true - name: Run Coverage report shell: bash run: | - cd up-cpp/build/ - mkdir -p ./Coverage - gcovr -r .. --html --html-details -o ./Coverage/index.html -e '.*test.*' + cd up-cpp/build/Release + mkdir -p ../Coverage + gcovr -r ../../ --html --html-details -o ../Coverage/index.html -e '.*test.*' cd .. - echo "Coverage report can be found here: ./Coverage/index.html" + echo "Coverage report can be found here: ../Coverage/index.html" - name: Extract and Print Coverage Percentage shell: bash @@ -79,7 +79,7 @@ jobs: - - name: Upload test results + - name: Upload coverage report uses: actions/upload-artifact@v4 if: success() || failure() with: From b12c4dd9bd36d0f04294fb57c9a422734dfd1b65 Mon Sep 17 00:00:00 2001 From: Greg Medding Date: Fri, 12 Jul 2024 17:01:46 -0700 Subject: [PATCH 10/10] Reverting some 1.0 branch-specific changes We don't want to bring every change from v1.0_up-v1.6.0 back to main. --- CMakeLists.txt | 7 +- README.md | 2 +- conanfile.txt | 7 +- include/up-cpp/utils/CyclicQueue.h | 61 ++++++++++++ include/up-cpp/utils/IpAddress.h | 124 ++++++++++++++++++++++++ include/up-cpp/utils/ThreadPool.h | 55 +++++++++++ include/up-cpp/utils/base64.h | 49 ++++++++++ src/utils/IpAddress.cpp | 12 +++ src/utils/ThreadPool.cpp | 12 +++ src/utils/base64.cpp | 12 +++ test/CMakeLists.txt | 5 + test/coverage/utils/CyclicQueueTest.cpp | 38 ++++++++ test/coverage/utils/IpAddressTest.cpp | 38 ++++++++ test/coverage/utils/ThreadPoolTest.cpp | 38 ++++++++ test/coverage/utils/base64Test.cpp | 38 ++++++++ 15 files changed, 492 insertions(+), 6 deletions(-) create mode 100644 include/up-cpp/utils/CyclicQueue.h create mode 100644 include/up-cpp/utils/IpAddress.h create mode 100644 include/up-cpp/utils/ThreadPool.h create mode 100644 include/up-cpp/utils/base64.h create mode 100644 src/utils/IpAddress.cpp create mode 100644 src/utils/ThreadPool.cpp create mode 100644 src/utils/base64.cpp create mode 100644 test/coverage/utils/CyclicQueueTest.cpp create mode 100644 test/coverage/utils/IpAddressTest.cpp create mode 100644 test/coverage/utils/ThreadPoolTest.cpp create mode 100644 test/coverage/utils/base64Test.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 6c21980eb..256391aae 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,9 +10,10 @@ # SPDX-License-Identifier: Apache-2.0 cmake_minimum_required(VERSION 3.10.0) -project(up-cpp VERSION 1.0.0 LANGUAGES CXX DESCRIPTION "C++ API for uProtocol") +project(up-cpp VERSION 1.1.0 LANGUAGES CXX DESCRIPTION "C++ API for uProtocol") find_package(protobuf REQUIRED) +find_package(spdlog REQUIRED) find_package(up-core-api REQUIRED) message("* Adding build types...") @@ -56,7 +57,8 @@ target_include_directories(${PROJECT_NAME} $ $ ${up-core-api_INCLUDE_DIR} - ${protobuf_INCLUDE_DIR}) + ${protobuf_INCLUDE_DIR} + ${spdlog_INCLUDE_DIR}) set_property(TARGET ${PROJECT_NAME} PROPERTY POSITION_INDEPENDENT_CODE ON) @@ -74,6 +76,7 @@ target_link_libraries(${PROJECT_NAME} PRIVATE up-core-api::up-core-api protobuf::libprotobuf + spdlog::spdlog gcov) enable_testing() diff --git a/README.md b/README.md index cc790eca3..7ef8572b8 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # uProtocol C++ Interface Library (up-cpp) -[![Build & Test](https://github.com/eclipse-uprotocol/up-cpp/actions/workflows/ci.yml/badge.svg?branch=v1.0_up-v1.6.0)](https://github.com/eclipse-uprotocol/up-cpp/actions/workflows/ci.yml) +[![CI](https://github.com/eclipse-uprotocol/up-cpp/actions/workflows/ci.yml/badge.svg)](https://github.com/eclipse-uprotocol/up-cpp/actions/workflows/ci.yml) ## Welcome! diff --git a/conanfile.txt b/conanfile.txt index eff1c7636..ec721a425 100644 --- a/conanfile.txt +++ b/conanfile.txt @@ -1,9 +1,10 @@ [requires] -up-core-api/1.6.0 -protobuf/3.21.12 +up-core-api/[~1.6] +spdlog/[~1.13] +protobuf/[~3.21] [test_requires] -gtest/1.14.0 +gtest/[~1.14] [generators] CMakeDeps diff --git a/include/up-cpp/utils/CyclicQueue.h b/include/up-cpp/utils/CyclicQueue.h new file mode 100644 index 000000000..2fc8ee13f --- /dev/null +++ b/include/up-cpp/utils/CyclicQueue.h @@ -0,0 +1,61 @@ +// SPDX-FileCopyrightText: 2024 Contributors to the Eclipse Foundation +// +// See the NOTICE file(s) distributed with this work for additional +// information regarding copyright ownership. +// +// This program and the accompanying materials are made available under the +// terms of the Apache License Version 2.0 which is available at +// https://www.apache.org/licenses/LICENSE-2.0 +// +// SPDX-License-Identifier: Apache-2.0 + +#ifndef UP_CPP_UTILS_CYCLICQUEUE_H +#define UP_CPP_UTILS_CYCLICQUEUE_H + +#include +#include +#include + +namespace uprotocol::utils { + +/// @brief Queue that enforces a maximum size by evicting the oldest entry to +/// make room for new ones. +template +class CyclicQueue final { +public: + explicit CyclicQueue(const size_t max_size); + + CyclicQueue(const CyclicQueue&) = delete; + CyclicQueue& operator=(const CyclicQueue&) = delete; + + virtual ~CyclicQueue() = default; + + void push(T&& data) noexcept; + void push(const T& data) noexcept; + + bool isFull() const noexcept; + bool isEmpty() const noexcept; + + // Blocking pop() + bool pop(T& popped_value) noexcept; + // Non-blocking pop() + bool tryPop(T& popped_value) noexcept; + // Time-limited blocking pop()s + bool tryPopFor(T& popped_value, std::chrono::milliseconds limit) noexcept; + bool tryPopUntil(T& popped_value, + std::chrono::system_clock::time_point when) noexcept; + + size_t size() const noexcept; + + void clear() noexcept; + +private: + size_t queueMaxSize_; + mutable std::mutex mutex_; + std::condition_variable conditionVariable_; + std::queue queue_; +}; + +} // namespace uprotocol::utils + +#endif // UP_CPP_UTILS_CYCLICQUEUE_H diff --git a/include/up-cpp/utils/IpAddress.h b/include/up-cpp/utils/IpAddress.h new file mode 100644 index 000000000..b8ed8c735 --- /dev/null +++ b/include/up-cpp/utils/IpAddress.h @@ -0,0 +1,124 @@ +// SPDX-FileCopyrightText: 2024 Contributors to the Eclipse Foundation +// +// See the NOTICE file(s) distributed with this work for additional +// information regarding copyright ownership. +// +// This program and the accompanying materials are made available under the +// terms of the Apache License Version 2.0 which is available at +// https://www.apache.org/licenses/LICENSE-2.0 +// +// SPDX-License-Identifier: Apache-2.0 + +#ifndef UP_CPP_UTILS_IPADDRESS_H +#define UP_CPP_UTILS_IPADDRESS_H + +#include +#include +#include +#include +#include + +namespace uprotocol::utils { + +/// @brief Utility for converting IP addresses between representations and for +/// normalizing string representations. +/// +/// IP addresses can be represented as either human-readable strings or binary +/// numbers, and may be either IPv4 or IPv6. Additionally, each address family +/// has multiple allowable string representations. For example these IPv6 +/// addresses are all the same: +/// +/// * `2001::C0FF:EE01` +/// * `2001:0000:0000:0000:0000:0000:c0ff:ee01` +/// * `2001:0000:0000:0000::0000:192.255.238.1` +/// +/// This class provides three utilities: +/// +/// 1. Given a string, it will detect if it contains an IP address and which +/// family (IPv4 or IPv6) it contains. +/// 2. Given a string IP address, it will convert the address to its binary +/// representation. +/// 3. Given a binary representation of an IP address, it will convert it +/// to a string. +/// 4. In all cases, it will produce a consistent, normalized string +/// representation of an IP address. +/// +/// Point #4 is particularly important in any scenario where a direct string +/// match is used on an address, such as in the Zenoh URI <-> Topic mapping +/// (https://github.com/eclipse-uprotocol/up-spec/blob/main/up-l1/zenoh.adoc). +struct IpAddress { + /// @brief Describes the type / family of an IP address + enum class Type { + /// @brief For IPv4 family addresses + IpV4, + /// @brief For IPv6 family addresses + IpV6, + /// @brief Used when constructed from a string that is not an IP + Invalid + }; + + /// @brief Constructs an IP address from a string representation of + /// an address. + explicit IpAddress(std::string_view const ip_string); + + /// @brief Constructs an IP address from a binary representation of + /// an address. + IpAddress(std::vector const& ip_bytes, Type type); + + /// @brief Get the type of this IP address. + [[nodiscard]] Type getType() const; + + /// @brief Gets the nomalized string representation of this IP address. + [[nodiscard]] const std::string& getString() const; + + /// @brief Gets the binary representation of this IP address. + [[nodiscard]] const std::vector& getBytes() const; + + /// @brief Gets the binary representation of this IP address, wrapped in a + /// string-like container to better interface with protobuf. + /// + /// Protobuf uses std::string as a generic byte container, so this can be + /// useful for embedding compact, binary representations of IP addresses + /// into a protobuf message. + [[nodiscard]] std::string getBytesString() const; + + /// @brief Number of bytes in IPv4 address. + static constexpr uint8_t ip_v4_address_bytes = 4; + + /// @brief Number of bytes in IPv6 address. + static constexpr uint8_t ip_v6_address_bytes = 16; + +private: + /// @brief Updates the state of this instance from the value of the + /// ipString_ field. + void fromString(); + + /// @brief Updates the state of this instance from the value of the + /// ipBytes_ field. + void fromBytes(); + + /// @brief Type of the IP addess contained in this instance. + Type type_{Type::Invalid}; + + /// @brief IP address in byte format. + std::vector ipBytes_{}; + + /// @brief IP address in string format. + std::string ipString_{}; + + /// @name String container compatibility checks + /// @{ + using Bytes = typename std::decay_t::value_type; + using StrBytes = typename std::string_view::value_type; + using StrBytesPtr = typename std::string_view::const_pointer; + + static_assert( + sizeof(StrBytes) == sizeof(Bytes), + "Mismatch in size between string_view value type and ipBytes_ " + "value type invalidates reinterpret_cast used in constructor."); + /// @} +}; + +} // namespace uprotocol::utils + +#endif // UP_CPP_UTILS_IPADDRESS_H diff --git a/include/up-cpp/utils/ThreadPool.h b/include/up-cpp/utils/ThreadPool.h new file mode 100644 index 000000000..d1d6cde46 --- /dev/null +++ b/include/up-cpp/utils/ThreadPool.h @@ -0,0 +1,55 @@ +// SPDX-FileCopyrightText: 2024 Contributors to the Eclipse Foundation +// +// See the NOTICE file(s) distributed with this work for additional +// information regarding copyright ownership. +// +// This program and the accompanying materials are made available under the +// terms of the Apache License Version 2.0 which is available at +// https://www.apache.org/licenses/LICENSE-2.0 +// +// SPDX-License-Identifier: Apache-2.0 + +#ifndef UP_CPP_UTILS_THREADPOOL_H +#define UP_CPP_UTILS_THREADPOOL_H + +#include + +#include +#include +#include +#include +#include +#include +#include + +namespace uprotocol::utils { + +class ThreadPool { +public: + ThreadPool(const ThreadPool&) = delete; + ThreadPool(ThreadPool&&) = delete; + + ThreadPool& operator=(const ThreadPool&) = delete; + ThreadPool& operator=(ThreadPool&&) = delete; + + ThreadPool(const size_t max_queue_size, const size_t max_num_of_threads, + std::chrono::milliseconds task_timeout); + + ~ThreadPool(); + + // Submit a function to be executed asynchronously by the pool + template + auto submit(F&& f, Args&&... args) -> std::future; + +private: + CyclicQueue> queue_; + bool terminate_; + size_t maxNumOfThreads_; + std::atomic numOfThreads_; + std::vector> threads_; + std::mutex mutex_; + const std::chrono::milliseconds timeout_; +}; +} // namespace uprotocol::utils + +#endif // UP_CPP_UTILS_THREADPOOL_H diff --git a/include/up-cpp/utils/base64.h b/include/up-cpp/utils/base64.h new file mode 100644 index 000000000..88453460e --- /dev/null +++ b/include/up-cpp/utils/base64.h @@ -0,0 +1,49 @@ +// SPDX-FileCopyrightText: 2024 Contributors to the Eclipse Foundation +// +// See the NOTICE file(s) distributed with this work for additional +// information regarding copyright ownership. +// +// This program and the accompanying materials are made available under the +// terms of the Apache License Version 2.0 which is available at +// https://www.apache.org/licenses/LICENSE-2.0 +// +// SPDX-License-Identifier: Apache-2.0 + +#ifndef UP_CPP_UTILS_BASE64_H +#define UP_CPP_UTILS_BASE64_H + +#include +#include +#include +#include + +/// @brief Utilities for encoding / decoding data in Base 64 format. +namespace uprotocol::utils::Base64 { + +/// @brief Encode a string of bytes to base64. +std::string encode(std::string_view); + +/// @brief Encode a vector of bytes to base64. +std::string encode(const std::vector&); + +/// @brief Decode a base64 string to a string of bytes. +std::string decodeAsString(std::string_view); + +/// @brief Decode a base64 string to a vector of bytes. +std::vector decodeAsBytes(std::string_view); + +/// @brief Given a string of bytes, calculate the length needed for a +/// base64 encoding of that string. +size_t encodedLen(std::string_view); + +/// @brief Given a vector of bytes, calculate the length needed for a +/// base64 encoding of that vector. +size_t encodedLen(std::vector); + +/// @brief Given a base64 encoded string, calculate the length of the +/// decoded data. +size_t decodedLen(std::string_view); + +} // namespace uprotocol::utils::Base64 + +#endif // UP_CPP_UTILS_BASE64_H diff --git a/src/utils/IpAddress.cpp b/src/utils/IpAddress.cpp new file mode 100644 index 000000000..8c222e49a --- /dev/null +++ b/src/utils/IpAddress.cpp @@ -0,0 +1,12 @@ +// SPDX-FileCopyrightText: 2024 Contributors to the Eclipse Foundation +// +// See the NOTICE file(s) distributed with this work for additional +// information regarding copyright ownership. +// +// This program and the accompanying materials are made available under the +// terms of the Apache License Version 2.0 which is available at +// https://www.apache.org/licenses/LICENSE-2.0 +// +// SPDX-License-Identifier: Apache-2.0 + +#include "up-cpp/utils/IpAddress.h" diff --git a/src/utils/ThreadPool.cpp b/src/utils/ThreadPool.cpp new file mode 100644 index 000000000..8733d7e32 --- /dev/null +++ b/src/utils/ThreadPool.cpp @@ -0,0 +1,12 @@ +// SPDX-FileCopyrightText: 2024 Contributors to the Eclipse Foundation +// +// See the NOTICE file(s) distributed with this work for additional +// information regarding copyright ownership. +// +// This program and the accompanying materials are made available under the +// terms of the Apache License Version 2.0 which is available at +// https://www.apache.org/licenses/LICENSE-2.0 +// +// SPDX-License-Identifier: Apache-2.0 + +#include "up-cpp/utils/ThreadPool.h" diff --git a/src/utils/base64.cpp b/src/utils/base64.cpp new file mode 100644 index 000000000..474163955 --- /dev/null +++ b/src/utils/base64.cpp @@ -0,0 +1,12 @@ +// SPDX-FileCopyrightText: 2024 Contributors to the Eclipse Foundation +// +// See the NOTICE file(s) distributed with this work for additional +// information regarding copyright ownership. +// +// This program and the accompanying materials are made available under the +// terms of the Apache License Version 2.0 which is available at +// https://www.apache.org/licenses/LICENSE-2.0 +// +// SPDX-License-Identifier: Apache-2.0 + +#include "up-cpp/utils/base64.h" diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 43e910031..3c9a6f573 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -30,6 +30,7 @@ function(add_coverage_test Name) PUBLIC up-core-api::up-core-api up-cpp::up-cpp + spdlog::spdlog protobuf::protobuf PRIVATE GTest::gtest_main @@ -49,7 +50,11 @@ endfunction() ########################### COVERAGE ########################################## # Utils add_coverage_test("ExpectedTest" coverage/utils/ExpectedTest.cpp) +add_coverage_test("base64Test" coverage/utils/base64Test.cpp) +add_coverage_test("IpAddressTest" coverage/utils/IpAddressTest.cpp) add_coverage_test("CallbackConnectionTest" coverage/utils/CallbackConnectionTest.cpp) +add_coverage_test("CyclicQueueTest" coverage/utils/CyclicQueueTest.cpp) +add_coverage_test("ThreadPoolTest" coverage/utils/ThreadPoolTest.cpp) # Validators add_coverage_test("UuidValidatorTest" coverage/datamodel/UuidValidatorTest.cpp) diff --git a/test/coverage/utils/CyclicQueueTest.cpp b/test/coverage/utils/CyclicQueueTest.cpp new file mode 100644 index 000000000..8160e8b33 --- /dev/null +++ b/test/coverage/utils/CyclicQueueTest.cpp @@ -0,0 +1,38 @@ +// SPDX-FileCopyrightText: 2024 Contributors to the Eclipse Foundation +// +// See the NOTICE file(s) distributed with this work for additional +// information regarding copyright ownership. +// +// This program and the accompanying materials are made available under the +// terms of the Apache License Version 2.0 which is available at +// https://www.apache.org/licenses/LICENSE-2.0 +// +// SPDX-License-Identifier: Apache-2.0 + +#include +#include + +namespace { + +class TestFixture : public testing::Test { +protected: + // Run once per TEST_F. + // Used to set up clean environments per test. + void SetUp() override {} + void TearDown() override {} + + // Run once per execution of the test application. + // Used for setup of all tests. Has access to this instance. + TestFixture() = default; + ~TestFixture() = default; + + // Run once per execution of the test application. + // Used only for global setup outside of tests. + static void SetUpTestSuite() {} + static void TearDownTestSuite() {} +}; + +// TODO replace +TEST_F(TestFixture, SomeTestName) {} + +} // namespace diff --git a/test/coverage/utils/IpAddressTest.cpp b/test/coverage/utils/IpAddressTest.cpp new file mode 100644 index 000000000..b243bd071 --- /dev/null +++ b/test/coverage/utils/IpAddressTest.cpp @@ -0,0 +1,38 @@ +// SPDX-FileCopyrightText: 2024 Contributors to the Eclipse Foundation +// +// See the NOTICE file(s) distributed with this work for additional +// information regarding copyright ownership. +// +// This program and the accompanying materials are made available under the +// terms of the Apache License Version 2.0 which is available at +// https://www.apache.org/licenses/LICENSE-2.0 +// +// SPDX-License-Identifier: Apache-2.0 + +#include +#include + +namespace { + +class TestFixture : public testing::Test { +protected: + // Run once per TEST_F. + // Used to set up clean environments per test. + void SetUp() override {} + void TearDown() override {} + + // Run once per execution of the test application. + // Used for setup of all tests. Has access to this instance. + TestFixture() = default; + ~TestFixture() = default; + + // Run once per execution of the test application. + // Used only for global setup outside of tests. + static void SetUpTestSuite() {} + static void TearDownTestSuite() {} +}; + +// TODO replace +TEST_F(TestFixture, SomeTestName) {} + +} // namespace diff --git a/test/coverage/utils/ThreadPoolTest.cpp b/test/coverage/utils/ThreadPoolTest.cpp new file mode 100644 index 000000000..22b1dfd6f --- /dev/null +++ b/test/coverage/utils/ThreadPoolTest.cpp @@ -0,0 +1,38 @@ +// SPDX-FileCopyrightText: 2024 Contributors to the Eclipse Foundation +// +// See the NOTICE file(s) distributed with this work for additional +// information regarding copyright ownership. +// +// This program and the accompanying materials are made available under the +// terms of the Apache License Version 2.0 which is available at +// https://www.apache.org/licenses/LICENSE-2.0 +// +// SPDX-License-Identifier: Apache-2.0 + +#include +#include + +namespace { + +class TestFixture : public testing::Test { +protected: + // Run once per TEST_F. + // Used to set up clean environments per test. + void SetUp() override {} + void TearDown() override {} + + // Run once per execution of the test application. + // Used for setup of all tests. Has access to this instance. + TestFixture() = default; + ~TestFixture() = default; + + // Run once per execution of the test application. + // Used only for global setup outside of tests. + static void SetUpTestSuite() {} + static void TearDownTestSuite() {} +}; + +// TODO replace +TEST_F(TestFixture, SomeTestName) {} + +} // namespace diff --git a/test/coverage/utils/base64Test.cpp b/test/coverage/utils/base64Test.cpp new file mode 100644 index 000000000..34fec4db0 --- /dev/null +++ b/test/coverage/utils/base64Test.cpp @@ -0,0 +1,38 @@ +// SPDX-FileCopyrightText: 2024 Contributors to the Eclipse Foundation +// +// See the NOTICE file(s) distributed with this work for additional +// information regarding copyright ownership. +// +// This program and the accompanying materials are made available under the +// terms of the Apache License Version 2.0 which is available at +// https://www.apache.org/licenses/LICENSE-2.0 +// +// SPDX-License-Identifier: Apache-2.0 + +#include +#include + +namespace { + +class TestFixture : public testing::Test { +protected: + // Run once per TEST_F. + // Used to set up clean environments per test. + void SetUp() override {} + void TearDown() override {} + + // Run once per execution of the test application. + // Used for setup of all tests. Has access to this instance. + TestFixture() = default; + ~TestFixture() = default; + + // Run once per execution of the test application. + // Used only for global setup outside of tests. + static void SetUpTestSuite() {} + static void TearDownTestSuite() {} +}; + +// TODO replace +TEST_F(TestFixture, SomeTestName) {} + +} // namespace