client-cpp  0.6.3
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 #ifdef KAA_DEFAULT_TCP_CHANNEL
23 
24 #include <cstdint>
25 #include <thread>
26 #include <array>
27 
28 #include <boost/asio.hpp>
29 
30 #include "kaa/KaaThread.hpp"
39 
40 
41 namespace kaa {
42 
43 class IKaaTcpRequest;
44 class KeyPair;
45 
46 class DefaultOperationTcpChannel : public IDataChannel {
47 public:
48  DefaultOperationTcpChannel(IKaaChannelManager *channelManager, const KeyPair& clientKeys);
49  virtual ~DefaultOperationTcpChannel();
50 
51  virtual void sync(TransportType type);
52  virtual void syncAll();
53  virtual void syncAck(TransportType type);
54 
55  virtual const std::string& getId() const { return CHANNEL_ID; }
56 
57  virtual TransportProtocolId getTransportProtocolId() const {
59  }
60 
61  virtual void setMultiplexer(IKaaDataMultiplexer *multiplexer);
62  virtual void setDemultiplexer(IKaaDataDemultiplexer *demultiplexer);
63  virtual void setServer(ITransportConnectionInfoPtr server);
64 
65  virtual void shutdown();
66  virtual void pause();
67  virtual void resume();
68 
69  virtual const std::map<TransportType, ChannelDirection>& getSupportedTransportTypes() const {
70  return SUPPORTED_TYPES;
71  }
72 
73  virtual ServerType getServerType() const {
75  }
76 
77  virtual void setConnectivityChecker(ConnectivityCheckerPtr checker) {
78  connectivityChecker_= checker;
79  }
80 
81  void onReadEvent(const boost::system::error_code& err);
82  void onPingTimeout(const boost::system::error_code& err);
83 
84  void onConnack(const ConnackMessage& message);
85  void onDisconnect(const DisconnectMessage& message);
86  void onKaaSync(const KaaSyncResponse& message);
87  void onPingResponse();
88 
89  void openConnection();
90  void closeConnection();
91  void onServerFailed();
92 
93 private:
94  static const std::uint16_t PING_TIMEOUT;
95  static const std::uint16_t RECONNECT_TIMEOUT;
96 
97  boost::system::error_code sendKaaSync(const std::map<TransportType, ChannelDirection>& transportTypes);
98  boost::system::error_code sendConnect();
99  boost::system::error_code sendDisconnect();
100  boost::system::error_code sendPingRequest();
101  boost::system::error_code sendData(const IKaaTcpRequest& request);
102 
103  void readFromSocket();
104  void setTimer();
105 
106  void createThreads();
107 
108  void doShutdown();
109 
110 private:
111  static const std::string CHANNEL_ID;
112  static const std::map<TransportType, ChannelDirection> SUPPORTED_TYPES;
113  static const std::uint16_t THREADPOOL_SIZE = 2;
114  static const std::uint32_t KAA_PLATFORM_PROTOCOL_AVRO_ID;
115 
116  std::list<TransportType> ackTypes_;
117  KeyPair clientKeys_;
118 
119  boost::asio::io_service io_;
120  boost::asio::io_service::work work_;
121  boost::asio::ip::tcp::socket sock_;
122  boost::asio::deadline_timer pingTimer_;
123  boost::asio::deadline_timer reconnectTimer_;
124  boost::asio::streambuf responseBuffer_;
125  std::array<std::thread, THREADPOOL_SIZE> channelThreads_;
126 
127  bool firstStart_;
128  bool isConnected_;
129  bool isFirstResponseReceived_;
130  bool isPendingSyncRequest_;
131  bool isShutdown_;
132  bool isPaused_;
133 
134  IKaaDataMultiplexer *multiplexer_;
135  IKaaDataDemultiplexer *demultiplexer_;
136  IKaaChannelManager *channelManager_;
137  std::shared_ptr<IPTransportInfo> currentServer_;
138  KaaTcpResponseProcessor responsePorcessor;
139  std::unique_ptr<RsaEncoderDecoder> encDec_;
140 
141  KAA_MUTEX_DECLARE(channelGuard_);
142 
143  ConnectivityCheckerPtr connectivityChecker_;
144 };
145 
146 }
147 
148 #endif
149 
150 #endif /* DEFAULTOPERATIONTCPCHANNEL_HPP_ */
std::shared_ptr< IConnectivityChecker > ConnectivityCheckerPtr
static const TransportProtocolId TCP_TRANSPORT_ID
ServerType
Definition: ServerType.hpp:22
#define KAA_MUTEX_DECLARE(name)
Definition: KaaThread.hpp:71
std::shared_ptr< ITransportConnectionInfo > ITransportConnectionInfoPtr