client-cpp  0.9.0
EventManager.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 EVENTMANAGER_HPP_
18 #define EVENTMANAGER_HPP_
19 
20 
21 #include <set>
22 #include <list>
23 
24 #include <cstdint>
25 #include <memory>
26 
27 #include "kaa/KaaDefaults.hpp"
28 #include "kaa/KaaThread.hpp"
29 #include "kaa/gen/EndpointGen.hpp"
37 
38 namespace kaa {
39 
40 class IExecutorContext;
41 
44  , public IEventDataProcessor
45  , public AbstractTransactable<std::list<Event> >
46 {
47 public:
49  : context_(context), currentEventIndex_(0),eventTransport_(nullptr)
50  {
51  }
52 
53  virtual void registerEventFamily(IEventFamily* eventFamily);
54 
55  virtual void produceEvent(const std::string& fqn
56  , const std::vector<std::uint8_t>& data
57  , const std::string& target
58  , TransactionIdPtr trxId);
59 
60  virtual void onEventsReceived(const EventSyncResponse::events_t& events);
61  virtual void onEventListenersReceived(const EventSyncResponse::eventListenersResponses_t& listeners);
62 
63  virtual std::map<std::int32_t, Event> releasePendingEvents();
64  virtual bool hasPendingEvents() const;
65 
66  virtual std::map<std::int32_t, std::list<std::string> > getPendingListenerRequests();
67  virtual bool hasPendingListenerRequests() const;
68 
69  virtual std::int32_t findEventListeners(const std::list<std::string>& eventFQNs, IFetchEventListenersPtr listener);
70 
71  virtual void setTransport(EventTransport *transport);
72 
74  {
76  }
77 
78  virtual void commit(TransactionIdPtr trxId, IKaaClientContext &context_);
79 
80  virtual void rollback(TransactionIdPtr trxId)
81  {
82  AbstractTransactable::rollback(trxId, context_);
83  }
84 private:
85  struct EventListenersInfo {
86  std::list<std::string> eventFQNs_;
87  IFetchEventListenersPtr listener_;
88  };
89 
90  void onEventFromServer(const std::string& eventClassFQN
91  , const std::vector<std::uint8_t>& data
92  , const std::string& source);
93 
94  void generateUniqueRequestId(std::string& requstId);
95 
96  void doSync();
97 
98 private:
99  IKaaClientContext &context_;
100 
101  std::set<IEventFamily*> eventFamilies_;
102  std::map<std::int32_t, Event> pendingEvents_;
103  KAA_MUTEX_MUTABLE_DECLARE(pendingEventsGuard_);
104 
105  std::int32_t currentEventIndex_;
106 
107  EventTransport * eventTransport_;
108 
109  std::map<std::int32_t/*request id*/, std::shared_ptr<EventListenersInfo> > eventListenersRequests_;
110  KAA_MUTEX_MUTABLE_DECLARE(eventListenersGuard_);
111 };
112 
113 } /* namespace kaa */
114 
115 #endif /* EVENTMANAGER_HPP_ */
virtual void setTransport(EventTransport *transport)
virtual std::map< std::int32_t, std::list< std::string > > getPendingListenerRequests()
virtual bool hasPendingListenerRequests() const
virtual void produceEvent(const std::string &fqn, const std::vector< std::uint8_t > &data, const std::string &target, TransactionIdPtr trxId)
std::shared_ptr< TransactionId > TransactionIdPtr
virtual TransactionIdPtr beginTransaction(IKaaClientContext &context_)
virtual void commit(TransactionIdPtr trxId, IKaaClientContext &context_)
virtual std::map< std::int32_t, Event > releasePendingEvents()
virtual void rollback(TransactionIdPtr trxId, IKaaClientContext &context_)
virtual void registerEventFamily(IEventFamily *eventFamily)
virtual void onEventsReceived(const EventSyncResponse::events_t &events)
virtual std::int32_t findEventListeners(const std::list< std::string > &eventFQNs, IFetchEventListenersPtr listener)
EventManager(IKaaClientContext &context)
virtual TransactionIdPtr beginTransaction()
std::shared_ptr< IFetchEventListeners > IFetchEventListenersPtr
virtual bool hasPendingEvents() const
virtual void rollback(TransactionIdPtr trxId)
virtual void onEventListenersReceived(const EventSyncResponse::eventListenersResponses_t &listeners)