17 #ifndef ABSTRACTHTTPCHANNEL_HPP_
18 #define ABSTRACTHTTPCHANNEL_HPP_
32 #include <boost/bind.hpp>
33 #include <boost/cstdint.hpp>
34 #include <boost/thread/mutex.hpp>
41 template <ChannelType Type>
62 virtual void processTypes(
const std::map<TransportType, ChannelDirection>& types, boost::unique_lock<boost::mutex>& lock);
65 virtual boost::shared_ptr<IHttpRequest> createRequest(
AbstractServerInfoPtr server,
const std::vector<boost::uint8_t>& body) = 0;
66 virtual std::string retrieveResponse(
const IHttpResponse& response) = 0;
71 bool lastConnectionFailed_;
79 boost::mutex channelGuard_;
82 template <ChannelType Type>
84 : clientKeys_(clientKeys), lastConnectionFailed_(false)
85 , multiplexer_(nullptr), demultiplexer_(nullptr), channelManager_(channelManager) {}
87 template <ChannelType Type>
92 const auto& bodyRaw = multiplexer_->compileRequest(types);
94 boost::shared_ptr<IHttpRequest> postRequest = createRequest(server, bodyRaw);
101 auto response = httpClient_.sendRequest(*postRequest);
104 boost::unique_lock<boost::mutex> lockInternal(channelGuard_);
108 const std::string& processedResponse = retrieveResponse(*response);
109 lastConnectionFailed_ =
false;
112 lockInternal.unlock();
115 if (!processedResponse.empty()) {
116 demultiplexer_->processResponse(
117 std::vector<boost::uint8_t>(reinterpret_cast<const boost::uint8_t *>(processedResponse.data()),
118 reinterpret_cast<const boost::uint8_t *>(processedResponse.data() + processedResponse.size())));
120 }
catch (std::exception& e) {
121 KAA_LOG_ERROR(boost::format(
"Connection failed, server %1%:%2%: %3%") % server->getHost() % server->getPort() % e.what());
124 boost::unique_lock<boost::mutex> lockInternal(channelGuard_);
127 lastConnectionFailed_ =
true;
130 lockInternal.unlock();
133 channelManager_->onServerFailed(server);
137 template <ChannelType Type>
140 const auto& supportedTypes = getSupportedTransportTypes();
141 auto it = supportedTypes.find(type);
144 boost::unique_lock<boost::mutex> lock(channelGuard_);
146 if (currentServer_) {
147 processTypes(std::map<TransportType, ChannelDirection>({ { type, it->second } }), lock);
149 lastConnectionFailed_ =
true;
150 KAA_LOG_WARN(boost::format(
"Can't sync channel %1%. Server is null") % getId());
153 KAA_LOG_ERROR(boost::format(
"Unsupported transport type for channel %1%") % getId());
157 template <ChannelType Type>
161 boost::unique_lock<boost::mutex> lock(channelGuard_);
163 if (currentServer_) {
164 processTypes(getSupportedTransportTypes(), lock);
166 lastConnectionFailed_ =
true;
167 KAA_LOG_WARN(boost::format(
"Can't sync channel %1%. Server is null") % getId());
171 template <ChannelType Type>
174 KAA_LOG_DEBUG(boost::format(
"Sync ack operation is not supported by channel %1%.") % getId());
177 template <ChannelType Type>
181 boost::unique_lock<boost::mutex> lock(channelGuard_);
183 multiplexer_ = multiplexer;
186 template <ChannelType Type>
190 boost::unique_lock<boost::mutex> lock(channelGuard_);
192 demultiplexer_ = demultiplexer;
195 template <ChannelType Type>
198 if (server->getChannelType() == getChannelType()) {
200 boost::unique_lock<boost::mutex> lock(channelGuard_);
203 boost::shared_ptr<IEncoderDecoder> encDec(
new RsaEncoderDecoder(clientKeys_.first, clientKeys_.second, currentServer_->getPublicKey()));
204 httpDataProcessor_.setEncoderDecoder(encDec);
205 if (lastConnectionFailed_) {
206 lastConnectionFailed_ =
false;
207 processTypes(getSupportedTransportTypes(), lock);
210 KAA_LOG_ERROR(boost::format(
"Invalid server info for channel %1%") % getId());
virtual void setDemultiplexer(IKaaDataDemultiplexer *demultiplexer)
std::pair< Botan::MemoryVector< boost::uint8_t >, std::string > KeyPair
#define KAA_LOG_DEBUG(message)
#define KAA_MUTEX_UNLOCKING(mutex_name)
#define KAA_MUTEX_LOCKED(mutex_name)
virtual ~AbstractHttpChannel()
#define KAA_LOG_ERROR(message)
virtual void setServer(IServerInfoPtr server)
#define KAA_LOG_WARN(message)
virtual void syncAck(TransportType type)
boost::shared_ptr< IServerInfo > IServerInfoPtr
virtual void setConnectivityChecker(ConnectivityCheckerPtr checker)
virtual void sync(TransportType type)
#define KAA_MUTEX_UNLOCKED(mutex_name)
virtual void processTypes(const std::map< TransportType, ChannelDirection > &types, boost::unique_lock< boost::mutex > &lock)
virtual ChannelType getChannelType() const
AbstractHttpChannel(IKaaChannelManager *channelManager, const KeyPair &clientKeys)
HttpDataProcessor * getHttpDataProcessor()
boost::shared_ptr< AbstractServerInfo< Type > > AbstractServerInfoPtr
boost::shared_ptr< IConnectivityChecker > ConnectivityCheckerPtr
virtual void setMultiplexer(IKaaDataMultiplexer *multiplexer)
#define KAA_MUTEX_LOCKING(mutex_name)