BeeeOn Gateway  v2020.3.1-2-g6f737dc
Platform to interconnect the IoT world
GWSResender.h
1 #pragma once
2 
3 #include <map>
4 #include <set>
5 
6 #include <Poco/Clock.h>
7 #include <Poco/Event.h>
8 #include <Poco/Mutex.h>
9 #include <Poco/Timespan.h>
10 
11 #include "loop/StoppableRunnable.h"
12 #include "loop/StopControl.h"
13 #include "server/GWSConnector.h"
14 #include "server/GWSListener.h"
15 #include "util/Loggable.h"
16 
17 namespace BeeeOn {
18 
30 class GWSResender :
31  public StoppableRunnable,
32  public GWSListener,
33  Loggable {
34 public:
35  GWSResender();
36 
37  void setConnector(GWSConnector::Ptr connector);
38 
42  void setResendTimeout(const Poco::Timespan &timeout);
43 
47  void run() override;
48  void stop() override;
49 
50  void onTrySend(const GWMessage::Ptr message) override;
51 
60  void onSent(const GWMessage::Ptr message) override;
61 
62  void onResponse(const GWResponse::Ptr response) override;
63  void onAck(const GWAck::Ptr ack) override;
64 
68  void onOther(const GWMessage::Ptr message) override;
69 
70 protected:
71  typedef std::multimap<Poco::Clock, GWMessage::Ptr> WaitingList;
72 
78  WaitingList::const_iterator resendOrGet(const Poco::Clock &now);
79 
83  WaitingList &waiting();
84 
93  bool resendable(const GWMessage::Ptr message) const;
94 
100  void findAndDrop(const GWMessage::Ptr message);
101 
102 private:
103  GWSConnector::Ptr m_connector;
104  Poco::Timespan m_resendTimeout;
105  WaitingList m_waiting;
106  std::map<GlobalID, WaitingList::iterator> m_refs;
107  std::set<GlobalID> m_pending;
108  StopControl m_stopControl;
109  Poco::Event m_event;
110  Poco::FastMutex m_lock;
111 };
112 
113 }
void run() override
Implement scheduler of the waiting messages.
Definition: GWSResender.cpp:39
void onOther(const GWMessage::Ptr message) override
Process GWSensorDataConfirm messages.
Definition: GWSResender.cpp:244
void setResendTimeout(const Poco::Timespan &timeout)
Configure timeout used to delay each resend.
Definition: GWSResender.cpp:31
WaitingList::const_iterator resendOrGet(const Poco::Clock &now)
Resend the oldest message waiting for resend if its timeout has expired.
Definition: GWSResender.cpp:83
void onAck(const GWAck::Ptr ack) override
When an ack is received, this event is fired.
Definition: GWSResender.cpp:201
void onTrySend(const GWMessage::Ptr message) override
Fire when a message is about to be sent to the server. After the send is successful (no network failu...
Definition: GWSResender.cpp:114
void stop() override
Definition: GWSResender.cpp:77
StopControl implements the stop mechanism generically.
Definition: StopControl.h:12
bool resendable(const GWMessage::Ptr message) const
Certain messages should be resended when there is no response/ack during the resendTimeout period...
Definition: GWSResender.cpp:250
WaitingList & waiting()
Definition: GWSResender.cpp:109
Definition: Loggable.h:19
void onSent(const GWMessage::Ptr message) override
Put the given message into the waiting list if it is re-sendable. After resendTimeout, such message is send again unless an appropriate response/ack has been delivered.
Definition: GWSResender.cpp:120
Definition: StoppableRunnable.h:8
void findAndDrop(const GWMessage::Ptr message)
Find the given message in the waiting list and remove it. Such message is considered as delivered or ...
Definition: GWSResender.cpp:266
GWSListener provides an interface for delivering of events and messages related to communication with...
Definition: GWSListener.h:19
Sending of messages via a GWSConnector might not be reliable. Messages that have been sent can never ...
Definition: GWSResender.h:30
void onResponse(const GWResponse::Ptr response) override
When a response is received, this event is fired.
Definition: GWSResender.cpp:187