Red Hat Data Grid HotRod C++ Client  7.2.0
CacheClientListener.h
Go to the documentation of this file.
1 /*
2  * ClientListener.h
3  *
4  * Created on: Aug 3, 2016
5  * Author: rigazilla
6  */
7 
8 #ifndef INCLUDE_INFINISPAN_HOTROD_CACHECLIENTLISTENER_H_
9 #define INCLUDE_INFINISPAN_HOTROD_CACHECLIENTLISTENER_H_
10 
15 #include <vector>
16 #include <list>
17 #include <functional>
18 
19 using namespace infinispan::hotrod;
20 
21 namespace infinispan {
22 namespace hotrod {
23 
24 namespace protocol {
25 class Codec20;
26 }
27 template <class K, class V>
29 
30 namespace event {
31 
32 template <class K, class V>
34 {
35 public:
36 
37  static const unsigned char INTEREST_FLAG_CREATED = 0x01;
38  static const unsigned char INTEREST_FLAG_MODIFIED = 0x02;
39  static const unsigned char INTEREST_FLAG_REMOVED = 0x04;
40  static const unsigned char INTEREST_FLAG_EXPIRED = 0x08;
41  static const unsigned char INTEREST_FLAG_ALL = 0x0f;
42 
43  RemoteCacheBase& cache;
44 
45  CacheClientListener(RemoteCache<K,V>& cache) : cache((RemoteCacheBase&)cache) {};
46  void add_listener(std::function<void(ClientCacheEntryCreatedEvent<K>)> callback) {
47  interestFlag|=INTEREST_FLAG_CREATED;
48  createdCallbacks.push_back(callback);
49  }
50  void add_listener(std::function<void(ClientCacheEntryExpiredEvent<K>)> callback) {
51  interestFlag|=INTEREST_FLAG_EXPIRED;
52  expiredCallbacks.push_back(callback);
53  }
54  void add_listener(std::function<void(ClientCacheEntryModifiedEvent<K>)> callback) {
55  interestFlag|=INTEREST_FLAG_MODIFIED;
56  modifiedCallbacks.push_back(callback);
57  }
58  void add_listener(std::function<void(ClientCacheEntryRemovedEvent<K>)> callback) {
59  interestFlag|=INTEREST_FLAG_REMOVED;
60  removedCallbacks.push_back(callback);
61  }
62  void add_listener(std::function<void(ClientCacheEntryCustomEvent)> callback) {
63  interestFlag|=INTEREST_FLAG_ALL;
64  customCallbacks.push_back(callback);
65  }
66 
67  virtual void processEvent(ClientCacheEntryCreatedEvent<std::vector<char>> marshEv, std::vector<char >listId, uint8_t isCustom) const
68  {
69  K* key= (K*)cache.baseKeyUnmarshall(marshEv.getKey());
70  ClientCacheEntryCreatedEvent<K> typedEvent(*key, marshEv.getVersion(), marshEv.isCommandRetried());
71  delete key;
72  for (auto callable: createdCallbacks)
73  {
74  callable(typedEvent);
75  }
76  }
77 
78  virtual void processEvent(ClientCacheEntryModifiedEvent<std::vector<char>> marshEv, std::vector<char >listId, uint8_t isCustom) const
79  {
80  K* key= (K*)cache.baseKeyUnmarshall(marshEv.getKey());
81  ClientCacheEntryModifiedEvent<K> typedEvent(*key, marshEv.getVersion(), marshEv.isCommandRetried());
82  delete key;
83  for (auto callable: modifiedCallbacks)
84  {
85  callable(typedEvent);
86  }
87  }
88 
89  virtual void processEvent(ClientCacheEntryRemovedEvent<std::vector<char>> marshEv, std::vector<char >listId, uint8_t isCustom) const
90  {
91  K* key= (K*)cache.baseKeyUnmarshall(marshEv.getKey());
92  ClientCacheEntryRemovedEvent<K> typedEvent(*key, marshEv.isCommandRetried());
93  delete key;
94  for (auto callable: removedCallbacks)
95  {
96  callable(typedEvent);
97  }
98  }
99 
100  virtual void processEvent(ClientCacheEntryExpiredEvent<std::vector<char>> marshEv, std::vector<char >listId, uint8_t isCustom) const
101  {
102  K* key= (K*)cache.baseKeyUnmarshall(marshEv.getKey());
103  ClientCacheEntryExpiredEvent<K> typedEvent(*key);
104  delete key;
105  for (auto callable: expiredCallbacks)
106  {
107  callable(typedEvent);
108  }
109  }
110 
111  virtual void processEvent(ClientCacheEntryCustomEvent ev, std::vector<char >listId, uint8_t isCustom) const
112  {
113  for (auto callable: customCallbacks)
114  {
115  callable(ev);
116  }
117  }
118 private:
119  std::list<std::function<void(ClientCacheEntryCreatedEvent<K>)>> createdCallbacks;
120  std::list<std::function<void(ClientCacheEntryExpiredEvent<K>)>> expiredCallbacks;
121  std::list<std::function<void(ClientCacheEntryModifiedEvent<K>)>> modifiedCallbacks;
122  std::list<std::function<void(ClientCacheEntryRemovedEvent<K>)>> removedCallbacks;
123  std::list<std::function<void(ClientCacheEntryCustomEvent)>> customCallbacks;
124 };
125 
126 }}}
127 
128 
129 #endif /* INCLUDE_INFINISPAN_HOTROD_CACHECLIENTLISTENER_H_ */
virtual void processEvent(ClientCacheEntryExpiredEvent< std::vector< char >> marshEv, std::vector< char >listId, uint8_t isCustom) const
Definition: CacheClientListener.h:100
virtual void processEvent(ClientCacheEntryRemovedEvent< std::vector< char >> marshEv, std::vector< char >listId, uint8_t isCustom) const
Definition: CacheClientListener.h:89
void add_listener(std::function< void(ClientCacheEntryRemovedEvent< K >)> callback)
Definition: CacheClientListener.h:58
Definition: CacheClientListener.h:28
RemoteCacheBase & cache
Definition: CacheClientListener.h:43
Definition: AuthenticationConfiguration.h:10
void add_listener(std::function< void(ClientCacheEntryModifiedEvent< K >)> callback)
Definition: CacheClientListener.h:54
virtual void processEvent(ClientCacheEntryCreatedEvent< std::vector< char >> marshEv, std::vector< char >listId, uint8_t isCustom) const
Definition: CacheClientListener.h:67
virtual void processEvent(ClientCacheEntryModifiedEvent< std::vector< char >> marshEv, std::vector< char >listId, uint8_t isCustom) const
Definition: CacheClientListener.h:78
Definition: ClientListener.h:35
virtual void processEvent(ClientCacheEntryCustomEvent ev, std::vector< char >listId, uint8_t isCustom) const
Definition: CacheClientListener.h:111
Definition: AuthenticationConfiguration.h:11
Definition: CacheClientListener.h:33
void add_listener(std::function< void(ClientCacheEntryExpiredEvent< K >)> callback)
Definition: CacheClientListener.h:50
void add_listener(std::function< void(ClientCacheEntryCreatedEvent< K >)> callback)
Definition: CacheClientListener.h:46
void add_listener(std::function< void(ClientCacheEntryCustomEvent)> callback)
Definition: CacheClientListener.h:62
CacheClientListener(RemoteCache< K, V > &cache)
Definition: CacheClientListener.h:45