VOM: support for pipes
[vpp.git] / extras / vom / vom / rpc_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_RPC_CMD_H__
17 #define __VOM_RPC_CMD_H__
18
19 #include <future>
20
21 #include "vom/cmd.hpp"
22 #include "vom/logger.hpp"
23
24 namespace VOM {
25 /**
26  * A base class for all RPC commands to VPP.
27  *  RPC commands are one of the sub-set of command types to VPP
28  * that modify/create state in VPP and thus return an error code.
29  * Commands are issued in one thread context, but read in another. The
30  * command has an associated std::promise that is met by the RX thread.
31  * this allows the sender, which waits on the promise's future, to
32  * experience a synchronous command.
33  *
34  * The command is templatised on the type of the HW::item to be set by
35  * the command, and the data returned in the promise,
36  */
37 template <typename HWITEM, typename MSG>
38 class rpc_cmd : public cmd
39 {
40 public:
41   /**
42    * convenient typedef
43    */
44   typedef MSG msg_t;
45
46   /**
47    * Constructor taking the HW item that will be updated by the command
48    */
49   rpc_cmd(HWITEM& item)
50     : cmd()
51     , m_hw_item(item)
52     , m_promise()
53   {
54   }
55
56   /**
57    * Desructor
58    */
59   virtual ~rpc_cmd() {}
60
61   /**
62    * return the HW item the command updates
63    */
64   HWITEM& item() { return m_hw_item; }
65
66   /**
67    * return the const HW item the command updates
68    */
69   const HWITEM& item() const { return m_hw_item; }
70
71   /**
72    * Fulfill the commands promise. Called from the RX thread
73    */
74   void fulfill(const HWITEM& d) { m_promise.set_value(d); }
75
76   /**
77    * Wait on the commands promise. i.e. block on the completion
78    * of the command.
79    */
80   rc_t wait()
81   {
82     std::future_status status;
83     std::future<HWITEM> result;
84
85     result = m_promise.get_future();
86     status = result.wait_for(std::chrono::seconds(5));
87
88     if (status != std::future_status::ready) {
89       m_hw_item.set(rc_t::TIMEOUT);
90     } else {
91       m_hw_item = result.get();
92     }
93
94     return (m_hw_item.rc());
95   }
96
97   /**
98    * Called by the HW Command Q when it is disabled to indicate the
99    * command can be considered successful without issuing it to HW
100    */
101   virtual void succeeded()
102   {
103     m_hw_item.set(rc_t::OK);
104     VOM_LOG(log_level_t::DEBUG) << to_string();
105   }
106
107   /**
108    * call operator used as a callback by VAPI when the reply is available
109    */
110   virtual vapi_error_e operator()(MSG& reply)
111   {
112     HWITEM hi = m_hw_item;
113     int retval = reply.get_response().get_payload().retval;
114     VOM_LOG(log_level_t::DEBUG) << to_string() << " " << retval;
115
116     /* set a temporary value in this callback thread */
117     hi.set(rc_t::from_vpp_retval(retval));
118     fulfill(hi);
119
120     return (VAPI_OK);
121   }
122
123   /**
124    * Retire/cancel a long running command
125    */
126   virtual void retire(connection& con) {}
127
128 protected:
129   /**
130    * A reference to an object's HW::item that the command will update
131    */
132   HWITEM& m_hw_item;
133
134   /**
135    * The promise that implements the synchronous issue
136    */
137   std::promise<HWITEM> m_promise;
138 };
139 };
140
141 /*
142  * fd.io coding-style-patch-verification: ON
143  *
144  * Local Variables:
145  * eval: (c-set-style "mozilla")
146  * End:
147  */
148
149 #endif