FD.io VPP  v21.06-3-gbb25fbf28
Vector Packet Processing
event_cmd.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef __VOM_EVENT_CMD_H__
17 #define __VOM_EVENT_CMD_H__
18 
19 #include <mutex>
20 
21 #include "vom/rpc_cmd.hpp"
22 
23 #include <vapi/vapi.hpp>
24 
25 namespace VOM {
26 /**
27  * An Event command base class.
28  * Events are one of the sub-set of command type to VPP.
29  * A client performs a one time 'registration/subsription' to VPP for the
30  * event in question and then is notified asynchronously when those events
31  * occur.
32  * The model here then is that the lifetime of the event command represensts
33  * the during of the clients subscription. When the command is 'issued' the
34  * subscription begins, when it is 'retired' the subscription ends. For the
35  * subscription duration the client will be notified as events are recieved.
36  * The client can then 'pop' these events from this command object.
37  */
38 template <typename WANT, typename EVENT>
39 class event_cmd : public rpc_cmd<HW::item<bool>, WANT>
40 {
41 public:
42  /**
43  * Default constructor
44  */
46  : rpc_cmd<HW::item<bool>, WANT>(b)
47  {
48  }
49 
50  /**
51  * Default destructor
52  */
53  virtual ~event_cmd() {}
54 
55  /**
56  * Typedef for the event type
57  */
60 
63 
64  const_iterator begin() { return (m_reg->get_result_set().begin()); }
65 
66  const_iterator end() { return (m_reg->get_result_set().end()); }
67 
68  void lock() { m_mutex.lock(); }
69  void unlock() { m_mutex.unlock(); }
70 
71  /**
72  * flush/free all the events thus far reeived.
73  * Call with the lock held!
74  */
75  void flush() { m_reg->get_result_set().free_all_responses(); }
76 
77  /**
78  * Retire the command. This is only appropriate for Event Commands
79  * As they persist until retired.
80  */
81  virtual void retire(connection& con) = 0;
82 
84  {
85  notify();
86 
87  return (VAPI_OK);
88  }
89 
90 protected:
91  /**
92  * Notify the command that data from VPP has arrived and been stored.
93  * The command should now inform its clients/listeners.
94  */
95  virtual void notify() = 0;
96 
97  /**
98  * The VAPI event registration
99  */
100  std::unique_ptr<vapi::Event_registration<EVENT>> m_reg;
101 
102  /**
103  * Mutex protection for the events
104  */
105  std::mutex m_mutex;
106 };
107 };
108 
109 /*
110  * fd.io coding-style-patch-verification: OFF
111  *
112  * Local Variables:
113  * eval: (c-set-style "mozilla")
114  * End:
115  */
116 
117 #endif
vapi_error_e
vapi_error_e
Definition: vapi_common.h:25
VOM::HW::item< bool >
VOM
The VPP Object Model (VOM) library.
Definition: acl_binding.cpp:19
vapi::Event_registration
Class representing event registration - incoming events (messages) from vpp as a result of a subscrip...
Definition: vapi.hpp:56
VOM::event_cmd::reg_t
vapi::Event_registration< EVENT > reg_t
Definition: event_cmd.hpp:59
VOM::rpc_cmd
A base class for all RPC commands to VPP.
Definition: rpc_cmd.hpp:40
VOM::event_cmd::operator()
vapi_error_e operator()(reg_t &dl)
Definition: event_cmd.hpp:83
con
Connection con
Definition: vapi_cpp_test.cpp:56
VAPI_OK
@ VAPI_OK
success
Definition: vapi_common.h:27
vapi::Event_registration::resp_type
typename M::shm_data_type resp_type
Definition: vapi.hpp:885
VOM::event_cmd
An Event command base class.
Definition: event_cmd.hpp:39
VOM::event_cmd::lock
void lock()
Definition: event_cmd.hpp:68
VOM::connection
A representation of the connection to VPP.
Definition: connection.hpp:33
VOM::event_cmd::unlock
void unlock()
Definition: event_cmd.hpp:69
vapi::Result_set::const_iterator
typename std::vector< Msg< M >, typename Msg< M >::Msg_allocator >::const_iterator const_iterator
Definition: vapi.hpp:723
VOM::event_cmd::retire
virtual void retire(connection &con)=0
Retire the command.
VOM::event_cmd::event_t
vapi::Event_registration< EVENT >::resp_type event_t
Typedef for the event type.
Definition: event_cmd.hpp:58
VOM::event_cmd::~event_cmd
virtual ~event_cmd()
Default destructor.
Definition: event_cmd.hpp:53
VOM::event_cmd::m_reg
std::unique_ptr< vapi::Event_registration< EVENT > > m_reg
The VAPI event registration.
Definition: event_cmd.hpp:100
VOM::event_cmd::end
const_iterator end()
Definition: event_cmd.hpp:66
VOM::rpc_cmd< HW::item< bool >, WANT >::item
HW::item< bool > & item()
return the HW item the command updates
Definition: rpc_cmd.hpp:66
vapi.hpp
C++ VPP API.
VOM::HW
Definition: hw.hpp:34
VOM::event_cmd::begin
const_iterator begin()
Definition: event_cmd.hpp:64
VOM::event_cmd::flush
void flush()
flush/free all the events thus far reeived.
Definition: event_cmd.hpp:75
bool
#define bool
Definition: radix.c:95
b
vlib_buffer_t ** b
Definition: nat44_ei_out2in.c:717
VOM::event_cmd::m_mutex
std::mutex m_mutex
Mutex protection for the events.
Definition: event_cmd.hpp:105
VOM::event_cmd::notify
virtual void notify()=0
Notify the command that data from VPP has arrived and been stored.
VOM::event_cmd::event_cmd
event_cmd(HW::item< bool > &b)
Default constructor.
Definition: event_cmd.hpp:45
rpc_cmd.hpp
VOM::event_cmd::const_iterator
vapi::Result_set< typename reg_t::resp_type >::const_iterator const_iterator
Definition: event_cmd.hpp:62