BeeeOn Gateway  v2020.3.1-2-g6f737dc
Platform to interconnect the IoT world
GWServerConnector.h
1 #pragma once
2 
3 #include <Poco/AtomicCounter.h>
4 #include <Poco/AutoPtr.h>
5 #include <Poco/Event.h>
6 #include <Poco/Mutex.h>
7 #include <Poco/Net/WebSocket.h>
8 #include <Poco/SharedPtr.h>
9 #include <Poco/Thread.h>
10 #include <Poco/Timespan.h>
11 #include <Poco/Timestamp.h>
12 #include <Poco/Util/Timer.h>
13 
14 #include "commands/NewDeviceCommand.h"
15 #include "commands/ServerDeviceListCommand.h"
16 #include "commands/ServerLastValueCommand.h"
17 #include "core/CommandHandler.h"
18 #include "core/CommandSender.h"
19 #include "core/Exporter.h"
20 #include "core/GatewayInfo.h"
21 #include "gwmessage/GWAck.h"
22 #include "gwmessage/GWDeviceAcceptRequest.h"
23 #include "gwmessage/GWListenRequest.h"
24 #include "gwmessage/GWMessage.h"
25 #include "gwmessage/GWMessage.h"
26 #include "gwmessage/GWRequest.h"
27 #include "gwmessage/GWResponse.h"
28 #include "gwmessage/GWSensorDataConfirm.h"
29 #include "gwmessage/GWSetValueRequest.h"
30 #include "gwmessage/GWUnpairRequest.h"
31 #include "loop/StoppableLoop.h"
32 #include "server/GWMessageContext.h"
33 #include "server/GWSOutputQueue.h"
34 #include "server/GWContextPoll.h"
35 #include "ssl/SSLClient.h"
36 #include "util/Loggable.h"
37 
38 namespace BeeeOn {
39 
50  public StoppableLoop,
51  public CommandSender,
52  public CommandHandler,
53  public Exporter,
54  protected Loggable {
55 public:
56  typedef Poco::SharedPtr<GWServerConnector> Ptr;
57 
59 
60  void start() override;
61  void stop() override;
62 
63  void setHost(const std::string &host);
64  void setPort(int port);
65  void setPollTimeout(const Poco::Timespan &timeout);
66  void setReceiveTimeout(const Poco::Timespan &timeout);
67  void setSendTimeout(const Poco::Timespan &timeout);
68  void setRetryConnectTimeout(const Poco::Timespan &timeout);
69  void setBusySleep(const Poco::Timespan &timeout);
70  void setResendTimeout(const Poco::Timespan &timeout);
71  void setMaxMessageSize(int size);
72  void setGatewayInfo(Poco::SharedPtr<GatewayInfo> info);
73  void setSSLConfig(Poco::SharedPtr<SSLClient> config);
74  void setInactiveMultiplier(int multiplier);
75 
76  bool accept(const Command::Ptr cmd) override;
77  void handle(Command::Ptr cmd, Answer::Ptr answer) override;
78 
79  bool ship(const SensorData &data) override;
80 
81 private:
86  void startReceiver();
87 
92  void runReceiver();
93 
97  void startSender();
98 
103  void runSender();
104 
109  bool connectUnlocked();
110 
114  bool registerUnlocked();
115 
119  void connectAndRegisterUnlocked();
120 
124  void disconnectUnlocked();
125 
130  void reconnect();
131 
135  void markDisconnected();
136 
140  void sendPing();
141 
146  void forwardContext(GWMessageContext::Ptr context);
147 
151  void forwardOutputQueue();
152  void sendMessage(const GWMessage::Ptr message);
153  void sendMessageUnlocked(const GWMessage::Ptr message);
154 
155  void doNewDeviceCommand(NewDeviceCommand::Ptr cmd, Answer::Ptr answer);
156  void doDeviceListCommand(ServerDeviceListCommand::Ptr cmd, Answer::Ptr answer);
157  void doLastValueCommand(ServerLastValueCommand::Ptr cmd, Answer::Ptr answer);
158 
159  void dispatchServerCommand(Command::Ptr cmd, const GlobalID &id, GWResponse::Ptr response);
160 
165  void handleRequest(GWRequest::Ptr request);
166 
167  void handleDeviceAcceptRequest(GWDeviceAcceptRequest::Ptr request);
168  void handleListenRequest(GWListenRequest::Ptr request);
169  void handleSetValueRequest(GWSetValueRequest::Ptr request);
170  void handleUnpairRequest(GWUnpairRequest::Ptr request);
171 
176  void handleResponse(GWResponse::Ptr response);
177 
182  void handleAck(GWAck::Ptr ack);
183 
188  void handleSensorDataConfirm(GWSensorDataConfirm::Ptr confirm);
189 
194  void handleMessage(GWMessage::Ptr msg);
195 
196  void enqueueFinishedAnswers();
197 
203  bool connectionSeemsBroken();
204  void updateLastReceived();
205 
206  GWMessage::Ptr receiveMessageUnlocked();
207 
208  Poco::Event &readyToSendEvent();
209 
210 private:
211  std::string m_host;
212  int m_port;
213  Poco::Timespan m_pollTimeout;
214  Poco::Timespan m_receiveTimeout;
215  Poco::Timespan m_sendTimeout;
216  Poco::Timespan m_retryConnectTimeout;
217  Poco::Timespan m_busySleep;
218  Poco::Timespan m_resendTimeout;
219  size_t m_maxMessageSize;
220  Poco::SharedPtr<GatewayInfo> m_gatewayInfo;
221  Poco::SharedPtr<SSLClient> m_sslConfig;
222  Poco::Timestamp m_lastReceived;
223  Poco::FastMutex m_lastReceivedMutex;
224  int m_inactiveMultiplier;
225 
226  Poco::FastMutex m_dispatchLock;
227 
228  Poco::Buffer<char> m_receiveBuffer;
229  Poco::SharedPtr<Poco::Net::WebSocket> m_socket;
230  Poco::FastMutex m_receiveMutex;
231  Poco::FastMutex m_sendMutex;
232  Poco::Thread m_senderThread;
233  Poco::Thread m_receiverThread;
234 
235  Poco::AtomicCounter m_isConnected;
236  Poco::Event m_connectedEvent;
237 
238  Poco::AtomicCounter m_stop;
239  Poco::Event m_stopEvent;
240 
241  GWContextPoll m_contextPoll;
242  GWSOutputQueue m_outputQueue;
243  Poco::Util::Timer m_timer;
244 };
245 
246 }
void start() override
Definition: GWServerConnector.cpp:67
The GWServerConnector allows the BeeeOn Gateway to communicate with the BeeeOn Server using WebSocket...
Definition: GWServerConnector.h:49
GWContextPoll stores contexts of sent messages. This is used for messages that expects the answer...
Definition: GWContextPoll.h:17
Definition: SensorData.h:20
Definition: GlobalID.h:10
Definition: StoppableLoop.h:17
Definition: CommandHandler.h:18
Definition: Exporter.h:11
Queue for all outgoing messages. Must be initialized with Poco::Event reference, which is notified on...
Definition: GWSOutputQueue.h:18
void stop() override
Definition: GWServerConnector.cpp:77
Definition: CommandSender.h:20
Definition: Loggable.h:19
bool ship(const SensorData &data) override
Definition: GWServerConnector.cpp:522