SPSP
Simple publish-subscribe protocol. Connects low power IoT clients to MQTT.
All Classes Files Functions Variables Typedefs Enumerations
layers.hpp
Go to the documentation of this file.
1 
10 #pragma once
11 
12 #include "spsp/local_message.hpp"
13 
14 namespace SPSP
15 {
16  // Forward declaration
17  template <typename TLocalLayer> class ILocalNode;
18  template <typename TFarLayer> class IFarNode;
19 
25  template <typename TLocalMessage>
27  {
28  void* m_node = nullptr;
29 
30  public:
31  using LocalAddrT = typename TLocalMessage::LocalAddrT;
32  using LocalMessageT = TLocalMessage;
33 
39  void setNode(void* n)
40  {
41  m_node = n;
42  }
43 
50  {
51  return static_cast<ILocalNode<ILocalLayer>*>(m_node);
52  }
53 
60  inline bool nodeConnected() const
61  {
62  return m_node != nullptr;
63  }
64 
76  virtual bool send(const TLocalMessage& msg) = 0;
77  };
78 
83  class IFarLayer
84  {
85  void* m_node = nullptr;
86 
87  public:
93  void setNode(void* n)
94  {
95  m_node = n;
96  }
97 
103  inline IFarNode<IFarLayer>* getNode() const
104  {
105  return static_cast<IFarNode<IFarLayer>*>(m_node);
106  }
107 
114  inline bool nodeConnected() const
115  {
116  return m_node != nullptr;
117  }
118 
130  virtual bool publish(const std::string& src, const std::string& topic,
131  const std::string& payload) = 0;
132 
142  virtual bool subscribe(const std::string& topic) = 0;
143 
153  virtual bool unsubscribe(const std::string& topic) = 0;
154  };
155 } // namespace SPSP
SPSP::ILocalNode
Generic local node of SPSP.
Definition: layers.hpp:17
SPSP::ILocalLayer::send
virtual bool send(const TLocalMessage &msg)=0
Sends the message to given node.
SPSP::IFarLayer::subscribe
virtual bool subscribe(const std::string &topic)=0
Subscribes to given topic.
SPSP::IFarLayer
Interface for far layer.
Definition: layers.hpp:83
SPSP::IFarLayer::getNode
IFarNode< IFarLayer > * getNode() const
Gets the node object.
Definition: layers.hpp:103
SPSP::IFarLayer::publish
virtual bool publish(const std::string &src, const std::string &topic, const std::string &payload)=0
Publishes message coming from node.
SPSP::IFarLayer::unsubscribe
virtual bool unsubscribe(const std::string &topic)=0
Unsubscribes from given topic.
local_message.hpp
Local message classes.
SPSP::IFarNode
Generic far layer node of SPSP.
Definition: layers.hpp:18
SPSP::ILocalLayer::getNode
ILocalNode< ILocalLayer > * getNode() const
Gets the node object.
Definition: layers.hpp:49
SPSP::IFarLayer::setNode
void setNode(void *n)
Sets pointer to the owner node.
Definition: layers.hpp:93
SPSP::IFarLayer::nodeConnected
bool nodeConnected() const
Checks whether the owner node is connected.
Definition: layers.hpp:114
SPSP::ILocalLayer::nodeConnected
bool nodeConnected() const
Checks whether the owner node is connected.
Definition: layers.hpp:60
SPSP::ILocalLayer::setNode
void setNode(void *n)
Sets pointer to the owner node.
Definition: layers.hpp:39
SPSP::ILocalLayer
Interface for local layer.
Definition: layers.hpp:26