GNU Radio Manual and C++ API Reference  3.7.4
The Free & Open Software Radio Ecosystem
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
ice_application_base.h
Go to the documentation of this file.
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2012 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 ICE_APPLICATION_BASE_H
24 #define ICE_APPLICATION_BASE_H
25 
26 #ifdef HAVE_WINDOWS_H
27 #include <winsock2.h>
28 #include <sys/time.h>
29 #endif
30 
31 #include <gnuradio/api.h>
32 #include <gnuradio/prefs.h>
33 #include <Ice/Ice.h>
34 #include <boost/thread.hpp>
35 #include <boost/thread/mutex.hpp>
36 #include <stdio.h>
37 #include <iostream>
38 #include <set>
39 #include <string>
40 #include <stdio.h>
41 
42 namespace {
43  static const unsigned int ICEAPPLICATION_ACTIVATION_TIMEOUT_MS(600);
44 };
45 
46 class GR_RUNTIME_API ice_application_common : public Ice::Application
47 {
48  public:
49  template<typename TserverBase, typename TserverClass> friend class ice_application_base;
53 
54  protected:
55  static bool d_main_called;
56  static bool d_have_ice_config;
57  static std::string d_endpointStr;
60  int run(int, char*[]);
61 };
62 
63 template<typename TserverBase, typename TserverClass>
65 {
66 public:
68  ice_application_base(TserverClass* _this);
70 
71  static TserverBase* i();
72  static const std::vector<std::string> endpoints();
73 
74 protected:
75  bool have_ice_config() { return d_application->d_have_ice_config; }
76  void set_endpoint(const std::string& endpoint) { d_application->d_endpointStr = endpoint;}
77 
78  //this one is the key... overwrite in templated/inherited variants
79  virtual TserverBase* i_impl() = 0;
80 
81  //tools for the i_impl...
82  //tell it when it has to resync with the communicator
83  virtual bool reacquire_sync();
84  virtual void sync_reacquire();
85 
86  static TserverClass* d_this;
87 
89  //static int d_reacquire_attributes;
90 
91 private:
92  void starticeexample();
93 
94  bool application_started();
95 
96  int run(int, char*[]);
97 
98  static void kickoff();
99 };
100 
101 template<typename TserverBase, typename TserverClass>
103 
104 //template<typename TserverBase, typename TserverClass>
105 //int ice_application_base<TserverBase, TserverClass>::d_reacquire_attributes(0);
106 
107 template<typename TserverBase, typename TserverClass>
109  : d_reacquire(0)
110 {
111  //d_reacquire_attributes = 0;
112  d_this = _this;
114 }
115 
116 template<typename TserverBase, typename TserverClass>
118 {
119  char* argv[2];
120  argv[0] = (char*)"";
121 
122  std::string conffile = gr::prefs::singleton()->get_string("ControlPort", "config", "");
123 
124  if(conffile.size() > 0) {
125  std::stringstream iceconf;
128  iceconf << conffile;
129  d_application->main(0, argv, iceconf.str().c_str());
130  }
131  else {
134  d_application->main(0, argv);
135  }
136 }
137 
138 template<typename TserverBase, typename TserverClass>
140 {
141  static bool run_once = false;
142 
143  //if(!d_this->application_started()) {
144  if(!run_once) {
145  ++d_this->d_application->d_reacquire_attributes;
146 
148  (new boost::thread(boost::bind(&ice_application_base::starticeexample, d_this)));
149 
150  ::timespec timer_ts, rem_ts;
151  timer_ts.tv_sec = 0; timer_ts.tv_nsec = ICEAPPLICATION_ACTIVATION_TIMEOUT_MS*1000;
152 
153  int iter = 0;
154  while(!d_this->application_started()) {
155  #if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
156  ::Sleep(timer_ts.tv_nsec / 1000000);
157  #else
158  ::nanosleep(&timer_ts, &rem_ts);
159  #endif
160  if(!d_this->application_started())
161  std::cout << "@";
162  if(iter++ > 100) {
163  std::cout << "ice_application_base::kickoff(), timeout waiting to get communicator() d_application->main() might have failed?!" << std::endl;;
164  break;
165  }
166  }
167 
168  run_once = true;
169  }
170 
171  return;
172 }
173 
174 
175 template<typename TserverBase, typename TserverClass>
177 {
178  return (d_this->d_reacquire != d_application->d_reacquire_attributes);
179 }
180 
181 template<typename TserverBase, typename TserverClass>
183 {
184  d_this->d_reacquire = d_application->d_reacquire_attributes;
185 }
186 
187 
188 template<typename TserverBase, typename TserverClass>
190 {
191  std::vector<std::string> ep; ep.push_back(d_this->d_application->d_endpointStr); return ep;
192 }
193 
194 template<typename TserverBase, typename TserverClass>
196 {
197  //printf("indacall\n");
198 
199  assert(d_this != 0);
200  if(!d_this->application_started()) {
201  //printf("anotherkickoff\n");
202  kickoff();
203  }
204  //printf("donekickedoff\n");
205 
206  /*else if(!d_proxy) {
207  d_proxy = d_this->i_impl();
208  assert(d_proxy != 0);
209  }*/
210 
211  return d_this->i_impl();
212 }
213 
214 /*template<typename TserverBase, typename TserverClass>
215  int ice_application_base<TserverBase, TserverClass>::run(int argc, char* argv[]) {
216  int implreturn(run_impl(argc, argv));
217  ice_application_base<TserverBase, TserverClass>::communicator()->waitForShutdown();
218  return implreturn;
219  }*/
220 
221 template<typename TserverBase, typename TImplClass>
223 {
225 }
226 
227 /*template<typename TserverBase, typename TImplClass>
228 int ice_application_base<TserverBase, TImplClass>::run_impl(int argc, char* argv[]) { return EXIT_SUCCESS; }
229 */
230 
231 #endif
Definition: ice_application_base.h:64
static bool d_have_ice_config
Definition: ice_application_base.h:56
Definition: ice_application_base.h:46
ice_application_base(TserverClass *_this)
Definition: ice_application_base.h:108
static bool d_main_called
Definition: ice_application_base.h:55
static TserverClass * d_this
Definition: ice_application_base.h:86
#define GR_RUNTIME_API
Definition: gnuradio-runtime/include/gnuradio/api.h:30
virtual TserverBase * i_impl()=0
static TserverBase * i()
Definition: ice_application_base.h:195
static boost::shared_ptr< ice_application_common > Instance()
boost::thread thread
Definition: thread.h:45
static boost::shared_ptr< boost::thread > d_thread
Definition: ice_application_base.h:58
~ice_application_common()
Definition: ice_application_base.h:51
ice_application_common()
Definition: ice_application_base.h:59
~ice_application_base()
Definition: ice_application_base.h:69
virtual const std::string get_string(const std::string &section, const std::string &option, const std::string &default_val)
If option exists return associated value; else default_val.
virtual void sync_reacquire()
Definition: ice_application_base.h:182
int d_reacquire
Definition: ice_application_base.h:88
static prefs * singleton()
static int d_reacquire_attributes
Definition: ice_application_base.h:52
void set_endpoint(const std::string &endpoint)
Definition: ice_application_base.h:76
bool have_ice_config()
Definition: ice_application_base.h:75
static std::string d_endpointStr
Definition: ice_application_base.h:57
Definition: pycallback_object.h:35
virtual bool reacquire_sync()
Definition: ice_application_base.h:176
static const std::vector< std::string > endpoints()
Definition: ice_application_base.h:189
boost::shared_ptr< ice_application_common > d_application
Definition: ice_application_base.h:67