19 #ifndef ABSTRACTHTTPCHANNEL_HPP_ 
   20 #define ABSTRACTHTTPCHANNEL_HPP_ 
   24 #if defined(KAA_DEFAULT_BOOTSTRAP_HTTP_CHANNEL) || defined (KAA_DEFAULT_OPERATION_HTTP_CHANNEL) 
   47 template <ChannelType Type>
 
   48 class AbstractHttpChannel : 
public ImpermanentDataChannel {
 
   50     AbstractHttpChannel(IKaaChannelManager *channelManager, 
const KeyPair& clientKeys);
 
   51     virtual ~AbstractHttpChannel() { }
 
   54     virtual void syncAll();
 
   56     virtual void setMultiplexer(IKaaDataMultiplexer *multiplexer);
 
   57     virtual void setDemultiplexer(IKaaDataDemultiplexer *demultiplexer);
 
   60     virtual ChannelType getChannelType()
 const  { 
return Type; }
 
   65     typedef std::shared_ptr<AbstractServerInfo<Type> > AbstractServerInfoPtr;
 
   67     HttpDataProcessor* getHttpDataProcessor() { 
return &httpDataProcessor_; }
 
   68     virtual void processTypes(
const std::map<TransportType, ChannelDirection>& types, 
KAA_MUTEX_UNIQUE& lock);
 
   71     virtual std::shared_ptr<IHttpRequest> createRequest(AbstractServerInfoPtr server, 
const std::vector<std::uint8_t>& body) = 0;
 
   72     virtual std::string retrieveResponse(
const IHttpResponse& response) = 0;
 
   77     bool lastConnectionFailed_;
 
   79     IKaaDataMultiplexer *multiplexer_;
 
   80     IKaaDataDemultiplexer *demultiplexer_;
 
   81     IKaaChannelManager *channelManager_;
 
   82     AbstractServerInfoPtr currentServer_;
 
   83     HttpDataProcessor httpDataProcessor_;
 
   84     HttpClient httpClient_;
 
   88 template <ChannelType Type>
 
   89 AbstractHttpChannel<Type>::AbstractHttpChannel(IKaaChannelManager *channelManager, 
const KeyPair& clientKeys)
 
   90     : clientKeys_(clientKeys), lastConnectionFailed_(false)
 
   91     , multiplexer_(nullptr), demultiplexer_(nullptr), channelManager_(channelManager) {}
 
   93 template <ChannelType Type>
 
   94 void AbstractHttpChannel<Type>::processTypes(
const std::map<TransportType, ChannelDirection>& types, 
KAA_MUTEX_UNIQUE& lock)
 
   96     AbstractServerInfoPtr server = currentServer_;
 
   98     const auto& bodyRaw = multiplexer_->compileRequest(types);
 
  100     std::shared_ptr<IHttpRequest> postRequest = createRequest(server, bodyRaw);
 
  107         auto response = httpClient_.sendRequest(*postRequest);
 
  114         const std::string& processedResponse = retrieveResponse(*response);
 
  115         lastConnectionFailed_ = 
false;
 
  121         if (!processedResponse.empty()) {
 
  122             demultiplexer_->processResponse(
 
  123                     std::vector<std::uint8_t>(reinterpret_cast<const std::uint8_t *>(processedResponse.data()),
 
  124                                               reinterpret_cast<const std::uint8_t *>(processedResponse.data() + processedResponse.size())));
 
  126     } 
catch (std::exception& e) {
 
  127         KAA_LOG_ERROR(boost::format(
"Connection failed, server %1%:%2%: %3%") % server->getHost() % server->getPort() % e.what());
 
  133         lastConnectionFailed_ = 
true;
 
  139         channelManager_->onServerFailed(server);
 
  143 template <ChannelType Type>
 
  146     const auto& supportedTypes = getSupportedTransportTypes();
 
  147     auto it = supportedTypes.find(type);
 
  152         if (currentServer_) {
 
  153             processTypes(std::map<TransportType, ChannelDirection>({ { type, it->second } }), lock);
 
  155             lastConnectionFailed_ = 
true;
 
  156             KAA_LOG_WARN(boost::format(
"Can't sync channel %1%. Server is null") % getId());
 
  159         KAA_LOG_ERROR(boost::format(
"Unsupported transport type for channel %1%") % getId());
 
  163 template <ChannelType Type>
 
  164 void AbstractHttpChannel<Type>::syncAll()
 
  169     if (currentServer_) {
 
  170         processTypes(getSupportedTransportTypes(), lock);
 
  172         lastConnectionFailed_ = 
true;
 
  173         KAA_LOG_WARN(boost::format(
"Can't sync channel %1%. Server is null") % getId());
 
  177 template <ChannelType Type>
 
  180     KAA_LOG_DEBUG(boost::format(
"Sync ack operation is not supported by channel %1%.") % getId());
 
  183 template <ChannelType Type>
 
  184 void AbstractHttpChannel<Type>::setMultiplexer(IKaaDataMultiplexer *multiplexer)
 
  189     multiplexer_ = multiplexer;
 
  192 template <ChannelType Type>
 
  193 void AbstractHttpChannel<Type>::setDemultiplexer(IKaaDataDemultiplexer *demultiplexer)
 
  198     demultiplexer_ = demultiplexer;
 
  201 template <ChannelType Type>
 
  204     if (server->getChannelType() == getChannelType()) {
 
  208         currentServer_ = std::dynamic_pointer_cast<AbstractServerInfo<Type>, IServerInfo>(server);
 
  209         std::shared_ptr<IEncoderDecoder> encDec(
new RsaEncoderDecoder(clientKeys_.first, clientKeys_.second, currentServer_->getPublicKey()));
 
  210         httpDataProcessor_.setEncoderDecoder(encDec);
 
  211         if (lastConnectionFailed_) {
 
  212             lastConnectionFailed_ = 
false;
 
  213             processTypes(getSupportedTransportTypes(), lock);
 
  216         KAA_LOG_ERROR(boost::format(
"Invalid server info for channel %1%") % getId());
 
std::pair< Botan::MemoryVector< std::uint8_t >, std::string > KeyPair
 
std::shared_ptr< IConnectivityChecker > ConnectivityCheckerPtr
 
#define KAA_LOG_DEBUG(message)
 
#define KAA_MUTEX_UNLOCKING(mutex_name)
 
#define KAA_MUTEX_LOCKED(mutex_name)
 
#define KAA_LOG_ERROR(message)
 
std::shared_ptr< IServerInfo > IServerInfoPtr
 
#define KAA_LOG_WARN(message)
 
#define KAA_MUTEX_DECLARE(name)
 
#define KAA_MUTEX_UNIQUE_DECLARE(name, mtx)
 
#define KAA_MUTEX_UNLOCKED(mutex_name)
 
#define KAA_MUTEX_LOCKING(mutex_name)