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