-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
LocalMsg: copy, rename from
LocalMessage
, remove templates
- Loading branch information
Showing
3 changed files
with
229 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/** | ||
* @file local_msg.hpp | ||
* @author Dávid Benko (davidbenko@davidbenko.dev) | ||
* @brief Local message classes | ||
* | ||
* @copyright Copyright (c) 2024 | ||
* | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <string> | ||
|
||
#include "kvik/local_addr.hpp" | ||
|
||
namespace kvik | ||
{ | ||
/** | ||
* @brief Local message types | ||
*/ | ||
enum class LocalMsgType : uint8_t | ||
{ | ||
NONE = 0x00, | ||
OK = 0x01, | ||
FAIL = 0x02, | ||
PROBE_REQ = 0x10, | ||
PROBE_RES = 0x11, | ||
PUB = 0x20, | ||
SUB_REQ = 0x30, | ||
SUB_DATA = 0x31, | ||
UNSUB = 0x32, | ||
TIME_REQ = 0x40, | ||
TIME_RES = 0x41, | ||
}; | ||
|
||
/** | ||
* @brief Local message FAIL reason | ||
*/ | ||
enum class LocalMsgFailReason : uint8_t | ||
{ | ||
NONE = 0x00, | ||
}; | ||
|
||
/** | ||
* @brief Helper to convert `LocalMsgType` to string representation. | ||
* | ||
* @param mt Message type | ||
* @return String representation | ||
*/ | ||
constexpr const char *localMsgTypeToStr(LocalMsgType mt) noexcept; | ||
|
||
/** | ||
* @brief Helper to convert `LocalMsgFailReason` to string representation. | ||
* | ||
* @param fr Fail reason | ||
* @return String representation | ||
*/ | ||
constexpr const char *localMsgFailReasonToStr(LocalMsgFailReason fr) noexcept; | ||
|
||
/** | ||
* @brief Local message representation | ||
* | ||
* Used primarily for communication between `LocalLayer` and `Node` classes. | ||
*/ | ||
struct LocalMsg | ||
{ | ||
LocalMsgType type = LocalMsgType::NONE; //!< Type of message | ||
LocalAddr addr = {}; //!< Source/destination address | ||
std::string topic = ""; //!< Topic of message | ||
std::string payload = ""; //!< Payload of message | ||
LocalMsgFailReason failReason = LocalMsgFailReason::NONE; //!< Fail reason | ||
|
||
bool operator==(const LocalMsg &other) const; | ||
bool operator!=(const LocalMsg &other) const; | ||
|
||
/** | ||
* @brief Converts `LocalMsg` to printable string | ||
* | ||
* Primarily for logging purposes | ||
* | ||
* @return String representation of contained data | ||
*/ | ||
std::string toString() const; | ||
}; | ||
} // namespace kvik | ||
|
||
// Define hasher function | ||
template <> | ||
struct std::hash<kvik::LocalMsg> | ||
{ | ||
std::size_t operator()(kvik::LocalMsg const &msg) const noexcept | ||
{ | ||
return std::hash<kvik::LocalMsgType>{}(msg.type) + | ||
std::hash<kvik::LocalAddr>{}(msg.addr) + | ||
std::hash<std::string>{}(msg.topic) + | ||
std::hash<std::string>{}(msg.payload) + | ||
std::hash<kvik::LocalMsgFailReason>{}(msg.failReason); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/** | ||
* @file local_msg.cpp | ||
* @author Dávid Benko (davidbenko@davidbenko.dev) | ||
* @brief Local message classes | ||
* | ||
* @copyright Copyright (c) 2024 | ||
* | ||
*/ | ||
|
||
#include <string> | ||
|
||
#include "kvik/local_msg.hpp" | ||
|
||
namespace kvik | ||
{ | ||
constexpr const char *localMsgTypeToStr(LocalMsgType mt) noexcept | ||
{ | ||
switch (mt) | ||
{ | ||
case LocalMsgType::NONE: | ||
return "NONE"; | ||
case LocalMsgType::OK: | ||
return "OK"; | ||
case LocalMsgType::FAIL: | ||
return "FAIL"; | ||
case LocalMsgType::PROBE_REQ: | ||
return "PROBE_REQ"; | ||
case LocalMsgType::PROBE_RES: | ||
return "PROBE_RES"; | ||
case LocalMsgType::PUB: | ||
return "PUB"; | ||
case LocalMsgType::SUB_REQ: | ||
return "SUB_REQ"; | ||
case LocalMsgType::SUB_DATA: | ||
return "SUB_DATA"; | ||
case LocalMsgType::UNSUB: | ||
return "UNSUB"; | ||
case LocalMsgType::TIME_REQ: | ||
return "TIME_REQ"; | ||
case LocalMsgType::TIME_RES: | ||
return "TIME_RES"; | ||
default: | ||
return "???"; | ||
} | ||
} | ||
|
||
constexpr const char *localMsgFailReasonToStr(LocalMsgFailReason fr) noexcept | ||
{ | ||
switch (fr) | ||
{ | ||
case LocalMsgFailReason::NONE: | ||
return "NONE"; | ||
default: | ||
return "???"; | ||
} | ||
} | ||
|
||
std::string LocalMsg::toString() const | ||
{ | ||
return std::string{localMsgTypeToStr(type)} + " " + | ||
(type == LocalMsgType::FAIL | ||
? ( | ||
std::string("(failed due to ") + | ||
localMsgFailReasonToStr(failReason) + | ||
") ") | ||
: "") + | ||
(!addr.empty() ? addr.toString() : "(no addr)") + " " + | ||
(!topic.empty() ? topic : "(no topic)") + " " + | ||
"(" + std::to_string(payload.length()) + " B payload)"; | ||
} | ||
|
||
bool LocalMsg::operator==(const LocalMsg &other) const | ||
{ | ||
return type == other.type && | ||
addr == other.addr && | ||
topic == other.topic && | ||
payload == other.payload && | ||
failReason == other.failReason; | ||
} | ||
|
||
bool LocalMsg::operator!=(const LocalMsg &other) const | ||
{ | ||
return !this->operator==(other); | ||
} | ||
} // namespace kvik |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#include <catch2/catch_test_macros.hpp> | ||
|
||
#include "kvik/local_msg.hpp" | ||
|
||
using namespace kvik; | ||
|
||
TEST_CASE("Comparison", "[LocalMsg]") | ||
{ | ||
LocalMsg msg1; | ||
LocalMsg msg2; | ||
|
||
SECTION("Equality") | ||
{ | ||
REQUIRE(msg1 == msg2); | ||
} | ||
|
||
SECTION("Different types") | ||
{ | ||
msg2.type = LocalMsgType::FAIL; | ||
REQUIRE(msg1 != msg2); | ||
} | ||
|
||
SECTION("Different addresses") | ||
{ | ||
msg2.addr.addr.push_back(0x01); | ||
REQUIRE(msg1 != msg2); | ||
} | ||
|
||
SECTION("Different topics") | ||
{ | ||
msg2.topic = "1"; | ||
REQUIRE(msg1 != msg2); | ||
} | ||
|
||
SECTION("Different payloads") | ||
{ | ||
msg2.payload = "1"; | ||
REQUIRE(msg1 != msg2); | ||
} | ||
|
||
// TODO: test this when another fail reason is implemented | ||
// SECTION("Different fail reasons") | ||
// { | ||
// } | ||
} |