GNU Radio 3.7.1 C++ API
rpcserver_ice.h
Go to the documentation of this file.
00001 /* -*- c++ -*- */
00002 /* 
00003  * Copyright 2012 Free Software Foundation, Inc.
00004  *
00005  * This file is part of GNU Radio
00006  * 
00007  * GNU Radio is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation; either version 3, or (at your option)
00010  * any later version.
00011  *
00012  * GNU Radio is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with GNU Radio; see the file COPYING.  If not, write to
00019  * the Free Software Foundation, Inc., 51 Franklin Street,
00020  * Boston, MA 02110-1301, USA.
00021  */
00022 
00023 #ifndef RPCSERVER_ICE_H
00024 #define RPCSERVER_ICE_H
00025 
00026 #include <gnuradio/rpcserver_base.h>
00027 #include <gnuradio/rpcpmtconverters_ice.h>
00028 #include <string>
00029 #include <sstream>
00030 #include <map>
00031 #include <gnuradio.h>
00032 #include <Ice/Exception.h>
00033 #include <boost/format.hpp>
00034 
00035 class rpcserver_ice : public virtual rpcserver_base, public GNURadio::ControlPort
00036 {
00037 public:
00038   rpcserver_ice();
00039   virtual ~rpcserver_ice();
00040 
00041   void registerConfigureCallback(const std::string &id, const configureCallback_t callback);
00042   void unregisterConfigureCallback(const std::string &id);
00043 
00044   void registerQueryCallback(const std::string &id, const queryCallback_t callback);
00045   void unregisterQueryCallback(const std::string &id);
00046 
00047   virtual void set(const GNURadio::KnobMap&, const Ice::Current&);
00048 
00049   GNURadio::KnobMap get(const GNURadio::KnobIDList&, const Ice::Current&);
00050 
00051   GNURadio::KnobMap getRe(const GNURadio::KnobIDList&, const Ice::Current&);
00052 
00053   GNURadio::KnobPropMap properties(const GNURadio::KnobIDList&, const Ice::Current&);
00054 
00055   virtual void shutdown(const Ice::Current&);
00056 
00057 private:
00058   typedef std::map<std::string, configureCallback_t> ConfigureCallbackMap_t;
00059   ConfigureCallbackMap_t d_setcallbackmap;
00060 
00061   typedef std::map<std::string, queryCallback_t> QueryCallbackMap_t;
00062   QueryCallbackMap_t d_getcallbackmap;
00063 
00064   template<typename T, typename TMap> struct set_f
00065     : public std::unary_function<T,void> 
00066   {
00067     set_f(const Ice::Current& _c, TMap& _setcallbackmap, const priv_lvl_t& _cur_priv) :
00068       c(_c), d_setcallbackmap(_setcallbackmap), cur_priv(_cur_priv)
00069     {;}
00070 
00071     void operator()(const T& p)
00072     {
00073       ConfigureCallbackMap_t::const_iterator iter(d_setcallbackmap.find(p.first));
00074       if(iter != d_setcallbackmap.end()) {
00075         if(cur_priv <= iter->second.priv) {
00076           (*iter->second.callback).post(pmt::PMT_NIL, rpcpmtconverter::to_pmt(p.second,c));
00077         }
00078         else {
00079           std::cout << "Key " << p.first << " requires PRIVLVL <= "
00080                     << iter->second.priv << " to set, currently at: "
00081                     << cur_priv << std::endl;
00082         }
00083       }
00084       else {
00085         throw IceUtil::NullHandleException(__FILE__, __LINE__);
00086       }
00087     }
00088 
00089     const Ice::Current& c;
00090     TMap& d_setcallbackmap;
00091     const priv_lvl_t& cur_priv;
00092   };
00093 
00094   template<typename T, typename TMap>
00095   struct get_f : public std::unary_function<T,void>
00096   {
00097     get_f(const Ice::Current& _c, TMap& _getcallbackmap,
00098           const priv_lvl_t& _cur_priv, GNURadio::KnobMap& _outknobs) :
00099       c(_c), d_getcallbackmap(_getcallbackmap), cur_priv(_cur_priv), outknobs(_outknobs)
00100     {}
00101     
00102     void operator()(const T& p)
00103     {
00104       QueryCallbackMap_t::const_iterator iter(d_getcallbackmap.find(p));
00105       if(iter != d_getcallbackmap.end()) {
00106         if(cur_priv <= iter->second.priv) {
00107           outknobs[p] = rpcpmtconverter::from_pmt((*iter->second.callback).retrieve(), c);
00108         }
00109         else {
00110           std::cout << "Key " << iter->first << " requires PRIVLVL: <= "
00111                     << iter->second.priv << " to get, currently at: "
00112                     << cur_priv << std::endl;
00113         }
00114       }
00115       else {
00116         std::stringstream ss;
00117         ss << "Ctrlport Key called with unregistered key (" << p << ")\n";
00118         std::cout << ss.str();
00119         throw IceUtil::IllegalArgumentException(__FILE__,__LINE__,ss.str().c_str());
00120       }
00121     }
00122 
00123     const Ice::Current& c;
00124     TMap& d_getcallbackmap;
00125     const priv_lvl_t& cur_priv;
00126     GNURadio::KnobMap& outknobs;
00127   };
00128 
00129   template<typename T, typename TMap, typename TKnobMap>
00130   struct get_all_f : public std::unary_function<T,void>
00131   {
00132     get_all_f(const Ice::Current& _c, TMap& _getcallbackmap,
00133               const priv_lvl_t& _cur_priv, TKnobMap& _outknobs) :
00134       c(_c), d_getcallbackmap(_getcallbackmap), cur_priv(_cur_priv), outknobs(_outknobs)
00135     {;}
00136 
00137     void operator()(const T& p)
00138     {
00139       if(cur_priv <= p.second.priv) {
00140         outknobs[p.first] = rpcpmtconverter::from_pmt(p.second.callback->retrieve(), c);
00141       }
00142       else {
00143         std::cout << "Key " << p.first << " requires PRIVLVL <= "
00144                   << p.second.priv << " to get, currently at: "
00145                   << cur_priv << std::endl;
00146       }
00147     }
00148 
00149     const Ice::Current& c;
00150     TMap& d_getcallbackmap;
00151     const priv_lvl_t& cur_priv;
00152     TKnobMap& outknobs;
00153   };
00154 
00155   template<typename T, typename TMap, typename TKnobMap>
00156   struct properties_all_f : public std::unary_function<T,void>
00157   {
00158     properties_all_f(const Ice::Current& _c, QueryCallbackMap_t& _getcallbackmap,
00159                      const priv_lvl_t& _cur_priv, GNURadio::KnobPropMap& _outknobs) :
00160       c(_c), d_getcallbackmap(_getcallbackmap), cur_priv(_cur_priv), outknobs(_outknobs)
00161     {;}
00162 
00163     void operator()(const T& p)
00164     {
00165       if(cur_priv <= p.second.priv) {
00166         GNURadio::KnobProp prop;//(new GNURadio::KnobProp());
00167         prop.type  = GNURadio::KNOBDOUBLE;
00168         prop.units = p.second.units;
00169         prop.description = p.second.description;
00170         prop.min   = rpcpmtconverter::from_pmt(p.second.min, c);
00171         prop.max   = rpcpmtconverter::from_pmt(p.second.max, c);
00172         prop.display = static_cast<uint32_t>(p.second.display);
00173         outknobs[p.first] = prop;
00174       }
00175       else {
00176         std::cout << "Key " << p.first << " requires PRIVLVL <= "
00177                   << p.second.priv << " to get, currently at: "
00178                   << cur_priv << std::endl;
00179       }
00180     }
00181 
00182     const Ice::Current& c;
00183     TMap& d_getcallbackmap;
00184     const priv_lvl_t& cur_priv;
00185     TKnobMap& outknobs;
00186   };
00187 
00188   template<class T, typename TMap, typename TKnobMap>
00189   struct properties_f : public std::unary_function<T,void>
00190   {
00191     properties_f(const Ice::Current& _c, TMap& _getcallbackmap,
00192                  const priv_lvl_t& _cur_priv, TKnobMap& _outknobs) :
00193       c(_c), d_getcallbackmap(_getcallbackmap), cur_priv(_cur_priv), outknobs(_outknobs)
00194     {;}
00195 
00196     void operator()(const T& p)
00197     {
00198       typename TMap::const_iterator iter(d_getcallbackmap.find(p));
00199       if(iter != d_getcallbackmap.end()) {
00200         if(cur_priv <= iter->second.priv) {
00201           GNURadio::KnobProp prop;
00202           prop.type  = GNURadio::KNOBDOUBLE;
00203           prop.units = iter->second.units;
00204           prop.description = iter->second.description;
00205           prop.min   = rpcpmtconverter::from_pmt(iter->second.min, c);
00206           prop.max   = rpcpmtconverter::from_pmt(iter->second.max, c);
00207           prop.display = static_cast<uint32_t>(iter->second.display);
00208           //outknobs[iter->first] = prop;
00209           outknobs[p] = prop;
00210         }
00211         else {
00212           std::cout << "Key " << iter->first << " requires PRIVLVL: <= " <<
00213             iter->second.priv << " to get, currently at: " << cur_priv << std::endl;
00214         }
00215       }
00216       else {
00217         throw IceUtil::NullHandleException(__FILE__, __LINE__);
00218       }
00219     }
00220 
00221     const Ice::Current& c;
00222     TMap& d_getcallbackmap;
00223     const priv_lvl_t& cur_priv;
00224     TKnobMap& outknobs;
00225   };
00226 };
00227 
00228 #endif /* RPCSERVER_ICE_H */