VOM: support for pipes
[vpp.git] / extras / vom / vom / event_cmd.hpp
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    */
45   event_cmd(HW::item<bool>& b)
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    */
58   typedef typename vapi::Event_registration<EVENT>::resp_type event_t;
59   typedef typename vapi::Event_registration<EVENT> reg_t;
60
61   typedef typename vapi::Result_set<typename reg_t::resp_type>::const_iterator
62     const_iterator;
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
83   vapi_error_e operator()(reg_t& dl)
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