client-cpp  0.0.1-SNAPSHOT
EventManager.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 EVENTMANAGER_HPP_
18 #define EVENTMANAGER_HPP_
19 
20 #include <set>
21 #include <list>
22 
23 #include <boost/cstdint.hpp>
24 #include <boost/shared_ptr.hpp>
25 #include <boost/scoped_ptr.hpp>
26 #include <boost/thread/mutex.hpp>
27 
28 #include "kaa/gen/EndpointGen.hpp"
34 
35 namespace kaa {
36 
37 class IUpdateManager;
38 
41  , public IEventDataProcessor
42 {
43 public:
45  : eventTransport_(nullptr)
46  , status_(status)
47  {
48  eventSequenceNumber_ = status_->getEventSequenceNumber();
49  }
50 
51  virtual void registerEventFamily(IEventFamily* eventFamily);
52 
53  virtual void produceEvent(const std::string& fqn
54  , const std::vector<boost::uint8_t>& data
55  , const std::string& target);
56 
57  virtual void onEventsReceived(const EventSyncResponse::events_t& events);
58  virtual void onEventListenersReceived(const EventSyncResponse::eventListenersResponses_t& listeners);
59 
60  virtual std::list<Event> getPendingEvents();
61 
62  virtual std::map<std::string, std::list<std::string> > getPendingListenerRequests() {
63  std::map<std::string, std::list<std::string> > result;
64  for (const auto& idToFqnList : eventListenersRequests_) {
65  result.insert(std::make_pair(idToFqnList.first, idToFqnList.second->eventFQNs_));
66  }
67  return result;
68  }
69 
70  virtual std::string findEventListeners(const std::list<std::string>& eventFQNs, IFetchEventListeners* listener);
71 
72  virtual void setTransport(EventTransport *transport) {
73  eventTransport_ = transport;
74  if (eventTransport_ != nullptr && (!pendingEvents_.empty() || !eventListenersRequests_.empty())) {
75  eventTransport_->sync();
76  }
77  }
78 private:
79  struct EventListenersInfo {
80  std::list<std::string> eventFQNs_;
81  IFetchEventListeners* listener_;
82  };
83 
84  void onEventFromServer(const std::string& eventClassFQN
85  , const std::vector<boost::uint8_t>& data
86  , const std::string& source);
87 
88 
89  void generateUniqueRequestId(std::string& requstId);
90 private:
91  typedef boost::mutex mutex_type;
92  typedef boost::unique_lock<mutex_type> lock_type;
93 
94  std::set<IEventFamily*> eventFamilies_;
95  std::list<Event> pendingEvents_;
96  mutex_type pendingEventsGuard_;
97 
98  boost::int32_t eventSequenceNumber_;
99  mutex_type sequenceGuard_;
100 
101  EventTransport * eventTransport_;
103 
104  std::map<std::string/*request id*/, boost::shared_ptr<EventListenersInfo> > eventListenersRequests_;
105 };
106 
107 } /* namespace kaa */
108 
109 #endif /* EVENTMANAGER_HPP_ */
virtual void setTransport(EventTransport *transport)
virtual std::map< std::string, std::list< std::string > > getPendingListenerRequests()
virtual void produceEvent(const std::string &fqn, const std::vector< boost::uint8_t > &data, const std::string &target)
boost::shared_ptr< IKaaClientStateStorage > IKaaClientStateStoragePtr
virtual std::string findEventListeners(const std::list< std::string > &eventFQNs, IFetchEventListeners *listener)
EventManager(IKaaClientStateStoragePtr status)
virtual void registerEventFamily(IEventFamily *eventFamily)
virtual void onEventsReceived(const EventSyncResponse::events_t &events)
virtual std::list< Event > getPendingEvents()
virtual void onEventListenersReceived(const EventSyncResponse::eventListenersResponses_t &listeners)