client-cpp  0.9.0
DefaultOperationTcpChannel.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2014-2016 CyberVision, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef DEFAULTOPERATIONTCPCHANNEL_HPP_
18 #define DEFAULTOPERATIONTCPCHANNEL_HPP_
19 
20 #include "kaa/KaaDefaults.hpp"
21 
22 #include <cstdint>
23 #include <thread>
24 #include <array>
25 #include <memory>
26 
27 #include <boost/asio.hpp>
28 
29 #include "kaa/KaaThread.hpp"
38 #include "kaa/utils/KaaTimer.hpp"
41 
42 
43 namespace kaa {
44 
45 class IKaaClient;
46 class IKaaTcpRequest;
47 class KeyPair;
48 
50 public:
51  DefaultOperationTcpChannel(IKaaChannelManager *channelManager, const KeyPair& clientKeys,
52  IKaaClientContext &context, IKaaClient *client);
54 
55  virtual void sync(TransportType type);
56  virtual void syncAll();
57  virtual void syncAck(TransportType type);
58 
59  virtual const std::string& getId() const { return CHANNEL_ID; }
60 
63  }
64 
65  virtual void setMultiplexer(IKaaDataMultiplexer *multiplexer);
66  virtual void setDemultiplexer(IKaaDataDemultiplexer *demultiplexer);
67  virtual void setServer(ITransportConnectionInfoPtr server);
68 
70  return std::dynamic_pointer_cast<ITransportConnectionInfo, IPTransportInfo>(currentServer_);
71  }
72 
73  virtual void shutdown();
74  virtual void pause();
75  virtual void resume();
76 
77  virtual const std::map<TransportType, ChannelDirection>& getSupportedTransportTypes() const {
78  return SUPPORTED_TYPES;
79  }
80 
81  virtual ServerType getServerType() const {
83  }
84 
85  virtual void setFailoverStrategy(IFailoverStrategyPtr strategy) {
86  failoverStrategy_ = strategy;
87  }
88 
90  connectivityChecker_= checker;
91  }
92 
93  void onReadEvent(const boost::system::error_code& err);
94  void onPingTimeout(const boost::system::error_code& err);
95  void onConnAckTimeout(const boost::system::error_code& err);
96 
97  void onConnack(const ConnackMessage& message);
98  void onDisconnect(const DisconnectMessage& message);
99  void onKaaSync(const KaaSyncResponse& message);
100  void onPingResponse();
101 
102  void openConnection();
103  void closeConnection();
105 
106 private:
107  static const std::uint16_t PING_TIMEOUT;
108  static const std::uint16_t CHANNEL_TIMEOUT;
109  static const std::uint16_t CONN_ACK_TIMEOUT;
110  static const std::uint16_t RECONNECT_TIMEOUT;
111 
112  boost::system::error_code sendKaaSync(const std::map<TransportType, ChannelDirection>& transportTypes);
113  boost::system::error_code sendConnect();
114  boost::system::error_code sendDisconnect();
115  boost::system::error_code sendPingRequest();
116  boost::system::error_code sendData(const IKaaTcpRequest& request);
117 
118  void readFromSocket();
119  void setTimer();
120  void setConnAckTimer();
121 
122  void createThreads();
123 
124  void doShutdown();
125 
126 private:
127  static const std::string CHANNEL_ID;
128  static const std::map<TransportType, ChannelDirection> SUPPORTED_TYPES;
129  static const std::uint16_t TIMER_THREADPOOL_SIZE = 1;
130  static const std::uint16_t SOCKET_THREADPOOL_SIZE = 1;
131  static const std::uint32_t KAA_PLATFORM_PROTOCOL_AVRO_ID;
132 
133  std::list<TransportType> ackTypes_;
134  KeyPair clientKeys_;
135 
136  boost::asio::io_service io_;
137  boost::asio::io_service socketIo_;
138  boost::asio::io_service::work work_;
139  boost::asio::io_service::work socketWork_;
140 
141  std::unique_ptr<boost::asio::ip::tcp::socket> sock_;
142  boost::asio::deadline_timer pingTimer_;
143  boost::asio::deadline_timer connAckTimer_;
144  //boost::asio::deadline_timer reconnectTimer_;
145  KaaTimer<void ()> retryTimer_;
146 
147  std::unique_ptr<boost::asio::streambuf> responseBuffer_;
148  std::array<std::thread, TIMER_THREADPOOL_SIZE> timerThreads_;
149  std::array<std::thread, SOCKET_THREADPOOL_SIZE> channelThreads_;
150 
151  bool firstStart_;
152  bool isConnected_;
153  bool isFirstResponseReceived_;
154  bool isPendingSyncRequest_;
155  bool isShutdown_;
156  bool isPaused_;
157  bool isFailoverInProgress_;
158 
159  IKaaDataMultiplexer *multiplexer_;
160  IKaaDataDemultiplexer *demultiplexer_;
161  IKaaChannelManager *channelManager_;
162  std::shared_ptr<IPTransportInfo> currentServer_;
163  KaaTcpResponseProcessor responsePorcessor;
164  std::unique_ptr<RsaEncoderDecoder> encDec_;
165 
166  KAA_MUTEX_DECLARE(channelGuard_);
167 
168  ConnectivityCheckerPtr connectivityChecker_;
169  IFailoverStrategyPtr failoverStrategy_;
170 
171  IKaaClientContext &context_;
172 
173  // Temporary solution to stop app
174  IKaaClient *client_;
175 };
176 
177 }
178 
179 #endif /* DEFAULTOPERATIONTCPCHANNEL_HPP_ */
void onConnack(const ConnackMessage &message)
std::shared_ptr< IConnectivityChecker > ConnectivityCheckerPtr
std::shared_ptr< IFailoverStrategy > IFailoverStrategyPtr
virtual void setMultiplexer(IKaaDataMultiplexer *multiplexer)
virtual void setServer(ITransportConnectionInfoPtr server)
virtual void syncAck(TransportType type)
void onServerFailed(KaaFailoverReason failoverReason=KaaFailoverReason::NO_CONNECTIVITY)
static const TransportProtocolId TCP_TRANSPORT_ID
ServerType
Definition: ServerType.hpp:22
virtual void setConnectivityChecker(ConnectivityCheckerPtr checker)
virtual ServerType getServerType() const
virtual ITransportConnectionInfoPtr getServer()
virtual const std::string & getId() const
virtual void setDemultiplexer(IKaaDataDemultiplexer *demultiplexer)
virtual void setFailoverStrategy(IFailoverStrategyPtr strategy)
DefaultOperationTcpChannel(IKaaChannelManager *channelManager, const KeyPair &clientKeys, IKaaClientContext &context, IKaaClient *client)
void onReadEvent(const boost::system::error_code &err)
virtual const std::map< TransportType, ChannelDirection > & getSupportedTransportTypes() const
void onPingTimeout(const boost::system::error_code &err)
virtual TransportProtocolId getTransportProtocolId() const
virtual void sync(TransportType type)
void onDisconnect(const DisconnectMessage &message)
void onConnAckTimeout(const boost::system::error_code &err)
void onKaaSync(const KaaSyncResponse &message)
std::shared_ptr< ITransportConnectionInfo > ITransportConnectionInfoPtr