Skip to content

Commit

Permalink
LocalPeer: implement
Browse files Browse the repository at this point in the history
Creates generic data structure for peer information to be used by nodes and
local layers.
  • Loading branch information
DavidB137 committed Sep 8, 2024
1 parent 67c3e4c commit 5adcd9c
Showing 1 changed file with 92 additions and 0 deletions.
92 changes: 92 additions & 0 deletions include/kvik/local_peer.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/**
* @file local_peer.hpp
* @author Dávid Benko (davidbenko@davidbenko.dev)
* @brief Local layer peer info
*
* @copyright Copyright (c) 2024
*
*/

#pragma once

#include <algorithm>
#include <array>
#include <cstdint>

#include "kvik/local_addr.hpp"

namespace kvik
{
/**
* @brief Retained local layer peer info
*
* Special form of `LocalPeer` without any dynamic memory allocation.
* Intended for storage in RTC memory of IoT microcontrollers (during deep
* sleep). Not used for anything else.
*/
struct RetainedLocalPeer
{
std::array<uint8_t, 32> addr = {};
uint8_t addrLen = 0;
uint16_t channel = 0;
};

/**
* @brief Local layer peer info
*/
struct LocalPeer
{
LocalAddr addr = {}; //!< Peer address
uint16_t channel = 0; //!< Wireless channel (only for wireless local layers)

bool operator==(const LocalPeer &other) const
{
return addr == other.addr;
}

bool operator!=(const LocalPeer &other) const
{
return !this->operator==(other);
}

inline bool empty() const
{
return addr.empty();
}

/**
* @brief Converts internal representation into printable string
* @return Printable string representation
*/
inline std::string toString() const
{
return addr.toString();
}

/**
* @brief Converts `LocalPeer` to `RetainedLocalPeer`
*
* See `RetainedLocalPeer`'s docstring for usage details.
*
* @return Retained local peer info
*/
inline const RetainedLocalPeer retain() const
{
RetainedLocalPeer rlp;
std::copy_n(addr.addr.begin(), rlp.addr.max_size(), rlp.addr.begin());
rlp.addrLen = addr.addr.size();
rlp.channel = channel;
return rlp;
}
};
}

// Define hasher function
template <>
struct std::hash<kvik::LocalPeer>
{
std::size_t operator()(kvik::LocalPeer const &peer) const noexcept
{
return std::hash<kvik::LocalAddr>{}(peer.addr);
}
};

0 comments on commit 5adcd9c

Please sign in to comment.