client-cpp  0.6.1
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 #ifdef KAA_USE_EVENTS
21 
22 
23 #include <set>
24 #include <list>
25 
26 #include <cstdint>
27 #include <memory>
28 
29 #include "kaa/KaaDefaults.hpp"
30 #include "kaa/KaaThread.hpp"
31 #include "kaa/gen/EndpointGen.hpp"
38 
39 namespace kaa {
40 
41 class EventManager : public IEventManager
42  , public IEventListenersResolver
43  , public IEventDataProcessor
44  , public AbstractTransactable<std::list<Event> >
45 {
46 public:
47  EventManager(IKaaClientStateStoragePtr status)
48  : eventTransport_(nullptr)
49  , status_(status)
50  {
51  eventSequenceNumber_ = status_->getEventSequenceNumber();
52  }
53 
54  virtual void registerEventFamily(IEventFamily* eventFamily);
55 
56  virtual void produceEvent(const std::string& fqn
57  , const std::vector<std::uint8_t>& data
58  , const std::string& target
59  , TransactionIdPtr trxId);
60 
61  virtual void onEventsReceived(const EventSyncResponse::events_t& events);
62  virtual void onEventListenersReceived(const EventSyncResponse::eventListenersResponses_t& listeners);
63 
64  virtual std::list<Event> getPendingEvents();
65 
66  virtual std::map<std::string, std::list<std::string> > getPendingListenerRequests() {
67  std::map<std::string, std::list<std::string> > result;
68  for (const auto& idToFqnList : eventListenersRequests_) {
69  result.insert(std::make_pair(idToFqnList.first, idToFqnList.second->eventFQNs_));
70  }
71  return result;
72  }
73 
74  virtual std::string findEventListeners(const std::list<std::string>& eventFQNs, IFetchEventListeners* listener);
75 
76  virtual void setTransport(EventTransport *transport) {
77  eventTransport_ = transport;
78  if (eventTransport_ != nullptr && (!pendingEvents_.empty() || !eventListenersRequests_.empty())) {
79  eventTransport_->sync();
80  }
81  }
82 
83  virtual TransactionIdPtr beginTransaction() {
85  }
86 
87  virtual void commit(TransactionIdPtr trxId);
88 
89  virtual void rollback(TransactionIdPtr trxId) {
91  }
92 private:
93  struct EventListenersInfo {
94  std::list<std::string> eventFQNs_;
95  IFetchEventListeners* listener_;
96  };
97 
98  void onEventFromServer(const std::string& eventClassFQN
99  , const std::vector<std::uint8_t>& data
100  , const std::string& source);
101 
102  void generateUniqueRequestId(std::string& requstId);
103 private:
104  std::set<IEventFamily*> eventFamilies_;
105  std::list<Event> pendingEvents_;
106  KAA_MUTEX_DECLARE(pendingEventsGuard_);
107 
108  std::int32_t eventSequenceNumber_;
109  KAA_MUTEX_DECLARE(sequenceGuard_);
110 
111  EventTransport * eventTransport_;
113 
114  std::map<std::string/*request id*/, std::shared_ptr<EventListenersInfo> > eventListenersRequests_;
115 };
116 
117 } /* namespace kaa */
118 
119 #endif
120 
121 #endif /* EVENTMANAGER_HPP_ */
std::shared_ptr< TransactionId > TransactionIdPtr
#define KAA_MUTEX_DECLARE(name)
Definition: KaaThread.hpp:71
std::shared_ptr< IKaaClientStateStorage > IKaaClientStateStoragePtr
virtual TransactionIdPtr beginTransaction()
virtual void rollback(TransactionIdPtr trxId)