client-cpp  0.10.0
KaaChannelManager.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2014-2016 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 KAACHANNELMANAGER_HPP_
18 #define KAACHANNELMANAGER_HPP_
19 
20 #include <map>
21 #include <set>
22 #include <list>
23 
24 #include "kaa/KaaThread.hpp"
25 #include "kaa/KaaDefaults.hpp"
29 #include "kaa/utils/KaaTimer.hpp"
31 
32 namespace kaa {
33 
34 class IKaaClient;
35 class IBootstrapManager;
36 
38 {
39 public:
41  const BootstrapServers& servers,
42  IKaaClientContext& context,
43  IKaaClient *client);
44 
45  ~KaaChannelManager() { doShutdown(); }
46 
47  virtual void setFailoverStrategy(IFailoverStrategyPtr strategy);
48  virtual void setChannel(TransportType type, IDataChannelPtr channel);
49  virtual void addChannel(IDataChannelPtr channel);
50  virtual void removeChannel(const std::string& id);
51  virtual void removeChannel(IDataChannelPtr channel);
52 
53  virtual std::list<IDataChannelPtr> getChannels();
54 
56  virtual IDataChannelPtr getChannel(const std::string& channelId);
57 
58  virtual void onServerFailed(ITransportConnectionInfoPtr connectionInfo, KaaFailoverReason reason);
59 
60  virtual void onConnected(const EndpointConnectionInfo& connection);
61 
63 
64  virtual void clearChannelList();
65 
66  virtual ITransportConnectionInfoPtr getPingServer() { return *(lastBSServers_.begin()->second); }
67 
68  virtual void setConnectivityChecker(ConnectivityCheckerPtr checker);
69 
70  void shutdown();
71  void pause();
72  void resume();
73 
74 private:
75  bool useChannelForType(const std::pair<TransportType, ChannelDirection>& type, IDataChannelPtr channel);
76  void useNewChannel(IDataChannelPtr channel);
77  void useNewChannelForType(TransportType type);
78  void replaceChannel(IDataChannelPtr channel);
79 
80  bool addChannelToList(IDataChannelPtr channel);
81 
82  void doShutdown();
83 
84  ITransportConnectionInfoPtr getCurrentBootstrapServer(const TransportProtocolId& protocolId);
85  ITransportConnectionInfoPtr getNextBootstrapServer(const TransportProtocolId& protocolId, bool forceFirstElement);
86 
87  void onBootstrapServerFailed(ITransportConnectionInfoPtr connectionInfo, KaaFailoverReason reason);
88 
89  void updateBootstrapServerAndSync(ITransportConnectionInfoPtr connectionInfo);
90 
91  void checkAuthenticationFailover(KaaFailoverReason failover);
92 
93 private:
94  IBootstrapManager& bootstrapManager_;
95  IKaaClientContext& context_;
96 
97  IKaaClient *client_;
98 
99  IFailoverStrategyPtr failoverStrategy_;
100 
101  KaaTimer<void ()> retryTimer_;
102 
103  bool_type isShutdown_;
104  bool_type isPaused_;
105 
106  std::map<TransportProtocolId, BootstrapServers> bootstrapServers_;
107  std::map<TransportProtocolId, BootstrapServers::iterator> lastBSServers_;
108 
109  KAA_MUTEX_DECLARE(lastOpsServersGuard_);
110  std::map<TransportProtocolId, ITransportConnectionInfoPtr> lastOpsServers_;
111 
112  KAA_R_MUTEX_DECLARE(channelGuard_);
113  std::set<IDataChannelPtr> channels_;
114 
115  KAA_R_MUTEX_DECLARE(mappedChannelGuard_);
116  std::map<TransportType, IDataChannelPtr> mappedChannels_;
117 
118  ConnectivityCheckerPtr connectivityChecker_;
119 };
120 
121 } /* namespace kaa */
122 
123 #endif /* KAACHANNELMANAGER_HPP_ */
KaaChannelManager(IBootstrapManager &manager, const BootstrapServers &servers, IKaaClientContext &context, IKaaClient *client)
KaaFailoverReason
std::shared_ptr< IConnectivityChecker > ConnectivityCheckerPtr
std::shared_ptr< IFailoverStrategy > IFailoverStrategyPtr
std::atomic_bool bool_type
Definition: KaaThread.hpp:54
virtual void setConnectivityChecker(ConnectivityCheckerPtr checker)
virtual void removeChannel(const std::string &id)
virtual void onConnected(const EndpointConnectionInfo &connection)
virtual IDataChannelPtr getChannel(const std::string &channelId)
virtual void setFailoverStrategy(IFailoverStrategyPtr strategy)
std::vector< ITransportConnectionInfoPtr > BootstrapServers
Definition: KaaDefaults.hpp:61
virtual void onTransportConnectionInfoUpdated(ITransportConnectionInfoPtr connectionInfo)
virtual IDataChannelPtr getChannelByTransportType(TransportType type)
virtual void clearChannelList()
virtual ITransportConnectionInfoPtr getPingServer()
virtual void addChannel(IDataChannelPtr channel)
virtual std::list< IDataChannelPtr > getChannels()
virtual void onServerFailed(ITransportConnectionInfoPtr connectionInfo, KaaFailoverReason reason)
virtual void setChannel(TransportType type, IDataChannelPtr channel)
std::shared_ptr< ITransportConnectionInfo > ITransportConnectionInfoPtr