SPSP
Simple publish-subscribe protocol. Connects low power IoT clients to MQTT.
All Classes Files Functions Variables Typedefs Enumerations
layers_dummy.hpp
Go to the documentation of this file.
1 
10 #pragma once
11 
12 #include <string>
13 #include <tuple>
14 #include <unordered_set>
15 #include <vector>
16 
17 #include "spsp/layers.hpp"
18 #include "spsp/local_addr.hpp"
19 #include "spsp/local_message.hpp"
20 
21 namespace SPSP::LocalLayers
22 {
27  class DummyLocalLayer : public ILocalLayer<LocalMessage<LocalAddr>>
28  {
29  public:
32  using SentMsgsSetT = std::unordered_set<LocalMessageT>;
33 
34  protected:
35  SentMsgsSetT m_sentMsgs;
36  size_t m_sentMsgsCount = 0;
37 
38  public:
48  virtual bool send(const LocalMessageT& msg)
49  {
50  m_sentMsgs.insert(msg);
51  m_sentMsgsCount++;
52  return true;
53  }
54 
63  void receiveDirect(const LocalMessageT& msg, int rssi = 0)
64  {
65  if (this->nodeConnected()) {
66  this->getNode()->receiveLocal(msg, rssi);
67  }
68  }
69 
75  const SentMsgsSetT& getSentMsgs()
76  {
77  return m_sentMsgs;
78  }
79 
86  {
87  return m_sentMsgsCount;
88  }
89  };
90 } // namespace SPSP::LocalLayers
91 
92 namespace SPSP::FarLayers
93 {
98  class DummyFarLayer : public IFarLayer
99  {
100  public:
101  using PubsSetT = std::unordered_set<std::string>; // not using tuple, because plain string if simpler
102  using SubsSetT = std::unordered_set<std::string>;
103  using SubsLogT = std::vector<std::string>;
104 
105  protected:
106  PubsSetT m_pubs;
107  SubsSetT m_subs;
108  SubsLogT m_subsLog;
109  SubsLogT m_unsubsLog;
110 
111  public:
121  virtual bool publish(const std::string& src, const std::string& topic,
122  const std::string& payload)
123  {
124  // `LocalMessage.toString()`-compatible string
125  m_pubs.insert("PUB " + src + " " + topic + " " + payload);
126  return true;
127  }
128 
136  virtual bool subscribe(const std::string& topic)
137  {
138  m_subs.insert(topic);
139  m_subsLog.push_back(topic);
140  return true;
141  }
142 
150  virtual bool unsubscribe(const std::string& topic)
151  {
152  m_unsubsLog.push_back(topic);
153  return m_subs.erase(topic);
154  }
155 
162  virtual void receiveDirect(const std::string& topic,
163  const std::string& payload)
164  {
165  if (this->nodeConnected()) {
166  this->getNode()->receiveFar(topic, payload);
167  }
168  }
169 
175  const PubsSetT& getPubs()
176  {
177  return m_pubs;
178  }
179 
185  const SubsSetT& getSubs()
186  {
187  return m_subs;
188  }
189 
195  const SubsLogT& getSubsLog()
196  {
197  return m_subsLog;
198  }
199 
205  const SubsLogT& getUnsubsLog()
206  {
207  return m_unsubsLog;
208  }
209  };
210 } // namespace SPSP::FarLayers
SPSP::LocalAddr
Local layer address container.
Definition: local_addr.hpp:25
SPSP::FarLayers::DummyFarLayer::receiveDirect
virtual void receiveDirect(const std::string &topic, const std::string &payload)
Simulates reception of data from far layer.
Definition: layers_dummy.hpp:162
SPSP::IFarLayer
Interface for far layer.
Definition: layers.hpp:83
SPSP::LocalLayers::DummyLocalLayer::getSentMsgs
const SentMsgsSetT & getSentMsgs()
Returns set of sent messages.
Definition: layers_dummy.hpp:75
SPSP::FarLayers::DummyFarLayer::getSubsLog
const SubsLogT & getSubsLog()
Returns current subscriptions log.
Definition: layers_dummy.hpp:195
SPSP::FarLayers::DummyFarLayer::getUnsubsLog
const SubsLogT & getUnsubsLog()
Returns current unsubscriptions log.
Definition: layers_dummy.hpp:205
SPSP::FarLayers::DummyFarLayer::getPubs
const PubsSetT & getPubs()
Returns current publishes set.
Definition: layers_dummy.hpp:175
layers.hpp
Local and far layers for SPSP.
SPSP::FarLayers::DummyFarLayer
Dummy far layer for testing.
Definition: layers_dummy.hpp:98
SPSP::IFarLayer::getNode
IFarNode< IFarLayer > * getNode() const
Gets the node object.
Definition: layers.hpp:103
SPSP::LocalLayers::DummyLocalLayer
Dummy local layer for testing.
Definition: layers_dummy.hpp:27
local_message.hpp
Local message classes.
SPSP::LocalLayers::DummyLocalLayer::receiveDirect
void receiveDirect(const LocalMessageT &msg, int rssi=0)
Simulates reception of data from local layer.
Definition: layers_dummy.hpp:63
SPSP::ILocalLayer< LocalMessage< LocalAddr > >::getNode
ILocalNode< ILocalLayer > * getNode() const
Gets the node object.
Definition: layers.hpp:49
local_addr.hpp
Local layer address container.
SPSP::FarLayers::DummyFarLayer::publish
virtual bool publish(const std::string &src, const std::string &topic, const std::string &payload)
Publishes message coming from node.
Definition: layers_dummy.hpp:121
SPSP::IFarLayer::nodeConnected
bool nodeConnected() const
Checks whether the owner node is connected.
Definition: layers.hpp:114
SPSP::ILocalLayer< LocalMessage< LocalAddr > >::nodeConnected
bool nodeConnected() const
Checks whether the owner node is connected.
Definition: layers.hpp:60
SPSP::FarLayers::DummyFarLayer::unsubscribe
virtual bool unsubscribe(const std::string &topic)
Unsubscribes from given topic.
Definition: layers_dummy.hpp:150
SPSP::LocalMessage
Local message representation.
Definition: local_message.hpp:70
SPSP::LocalLayers::DummyLocalLayer::send
virtual bool send(const LocalMessageT &msg)
Sends the message to given node.
Definition: layers_dummy.hpp:48
SPSP::LocalLayers::DummyLocalLayer::getSentMsgsCount
size_t getSentMsgsCount()
Returns count of sent messages.
Definition: layers_dummy.hpp:85
SPSP::ILocalLayer
Interface for local layer.
Definition: layers.hpp:26
SPSP::FarLayers::DummyFarLayer::subscribe
virtual bool subscribe(const std::string &topic)
Subscribes to given topic.
Definition: layers_dummy.hpp:136
SPSP::FarLayers::DummyFarLayer::getSubs
const SubsSetT & getSubs()
Returns current subscriptions set.
Definition: layers_dummy.hpp:185