client-cpp  0.10.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 
75  {
77  }
78 
79  virtual void commit(TransactionIdPtr trxId, IKaaClientContext &context_);
80 
82  virtual void rollback(TransactionIdPtr trxId)
83  {
84  AbstractTransactable::rollback(trxId, context_);
85  }
86 private:
87  struct EventListenersInfo {
88  std::list<std::string> eventFQNs_;
89  IFetchEventListenersPtr listener_;
90  };
91 
92  void onEventFromServer(const std::string& eventClassFQN
93  , const std::vector<std::uint8_t>& data
94  , const std::string& source);
95 
96  void generateUniqueRequestId(std::string& requstId);
97 
98  void doSync();
99 
100 private:
101  IKaaClientContext &context_;
102 
103  std::set<IEventFamily*> eventFamilies_;
104  std::map<std::int32_t, Event> pendingEvents_;
105  KAA_MUTEX_MUTABLE_DECLARE(pendingEventsGuard_);
106 
107  std::int32_t currentEventIndex_;
108 
109  EventTransport * eventTransport_;
110 
111  std::map<std::int32_t/*request id*/, std::shared_ptr<EventListenersInfo> > eventListenersRequests_;
112  KAA_MUTEX_MUTABLE_DECLARE(eventListenersGuard_);
113 };
114 
115 } /* namespace kaa */
116 
117 #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)