client-cpp  0.6.3
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> releasePendingEvents();
65  virtual bool hasPendingEvents() const;
66 
67  virtual std::map<std::int32_t, std::list<std::string> > getPendingListenerRequests();
68  virtual bool hasPendingListenerRequests() const;
69 
70  virtual std::int32_t findEventListeners(const std::list<std::string>& eventFQNs, IFetchEventListeners* listener);
71 
72  virtual void setTransport(EventTransport *transport);
73 
74  virtual TransactionIdPtr beginTransaction()
75  {
77  }
78 
79  virtual void commit(TransactionIdPtr trxId);
80 
81  virtual void rollback(TransactionIdPtr trxId)
82  {
84  }
85 private:
86  struct EventListenersInfo {
87  std::list<std::string> eventFQNs_;
88  IFetchEventListeners* listener_;
89  };
90 
91  void onEventFromServer(const std::string& eventClassFQN
92  , const std::vector<std::uint8_t>& data
93  , const std::string& source);
94 
95  void generateUniqueRequestId(std::string& requstId);
96 private:
97  std::set<IEventFamily*> eventFamilies_;
98  std::list<Event> pendingEvents_;
99  KAA_MUTEX_MUTABLE_DECLARE(pendingEventsGuard_);
100 
101  std::int32_t eventSequenceNumber_;
102  KAA_MUTEX_DECLARE(sequenceGuard_);
103 
104  EventTransport * eventTransport_;
106 
107  std::map<std::int32_t/*request id*/, std::shared_ptr<EventListenersInfo> > eventListenersRequests_;
108  KAA_MUTEX_MUTABLE_DECLARE(eventListenersGuard_);
109 };
110 
111 } /* namespace kaa */
112 
113 #endif
114 
115 #endif /* EVENTMANAGER_HPP_ */
std::shared_ptr< TransactionId > TransactionIdPtr
#define KAA_MUTEX_DECLARE(name)
Definition: KaaThread.hpp:71
std::shared_ptr< IKaaClientStateStorage > IKaaClientStateStoragePtr
#define KAA_MUTEX_MUTABLE_DECLARE(name)
Definition: KaaThread.hpp:72
virtual TransactionIdPtr beginTransaction()
virtual void rollback(TransactionIdPtr trxId)