GNU Radio Manual and C++ API Reference 3.7.14.0
The Free & Open Software Radio Ecosystem
rpcserver_thrift.h
Go to the documentation of this file.
1/* -*- c++ -*- */
2/*
3 * Copyright 2014,2015 Free Software Foundation, Inc.
4 *
5 * This file is part of GNU Radio
6 *
7 * GNU Radio is free software you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation either version 3, or (at your option)
10 * any later version.
11 *
12 * GNU Radio is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with GNU Radio see the file COPYING. If not, write to
19 * the Free Software Foundation, Inc., 51 Franklin Street,
20 * Boston, MA 02110-1301, USA.
21 */
22
23#ifndef RPCSERVER_THRIFT_H
24#define RPCSERVER_THRIFT_H
25
26#include "thrift/ControlPort.h"
27#include "thrift/gnuradio_types.h"
30#include <boost/format.hpp>
31#include <boost/thread/mutex.hpp>
32#include <iostream>
33#include <map>
34#include <sstream>
35#include <string>
36
37#define S(x) #x
38#define S_(x) S(x)
39#define S__LINE__ S_(__LINE__)
40
41class rpcserver_thrift : public virtual rpcserver_base, public GNURadio::ControlPortIf
42{
43public:
46
47 void registerConfigureCallback(const std::string& id,
48 const configureCallback_t callback);
49 void unregisterConfigureCallback(const std::string& id);
50
51 void registerQueryCallback(const std::string& id, const queryCallback_t callback);
52 void unregisterQueryCallback(const std::string& id);
53
54 void registerHandlerCallback(const std::string& id, const handlerCallback_t callback);
55 void unregisterHandlerCallback(const std::string& id);
56
57 void setKnobs(const GNURadio::KnobMap&);
58 void getKnobs(GNURadio::KnobMap&, const GNURadio::KnobIDList&);
59 void getRe(GNURadio::KnobMap&, const GNURadio::KnobIDList&);
60 void properties(GNURadio::KnobPropMap&, const GNURadio::KnobIDList& knobs);
61
62 /*!
63 * \brief Call this to post a message to the \p port for the block
64 * identified by \p alias.
65 *
66 * The message, \p msg, is passed as a serialized PMT that is then
67 * passed to the message handler function identified by \p port to
68 * the block identified by \p alias. The \p alias and \p port
69 * values are passed as serialized PMT symbols (see
70 * pmt::intern). The message is whatever PMT format is appropriate
71 * for the message handler function.
72 *
73 * To use this function, the message handler function must have
74 * been registered (most likely in setup_rpc) in the block during
75 * construction using rpcbasic_register_handler.
76 *
77 * \param alias The alias of the block, which is used to map to the
78 * real block through the global_block_registry. Passed in
79 * as a serialized PMT symbol.
80 * \param port The name of the message port. Passed in as a
81 * serialized PMT symbol.
82 * \param msg The actual message to pass to \p port. This is a
83 * serialized PMT where the PMT is whatever form appropriate
84 * for the message handler function.
85 */
86 void postMessage(const std::string& alias,
87 const std::string& port,
88 const std::string& msg);
89
90 virtual void shutdown();
91
92private:
93 boost::mutex d_callback_map_lock;
94
95 typedef std::map<std::string, configureCallback_t> ConfigureCallbackMap_t;
96 ConfigureCallbackMap_t d_setcallbackmap;
97
98 typedef std::map<std::string, queryCallback_t> QueryCallbackMap_t;
99 QueryCallbackMap_t d_getcallbackmap;
100
101 typedef std::map<std::string, handlerCallback_t> HandlerCallbackMap_t;
102 HandlerCallbackMap_t d_handlercallbackmap;
103
104 /*!
105 * \brief Manages calling the callback function for a message handler posting.
106 */
107 void set_h(const handlerCallback_t& _handlerCallback,
108 const priv_lvl_t& _cur_priv,
109 pmt::pmt_t port,
111 {
112 if (cur_priv <= _handlerCallback.priv) {
113 _handlerCallback.callback->post(port, msg);
114 } else {
115 std::cerr << "Message " << _handlerCallback.description
116 << " requires PRIVLVL <= " << _handlerCallback.priv
117 << " to set, currently at: " << cur_priv << std::endl;
118 }
119 }
120
121
122 template <typename T, typename TMap>
123 struct set_f : public std::unary_function<T, void> {
124 set_f(TMap& _setcallbackmap, const priv_lvl_t& _cur_priv)
125 : d_setcallbackmap(_setcallbackmap), cur_priv(_cur_priv)
126 {
127 ;
128 }
129
130 void operator()(const T& p)
131 {
132 ConfigureCallbackMap_t::const_iterator iter(d_setcallbackmap.find(p.first));
133 if (iter != d_setcallbackmap.end()) {
134 if (cur_priv <= iter->second.priv) {
135 (*iter->second.callback)
137 } else {
138 std::cerr << "Key " << p.first
139 << " requires PRIVLVL <= " << iter->second.priv
140 << " to set, currently at: " << cur_priv << std::endl;
141 }
142 } else {
143 throw apache::thrift::TApplicationException(__FILE__ " " S__LINE__);
144 }
145 }
146
147 TMap& d_setcallbackmap;
148 const priv_lvl_t& cur_priv;
149 };
150
151 template <typename T, typename TMap>
152 struct get_f : public std::unary_function<T, void> {
153 get_f(TMap& _getcallbackmap,
154 const priv_lvl_t& _cur_priv,
155 GNURadio::KnobMap& _outknobs)
156 : d_getcallbackmap(_getcallbackmap), cur_priv(_cur_priv), outknobs(_outknobs)
157 {
158 }
159
160 void operator()(const T& p)
161 {
162 QueryCallbackMap_t::const_iterator iter(d_getcallbackmap.find(p));
163 if (iter != d_getcallbackmap.end()) {
164 if (cur_priv <= iter->second.priv) {
165 outknobs[p] =
166 rpcpmtconverter::from_pmt((*iter->second.callback).retrieve());
167 } else {
168 std::cerr << "Key " << iter->first
169 << " requires PRIVLVL: <= " << iter->second.priv
170 << " to get, currently at: " << cur_priv << std::endl;
171 }
172 } else {
173 std::stringstream ss;
174 ss << "Ctrlport Key called with unregistered key (" << p << ")\n";
175 std::cerr << ss.str();
176 throw apache::thrift::TApplicationException(__FILE__ " " S__LINE__);
177 }
178 }
179
180 TMap& d_getcallbackmap;
181 const priv_lvl_t& cur_priv;
182 GNURadio::KnobMap& outknobs;
183 };
184
185 template <typename T, typename TMap, typename TKnobMap>
186 struct get_all_f : public std::unary_function<T, void> {
187 get_all_f(TMap& _getcallbackmap, const priv_lvl_t& _cur_priv, TKnobMap& _outknobs)
188 : d_getcallbackmap(_getcallbackmap), cur_priv(_cur_priv), outknobs(_outknobs)
189 {
190 ;
191 }
192
193 void operator()(const T& p)
194 {
195 if (cur_priv <= p.second.priv) {
196 outknobs[p.first] =
197 rpcpmtconverter::from_pmt(p.second.callback->retrieve());
198 } else {
199 std::cerr << "Key " << p.first << " requires PRIVLVL <= " << p.second.priv
200 << " to get, currently at: " << cur_priv << std::endl;
201 }
202 }
203
204 TMap& d_getcallbackmap;
205 const priv_lvl_t& cur_priv;
206 TKnobMap& outknobs;
207 };
208
209 template <typename T, typename TMap, typename TKnobMap>
210 struct properties_all_f : public std::unary_function<T, void> {
211 properties_all_f(QueryCallbackMap_t& _getcallbackmap,
212 const priv_lvl_t& _cur_priv,
213 GNURadio::KnobPropMap& _outknobs)
214 : d_getcallbackmap(_getcallbackmap), cur_priv(_cur_priv), outknobs(_outknobs)
215 {
216 ;
217 }
218
219 void operator()(const T& p)
220 {
221 if (cur_priv <= p.second.priv) {
222 GNURadio::KnobProp prop;
224 prop.units = p.second.units;
225 prop.description = p.second.description;
226 prop.min = rpcpmtconverter::from_pmt(p.second.min);
227 prop.max = rpcpmtconverter::from_pmt(p.second.max);
228 prop.display = static_cast<uint32_t>(p.second.display);
229 outknobs[p.first] = prop;
230 } else {
231 std::cerr << "Key " << p.first << " requires PRIVLVL <= " << p.second.priv
232 << " to get, currently at: " << cur_priv << std::endl;
233 }
234 }
235
236 TMap& d_getcallbackmap;
237 const priv_lvl_t& cur_priv;
238 TKnobMap& outknobs;
239 };
240
241 template <class T, typename TMap, typename TKnobMap>
242 struct properties_f : public std::unary_function<T, void> {
243 properties_f(TMap& _getcallbackmap,
244 const priv_lvl_t& _cur_priv,
245 TKnobMap& _outknobs)
246 : d_getcallbackmap(_getcallbackmap), cur_priv(_cur_priv), outknobs(_outknobs)
247 {
248 ;
249 }
250
251 void operator()(const T& p)
252 {
253 typename TMap::const_iterator iter(d_getcallbackmap.find(p));
254 if (iter != d_getcallbackmap.end()) {
255 if (cur_priv <= iter->second.priv) {
256 GNURadio::KnobProp prop;
258 prop.units = iter->second.units;
259 prop.description = iter->second.description;
260 prop.min = rpcpmtconverter::from_pmt(iter->second.min);
261 prop.max = rpcpmtconverter::from_pmt(iter->second.max);
262 prop.display = static_cast<uint32_t>(iter->second.display);
263 outknobs[p] = prop;
264 } else {
265 std::cerr << "Key " << iter->first
266 << " requires PRIVLVL: <= " << iter->second.priv
267 << " to get, currently at: " << cur_priv << std::endl;
268 }
269 } else {
270 throw apache::thrift::TApplicationException(__FILE__ " " S__LINE__);
271 }
272 }
273
274 TMap& d_getcallbackmap;
275 const priv_lvl_t& cur_priv;
276 TKnobMap& outknobs;
277 };
278};
279
280#endif /* RPCSERVER_THRIFT_H */
Definition: rpccallbackregister_base.h:95
Tsptr callback
Definition: rpccallbackregister_base.h:117
static To_PMT instance
Definition: rpcpmtconverters_thrift.h:89
Definition: rpcserver_base.h:29
priv_lvl_t cur_priv
Definition: rpcserver_base.h:51
Definition: rpcserver_thrift.h:42
void unregisterQueryCallback(const std::string &id)
void registerConfigureCallback(const std::string &id, const configureCallback_t callback)
virtual ~rpcserver_thrift()
void getRe(GNURadio::KnobMap &, const GNURadio::KnobIDList &)
void setKnobs(const GNURadio::KnobMap &)
void properties(GNURadio::KnobPropMap &, const GNURadio::KnobIDList &knobs)
void getKnobs(GNURadio::KnobMap &, const GNURadio::KnobIDList &)
void registerQueryCallback(const std::string &id, const queryCallback_t callback)
virtual void shutdown()
void postMessage(const std::string &alias, const std::string &port, const std::string &msg)
Call this to post a message to the port for the block identified by alias.
void unregisterHandlerCallback(const std::string &id)
void registerHandlerCallback(const std::string &id, const handlerCallback_t callback)
void unregisterConfigureCallback(const std::string &id)
static purpose_t msg
Definition: source_logger.h:39
boost::mutex mutex
Definition: thread.h:48
boost::intrusive_ptr< pmt_base > pmt_t
typedef for shared pointer (transparent reference counting). See http://www.boost....
Definition: pmt.h:56
GNURadio::Knob from_pmt(const pmt::pmt_t &knob)
#define PMT_NIL
Definition: pmt.h:103
priv_lvl_t
Definition: rpccallbackregister_base.h:46
@ KNOBDOUBLE
Definition: rpccallbackregister_base.h:53
#define S__LINE__
Definition: rpcserver_thrift.h:39
std::string description
Definition: rpccallbackregister_base.h:88
priv_lvl_t priv
Definition: rpccallbackregister_base.h:87