From a9f663b6c60dd39321b771d8b63cd79f88f896ec Mon Sep 17 00:00:00 2001 From: Dominik Meyer Date: Sat, 21 Aug 2021 23:48:06 +0200 Subject: [PATCH] ADD: will not use --- CMakeLists.txt | 17 +- include/Whisper/Message.hpp | 88 ++++++ include/Whisper/NIS.hpp | 26 ++ include/Whisper/Network/Encoding.hpp | 88 ++++++ include/Whisper/Network/Protocol.hpp | 34 ++ include/Whisper/Payload.hpp | 64 ++++ include/Whisper/Transmittable.hpp | 40 +++ include/Whisper/types.hpp | 7 + libs/libtree/CMakeLists.txt | 22 ++ libs/libtree/LICENSE | 373 ++++++++++++++++++++++ libs/libtree/include/Tree/tree.hpp | 453 +++++++++++++++++++++++++++ libs/libtree/src/Tree/tree.cpp | 119 +++++++ libs/libtree/src/config.hpp | 10 + libs/libtree/src/config.hpp.in | 10 + src/Whisper/Message.cpp | 69 ++++ src/Whisper/Payload.cpp | 20 ++ tests/test_message.cpp | 58 ++++ 17 files changed, 1497 insertions(+), 1 deletion(-) create mode 100644 include/Whisper/Message.hpp create mode 100644 include/Whisper/NIS.hpp create mode 100644 include/Whisper/Network/Encoding.hpp create mode 100644 include/Whisper/Network/Protocol.hpp create mode 100644 include/Whisper/Payload.hpp create mode 100644 include/Whisper/Transmittable.hpp create mode 100644 include/Whisper/types.hpp create mode 100644 libs/libtree/CMakeLists.txt create mode 100644 libs/libtree/LICENSE create mode 100644 libs/libtree/include/Tree/tree.hpp create mode 100644 libs/libtree/src/Tree/tree.cpp create mode 100644 libs/libtree/src/config.hpp create mode 100644 libs/libtree/src/config.hpp.in create mode 100644 src/Whisper/Message.cpp create mode 100644 src/Whisper/Payload.cpp create mode 100644 tests/test_message.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 69d925f..57b78f4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,13 +19,23 @@ IF(NOT TARGET loguru) set_property(TARGET loguru PROPERTY POSITION_INDEPENDENT_CODE 1) ENDIF() +IF(NOT TARGET tree) + add_subdirectory(libs/libtree EXCLUDE_FROM_ALL) +ENDIF() set(WHISPER_SOURCES include/Whisper/Data/Classification.hpp src/Whisper/Data/Classification.cpp + include/Whisper/Payload.hpp + src/Whisper/Payload.cpp + include/Whisper/Network/Encoding.hpp + include/Whisper/Transmittable.hpp + include/Whisper/Message.hpp + src/Whisper/Message.cpp + ) -set(WHISPER_REQUIRED_LIBRARIES loguru) +set(WHISPER_REQUIRED_LIBRARIES loguru tree) add_library(objlib OBJECT ${WHISPER_SOURCES}) set_property(TARGET objlib PROPERTY POSITION_INDEPENDENT_CODE 1) @@ -81,6 +91,11 @@ IF(${WHISPER_TESTS}) add_executable(test_classification tests/test_classification.cpp) target_link_libraries(test_classification PUBLIC Catch2::Catch2 whisper-static) catch_discover_tests(test_classification) + +add_executable(test_message tests/test_message.cpp) +target_link_libraries(test_message PUBLIC Catch2::Catch2 whisper-static) +catch_discover_tests(test_message) + # # add_executable(test_message tests/test_message.cpp) # target_link_libraries(test_message PUBLIC Catch2::Catch2 bc-ng loguru) diff --git a/include/Whisper/Message.hpp b/include/Whisper/Message.hpp new file mode 100644 index 0000000..8529351 --- /dev/null +++ b/include/Whisper/Message.hpp @@ -0,0 +1,88 @@ +#pragma once +/* This Source Code Form is subject to the terms of the Mozilla Public +* License, v. 2.0. If a copy of the MPL was not distributed with this +* file, You can obtain one at https://mozilla.org/MPL/2.0/. +*/ +/** @file */ +/** @copyright 2021 MPLv2 */ +#include +#include +/** +* @brief This is the main namespace for all whisper-com related datatypes and classed. +*/ +namespace Whisper +{ + + /** + * @brief All available Message types. + * + * Message Types identify the real type of the payload of the message + */ + enum class MessageType : std::uint8_t + { + /// no message type set or identified + NONE, + /// the message is a Announcement Message Version 1 + ANNOUNCEMENT_V1 + + }; // enum class MessageType + + class Message : public Whisper::Transmittable + { + private: + std::uint64_t dstID_; + std::uint64_t srcID_; + std::uint64_t messageID_; + std::uint64_t replyID_; + + Whisper::MessageType type_; + + std::shared_ptr payload_; + std::shared_ptr> rawPayload_; + + /** + * @brief Method generates a random uint64 value > 0 + * + * Please be aware that this method will never return zero, as + * zero indicates that the id has never been set. + * + * @return std::uint64_t > 0 + */ + std::uint64_t randomID_(); + + void decode(std::shared_ptr> data); + + public: + Message() : + dstID_(0), + srcID_(0), + messageID_(randomID_()), + replyID_(0), + type_(Whisper::MessageType::NONE), + payload_(nullptr), + rawPayload_(nullptr), + Transmittable() {} + + Message(std::shared_ptr> data) : Message() {decode(data);} + + std::uint64_t size(); + + Whisper::MessageType type() const { return type_;} + + void type(Whisper::MessageType type) {type_=type;} + + std::uint64_t id() const {return messageID_;} + std::uint64_t replyId() const { return replyID_;} + + std::uint64_t src() const {return srcID_;} + std::uint64_t dst() const {return dstID_;} + + std::shared_ptr> encode(); + + std::shared_ptr getPayload(); + + }; // Message + + + +}; // namespace Whisper diff --git a/include/Whisper/NIS.hpp b/include/Whisper/NIS.hpp new file mode 100644 index 0000000..849da51 --- /dev/null +++ b/include/Whisper/NIS.hpp @@ -0,0 +1,26 @@ +#pragma once +/* This Source Code Form is subject to the terms of the Mozilla Public +* License, v. 2.0. If a copy of the MPL was not distributed with this +* file, You can obtain one at https://mozilla.org/MPL/2.0/. +*/ +/** @file */ +/** @copyright 2021 MPLv2 */ + +/** +* @brief This is the main namespace for all whisper-com related datatypes and classed. +*/ +namespace Whisper +{ + + + class NIS + { + + private: + Whisper::Network::Protocols::Announcement announcementProtocol_; + + }; // NIS + + + +}; // namespace Whisper diff --git a/include/Whisper/Network/Encoding.hpp b/include/Whisper/Network/Encoding.hpp new file mode 100644 index 0000000..ac85999 --- /dev/null +++ b/include/Whisper/Network/Encoding.hpp @@ -0,0 +1,88 @@ +#pragma once +/* This Source Code Form is subject to the terms of the Mozilla Public +* License, v. 2.0. If a copy of the MPL was not distributed with this +* file, You can obtain one at https://mozilla.org/MPL/2.0/. +*/ +/** @file */ +/** @copyright 2021 MPLv2 */ +#include +#include +#include +#include +#include + +/** +* @brief This is the main namespace for all whisper-com related datatypes and classed. +*/ +namespace Whisper +{ + + + /** + * @brief This namespace is for all network related datatypes and classes. + */ + namespace Network + { + + class Encoding + { + + public: + /** + * \defgroup conversion_methods Conversion Functions + * Whisper-com requires conversion functions/ methods to convert from + * datatypes like std::uint8_t, std::int32_t, double, float, std::string, etc. + * to a byte vector, which can be transmitted as a payload through a network. + * This module holds are all of these conversion functions. + */ + + + /* + * @brief template function to byte swap int/uint datatypes - used for endianess conversion + * + * @param v - variable of some int/uint datatype + * @return the swapped bytes in the same variable type + */ + template + T swapBytes(T &v) + { + T help=v; + std::uint8_t *byte = (std::uint8_t *)&help; + + for (unsigned int i = 0; i < sizeof(T)/2; ++i) + { + std::swap(byte[i], byte[sizeof(T)-1-i]); + } + + return help; + } + + /** + * @brief template for converting of simple datatypes to a byte_array, including endiannes conversion to big endian + * @ingroup conversion_methods + * @param variable - the variable to convert to a byte vector, needs to be int/uint datatype + * + * @return the std::vector + */ + template + std::vector encodeSimple(T &variable) + { + } + + /** + * @brief function to convert a std::string to an std::vector + * @ingroup conversion_methods + * + * @param variable - the std::string to convert + * + * @return the std::vector + */ + std::vector fromStringToByteArray(const std::string &variable); + + + }; // class Encoding + + + }; // namespace Network + +}; // namespace Whisper diff --git a/include/Whisper/Network/Protocol.hpp b/include/Whisper/Network/Protocol.hpp new file mode 100644 index 0000000..50045fb --- /dev/null +++ b/include/Whisper/Network/Protocol.hpp @@ -0,0 +1,34 @@ +#pragma once +/* This Source Code Form is subject to the terms of the Mozilla Public +* License, v. 2.0. If a copy of the MPL was not distributed with this +* file, You can obtain one at https://mozilla.org/MPL/2.0/. +*/ +/** @file */ +/** @copyright 2021 MPLv2 */ + +/** +* @brief This is the main namespace for all whisper-com related datatypes and classed. +*/ +namespace Whisper +{ + + + /** + * @brief This namespace is for all network related datatypes and classes. + */ + namespace Network + { + + class Protocol + { + + protected: + + public: + + }; // class Protocol + + + }; // namespace Network + +}; // namespace Whisper diff --git a/include/Whisper/Payload.hpp b/include/Whisper/Payload.hpp new file mode 100644 index 0000000..f0e0374 --- /dev/null +++ b/include/Whisper/Payload.hpp @@ -0,0 +1,64 @@ +#pragma once +/* This Source Code Form is subject to the terms of the Mozilla Public +* License, v. 2.0. If a copy of the MPL was not distributed with this +* file, You can obtain one at https://mozilla.org/MPL/2.0/. +*/ +/** @file */ +/** @copyright 2021 MPLv2 */ +#include +#include +#include + +//#define DEFINE_TYPE_NAME(type, name) template<> Whisper::PayloadType GetType(){return name;} + + + +/** +* @brief This is the main namespace for all whisper-com related datatypes and classed. +*/ +namespace Whisper +{ + + enum class PayloadType : std::uint8_t + { + UINT8, + UINT16, + UINT32, + UINT64, + INT8, + INT16, + INT32, + INT64, + FLOAT, + DOUBLE, + STRING, + PAYLOAD + }; + + template Whisper::PayloadType GetType(); + + template + class Payload : public Tree::BaseNode + { + private: + + std::string fieldName_; + + T value_; + + public: + + Payload(const std::string field, T value) : + fieldName_(field), value_(value), Tree::BaseNode() {} + + Payload(const std::string field) : + fieldName_(field), Tree::BaseNode() {} + + Whisper::PayloadType getType() {return Whisper::GetType();} + T getValue() const { return value_;} + std::string getFieldName() const { return fieldName_;} + + }; // class Payload + + +}; // namespace Whisper diff --git a/include/Whisper/Transmittable.hpp b/include/Whisper/Transmittable.hpp new file mode 100644 index 0000000..418c505 --- /dev/null +++ b/include/Whisper/Transmittable.hpp @@ -0,0 +1,40 @@ +#pragma once +/* This Source Code Form is subject to the terms of the Mozilla Public +* License, v. 2.0. If a copy of the MPL was not distributed with this +* file, You can obtain one at https://mozilla.org/MPL/2.0/. +*/ +/** @file */ +/** @copyright 2021 MPLv2 */ +#include +#include +#include + +/** +* @brief This is the main namespace for all whisper-com related datatypes and classed. +*/ +namespace Whisper +{ + + + class Transmittable : public std::enable_shared_from_this + { + private: + std::shared_ptr encoding_; + + public: + Transmittable() : encoding_(nullptr) {} + ~Transmittable() {encoding_=nullptr;} + void setEncoding(std::shared_ptr encoding) {encoding_=encoding;} + std::shared_ptr encoding() { return encoding_;} + bool hasEnconding() { return encoding_ != nullptr;} + + virtual std::shared_ptr> encode() = 0; + virtual void decode(std::shared_ptr> data) = 0; + virtual std::shared_ptr getPayload() = 0; + + + }; // Transmittable + + + +}; // namespace Whisper diff --git a/include/Whisper/types.hpp b/include/Whisper/types.hpp new file mode 100644 index 0000000..bba1baa --- /dev/null +++ b/include/Whisper/types.hpp @@ -0,0 +1,7 @@ +#pragma once +#include + +template const std::string GetTypeName(); + +#define DEFINE_TYPE_NAME(type, name) \ + template<>const std::string GetTypeName(){return name;} diff --git a/libs/libtree/CMakeLists.txt b/libs/libtree/CMakeLists.txt new file mode 100644 index 0000000..a13c976 --- /dev/null +++ b/libs/libtree/CMakeLists.txt @@ -0,0 +1,22 @@ +cmake_minimum_required (VERSION 3.1 FATAL_ERROR) +project (libtree VERSION 0.1.0 LANGUAGES CXX) + +set(CMAKE_CXX_STANDARD 20) +set(CMAKE_CXX_STANDARD_REQUIRED ON) +set(CMAKE_EXPORT_COMPILE_COMMANDS 1) + + +configure_file(${PROJECT_SOURCE_DIR}/src/config.hpp.in ${PROJECT_SOURCE_DIR}/src/config.hpp @ONLY) + + +add_library(tree STATIC + src/config.hpp + include/Tree/tree.hpp + src/Tree/tree.cpp +) + +target_include_directories(tree PUBLIC + $ + $ + PRIVATE src +) diff --git a/libs/libtree/LICENSE b/libs/libtree/LICENSE new file mode 100644 index 0000000..14e2f77 --- /dev/null +++ b/libs/libtree/LICENSE @@ -0,0 +1,373 @@ +Mozilla Public License Version 2.0 +================================== + +1. Definitions +-------------- + +1.1. "Contributor" + means each individual or legal entity that creates, contributes to + the creation of, or owns Covered Software. + +1.2. "Contributor Version" + means the combination of the Contributions of others (if any) used + by a Contributor and that particular Contributor's Contribution. + +1.3. "Contribution" + means Covered Software of a particular Contributor. + +1.4. "Covered Software" + means Source Code Form to which the initial Contributor has attached + the notice in Exhibit A, the Executable Form of such Source Code + Form, and Modifications of such Source Code Form, in each case + including portions thereof. + +1.5. "Incompatible With Secondary Licenses" + means + + (a) that the initial Contributor has attached the notice described + in Exhibit B to the Covered Software; or + + (b) that the Covered Software was made available under the terms of + version 1.1 or earlier of the License, but not also under the + terms of a Secondary License. + +1.6. "Executable Form" + means any form of the work other than Source Code Form. + +1.7. "Larger Work" + means a work that combines Covered Software with other material, in + a separate file or files, that is not Covered Software. + +1.8. "License" + means this document. + +1.9. "Licensable" + means having the right to grant, to the maximum extent possible, + whether at the time of the initial grant or subsequently, any and + all of the rights conveyed by this License. + +1.10. "Modifications" + means any of the following: + + (a) any file in Source Code Form that results from an addition to, + deletion from, or modification of the contents of Covered + Software; or + + (b) any new file in Source Code Form that contains any Covered + Software. + +1.11. "Patent Claims" of a Contributor + means any patent claim(s), including without limitation, method, + process, and apparatus claims, in any patent Licensable by such + Contributor that would be infringed, but for the grant of the + License, by the making, using, selling, offering for sale, having + made, import, or transfer of either its Contributions or its + Contributor Version. + +1.12. "Secondary License" + means either the GNU General Public License, Version 2.0, the GNU + Lesser General Public License, Version 2.1, the GNU Affero General + Public License, Version 3.0, or any later versions of those + licenses. + +1.13. "Source Code Form" + means the form of the work preferred for making modifications. + +1.14. "You" (or "Your") + means an individual or a legal entity exercising rights under this + License. For legal entities, "You" includes any entity that + controls, is controlled by, or is under common control with You. For + purposes of this definition, "control" means (a) the power, direct + or indirect, to cause the direction or management of such entity, + whether by contract or otherwise, or (b) ownership of more than + fifty percent (50%) of the outstanding shares or beneficial + ownership of such entity. + +2. License Grants and Conditions +-------------------------------- + +2.1. Grants + +Each Contributor hereby grants You a world-wide, royalty-free, +non-exclusive license: + +(a) under intellectual property rights (other than patent or trademark) + Licensable by such Contributor to use, reproduce, make available, + modify, display, perform, distribute, and otherwise exploit its + Contributions, either on an unmodified basis, with Modifications, or + as part of a Larger Work; and + +(b) under Patent Claims of such Contributor to make, use, sell, offer + for sale, have made, import, and otherwise transfer either its + Contributions or its Contributor Version. + +2.2. Effective Date + +The licenses granted in Section 2.1 with respect to any Contribution +become effective for each Contribution on the date the Contributor first +distributes such Contribution. + +2.3. Limitations on Grant Scope + +The licenses granted in this Section 2 are the only rights granted under +this License. No additional rights or licenses will be implied from the +distribution or licensing of Covered Software under this License. +Notwithstanding Section 2.1(b) above, no patent license is granted by a +Contributor: + +(a) for any code that a Contributor has removed from Covered Software; + or + +(b) for infringements caused by: (i) Your and any other third party's + modifications of Covered Software, or (ii) the combination of its + Contributions with other software (except as part of its Contributor + Version); or + +(c) under Patent Claims infringed by Covered Software in the absence of + its Contributions. + +This License does not grant any rights in the trademarks, service marks, +or logos of any Contributor (except as may be necessary to comply with +the notice requirements in Section 3.4). + +2.4. Subsequent Licenses + +No Contributor makes additional grants as a result of Your choice to +distribute the Covered Software under a subsequent version of this +License (see Section 10.2) or under the terms of a Secondary License (if +permitted under the terms of Section 3.3). + +2.5. Representation + +Each Contributor represents that the Contributor believes its +Contributions are its original creation(s) or it has sufficient rights +to grant the rights to its Contributions conveyed by this License. + +2.6. Fair Use + +This License is not intended to limit any rights You have under +applicable copyright doctrines of fair use, fair dealing, or other +equivalents. + +2.7. Conditions + +Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted +in Section 2.1. + +3. Responsibilities +------------------- + +3.1. Distribution of Source Form + +All distribution of Covered Software in Source Code Form, including any +Modifications that You create or to which You contribute, must be under +the terms of this License. You must inform recipients that the Source +Code Form of the Covered Software is governed by the terms of this +License, and how they can obtain a copy of this License. You may not +attempt to alter or restrict the recipients' rights in the Source Code +Form. + +3.2. Distribution of Executable Form + +If You distribute Covered Software in Executable Form then: + +(a) such Covered Software must also be made available in Source Code + Form, as described in Section 3.1, and You must inform recipients of + the Executable Form how they can obtain a copy of such Source Code + Form by reasonable means in a timely manner, at a charge no more + than the cost of distribution to the recipient; and + +(b) You may distribute such Executable Form under the terms of this + License, or sublicense it under different terms, provided that the + license for the Executable Form does not attempt to limit or alter + the recipients' rights in the Source Code Form under this License. + +3.3. Distribution of a Larger Work + +You may create and distribute a Larger Work under terms of Your choice, +provided that You also comply with the requirements of this License for +the Covered Software. If the Larger Work is a combination of Covered +Software with a work governed by one or more Secondary Licenses, and the +Covered Software is not Incompatible With Secondary Licenses, this +License permits You to additionally distribute such Covered Software +under the terms of such Secondary License(s), so that the recipient of +the Larger Work may, at their option, further distribute the Covered +Software under the terms of either this License or such Secondary +License(s). + +3.4. Notices + +You may not remove or alter the substance of any license notices +(including copyright notices, patent notices, disclaimers of warranty, +or limitations of liability) contained within the Source Code Form of +the Covered Software, except that You may alter any license notices to +the extent required to remedy known factual inaccuracies. + +3.5. Application of Additional Terms + +You may choose to offer, and to charge a fee for, warranty, support, +indemnity or liability obligations to one or more recipients of Covered +Software. However, You may do so only on Your own behalf, and not on +behalf of any Contributor. You must make it absolutely clear that any +such warranty, support, indemnity, or liability obligation is offered by +You alone, and You hereby agree to indemnify every Contributor for any +liability incurred by such Contributor as a result of warranty, support, +indemnity or liability terms You offer. You may include additional +disclaimers of warranty and limitations of liability specific to any +jurisdiction. + +4. Inability to Comply Due to Statute or Regulation +--------------------------------------------------- + +If it is impossible for You to comply with any of the terms of this +License with respect to some or all of the Covered Software due to +statute, judicial order, or regulation then You must: (a) comply with +the terms of this License to the maximum extent possible; and (b) +describe the limitations and the code they affect. Such description must +be placed in a text file included with all distributions of the Covered +Software under this License. Except to the extent prohibited by statute +or regulation, such description must be sufficiently detailed for a +recipient of ordinary skill to be able to understand it. + +5. Termination +-------------- + +5.1. The rights granted under this License will terminate automatically +if You fail to comply with any of its terms. However, if You become +compliant, then the rights granted under this License from a particular +Contributor are reinstated (a) provisionally, unless and until such +Contributor explicitly and finally terminates Your grants, and (b) on an +ongoing basis, if such Contributor fails to notify You of the +non-compliance by some reasonable means prior to 60 days after You have +come back into compliance. Moreover, Your grants from a particular +Contributor are reinstated on an ongoing basis if such Contributor +notifies You of the non-compliance by some reasonable means, this is the +first time You have received notice of non-compliance with this License +from such Contributor, and You become compliant prior to 30 days after +Your receipt of the notice. + +5.2. If You initiate litigation against any entity by asserting a patent +infringement claim (excluding declaratory judgment actions, +counter-claims, and cross-claims) alleging that a Contributor Version +directly or indirectly infringes any patent, then the rights granted to +You by any and all Contributors for the Covered Software under Section +2.1 of this License shall terminate. + +5.3. In the event of termination under Sections 5.1 or 5.2 above, all +end user license agreements (excluding distributors and resellers) which +have been validly granted by You or Your distributors under this License +prior to termination shall survive termination. + +************************************************************************ +* * +* 6. Disclaimer of Warranty * +* ------------------------- * +* * +* Covered Software is provided under this License on an "as is" * +* basis, without warranty of any kind, either expressed, implied, or * +* statutory, including, without limitation, warranties that the * +* Covered Software is free of defects, merchantable, fit for a * +* particular purpose or non-infringing. The entire risk as to the * +* quality and performance of the Covered Software is with You. * +* Should any Covered Software prove defective in any respect, You * +* (not any Contributor) assume the cost of any necessary servicing, * +* repair, or correction. This disclaimer of warranty constitutes an * +* essential part of this License. No use of any Covered Software is * +* authorized under this License except under this disclaimer. * +* * +************************************************************************ + +************************************************************************ +* * +* 7. Limitation of Liability * +* -------------------------- * +* * +* Under no circumstances and under no legal theory, whether tort * +* (including negligence), contract, or otherwise, shall any * +* Contributor, or anyone who distributes Covered Software as * +* permitted above, be liable to You for any direct, indirect, * +* special, incidental, or consequential damages of any character * +* including, without limitation, damages for lost profits, loss of * +* goodwill, work stoppage, computer failure or malfunction, or any * +* and all other commercial damages or losses, even if such party * +* shall have been informed of the possibility of such damages. This * +* limitation of liability shall not apply to liability for death or * +* personal injury resulting from such party's negligence to the * +* extent applicable law prohibits such limitation. Some * +* jurisdictions do not allow the exclusion or limitation of * +* incidental or consequential damages, so this exclusion and * +* limitation may not apply to You. * +* * +************************************************************************ + +8. Litigation +------------- + +Any litigation relating to this License may be brought only in the +courts of a jurisdiction where the defendant maintains its principal +place of business and such litigation shall be governed by laws of that +jurisdiction, without reference to its conflict-of-law provisions. +Nothing in this Section shall prevent a party's ability to bring +cross-claims or counter-claims. + +9. Miscellaneous +---------------- + +This License represents the complete agreement concerning the subject +matter hereof. If any provision of this License is held to be +unenforceable, such provision shall be reformed only to the extent +necessary to make it enforceable. Any law or regulation which provides +that the language of a contract shall be construed against the drafter +shall not be used to construe this License against a Contributor. + +10. Versions of the License +--------------------------- + +10.1. New Versions + +Mozilla Foundation is the license steward. Except as provided in Section +10.3, no one other than the license steward has the right to modify or +publish new versions of this License. Each version will be given a +distinguishing version number. + +10.2. Effect of New Versions + +You may distribute the Covered Software under the terms of the version +of the License under which You originally received the Covered Software, +or under the terms of any subsequent version published by the license +steward. + +10.3. Modified Versions + +If you create software not governed by this License, and you want to +create a new license for such software, you may create and use a +modified version of this License if you rename the license and remove +any references to the name of the license steward (except to note that +such modified license differs from this License). + +10.4. Distributing Source Code Form that is Incompatible With Secondary +Licenses + +If You choose to distribute Source Code Form that is Incompatible With +Secondary Licenses under the terms of this version of the License, the +notice described in Exhibit B of this License must be attached. + +Exhibit A - Source Code Form License Notice +------------------------------------------- + + This Source Code Form is subject to the terms of the Mozilla Public + License, v. 2.0. If a copy of the MPL was not distributed with this + file, You can obtain one at http://mozilla.org/MPL/2.0/. + +If it is not possible or desirable to put the notice in a particular +file, then You may include the notice in a location (such as a LICENSE +file in a relevant directory) where a recipient would be likely to look +for such a notice. + +You may add additional accurate notices of copyright ownership. + +Exhibit B - "Incompatible With Secondary Licenses" Notice +--------------------------------------------------------- + + This Source Code Form is "Incompatible With Secondary Licenses", as + defined by the Mozilla Public License, v. 2.0. diff --git a/libs/libtree/include/Tree/tree.hpp b/libs/libtree/include/Tree/tree.hpp new file mode 100644 index 0000000..37caf40 --- /dev/null +++ b/libs/libtree/include/Tree/tree.hpp @@ -0,0 +1,453 @@ +#ifndef __TREE_BASENODE_HPP__ +#define __TREE_BASENODE_HPP__ +/* This Source Code Form is subject to the terms of the Mozilla Public +* License, v. 2.0. If a copy of the MPL was not distributed with this +* file, You can obtain one at https://mozilla.org/MPL/2.0/. +*/ +/** +* @file +* @brief header file for the libtree++ library +* @author Dominik Meyer +* @date 2019-03-03 +* @copyright 2021 MPLv2 by Dominik Meyer +*/ +#include +#include +#include +#include +#include + +/** +* @brief main namespace for the libtree++ library +*/ +namespace Tree +{ + /** + * @brief base node for all nodes within the tree, implementing base tree methods + */ + class BaseNode : public std::enable_shared_from_this + { + private: + /// the parent node of a node ;) + std::shared_ptr parent; + + /// all child nodes of a node + std::list> children; + + + virtual std::shared_ptr clone_() const = 0; + + + public: + + /** + * @brief clear the node of parent and children, but do not delete the children, required for cloning + * + */ + void clear() { + children.clear(); + parent=nullptr; + }; + + /** + * @brief get the root of the tree + * + */ + std::shared_ptr getRoot() const + { + if (parent==nullptr) + { + return std::const_pointer_cast(shared_from_this()); + } + + return parent->getRoot(); + + } + + /** + * @brief method to deep clone this node + * + * @return pointer to the cloned object + */ + std::shared_ptr clone() const + { + std::shared_ptr node = clone_(); + node->clear(); + + std::list>::const_iterator it; + for(it = children.begin(); it != children.end(); ++it) + { + node->addChild((*it)->clone()); + } + + return node; + } + + /** + * @brief print the tree starting with the current node to stdout + * + * @param depth - used for indenting the tree + */ + void printTree(const uint32_t depth); + + /** + * @brief print the tree starting with the current node to stdout + * + * just calls printTree(0) + */ + void printTree() { printTree(0); } + + /** + * @brief returns a string representing this node + * + * this method has to be implemented by all nodes extending this class + * + * @return std::string representing this node + */ + virtual const std::string toString() const = 0; + + /** + * @brief returns the type of this node as a string + * + * this method has to be implemented by all nodes extending this class + * + * @return std::string representing the type of this node + */ + virtual const std::string type() const = 0; + + /** + * @brief returns the base of this node as a string + * + * this method has to be implemented by all nodes extending this class + * + * a base is some kind of way to group nodes belonging to a certain topic + * + * @return std::string representing the type of this node + */ + virtual const std::string base() const = 0; + + /** + * @brief return the parent node of the node + * + * @return nullptr - no parent is set, therefore, it should be a root node + * @return a std::shared_ptr to the the parent node + */ + const std::shared_ptr getParent() const { return parent;} + + /** + * @brief set the parent node of this node + * + * @param p - the parent node as a shared_ptr + */ + void setParent(const std::shared_ptr &p) {parent=p;} + + /** + * @brief returns an iterator to the beginning of the list of children + * + * @return iterator to the beginning of the list of children + */ + std::list>::const_iterator getChildrenBegin() const {return children.begin();} + + /** + * @brief returns an iterator to the end of the list of children + * + * @return iterator to the end of the list of children + */ + std::list>::const_iterator getChildrenEnd() const {return children.end();} + + /** + * @brief returns the nr of direct children + * + * @return the nr of direct children + */ + const unsigned int getNrDirectChildren() const { return children.size(); } + + /** + * @brief returns the nr of all children + * + * @return the nr of all children + */ + const unsigned int getNrChildren() const; + + /** + * @brief get the iterator of the given child + * + * @param child - the childnode to find the iterator for + * @return - the const iterator for the given child + */ + std::list>::const_iterator getChildIterator(const std::shared_ptr child) const; + + /** + * @brief deletes all the children and their children and ... from the tree + */ + void deleteChildren(); + + + /** + * @brief deletes the given node and all its children + * + * @param c - the node to delete + * + */ + void deleteChild(const std::shared_ptr &c); + + /** + * @brief remove the given node from the children of the node + * + * remove is not delete. The node gets just disconnected from the tree + * + * @param c - the node to remove from the nodes list of children + * + */ + void removeChild(const std::shared_ptr &c); + + /** + * @brief adds a child to the node which is already a shared_ptr + * + * @param c - a shared_ptr to a BaseNode + */ + void addChild(const std::shared_ptr &c) { + if (c!=nullptr) + { + c->setParent(shared_from_this()); + children.push_back(c); + } + } + + + /** + * @brief prepends a child to the node which is already a shared_ptr + * + * @param c - a shared_ptr to a BaseNode + */ + void prependChild(const std::shared_ptr &c) {c->setParent(shared_from_this()); children.push_front(c); } + + /** + * @brief adds a container of nodes to the list of children + * + * @param begin - iterator to the beginning of the container of objects of type std::shared_ptr + * @param end - iterator to the end of the container of objects of type std::shared_ptr + */ + template + void addChildren(Iter begin, Iter end) { std::copy( begin, end, std::back_inserter( children ) );} + + + /** + * @brief prepends a container of nodes to the list of children + * + * @param begin - iterator to the beginning of the container of objects of type std::shared_ptr + * @param end - iterator to the end of the container of objects of type std::shared_ptr + */ + template + void prependChildren(Iter begin, Iter end) { std::copy( begin, end, std::front_inserter( children ) );} + + /** + * @brief inserts a child before the given iterator which is already a shared_ptr + * + * @param c - a shared_ptr to a BaseNode + * @param it - the iterator before which to insert the node + */ + void insertChild(const std::shared_ptr &c, std::list>::const_iterator it) {c->setParent(shared_from_this());children.insert(it,c);} + + + /** + * @brief finds the next node in the tree of a given type + * + * @param t - the type of the node as a string + * + * @return nullptr - a node of that type could not be found + * @return the found node as a shared_ptr + */ + std::shared_ptr findNext(const std::string &t) const; + + /** + * @brief finds all nodes of a given type + * + * @param t - the type of the node as a string + * @param c - a container for holding all found nodes, has to be holding objects of type std::shared_ptr + */ + template + void findAll(const std::string &t, Container &c) const + { + std::list>::const_iterator it; + if (type() == t) + { + c.push_back(std::const_pointer_cast(shared_from_this())); + } + + for(it = children.begin(); it != children.end(); ++it) + { + (*it)->findAll(t,c); + } + + } + + /** + * @brief finds all nodes of a given base + * + * @param b - the base type of the node as a string + * @param c - a container for holding all found nodes, has to be holding objects of type std::shared_ptr + */ + template + void findAllBase(const std::string &b, Container &c) const + { + std::list>::const_iterator it; + + if (base() == b) + { + c.push_back(std::const_pointer_cast(shared_from_this())); + } + + for(it = children.begin(); it != children.end(); ++it) + { + (*it)->findAllBase(b,c); + } + + } + + /** + * @brief checks if the given node is a direct child of this node + * + * @param n - shared_ptr of the node to check + * + * @return true - the node is a direct child + * @return false - the node is not a direct child + */ + const bool isDirectChild(std::shared_ptr &n) const; + + + /** + * @brief checks if the given node is a child of this node + * + * @param n - shared_ptr of the node to check + * + * @return true - the node is a child + * @return false - the node is not a child + */ + const bool isChild(std::shared_ptr &n) const; + + + /** + * @brief replaces this node within the tree with the given one + * + * this means moving all children to the given node and setting all of their parents + * to the new node + * + * @param n - the new node to replace the current one with + */ + void replace(std::shared_ptr n); + + /** + * @brief overriding the to string operator + * + * used to convert the object on the fly to a string + * + * @return a string representing this object + */ + operator std::string() const { return toString(); } + + }; + + /** + * @brief stream operator override + * + * make it possible to use nodes within output streams + * + * @param Str - the output stream to stream to ;) + * @param v - the node which should be streamed to Str + * + * @return the outstream returned + */ + + inline std::ostream & operator<<(std::ostream & Str, const Tree::BaseNode &v) { + Str << v.toString(); + return Str; + } + + /* + * Some basic Node Types + */ + + /** + * @brief class representing the Root of a Tree + */ + class TreeRoot : public Tree::BaseNode + { + private: + virtual std::shared_ptr clone_() const {return std::make_shared();}; + + public: + /** + * @brief returns the type of this node as a string + * + * this method has to be implemented by all nodes extending this class + * + * @return std::string representing the type of this node + */ + const std::string type() const { return "Tree::TreeRoot";} + + /** + * @brief returns the base of this node as a string + * + * this method has to be implemented by all nodes extending this class + * + * a base is some kind of way to group nodes belonging to a certain topic + * + * @return std::string representing the type of this node + */ + const std::string base() const { return "Tree::Base";} + + + /** + * @brief returns a string representing this node + * + * this method has to be implemented by all nodes extending this class + * + * @return std::string representing this node + */ + virtual const std::string toString() const { return type();}; + }; + + /** + * @brief class representing a temporary node within the tree + */ + class TempNode : public Tree::BaseNode + { + private: + virtual std::shared_ptr clone_() const {return std::make_shared();}; + public: + /** + * @brief returns the type of this node as a string + * + * this method has to be implemented by all nodes extending this class + * + * @return std::string representing the type of this node + */ + const std::string type() const { return "Tree::TempNode";} + + /** + * @brief returns the base of this node as a string + * + * this method has to be implemented by all nodes extending this class + * + * a base is some kind of way to group nodes belonging to a certain topic + * + * @return std::string representing the type of this node + */ + const std::string base() const { return "Tree::Base";} + + + /** + * @brief returns a string representing this node + * + * this method has to be implemented by all nodes extending this class + * + * @return std::string representing this node + */ + virtual const std::string toString() const { return type();}; + }; + +}; // namespace Tree + + +#endif diff --git a/libs/libtree/src/Tree/tree.cpp b/libs/libtree/src/Tree/tree.cpp new file mode 100644 index 0000000..401c5f4 --- /dev/null +++ b/libs/libtree/src/Tree/tree.cpp @@ -0,0 +1,119 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public +* License, v. 2.0. If a copy of the MPL was not distributed with this +* file, You can obtain one at https://mozilla.org/MPL/2.0/. +*/ + +#include +#include +#include + +std::list>::const_iterator Tree::BaseNode::getChildIterator(const std::shared_ptr child) const +{ + std::list>::const_iterator childIT; + + for(childIT=children.begin(); childIT != children.end(); ++childIT) + { + if (*childIT == child) + { + return childIT; + } + } + + throw std::runtime_error("child does not exist"); +} + +std::shared_ptr Tree::BaseNode::findNext(const std::string &t) const +{ + std::list>::const_iterator childIT; + std::shared_ptr temp; + + if (type() == t ) + { + return shared_from_this(); + } + + for(childIT=children.begin(); childIT != children.end(); ++childIT) + { + temp=(*childIT)->findNext(t); + if (temp != nullptr) + { + return temp; + } + } + + return nullptr; +} + + + +/* +* return the number of children +*/ +const unsigned int Tree::BaseNode::getNrChildren() const +{ + std::list>::const_iterator childIT; + unsigned int nr=children.size(); + + for(childIT=children.begin(); childIT != children.end(); ++childIT) + { + nr += (*childIT)->getNrChildren(); + } + + return nr; +} + +/* +* print the tree and indent according to depth +*/ +void Tree::BaseNode::printTree(const uint32_t depth) +{ + for (uint32_t i=0; i< depth; i++) + { + std::cout << " "; + } + + std::cout << toString() << std::endl; + + std::list>::const_iterator childIT; + + for(childIT=getChildrenBegin(); childIT != getChildrenEnd(); ++childIT) + { + (*childIT)->printTree(depth+1); + } + +} + + +void Tree::BaseNode::removeChild(const std::shared_ptr &c) +{ + std::list>::iterator it; + it=std::find(children.begin(), children.end(),c); + children.erase(it); +} + +void Tree::BaseNode::deleteChild(const std::shared_ptr &c) +{ + std::list>::iterator it; + it=std::find(children.begin(), children.end(),c); + children.erase(it); + + c->setParent(nullptr); +} + + +/* +* delete all children +*/ +void Tree::BaseNode::deleteChildren() +{ + std::list>::iterator it; + + for(it=children.begin(); it!=children.end(); ++it) + { + if ((*it) != nullptr ) + { + deleteChild((*it)); + } + } + +} diff --git a/libs/libtree/src/config.hpp b/libs/libtree/src/config.hpp new file mode 100644 index 0000000..1e25295 --- /dev/null +++ b/libs/libtree/src/config.hpp @@ -0,0 +1,10 @@ +#ifndef __CONFIG_HPP__ +#define __CONFIG_HPP__ + +#define LIBTREE_PROJECT_NAME "libtree" +#define LIBTREE_VERSION_MAJOR 0 +#define LIBTREE_VERSION_MINOR 1 +#define LIBTREE_VERSION_PATCH 0 +#define LIBTREE_PROJECT_VERSION "0.1.0" + +#endif diff --git a/libs/libtree/src/config.hpp.in b/libs/libtree/src/config.hpp.in new file mode 100644 index 0000000..f0609d9 --- /dev/null +++ b/libs/libtree/src/config.hpp.in @@ -0,0 +1,10 @@ +#ifndef __CONFIG_HPP__ +#define __CONFIG_HPP__ + +#define LIBTREE_PROJECT_NAME "@PROJECT_NAME@" +#define LIBTREE_VERSION_MAJOR @PROJECT_VERSION_MAJOR@ +#define LIBTREE_VERSION_MINOR @PROJECT_VERSION_MINOR@ +#define LIBTREE_VERSION_PATCH @PROJECT_VERSION_PATCH@ +#define LIBTREE_PROJECT_VERSION "@PROJECT_VERSION_MAJOR@.@PROJECT_VERSION_MINOR@.@PROJECT_VERSION_PATCH@" + +#endif diff --git a/src/Whisper/Message.cpp b/src/Whisper/Message.cpp new file mode 100644 index 0000000..b044f76 --- /dev/null +++ b/src/Whisper/Message.cpp @@ -0,0 +1,69 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public +* License, v. 2.0. If a copy of the MPL was not distributed with this +* file, You can obtain one at https://mozilla.org/MPL/2.0/. +*/ +/** @file */ +/** @copyright 2021 MPLv2 */ +#include +#include +#include + +std::uint64_t Whisper::Message::randomID_() +{ + std::random_device rd; + std::mt19937 rng(rd()); + + std::uniform_int_distribution dist(1,std::numeric_limits::max()); + + return dist(rng); +} + + +std::shared_ptr Whisper::Message::getPayload() +{ + + if (payload_ == nullptr) + { + throw std::runtime_error("no payload set yet"); + } + + std::shared_ptr payload = std::make_shared(); + payload->addChild(std::make_shared>("type",(std::uint8_t)type_)); + payload->addChild(std::make_shared>("src",srcID_)); + payload->addChild(std::make_shared>("dst",dstID_)); + payload->addChild(std::make_shared>("messageID",messageID_)); + payload->addChild(std::make_shared>("replyID",replyID_)); + payload->addChild(payload_->getPayload()); + + + return payload; +} + + +std::shared_ptr> Whisper::Message::encode() +{ + if (!hasEnconding()) + { + throw std::runtime_error("no encoding set yet"); + } + + if (payload_ == nullptr) + { + throw std::runtime_error("no payload set yet"); + } + + // set the encoding for our payload + payload_->setEncoding(encoding()); + + +} + +void Whisper::Message::decode(std::shared_ptr> data) +{ + +} + +std::uint64_t Whisper::Message::size() +{ + +} diff --git a/src/Whisper/Payload.cpp b/src/Whisper/Payload.cpp new file mode 100644 index 0000000..9230fbd --- /dev/null +++ b/src/Whisper/Payload.cpp @@ -0,0 +1,20 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public +* License, v. 2.0. If a copy of the MPL was not distributed with this +* file, You can obtain one at https://mozilla.org/MPL/2.0/. +*/ +/** @file */ +/** @copyright 2021 MPLv2 */ +#include + +template<> Whisper::PayloadType Whisper::GetType(){return Whisper::PayloadType::UINT8;} +template<> Whisper::PayloadType Whisper::GetType(){return Whisper::PayloadType::UINT16;} +template<> Whisper::PayloadType Whisper::GetType(){return Whisper::PayloadType::UINT32;} +template<> Whisper::PayloadType Whisper::GetType(){return Whisper::PayloadType::UINT64;} +template<> Whisper::PayloadType Whisper::GetType(){return Whisper::PayloadType::INT8;} +template<> Whisper::PayloadType Whisper::GetType(){return Whisper::PayloadType::INT16;} +template<> Whisper::PayloadType Whisper::GetType(){return Whisper::PayloadType::INT32;} +template<> Whisper::PayloadType Whisper::GetType(){return Whisper::PayloadType::INT64;} +template<> Whisper::PayloadType Whisper::GetType(){return Whisper::PayloadType::FLOAT;} +template<> Whisper::PayloadType Whisper::GetType(){return Whisper::PayloadType::DOUBLE;} +template<> Whisper::PayloadType Whisper::GetType(){return Whisper::PayloadType::STRING;} +template<> Whisper::PayloadType Whisper::GetType(){return Whisper::PayloadType::PAYLOAD;} diff --git a/tests/test_message.cpp b/tests/test_message.cpp new file mode 100644 index 0000000..ab5b5ae --- /dev/null +++ b/tests/test_message.cpp @@ -0,0 +1,58 @@ +/* +* This Source Code Form is subject to the terms of the Mozilla Public +* License, v. 2.0. If a copy of the MPL was not distributed with this +* file, You can obtain one at https://mozilla.org/MPL/2.0/. +*/ + +/** @file */ + +/** +* \defgroup test_message_class Tests for Whisper::Message +* Test include functional correctness. +*/ + + +#define CATCH_CONFIG_MAIN +#include +#include +#include + +/** +* \brief Verify that data classification compile and can be used +* \ingroup compile_data_tests +*/ +SCENARIO("instantiating a Whisper::Message","[message]") +{ + GIVEN("nothing") + { + std::unique_ptr msg; + + + WHEN("instantiating some classifications") + { + REQUIRE_NOTHROW([&]() + { + msg = std::make_unique(); + }()); + + + THEN("default values are set") + { + REQUIRE(msg->type() == Whisper::MessageType::NONE); + REQUIRE(msg->hasEnconding() == false); + REQUIRE(msg->id() > 0); + REQUIRE(msg->replyId() == 0); + REQUIRE(msg->src() == 0); + REQUIRE(msg->dst() == 0); + + REQUIRE_THROWS([&]() + { + msg->encode(); + }()); + + } + } + + } + +}