client-cpp  0.6.1
NotificationManager.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 DEFAULTNOTIFICATONMANAGER_HPP_
18 #define DEFAULTNOTIFICATONMANAGER_HPP_
19 
20 #include "kaa/KaaDefaults.hpp"
21 
22 #ifdef KAA_USE_NOTIFICATIONS
23 
24 #include "kaa/KaaThread.hpp"
25 
26 #include <memory>
27 #include <string>
28 #include <unordered_map>
29 #include <unordered_set>
30 
31 #include "kaa/gen/EndpointGen.hpp"
38 
40 
41 namespace kaa {
42 
43 class NotificationManager : public INotificationManager, public INotificationProcessor {
44 public:
45  NotificationManager(IKaaClientStateStoragePtr status);
46 
47  virtual void topicsListUpdated(const Topics& topics);
48 
49  virtual void notificationReceived(const Notifications& notifications);
50 
51  virtual void addTopicListListener(INotificationTopicListListenerPtr listener);
52 
53  virtual void removeTopicListListener(INotificationTopicListListenerPtr listener);
54 
55  virtual Topics getTopics();
56 
57  virtual void addNotificationListener(INotificationListenerPtr listener);
58 
59  virtual void addNotificationListener(const std::string& topidId, INotificationListenerPtr listener);
60 
61  virtual void removeNotificationListener(INotificationListenerPtr listener);
62 
63  virtual void removeNotificationListener(const std::string& topidId, INotificationListenerPtr listener);
64 
65  virtual void subscribeToTopic(const std::string& id, bool forceSync);
66 
67  virtual void subscribeToTopics(const std::list<std::string>& idList, bool forceSync);
68 
69  virtual void unsubscribeFromTopic(const std::string& id, bool forceSync);
70 
71  virtual void unsubscribeFromTopics(const std::list<std::string>& idList, bool forceSync);
72 
73  virtual void sync();
74 
78  virtual void setTransport(std::shared_ptr<NotificationTransport> transport);
79 private:
80  void updateSubscriptionInfo(const std::string& id, SubscriptionCommandType type);
81  void updateSubscriptionInfo(const SubscriptionCommands& newSubscriptions);
82 
83  const Topic& findTopic(const std::string& id);
84 
85  void notifyTopicUpdateSubscribers(const Topics& topics);
86  void notifyMandatoryNotificationSubscribers(const Notification& notification);
87  bool notifyOptionalNotificationSubscribers(const Notification& notification);
88 
89 private:
90  std::shared_ptr<NotificationTransport> transport_;
91  IKaaClientStateStoragePtr clientStatus_;
92 
93  std::unordered_map<std::string/*Topic ID*/, Topic> topics_;
94 
95  KAA_MUTEX_DECLARE(topicsGuard_);
96  KAA_MUTEX_DECLARE(optionalListenersGuard_);
97 
98  typedef KaaObservable<
99  void(const std::string& topicId,
100  const std::vector<std::uint8_t>& notification),
101  INotificationListenerPtr> NotificationObservable;
102 
103  typedef std::shared_ptr<NotificationObservable> NotificationObservablePtr;
104 
105  KaaObservable<void (const Topics& list), INotificationTopicListListenerPtr> topicListeners_;
106  NotificationObservable mandatoryListeners_;
107  std::unordered_map<std::string/*Topic ID*/, NotificationObservablePtr> optionalListeners_;
108 
109  SubscriptionCommands subscriptions_;
110  KAA_MUTEX_DECLARE(subscriptionsGuard_);
111 };
112 
113 } /* namespace kaa */
114 
115 #endif
116 
117 #endif /* DEFAULTNOTIFICATONMANAGER_HPP_ */
#define KAA_MUTEX_DECLARE(name)
Definition: KaaThread.hpp:71
std::shared_ptr< IKaaClientStateStorage > IKaaClientStateStoragePtr
std::list< SubscriptionCommand > SubscriptionCommands