client-cpp  0.7.4
DefaultOperationTcpChannel.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2014 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"
40 
41 
42 namespace kaa {
43 
44 class IKaaTcpRequest;
45 class KeyPair;
46 
48 public:
49  DefaultOperationTcpChannel(IKaaChannelManager *channelManager, const KeyPair& clientKeys, IKaaClientStateStoragePtr clientState);
51 
52  virtual void sync(TransportType type);
53  virtual void syncAll();
54  virtual void syncAck(TransportType type);
55 
56  virtual const std::string& getId() const { return CHANNEL_ID; }
57 
60  }
61 
62  virtual void setMultiplexer(IKaaDataMultiplexer *multiplexer);
63  virtual void setDemultiplexer(IKaaDataDemultiplexer *demultiplexer);
64  virtual void setServer(ITransportConnectionInfoPtr server);
65 
67  return std::dynamic_pointer_cast<ITransportConnectionInfo, IPTransportInfo>(currentServer_);
68  }
69 
70  virtual void shutdown();
71  virtual void pause();
72  virtual void resume();
73 
74  virtual const std::map<TransportType, ChannelDirection>& getSupportedTransportTypes() const {
75  return SUPPORTED_TYPES;
76  }
77 
78  virtual ServerType getServerType() const {
80  }
81 
82  virtual void setFailoverStrategy(IFailoverStrategyPtr strategy) {
83  failoverStrategy_ = strategy;
84  }
85 
87  connectivityChecker_= checker;
88  }
89 
90  void onReadEvent(const boost::system::error_code& err);
91  void onPingTimeout(const boost::system::error_code& err);
92  void onConnAckTimeout(const boost::system::error_code& err);
93 
94  void onConnack(const ConnackMessage& message);
95  void onDisconnect(const DisconnectMessage& message);
96  void onKaaSync(const KaaSyncResponse& message);
97  void onPingResponse();
98 
99  void openConnection();
100  void closeConnection();
101  void onServerFailed();
102 
103 private:
104  static const std::uint16_t PING_TIMEOUT;
105  static const std::uint16_t CONN_ACK_TIMEOUT;
106  static const std::uint16_t RECONNECT_TIMEOUT;
107 
108  boost::system::error_code sendKaaSync(const std::map<TransportType, ChannelDirection>& transportTypes);
109  boost::system::error_code sendConnect();
110  boost::system::error_code sendDisconnect();
111  boost::system::error_code sendPingRequest();
112  boost::system::error_code sendData(const IKaaTcpRequest& request);
113 
114  void readFromSocket();
115  void setTimer();
116  void setConnAckTimer();
117 
118  void createThreads();
119 
120  void doShutdown();
121 
122 private:
123  static const std::string CHANNEL_ID;
124  static const std::map<TransportType, ChannelDirection> SUPPORTED_TYPES;
125  static const std::uint16_t TIMER_THREADPOOL_SIZE = 1;
126  static const std::uint16_t SOCKET_THREADPOOL_SIZE = 1;
127  static const std::uint32_t KAA_PLATFORM_PROTOCOL_AVRO_ID;
128 
129  std::list<TransportType> ackTypes_;
130  KeyPair clientKeys_;
131 
132  boost::asio::io_service io_;
133  boost::asio::io_service socketIo_;
134  boost::asio::io_service::work work_;
135  boost::asio::io_service::work socketWork_;
136 
137  std::unique_ptr<boost::asio::ip::tcp::socket> sock_;
138  boost::asio::deadline_timer pingTimer_;
139  boost::asio::deadline_timer connAckTimer_;
140  //boost::asio::deadline_timer reconnectTimer_;
141  KaaTimer<void ()> retryTimer_;
142 
143  std::unique_ptr<boost::asio::streambuf> responseBuffer_;
144  std::array<std::thread, TIMER_THREADPOOL_SIZE> timerThreads_;
145  std::array<std::thread, SOCKET_THREADPOOL_SIZE> channelThreads_;
146 
147  bool firstStart_;
148  bool isConnected_;
149  bool isFirstResponseReceived_;
150  bool isPendingSyncRequest_;
151  bool isShutdown_;
152  bool isPaused_;
153  bool isFailoverInProgress_;
154 
155  IKaaDataMultiplexer *multiplexer_;
156  IKaaDataDemultiplexer *demultiplexer_;
157  IKaaChannelManager *channelManager_;
158  std::shared_ptr<IPTransportInfo> currentServer_;
159  KaaTcpResponseProcessor responsePorcessor;
160  std::unique_ptr<RsaEncoderDecoder> encDec_;
161 
162  KAA_MUTEX_DECLARE(channelGuard_);
163 
164  ConnectivityCheckerPtr connectivityChecker_;
165  IFailoverStrategyPtr failoverStrategy_;
166 
167  IKaaClientStateStoragePtr clientState_;
168 };
169 
170 }
171 
172 #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)
DefaultOperationTcpChannel(IKaaChannelManager *channelManager, const KeyPair &clientKeys, IKaaClientStateStoragePtr clientState)
virtual void syncAck(TransportType type)
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)
void onReadEvent(const boost::system::error_code &err)
std::shared_ptr< IKaaClientStateStorage > IKaaClientStateStoragePtr
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