GNU Radio Manual and C++ API Reference  3.8.0.0
The Free & Open Software Radio Ecosystem
basic_block.h
Go to the documentation of this file.
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2006,2008,2009,2011,2013 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 INCLUDED_GR_BASIC_BLOCK_H
24 #define INCLUDED_GR_BASIC_BLOCK_H
25 
26 #include <gnuradio/api.h>
27 #include <gnuradio/io_signature.h>
28 #include <gnuradio/msg_accepter.h>
29 #include <gnuradio/runtime_types.h>
30 #include <gnuradio/sptr_magic.h>
31 #include <gnuradio/thread/thread.h>
32 #include <boost/enable_shared_from_this.hpp>
33 #include <boost/foreach.hpp>
34 #include <boost/function.hpp>
35 #include <boost/thread/condition_variable.hpp>
36 #include <deque>
37 #include <iostream>
38 #include <map>
39 #include <string>
40 
41 #ifdef GR_CTRLPORT
43 #endif
44 
45 namespace gr {
46 
47 /*!
48  * \brief The abstract base class for all signal processing blocks.
49  * \ingroup internal
50  *
51  * Basic blocks are the bare abstraction of an entity that has a
52  * name, a set of inputs and outputs, and a message queue. These
53  * are never instantiated directly; rather, this is the abstract
54  * parent class of both gr_hier_block, which is a recursive
55  * container, and block, which implements actual signal
56  * processing functions.
57  */
59  public boost::enable_shared_from_this<basic_block>
60 {
61  typedef boost::function<void(pmt::pmt_t)> msg_handler_t;
62 
63 private:
64  typedef std::map<pmt::pmt_t, msg_handler_t, pmt::comparator> d_msg_handlers_t;
65  d_msg_handlers_t d_msg_handlers;
66 
67  typedef std::deque<pmt::pmt_t> msg_queue_t;
68  typedef std::map<pmt::pmt_t, msg_queue_t, pmt::comparator> msg_queue_map_t;
69  typedef std::map<pmt::pmt_t, msg_queue_t, pmt::comparator>::iterator
70  msg_queue_map_itr;
71  std::map<pmt::pmt_t, boost::shared_ptr<boost::condition_variable>, pmt::comparator>
72  msg_queue_ready;
73 
74  gr::thread::mutex mutex; //< protects all vars
75 
76 protected:
77  friend class flowgraph;
78  friend class flat_flowgraph; // TODO: will be redundant
79  friend class tpb_thread_body;
80 
81  enum vcolor { WHITE, GREY, BLACK };
82 
83  std::string d_name;
88  std::string d_symbol_name;
89  std::string d_symbol_alias;
91  bool d_rpc_set;
92 
93  msg_queue_map_t msg_queue;
94  std::vector<boost::any> d_rpc_vars; // container for all RPC variables
95 
96  basic_block(void) {} // allows pure virtual interface sub-classes
97 
98  //! Protected constructor prevents instantiation by non-derived classes
99  basic_block(const std::string& name,
100  gr::io_signature::sptr input_signature,
101  gr::io_signature::sptr output_signature);
102 
103  //! may only be called during constructor
104  void set_input_signature(gr::io_signature::sptr iosig) { d_input_signature = iosig; }
105 
106  //! may only be called during constructor
108  {
109  d_output_signature = iosig;
110  }
111 
112  /*!
113  * \brief Allow the flowgraph to set for sorting and partitioning
114  */
115  void set_color(vcolor color) { d_color = color; }
116  vcolor color() const { return d_color; }
117 
118  /*!
119  * \brief Tests if there is a handler attached to port \p which_port
120  */
121  virtual bool has_msg_handler(pmt::pmt_t which_port)
122  {
123  return (d_msg_handlers.find(which_port) != d_msg_handlers.end());
124  }
125 
126  /*
127  * This function is called by the runtime system to dispatch messages.
128  *
129  * The thread-safety guarantees mentioned in set_msg_handler are
130  * implemented by the callers of this method.
131  */
132  virtual void dispatch_msg(pmt::pmt_t which_port, pmt::pmt_t msg)
133  {
134  // AA Update this
135  if (has_msg_handler(which_port)) { // Is there a handler?
136  d_msg_handlers[which_port](msg); // Yes, invoke it.
137  }
138  }
139 
140  // Message passing interface
142 
143 public:
144  pmt::pmt_t message_subscribers(pmt::pmt_t port);
145  virtual ~basic_block();
146  long unique_id() const { return d_unique_id; }
147  long symbolic_id() const { return d_symbolic_id; }
148 
149  /*! The name of the block */
150  std::string name() const { return d_name; }
151 
152  /*!
153  * The sybolic name of the block, which is used in the
154  * block_registry. The name is assigned by the block's constructor
155  * and never changes during the life of the block.
156  */
157  std::string symbol_name() const { return d_symbol_name; }
158  std::string identifier() const
159  {
160  return this->name() + "(" + std::to_string(this->unique_id()) + ")";
161  }
162 
163  gr::io_signature::sptr input_signature() const { return d_input_signature; }
164  gr::io_signature::sptr output_signature() const { return d_output_signature; }
165  basic_block_sptr to_basic_block(); // Needed for Python type coercion
166 
167  /*!
168  * True if the block has an alias (see set_block_alias).
169  */
170  bool alias_set() const { return !d_symbol_alias.empty(); }
171 
172  /*!
173  * Returns the block's alias as a string.
174  */
175  std::string alias() const { return alias_set() ? d_symbol_alias : symbol_name(); }
176 
177  /*!
178  * Returns the block's alias as PMT.
179  */
180  pmt::pmt_t alias_pmt() const { return pmt::intern(alias()); }
181 
182  /*!
183  * Set's a new alias for the block; also adds an entry into the
184  * block_registry to get the block using either the alias or the
185  * original symbol name.
186  */
187  void set_block_alias(std::string name);
188 
189  // ** Message passing interface **
190  void message_port_register_in(pmt::pmt_t port_id);
191  void message_port_register_out(pmt::pmt_t port_id);
192  void message_port_pub(pmt::pmt_t port_id, pmt::pmt_t msg);
193  void message_port_sub(pmt::pmt_t port_id, pmt::pmt_t target);
194  void message_port_unsub(pmt::pmt_t port_id, pmt::pmt_t target);
195 
196  virtual bool message_port_is_hier(pmt::pmt_t port_id)
197  {
198  (void)port_id;
199  return false;
200  }
201  virtual bool message_port_is_hier_in(pmt::pmt_t port_id)
202  {
203  (void)port_id;
204  return false;
205  }
206  virtual bool message_port_is_hier_out(pmt::pmt_t port_id)
207  {
208  (void)port_id;
209  return false;
210  }
211 
212  /*!
213  * \brief Get input message port names.
214  *
215  * Returns the available input message ports for a block. The
216  * return object is a PMT vector that is filled with PMT symbols.
217  */
218  pmt::pmt_t message_ports_in();
219 
220  /*!
221  * \brief Get output message port names.
222  *
223  * Returns the available output message ports for a block. The
224  * return object is a PMT vector that is filled with PMT symbols.
225  */
226  pmt::pmt_t message_ports_out();
227 
228  /*!
229  * Accept msg, place in queue, arrange for thread to be awakened if it's not already.
230  */
231  void _post(pmt::pmt_t which_port, pmt::pmt_t msg);
232 
233  //! is the queue empty?
234  bool empty_p(pmt::pmt_t which_port)
235  {
236  if (msg_queue.find(which_port) == msg_queue.end())
237  throw std::runtime_error("port does not exist!");
238  return msg_queue[which_port].empty();
239  }
240  bool empty_p()
241  {
242  bool rv = true;
243  BOOST_FOREACH (msg_queue_map_t::value_type& i, msg_queue) {
244  rv &= msg_queue[i.first].empty();
245  }
246  return rv;
247  }
248 
249  //! are all msg ports with handlers empty?
250  bool empty_handled_p(pmt::pmt_t which_port)
251  {
252  return (empty_p(which_port) || !has_msg_handler(which_port));
253  }
255  {
256  bool rv = true;
257  BOOST_FOREACH (msg_queue_map_t::value_type& i, msg_queue) {
258  rv &= empty_handled_p(i.first);
259  }
260  return rv;
261  }
262 
263  //! How many messages in the queue?
264  size_t nmsgs(pmt::pmt_t which_port)
265  {
266  if (msg_queue.find(which_port) == msg_queue.end())
267  throw std::runtime_error("port does not exist!");
268  return msg_queue[which_port].size();
269  }
270 
271  //| Acquires and release the mutex
272  void insert_tail(pmt::pmt_t which_port, pmt::pmt_t msg);
273  /*!
274  * \returns returns pmt at head of queue or pmt::pmt_t() if empty.
275  */
276  pmt::pmt_t delete_head_nowait(pmt::pmt_t which_port);
277 
278  msg_queue_t::iterator get_iterator(pmt::pmt_t which_port)
279  {
280  return msg_queue[which_port].begin();
281  }
282 
283  void erase_msg(pmt::pmt_t which_port, msg_queue_t::iterator it)
284  {
285  msg_queue[which_port].erase(it);
286  }
287 
288  virtual bool has_msg_port(pmt::pmt_t which_port)
289  {
290  if (msg_queue.find(which_port) != msg_queue.end()) {
291  return true;
292  }
293  if (pmt::dict_has_key(d_message_subscribers, which_port)) {
294  return true;
295  }
296  return false;
297  }
298 
299  const msg_queue_map_t& get_msg_map(void) const { return msg_queue; }
300 
301 #ifdef GR_CTRLPORT
302  /*!
303  * \brief Add an RPC variable (get or set).
304  *
305  * Using controlport, we create new getters/setters and need to
306  * store them. Each block has a vector to do this, and these never
307  * need to be accessed again once they are registered with the RPC
308  * backend. This function takes a
309  * boost::shared_sptr<rpcbasic_base> so that when the block is
310  * deleted, all RPC registered variables are cleaned up.
311  *
312  * \param s an rpcbasic_sptr of the new RPC variable register to store.
313  */
314  void add_rpc_variable(rpcbasic_sptr s) { d_rpc_vars.push_back(s); }
315 #endif /* GR_CTRLPORT */
316 
317  /*!
318  * \brief Set up the RPC registered variables.
319  *
320  * This must be overloaded by a block that wants to use
321  * controlport. This is where rpcbasic_register_{get,set} pointers
322  * are created, which then get wrapped as shared pointers
323  * (rpcbasic_sptr(...)) and stored using add_rpc_variable.
324  */
325  virtual void setup_rpc(){};
326 
327  /*!
328  * \brief Ask if this block has been registered to the RPC.
329  *
330  * We can only register a block once, so we use this to protect us
331  * from calling it multiple times.
332  */
333  bool is_rpc_set() { return d_rpc_set; }
334 
335  /*!
336  * \brief When the block is registered with the RPC, set this.
337  */
338  void rpc_set() { d_rpc_set = true; }
339 
340  /*!
341  * \brief Confirm that ninputs and noutputs is an acceptable combination.
342  *
343  * \param ninputs number of input streams connected
344  * \param noutputs number of output streams connected
345  *
346  * \returns true if this is a valid configuration for this block.
347  *
348  * This function is called by the runtime system whenever the
349  * topology changes. Most classes do not need to override this.
350  * This check is in addition to the constraints specified by the
351  * input and output gr::io_signatures.
352  */
353  virtual bool check_topology(int ninputs, int noutputs)
354  {
355  (void)ninputs;
356  (void)noutputs;
357  return true;
358  }
359 
360  /*!
361  * \brief Set the callback that is fired when messages are available.
362  *
363  * \p msg_handler can be any kind of function pointer or function object
364  * that has the signature:
365  * <pre>
366  * void msg_handler(pmt::pmt msg);
367  * </pre>
368  *
369  * (You may want to use boost::bind to massage your callable into
370  * the correct form. See gr::blocks::nop for an example that sets
371  * up a class method as the callback.)
372  *
373  * Blocks that desire to handle messages must call this method in
374  * their constructors to register the handler that will be invoked
375  * when messages are available.
376  *
377  * If the block inherits from block, the runtime system will
378  * ensure that msg_handler is called in a thread-safe manner, such
379  * that work and msg_handler will never be called concurrently.
380  * This allows msg_handler to update state variables without
381  * having to worry about thread-safety issues with work,
382  * general_work or another invocation of msg_handler.
383  *
384  * If the block inherits from hier_block2, the runtime system
385  * will ensure that no reentrant calls are made to msg_handler.
386  */
387  template <typename T>
389  {
390  if (msg_queue.find(which_port) == msg_queue.end()) {
391  throw std::runtime_error(
392  "attempt to set_msg_handler() on bad input message port!");
393  }
394  d_msg_handlers[which_port] = msg_handler_t(msg_handler);
395  }
396 
397  virtual void set_processor_affinity(const std::vector<int>& mask) = 0;
398 
399  virtual void unset_processor_affinity() = 0;
400 
401  virtual std::vector<int> processor_affinity() = 0;
402 
403  virtual void set_log_level(std::string level) = 0;
404 
405  virtual std::string log_level() = 0;
406 };
407 
408 inline bool operator<(basic_block_sptr lhs, basic_block_sptr rhs)
409 {
410  return lhs->unique_id() < rhs->unique_id();
411 }
412 
413 typedef std::vector<basic_block_sptr> basic_block_vector_t;
414 typedef std::vector<basic_block_sptr>::iterator basic_block_viter_t;
415 
417 
418 inline std::ostream& operator<<(std::ostream& os, basic_block_sptr basic_block)
419 {
420  os << basic_block->identifier();
421  return os;
422 }
423 
424 } /* namespace gr */
425 
426 #endif /* INCLUDED_GR_BASIC_BLOCK_H */
boost::shared_ptr< io_signature > sptr
Definition: io_signature.h:46
Provide a comparator function object to allow pmt use in stl types.
Definition: pmt.h:986
std::string d_symbol_alias
Definition: basic_block.h:89
std::string identifier() const
Definition: basic_block.h:158
void set_input_signature(gr::io_signature::sptr iosig)
may only be called during constructor
Definition: basic_block.h:104
size_t nmsgs(pmt::pmt_t which_port)
How many messages in the queue?
Definition: basic_block.h:264
virtual void setup_rpc()
Set up the RPC registered variables.
Definition: basic_block.h:325
boost::shared_ptr< pmt_base > pmt_t
typedef for shared pointer (transparent reference counting). See http://www.boost.org/libs/smart_ptr/smart_ptr.htm
Definition: pmt.h:96
bool empty_p()
Definition: basic_block.h:240
pmt::pmt_t alias_pmt() const
Definition: basic_block.h:180
std::string d_symbol_name
Definition: basic_block.h:88
bool is_rpc_set()
Ask if this block has been registered to the RPC.
Definition: basic_block.h:333
void set_color(vcolor color)
Allow the flowgraph to set for sorting and partitioning.
Definition: basic_block.h:115
virtual bool message_port_is_hier(pmt::pmt_t port_id)
Definition: basic_block.h:196
msg_queue_map_t msg_queue
Definition: basic_block.h:93
void erase_msg(pmt::pmt_t which_port, msg_queue_t::iterator it)
Definition: basic_block.h:283
virtual bool has_msg_handler(pmt::pmt_t which_port)
Tests if there is a handler attached to port which_port.
Definition: basic_block.h:121
bool empty_handled_p(pmt::pmt_t which_port)
are all msg ports with handlers empty?
Definition: basic_block.h:250
Class representing a directed, acyclic graph of basic blocks.
Definition: flowgraph.h:162
PMT_API pmt_t intern(const std::string &s)
Alias for pmt_string_to_symbol.
void rpc_set()
When the block is registered with the RPC, set this.
Definition: basic_block.h:338
std::string name() const
Definition: basic_block.h:150
virtual bool has_msg_port(pmt::pmt_t which_port)
Definition: basic_block.h:288
Accepts messages and inserts them into a message queue, then notifies subclass gr::basic_block there ...
Definition: msg_accepter.h:35
#define GR_RUNTIME_API
Definition: gnuradio-runtime/include/gnuradio/api.h:30
long unique_id() const
Definition: basic_block.h:146
gr::io_signature::sptr d_input_signature
Definition: basic_block.h:84
gr::io_signature::sptr output_signature() const
Definition: basic_block.h:164
GNU Radio logging wrapper for log4cpp library (C++ port of log4j)
Definition: basic_block.h:45
vcolor
Definition: basic_block.h:81
const msg_queue_map_t & get_msg_map(void) const
Definition: basic_block.h:299
The abstract base class for all signal processing blocks.Basic blocks are the bare abstraction of an ...
Definition: basic_block.h:58
gr::io_signature::sptr input_signature() const
Definition: basic_block.h:163
std::string d_name
Definition: basic_block.h:83
vcolor color() const
Definition: basic_block.h:116
basic_block(void)
Definition: basic_block.h:96
virtual bool message_port_is_hier_in(pmt::pmt_t port_id)
Definition: basic_block.h:201
virtual bool message_port_is_hier_out(pmt::pmt_t port_id)
Definition: basic_block.h:206
bool empty_p(pmt::pmt_t which_port)
is the queue empty?
Definition: basic_block.h:234
bool empty_handled_p()
Definition: basic_block.h:254
virtual bool check_topology(int ninputs, int noutputs)
Confirm that ninputs and noutputs is an acceptable combination.
Definition: basic_block.h:353
std::string alias() const
Definition: basic_block.h:175
std::vector< boost::any > d_rpc_vars
Definition: basic_block.h:94
std::vector< basic_block_sptr > basic_block_vector_t
Definition: basic_block.h:413
bool alias_set() const
Definition: basic_block.h:170
msg_queue_t::iterator get_iterator(pmt::pmt_t which_port)
Definition: basic_block.h:278
long d_symbolic_id
Definition: basic_block.h:87
std::vector< basic_block_sptr >::iterator basic_block_viter_t
Definition: basic_block.h:414
gr::io_signature::sptr d_output_signature
Definition: basic_block.h:85
std::ostream & operator<<(std::ostream &os, basic_block_sptr basic_block)
Definition: basic_block.h:418
GR_RUNTIME_API long basic_block_ncurrently_allocated()
boost::mutex mutex
Definition: thread.h:48
PMT_API bool dict_has_key(const pmt_t &dict, const pmt_t &key)
Return true if key exists in dict.
long symbolic_id() const
Definition: basic_block.h:147
bool d_rpc_set
Definition: basic_block.h:91
virtual void dispatch_msg(pmt::pmt_t which_port, pmt::pmt_t msg)
Definition: basic_block.h:132
vcolor d_color
Definition: basic_block.h:90
Definition: basic_block.h:81
pmt::pmt_t d_message_subscribers
Definition: basic_block.h:141
void set_msg_handler(pmt::pmt_t which_port, T msg_handler)
Set the callback that is fired when messages are available.
Definition: basic_block.h:388
long d_unique_id
Definition: basic_block.h:86
bool operator<(basic_block_sptr lhs, basic_block_sptr rhs)
Definition: basic_block.h:408
void set_output_signature(gr::io_signature::sptr iosig)
may only be called during constructor
Definition: basic_block.h:107
std::string symbol_name() const
Definition: basic_block.h:157
abstract class of message handlers
Definition: msg_handler.h:38