SPSP
Simple publish-subscribe protocol. Connects low power IoT clients to MQTT.
All Classes Files Functions Variables Typedefs Enumerations
local_message.hpp
Go to the documentation of this file.
1 
10 #pragma once
11 
12 #include <string>
13 
14 #include "spsp/local_addr.hpp"
15 
16 namespace SPSP
17 {
22  enum class LocalMessageType : uint8_t
23  {
24  NONE = 0,
25  OK = 1, // currently unused
26  FAIL = 2, // currently unused
27  PROBE_REQ = 10,
28  PROBE_RES = 11,
29  PUB = 20,
30  SUB_REQ = 30,
31  SUB_DATA = 31,
32  UNSUB = 32,
33  TIME_REQ = 40,
34  TIME_RES = 41,
35  };
36 
43  constexpr const char* localMessageTypeToStr(LocalMessageType mt) noexcept
44  {
45  switch (mt)
46  {
47  case LocalMessageType::NONE: return "NONE";
48  case LocalMessageType::OK: return "OK";
49  case LocalMessageType::FAIL: return "FAIL";
50  case LocalMessageType::PROBE_REQ: return "PROBE_REQ";
51  case LocalMessageType::PROBE_RES: return "PROBE_RES";
52  case LocalMessageType::PUB: return "PUB";
53  case LocalMessageType::SUB_REQ: return "SUB_REQ";
54  case LocalMessageType::SUB_DATA: return "SUB_DATA";
55  case LocalMessageType::UNSUB: return "UNSUB";
56  case LocalMessageType::TIME_REQ: return "TIME_REQ";
57  case LocalMessageType::TIME_RES: return "TIME_RES";
58  default: return "???";
59  }
60  }
61 
69  template <typename TLocalAddr>
70  struct LocalMessage
71  {
72  using LocalAddrT = TLocalAddr;
73 
75  TLocalAddr addr = {};
76  std::string topic = "";
77  std::string payload = "";
78 
86  std::string toString() const
87  {
88  return std::string{localMessageTypeToStr(type)} + " " +
89  (addr.str.length() > 0 ? addr.str : "(no addr)") + " " +
90  (topic.length() > 0 ? topic : "(no topic)") + " " +
91  "(" + std::to_string(payload.length()) + " B payload)";
92  }
93 
94  bool operator==(const LocalMessage& other) const
95  {
96  return type == other.type
97  && addr == other.addr
98  && topic == other.topic
99  && payload == other.payload;
100  }
101  };
102 } // namespace SPSP
103 
104 // Define hasher function
105 template <typename TLocalAddr>
106 struct std::hash<SPSP::LocalMessage<TLocalAddr>>
107 {
108  std::size_t operator()(SPSP::LocalMessage<TLocalAddr> const& msg) const noexcept
109  {
110  return std::hash<SPSP::LocalMessageType>{}(msg.type)
111  + std::hash<TLocalAddr>{}(msg.addr)
112  + std::hash<std::string>{}(msg.topic)
113  + std::hash<std::string>{}(msg.payload);
114  }
115 };
SPSP::LocalMessage::topic
std::string topic
Topic of message.
Definition: local_message.hpp:76
SPSP::LocalMessage::addr
TLocalAddr addr
Source/destination address.
Definition: local_message.hpp:75
SPSP::LocalMessage::type
LocalMessageType type
Type of message.
Definition: local_message.hpp:74
SPSP::localMessageTypeToStr
constexpr const char * localMessageTypeToStr(LocalMessageType mt) noexcept
Helper to convert LocalMessageType to string representation.
Definition: local_message.hpp:43
SPSP::LocalMessage::payload
std::string payload
Payload of message.
Definition: local_message.hpp:77
local_addr.hpp
Local layer address container.
SPSP::LocalMessageType
LocalMessageType
Local message types.
Definition: local_message.hpp:22
SPSP::LocalMessage::toString
std::string toString() const
Converts LocalMessage to printable string.
Definition: local_message.hpp:86
SPSP::LocalMessage
Local message representation.
Definition: local_message.hpp:70