client-cpp  0.6.1
DefaultOperationLongPollChannel.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 DEFAULTOPERATIONLONGPOLLCHANNEL_HPP_
18 #define DEFAULTOPERATIONLONGPOLLCHANNEL_HPP_
19 
20 #include "kaa/KaaDefaults.hpp"
21 
22 #ifdef KAA_DEFAULT_LONG_POLL_CHANNEL
23 
26 #include "kaa/http/HttpClient.hpp"
27 #include <boost/asio.hpp>
31 #include "kaa/KaaThread.hpp"
32 
33 #include <cstdint>
34 #include <thread>
35 
36 namespace kaa {
37 
38 class DefaultOperationLongPollChannel : public IDataChannel {
39 public:
40  DefaultOperationLongPollChannel(IKaaChannelManager *channelManager, const KeyPair& clientKeys);
41  virtual ~DefaultOperationLongPollChannel();
42 
43  virtual void sync(TransportType type);
44  virtual void syncAll();
45  virtual void syncAck(TransportType type);
46  virtual const std::string& getId() const { return CHANNEL_ID; }
47  virtual ChannelType getChannelType() const { return ChannelType::HTTP_LP; }
48  virtual ServerType getServerType() const { return ServerType::OPERATIONS; }
49 
50  virtual void setMultiplexer(IKaaDataMultiplexer *multiplexer);
51  virtual void setDemultiplexer(IKaaDataDemultiplexer *demultiplexer);
52  virtual void setServer(IServerInfoPtr server);
53 
54  virtual const std::map<TransportType, ChannelDirection>& getSupportedTransportTypes() const { return SUPPORTED_TYPES; }
55 
56  virtual void shutdown();
57  virtual void pause();
58  virtual void resume();
59 
60  virtual void setConnectivityChecker(ConnectivityCheckerPtr checker) {}
61 
62 private:
63  void startPoll();
64  void stopPoll();
65  void postTask();
66  void executeTask();
67  void doShutdown();
68 
69 private:
70  static const std::string CHANNEL_ID;
71  static const std::map<TransportType, ChannelDirection> SUPPORTED_TYPES;
72 
73  KeyPair clientKeys_;
74 
75  boost::asio::io_service io_;
76  boost::asio::io_service::work work_;
77  std::thread pollThread_;
78  bool stopped_;
79  bool isShutdown_;
80  bool isPaused_;
81  bool connectionInProgress_;
82  bool taskPosted_;
83  bool firstStart_;
84  IKaaDataMultiplexer *multiplexer_;
85  IKaaDataDemultiplexer *demultiplexer_;
86  IKaaChannelManager *channelManager_;
87  OperationServerLongPollInfoPtr currentServer_;
88  HttpDataProcessor httpDataProcessor_;
89  HttpClient httpClient_;
90  KAA_CONDITION_VARIABLE_DECLARE(waitCondition_);
91  KAA_MUTEX_DECLARE(conditionMutex_);
92  KAA_MUTEX_DECLARE(channelGuard_);
93 };
94 
95 }
96 
97 #endif
98 
99 #endif /* DEFAULTOPERATIONLONGPOLLCHANNEL_HPP_ */
std::pair< Botan::MemoryVector< std::uint8_t >, std::string > KeyPair
Definition: KeyUtils.hpp:29
std::shared_ptr< IConnectivityChecker > ConnectivityCheckerPtr
std::shared_ptr< HttpLPServerInfo > OperationServerLongPollInfoPtr
ServerType
Definition: ServerType.hpp:22
std::shared_ptr< IServerInfo > IServerInfoPtr
Definition: IServerInfo.hpp:53
#define KAA_MUTEX_DECLARE(name)
Definition: KaaThread.hpp:71
#define KAA_CONDITION_VARIABLE_DECLARE(name)
Definition: KaaThread.hpp:70