client-cpp  0.10.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 IKaaTcpRequest;
46 class KeyPair;
47 
49 public:
51  const KeyPair& clientKeys,
52  IKaaClientContext& context);
53 
55 
56  virtual void sync(TransportType type);
57  virtual void syncAll();
58  virtual void syncAck(TransportType type);
59 
60  virtual const std::string& getId() const { return CHANNEL_ID; }
61 
64  }
65 
66  virtual void setMultiplexer(IKaaDataMultiplexer *multiplexer);
67  virtual void setDemultiplexer(IKaaDataDemultiplexer *demultiplexer);
68  virtual void setServer(ITransportConnectionInfoPtr server);
69 
71  return std::dynamic_pointer_cast<ITransportConnectionInfo, IPTransportInfo>(currentServer_);
72  }
73 
74  virtual void shutdown();
75  virtual void pause();
76  virtual void resume();
77 
78  virtual const std::map<TransportType, ChannelDirection>& getSupportedTransportTypes() const {
79  return SUPPORTED_TYPES;
80  }
81 
82  virtual ServerType getServerType() const {
84  }
85 
86  virtual void setFailoverStrategy(IFailoverStrategyPtr strategy) {
87  failoverStrategy_ = strategy;
88  }
89 
91  connectivityChecker_= checker;
92  }
93 
94  void onReadEvent(const boost::system::error_code& err);
95  void onPingTimeout(const boost::system::error_code& err);
96  void onConnAckTimeout(const boost::system::error_code& err);
97 
98  void onConnack(const ConnackMessage& message);
99  void onDisconnect(const DisconnectMessage& message);
100  void onKaaSync(const KaaSyncResponse& message);
101  void onPingResponse();
102 
103  void openConnection();
104  void closeConnection();
106 
107 private:
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 setPingTimer();
116  void setConnAckTimer();
117 
118  void startThreads();
119  void stopThreads();
120 
121  void doShutdown();
122 
123 private:
124  static const std::string CHANNEL_ID;
125 
126  static const std::map<TransportType, ChannelDirection> SUPPORTED_TYPES;
127 
128  static const std::uint16_t THREADPOOL_SIZE = 2;
129  static const std::uint32_t KAA_PLATFORM_PROTOCOL_AVRO_ID = 0xf291f2d4;
130 
131  static const std::uint16_t CHANNEL_TIMEOUT = 200;
132  static const std::uint16_t PING_TIMEOUT = CHANNEL_TIMEOUT / 2;
133  static const std::uint16_t CONN_ACK_TIMEOUT = 20;
134 
135 private:
136  IKaaClientContext& context_;
137  IKaaChannelManager& channelManager_;
138  KeyPair clientKeys_;
139 
140  std::list<TransportType> ackTypes_;
141 
142  boost::asio::io_service io_;
143  boost::asio::io_service::work work_;
144  std::unique_ptr<boost::asio::ip::tcp::socket> sock_;
145  boost::asio::deadline_timer pingTimer_;
146  boost::asio::deadline_timer connAckTimer_;
147  std::vector<std::thread> ioThreads_;
148  std::unique_ptr<boost::asio::streambuf> responseBuffer_;
149 
150  // TODO: http://jira.kaaproject.org/browse/KAA-1321
151  // Use states and present them as enum
152  bool isConnected_ = false;
153  bool isFirstResponseReceived_ = false;
154  bool isPendingSyncRequest_ = false;
155  bool isShutdown_ = false;
156  bool isPaused_ = false;
157  bool isFailoverInProgress_ = false;
158 
159  IKaaDataMultiplexer *multiplexer_ = nullptr;
160  IKaaDataDemultiplexer *demultiplexer_ = nullptr;
161 
162  KaaTcpResponseProcessor responseProcessor;
163 
164  // To avoid simultaneously re-creation/access a shared pointer is used.
165  std::shared_ptr<RsaEncoderDecoder> encDec_;
166 
167  KAA_MUTEX_DECLARE(channelGuard_);
168 
169  std::shared_ptr<IPTransportInfo> currentServer_;
170  ConnectivityCheckerPtr connectivityChecker_;
171  IFailoverStrategyPtr failoverStrategy_;
172 };
173 
174 }
175 
176 #endif /* DEFAULTOPERATIONTCPCHANNEL_HPP_ */
void onConnack(const ConnackMessage &message)
KaaFailoverReason
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)
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
void onServerFailed(KaaFailoverReason failoverReason=KaaFailoverReason::CURRENT_OPERATIONS_SERVER_NA)
virtual void sync(TransportType type)
void onDisconnect(const DisconnectMessage &message)
void onConnAckTimeout(const boost::system::error_code &err)
void onKaaSync(const KaaSyncResponse &message)
DefaultOperationTcpChannel(IKaaChannelManager &channelManager, const KeyPair &clientKeys, IKaaClientContext &context)
std::shared_ptr< ITransportConnectionInfo > ITransportConnectionInfoPtr