OpenSDN source code
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
event_notifier.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017 Juniper Networks, Inc. All rights reserved.
3  */
4 
5 #include <cmn/agent_cmn.h>
6 #include <cmn/event_notifier.h>
7 
8 //Event registeration handle
10  key_(key), cb_(cb) {
11 }
12 
14 }
15 
16 //Event notify manager
18  work_queue_(agent->task_scheduler()->GetTaskId(kEventNotifierTask), 0,
19  boost::bind(&EventNotifier::Process, this, _1)),
20  agent_(agent) {
21  work_queue_.set_name("Event Notify Manager");
22 }
23 
25  work_queue_.Shutdown();
26  assert(map_.empty() == true);
27 }
28 
30  work_queue_.Enqueue(data);
31  return true;
32 }
33 
35  if (data->type_ == WorkQueueMessage::PUBLISHER) {
36  NotifyInternal(data->handle_ptr_->key());
37  } else if (data->type_ == WorkQueueMessage::REGISTER_SUBSCRIBER) {
38  RegisterSubscriberInternal(data->handle_ptr_);
39  } else if (data->type_ == WorkQueueMessage::DEREGISTER_SUBSCRIBER) {
40  DeRegisterSubscriberInternal(data->handle_ptr_);
41  }
42  return true;
43 }
44 
48  handle));
49  work_queue_.Enqueue(data);
50 }
51 
53  EventNotifier::NotifyMapIter map_it = map_.find(key);
54  if (map_it == map_.end()) {
55  //No subscribers
56  return;
57  }
58  EventNotifier::SubscribersList list = map_it->second;
59  for (EventNotifier::SubscribersListIter it = list.begin();
60  it != list.end(); it++) {
61  (*it)->Notify();
62  }
63 }
64 
67  Callback cb) {
68  EventNotifyHandle::Ptr handle(new EventNotifyHandle(KeyPtr(key), cb));
71  work_queue_.Enqueue(data);
72  return handle;
73 }
74 
75 void
77  //It is not ensured that same subscriber is not registered multiple times.
78  map_[handle->key()].push_back(handle);
79 }
80 
84  work_queue_.Enqueue(data);
85 }
86 
87 void
89  if (handle == NULL)
90  return;
91 
92  KeyPtr key_ptr = static_cast<KeyPtr>(handle->key());
93  EventNotifier::NotifyMapIter map_it = map_.find(key_ptr);
94  if (map_it == map_.end()) {
95  return;
96  }
97 
99  std::find(map_it->second.begin(), map_it->second.end(), handle);
100  if (it == map_it->second.end()) {
101  return;
102  }
103  map_it->second.erase(it);
104 
105  if (map_it->second.empty()) {
106  map_.erase(map_it);
107  }
108 
109  return;
110 }
111 
114  type_(type), handle_ptr_(ptr) {
115 }
116 
117 //EventNotifyKey
119  ref_count_ = 0;
120 }
121 
123 }
124 
125 bool EventNotifyKey::IsLess(const EventNotifyKey &rhs) const {
126  if (type() != rhs.type()) {
127  return (type() < rhs.type());
128  }
129  return false;
130 }
#define kEventNotifierTask
Definition: agent.h:356
EventNotifyHandle(KeyPtr key, Callback cb)
void NotifyInternal(KeyPtr key)
WorkQueueMessage(Type type, EventNotifyHandle::Ptr handle)
boost::function< bool(void)> Callback
EventNotifyKey(Type type)
virtual ~EventNotifier()
boost::function< bool(void)> Callback
void DeRegisterSubscriberInternal(EventNotifyHandle::Ptr ptr)
boost::shared_ptr< EventNotifyKey > KeyPtr
std::vector< EventNotifyHandle::Ptr > SubscribersList
uint8_t type
Definition: load_balance.h:109
Definition: agent.h:358
virtual ~EventNotifyKey()
WorkQueue< WorkQueueMessage::Ptr > work_queue_
virtual bool IsLess(const EventNotifyKey &rhs) const
void Notify(EventNotifyKey *key)
bool Process(WorkQueueMessage::Ptr data)
NotifyMap map_
tbb::atomic< int > ref_count_
Type type() const
NotifyMap::iterator NotifyMapIter
bool Enqueue(WorkQueueMessage::Ptr data)
boost::shared_ptr< EventNotifyHandle > Ptr
void DeregisterSubscriber(EventNotifyHandle::Ptr ptr)
SubscribersList::iterator SubscribersListIter
virtual ~EventNotifyHandle()
EventNotifyHandle::Ptr RegisterSubscriber(EventNotifyKey *key, Callback callback)
EventNotifier(Agent *agent)
void RegisterSubscriberInternal(EventNotifyHandle::Ptr ptr)
boost::shared_ptr< EventNotifyKey > KeyPtr
boost::shared_ptr< WorkQueueMessage > Ptr