GNU Radio Manual and C++ API Reference  3.7.8
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 
28 #include <string>
29 #include <iostream>
30 #include <sstream>
31 #include <map>
32 #include "thrift/ControlPort.h"
33 #include "thrift/gnuradio_types.h"
34 #include <boost/format.hpp>
35 #include <boost/thread/mutex.hpp>
36 
37 #define S(x) #x
38 #define S_(x) S(x)
39 #define S__LINE__ S_(__LINE__)
40 
41 class rpcserver_thrift : public virtual rpcserver_base, public GNURadio::ControlPortIf
42 {
43 public:
45  virtual ~rpcserver_thrift();
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,
52  const queryCallback_t callback);
53  void unregisterQueryCallback(const std::string &id);
54 
55  void setKnobs(const GNURadio::KnobMap&);
56  void getKnobs(GNURadio::KnobMap&,
57  const GNURadio::KnobIDList&);
58  void getRe(GNURadio::KnobMap&,
59  const GNURadio::KnobIDList&);
60  void properties(GNURadio::KnobPropMap&,
61  const GNURadio::KnobIDList& knobs);
62  virtual void shutdown();
63 
64  private:
65  boost::mutex d_callback_map_lock;
66 
67  typedef std::map<std::string, configureCallback_t> ConfigureCallbackMap_t;
68  ConfigureCallbackMap_t d_setcallbackmap;
69 
70  typedef std::map<std::string, queryCallback_t> QueryCallbackMap_t;
71  QueryCallbackMap_t d_getcallbackmap;
72 
73  template<typename T, typename TMap> struct set_f
74  : public std::unary_function<T,void>
75  {
76  set_f(TMap &_setcallbackmap, const priv_lvl_t &_cur_priv)
77  : d_setcallbackmap(_setcallbackmap), cur_priv(_cur_priv)
78  {
79  ;
80  }
81 
82  void operator()(const T& p)
83  {
84  ConfigureCallbackMap_t::const_iterator iter(d_setcallbackmap.find(p.first));
85  if(iter != d_setcallbackmap.end()) {
86  if(cur_priv <= iter->second.priv) {
87  (*iter->second.callback).post(pmt::PMT_NIL, rpcpmtconverter::To_PMT::instance(p.second));
88  }
89  else {
90  std::cout << "Key " << p.first << " requires PRIVLVL <= "
91  << iter->second.priv << " to set, currently at: "
92  << cur_priv << std::endl;
93  }
94  }
95  else {
96  throw apache::thrift::TApplicationException(__FILE__ " " S__LINE__);
97  }
98  }
99 
100  TMap& d_setcallbackmap;
101  const priv_lvl_t& cur_priv;
102  };
103 
104  template<typename T, typename TMap>
105  struct get_f : public std::unary_function<T,void>
106  {
107  get_f(TMap &_getcallbackmap, const priv_lvl_t &_cur_priv, GNURadio::KnobMap &_outknobs) :
108  d_getcallbackmap(_getcallbackmap), cur_priv(_cur_priv), outknobs(_outknobs)
109  {}
110 
111  void operator()(const T& p)
112  {
113  QueryCallbackMap_t::const_iterator iter(d_getcallbackmap.find(p));
114  if(iter != d_getcallbackmap.end()) {
115  if(cur_priv <= iter->second.priv) {
116  outknobs[p] = rpcpmtconverter::from_pmt((*iter->second.callback).retrieve());
117  }
118  else {
119  std::cout << "Key " << iter->first << " requires PRIVLVL: <= "
120  << iter->second.priv << " to get, currently at: "
121  << cur_priv << std::endl;
122  }
123  }
124  else {
125  std::stringstream ss;
126  ss << "Ctrlport Key called with unregistered key (" << p << ")\n";
127  std::cout << ss.str();
128  throw apache::thrift::TApplicationException(__FILE__ " " S__LINE__);
129  }
130  }
131 
132  TMap& d_getcallbackmap;
133  const priv_lvl_t& cur_priv;
134  GNURadio::KnobMap& outknobs;
135  };
136 
137  template<typename T, typename TMap, typename TKnobMap>
138  struct get_all_f : public std::unary_function<T,void>
139  {
140  get_all_f(TMap &_getcallbackmap, const priv_lvl_t &_cur_priv, TKnobMap &_outknobs) :
141  d_getcallbackmap(_getcallbackmap), cur_priv(_cur_priv), outknobs(_outknobs)
142  {;}
143 
144  void operator()(const T& p)
145  {
146  if(cur_priv <= p.second.priv) {
147  outknobs[p.first] = rpcpmtconverter::from_pmt(p.second.callback->retrieve());
148  }
149  else {
150  std::cout << "Key " << p.first << " requires PRIVLVL <= "
151  << p.second.priv << " to get, currently at: "
152  << cur_priv << std::endl;
153  }
154  }
155 
156  TMap& d_getcallbackmap;
157  const priv_lvl_t& cur_priv;
158  TKnobMap& outknobs;
159  };
160 
161  template<typename T, typename TMap, typename TKnobMap>
162  struct properties_all_f : public std::unary_function<T,void>
163  {
164  properties_all_f(QueryCallbackMap_t &_getcallbackmap,
165  const priv_lvl_t &_cur_priv,
166  GNURadio::KnobPropMap &_outknobs)
167  : d_getcallbackmap(_getcallbackmap),
168  cur_priv(_cur_priv),
169  outknobs(_outknobs)
170  {;}
171 
172  void operator()(const T& p)
173  {
174  if(cur_priv <= p.second.priv) {
175  GNURadio::KnobProp prop;
176  prop.type = GNURadio::KnobType::KNOBDOUBLE;
177  prop.units = p.second.units;
178  prop.description = p.second.description;
179  prop.min = rpcpmtconverter::from_pmt(p.second.min);
180  prop.max = rpcpmtconverter::from_pmt(p.second.max);
181  prop.display = static_cast<uint32_t>(p.second.display);
182  outknobs[p.first] = prop;
183  }
184  else {
185  std::cout << "Key " << p.first << " requires PRIVLVL <= "
186  << p.second.priv << " to get, currently at: "
187  << cur_priv << std::endl;
188  }
189  }
190 
191  TMap& d_getcallbackmap;
192  const priv_lvl_t& cur_priv;
193  TKnobMap& outknobs;
194  };
195 
196  template<class T, typename TMap, typename TKnobMap>
197  struct properties_f : public std::unary_function<T,void>
198  {
199  properties_f(TMap &_getcallbackmap, const priv_lvl_t &_cur_priv, TKnobMap &_outknobs) :
200  d_getcallbackmap(_getcallbackmap), cur_priv(_cur_priv), outknobs(_outknobs)
201  {;}
202 
203  void operator()(const T& p)
204  {
205  typename TMap::const_iterator iter(d_getcallbackmap.find(p));
206  if(iter != d_getcallbackmap.end()) {
207  if(cur_priv <= iter->second.priv) {
208  GNURadio::KnobProp prop;
209  prop.type = GNURadio::KnobType::KNOBDOUBLE;
210  prop.units = iter->second.units;
211  prop.description = iter->second.description;
212  prop.min = rpcpmtconverter::from_pmt(iter->second.min);
213  prop.max = rpcpmtconverter::from_pmt(iter->second.max);
214  prop.display = static_cast<uint32_t>(iter->second.display);
215  outknobs[p] = prop;
216  }
217  else {
218  std::cout << "Key " << iter->first << " requires PRIVLVL: <= "
219  << iter->second.priv << " to get, currently at: " << cur_priv << std::endl;
220  }
221  }
222  else {
223  throw apache::thrift::TApplicationException(__FILE__ " " S__LINE__);
224  }
225  }
226 
227  TMap& d_getcallbackmap;
228  const priv_lvl_t& cur_priv;
229  TKnobMap& outknobs;
230  };
231 };
232 
233 #endif /* RPCSERVER_THRIFT_H */
Definition: rpccallbackregister_base.h:80
void registerQueryCallback(const std::string &id, const queryCallback_t callback)
void properties(GNURadio::KnobPropMap &, const GNURadio::KnobIDList &knobs)
priv_lvl_t
Definition: rpccallbackregister_base.h:46
void registerConfigureCallback(const std::string &id, const configureCallback_t callback)
void getKnobs(GNURadio::KnobMap &, const GNURadio::KnobIDList &)
Definition: rpccallbackregister_base.h:54
callback_t< gr::messages::msg_accepter, gr::messages::msg_accepter_sptr > configureCallback_t
Definition: rpccallbackregister_base.h:100
void getRe(GNURadio::KnobMap &, const GNURadio::KnobIDList &)
void unregisterConfigureCallback(const std::string &id)
#define PMT_NIL
Definition: pmt.h:103
Definition: rpcserver_base.h:28
#define S__LINE__
Definition: rpcserver_thrift.h:39
static To_PMT instance
Definition: rpcpmtconverters_thrift.h:59
void unregisterQueryCallback(const std::string &id)
boost::mutex mutex
Definition: thread.h:46
Definition: rpcserver_thrift.h:41
void setKnobs(const GNURadio::KnobMap &)
GNURadio::Knob from_pmt(const pmt::pmt_t &knob)
priv_lvl_t cur_priv
Definition: rpcserver_base.h:42
virtual void shutdown()
virtual ~rpcserver_thrift()