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