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