FD.io VPP  v18.01.2-1-g9b554f3
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>, rc_t, WANT>
40 {
41 public:
42  /**
43  * Default constructor
44  */
46  : rpc_cmd<HW::item<bool>, rc_t, 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: ON
111  *
112  * Local Variables:
113  * eval: (c-set-style "mozilla")
114  * End:
115  */
116 
117 #endif
virtual void notify()=0
Notify the command that data from VPP has arrived and been stored.
virtual ~event_cmd()
Default destructor.
Definition: event_cmd.hpp:53
Definition: hw.hpp:33
vapi_error_e operator()(reg_t &dl)
Definition: event_cmd.hpp:83
Error codes that VPP will return during a HW write.
Definition: types.hpp:84
void flush()
flush/free all the events thus far reeived.
Definition: event_cmd.hpp:75
std::mutex m_mutex
Mutex protection for the events.
Definition: event_cmd.hpp:105
const_iterator begin()
Definition: event_cmd.hpp:64
C++ VPP API.
Class representing event registration - incoming events (messages) from vpp as a result of a subscrip...
Definition: vapi.hpp:56
A representation of the connection to VPP.
Definition: connection.hpp:33
HW::item< bool > & item()
return the HW item the command updates
Definition: rpc_cmd.hpp:64
A base class for all RPC commands to VPP.
Definition: rpc_cmd.hpp:38
vapi::Event_registration< EVENT >::resp_type event_t
Typedef for the event type.
Definition: event_cmd.hpp:58
vapi::Result_set< typename reg_t::resp_type >::const_iterator const_iterator
Definition: event_cmd.hpp:62
std::unique_ptr< vapi::Event_registration< EVENT > > m_reg
The VAPI event registration.
Definition: event_cmd.hpp:100
const_iterator end()
Definition: event_cmd.hpp:66
typename std::vector< Msg< M >, typename Msg< M >::Msg_allocator >::const_iterator const_iterator
Definition: vapi.hpp:720
vapi::Event_registration< EVENT > reg_t
Definition: event_cmd.hpp:59
success
Definition: vapi_common.h:27
#define bool
Definition: radix.c:95
The VPP Object Model (VOM) library.
Definition: acl_binding.cpp:19
typename M::shm_data_type resp_type
Definition: vapi.hpp:882
An Event command base class.
Definition: event_cmd.hpp:39
virtual void retire(connection &con)=0
Retire the command.
event_cmd(HW::item< bool > &b)
Default constructor.
Definition: event_cmd.hpp:45
vapi_error_e
Definition: vapi_common.h:25