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