BeeeOn Gateway  v2020.3.1-2-g6f737dc
Platform to interconnect the IoT world
GWSConnectorImpl.h
1 #pragma once
2 
3 #include <string>
4 
5 #include <Poco/AutoPtr.h>
6 #include <Poco/Buffer.h>
7 #include <Poco/Clock.h>
8 #include <Poco/Net/SocketNotification.h>
9 #include <Poco/Net/WebSocket.h>
10 
11 #include "core/GatewayInfo.h"
12 #include "loop/StoppableRunnable.h"
13 #include "loop/StopControl.h"
14 #include "server/AbstractGWSConnector.h"
15 #include "ssl/SSLClient.h"
16 
17 namespace BeeeOn {
18 
32  public AbstractGWSConnector,
33  public StoppableRunnable {
34 public:
36 
37  void setHost(const std::string &host);
38  void setPort(int port);
39  void setMaxMessageSize(int size);
40  void setSSLConfig(SSLClient::Ptr config);
41  void setReceiveTimeout(const Poco::Timespan &timeout);
42  void setSendTimeout(const Poco::Timespan &timeout);
43  void setReconnectDelay(const Poco::Timespan &delay);
44  void setKeepAliveTimeout(const Poco::Timespan &timeout);
45  void setMaxFailedReceives(int count);
46  void setGatewayInfo(GatewayInfo::Ptr info);
47 
48  void run();
49  void stop();
50 
51 protected:
52  void waitBeforeReconnect();
53 
54  Poco::SharedPtr<Poco::Net::WebSocket> connect(
55  const std::string &host,
56  int port) const;
57  void performRegister(Poco::Net::WebSocket &socket) const;
58  bool performOutput(Poco::Net::WebSocket &socket);
59  void performPing(Poco::Net::WebSocket &socket);
60  void checkPingTimeout() const;
61  void outputLoop(
62  StopControl::Run &run,
63  Poco::Net::WebSocket &socket);
64 
71  bool waitOutputs();
72 
76  const Poco::Timespan keepAliveRemaining() const;
77 
78  void onReadable(const Poco::AutoPtr<Poco::Net::ReadableNotification> &n);
79 
80  void sendMessage(
81  Poco::Net::WebSocket &socket,
82  const GWMessage &message) const;
83  void sendFrame(
84  Poco::Net::WebSocket &socket,
85  const std::string &payload,
86  const int flags) const;
87 
88  GWMessage::Ptr receiveMessage(Poco::Net::WebSocket &socket) const;
89  int receiveFrame(
90  Poco::Net::WebSocket &socket,
91  Poco::Buffer<char> &buffer,
92  int &flags) const;
93 
94 private:
95  std::string m_host;
96  int m_port;
97  size_t m_maxMessageSize;
98  SSLClient::Ptr m_sslConfig;
99  Poco::Timespan m_receiveTimeout;
100  Poco::Timespan m_sendTimeout;
101  Poco::Timespan m_reconnectDelay;
102  Poco::Timespan m_keepAliveTimeout;
103  int m_maxFailedReceives;
104  GatewayInfo::Ptr m_gatewayInfo;
105 
106  mutable Poco::FastMutex m_sendLock;
107  mutable Poco::FastMutex m_receiveLock;
108 
109  StopControl m_stopControl;
110  mutable Poco::Clock m_lastActivity;
111  mutable Poco::FastMutex m_lock;
112 
113  Poco::Clock m_lastPing;
114  Poco::AtomicCounter m_receiveFailed;
115 };
116 
117 }
Helper class for managing a common stoppable loop situation:
Definition: StopControl.h:35
Implements the communication via WebSockets with the remote server. Outgoing messages are prioritized...
Definition: GWSConnectorImpl.h:31
The GWMessage is abstract class representing messages (including their contents), that are being sent...
Definition: GWMessage.h:21
Most GWSConnector implementations would solve the issue of sending prioritization and asynchronous qu...
Definition: AbstractGWSConnector.h:38
const Poco::Timespan keepAliveRemaining() const
Definition: GWSConnectorImpl.cpp:254
StopControl implements the stop mechanism generically.
Definition: StopControl.h:12
void stop()
Definition: GWSConnectorImpl.cpp:188
Definition: StoppableRunnable.h:8
bool waitOutputs()
Wait while the output queues are empty. The waiting delay is driver by the keepAliveTimeout.
Definition: GWSConnectorImpl.cpp:225