GNU Radio 3.6.3 C++ API
gr_basic_block.h
Go to the documentation of this file.
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2006,2008,2009,2011 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 <gr_core_api.h>
27 #include <gr_runtime_types.h>
28 #include <gr_sptr_magic.h>
29 #include <boost/enable_shared_from_this.hpp>
30 #include <boost/function.hpp>
31 #include <gr_msg_accepter.h>
32 #include <string>
33 #include <deque>
34 #include <map>
35 #include <gr_io_signature.h>
36 #include <gruel/thread.h>
37 #include <boost/foreach.hpp>
38 #include <boost/thread/condition_variable.hpp>
39 #include <iostream>
40 
41 /*!
42  * \brief The abstract base class for all signal processing blocks.
43  * \ingroup internal
44  *
45  * Basic blocks are the bare abstraction of an entity that has a name,
46  * a set of inputs and outputs, and a message queue. These are never instantiated
47  * directly; rather, this is the abstract parent class of both gr_hier_block,
48  * which is a recursive container, and gr_block, which implements actual
49  * signal processing functions.
50  */
51 
52 class GR_CORE_API gr_basic_block : public gr_msg_accepter, public boost::enable_shared_from_this<gr_basic_block>
53 {
54  typedef boost::function<void(pmt::pmt_t)> msg_handler_t;
55 
56  private:
57  /*
58  * This function is called by the runtime system to dispatch messages.
59  *
60  * The thread-safety guarantees mentioned in set_msg_handler are implemented
61  * by the callers of this method.
62  */
63  void dispatch_msg(pmt::pmt_t which_port, pmt::pmt_t msg)
64  {
65  // AA Update this
66  if (d_msg_handlers.find(which_port) != d_msg_handlers.end()) // Is there a handler?
67  d_msg_handlers[which_port](msg); // Yes, invoke it.
68  };
69 
70  //msg_handler_t d_msg_handler;
71  typedef std::map<pmt::pmt_t , msg_handler_t, pmt::pmt_comperator> d_msg_handlers_t;
72  d_msg_handlers_t d_msg_handlers;
73 
74  typedef std::deque<pmt::pmt_t> msg_queue_t;
75  typedef std::map<pmt::pmt_t, msg_queue_t, pmt::pmt_comperator> msg_queue_map_t;
76  typedef std::map<pmt::pmt_t, msg_queue_t, pmt::pmt_comperator>::iterator msg_queue_map_itr;
77  std::map<pmt::pmt_t, boost::shared_ptr<boost::condition_variable>, pmt::pmt_comperator> msg_queue_ready;
78 
79  gruel::mutex mutex; //< protects all vars
80 
81  protected:
82  friend class gr_flowgraph;
83  friend class gr_flat_flowgraph; // TODO: will be redundant
84  friend class gr_tpb_thread_body;
85 
86  enum vcolor { WHITE, GREY, BLACK };
87 
88  std::string d_name;
93  std::string d_symbol_name;
94  std::string d_symbol_alias;
96  msg_queue_map_t msg_queue;
97 
98  gr_basic_block(void){} //allows pure virtual interface sub-classes
99 
100  //! Protected constructor prevents instantiation by non-derived classes
101  gr_basic_block(const std::string &name,
102  gr_io_signature_sptr input_signature,
103  gr_io_signature_sptr output_signature);
104 
105  //! may only be called during constructor
107  d_input_signature = iosig;
108  }
109 
110  //! may only be called during constructor
112  d_output_signature = iosig;
113  }
114 
115  /*!
116  * \brief Allow the flowgraph to set for sorting and partitioning
117  */
118  void set_color(vcolor color) { d_color = color; }
119  vcolor color() const { return d_color; }
120 
121  // Message passing interface
123 
124  public:
125  virtual ~gr_basic_block();
126  long unique_id() const { return d_unique_id; }
127  long symbolic_id() const { return d_symbolic_id; }
128  std::string name() const { return d_name; }
129  std::string symbol_name() const { return d_symbol_name; }
130  gr_io_signature_sptr input_signature() const { return d_input_signature; }
131  gr_io_signature_sptr output_signature() const { return d_output_signature; }
132  gr_basic_block_sptr to_basic_block(); // Needed for Python type coercion
133  bool alias_set() { return !d_symbol_alias.empty(); }
134  std::string alias(){ return alias_set()?d_symbol_alias:symbol_name(); }
135  pmt::pmt_t alias_pmt(){ return pmt::pmt_intern(alias()); }
136  void set_block_alias(std::string name);
137 
138  // ** Message passing interface **
139  void message_port_register_in(pmt::pmt_t port_id);
140  void message_port_register_out(pmt::pmt_t port_id);
141  void message_port_pub(pmt::pmt_t port_id, pmt::pmt_t msg);
142  void message_port_sub(pmt::pmt_t port_id, pmt::pmt_t target);
143  void message_port_unsub(pmt::pmt_t port_id, pmt::pmt_t target);
144 
145  virtual bool message_port_is_hier(pmt::pmt_t port_id) { (void) port_id; std::cout << "is_hier\n"; return false; }
146  virtual bool message_port_is_hier_in(pmt::pmt_t port_id) { (void) port_id; std::cout << "is_hier_in\n"; return false; }
147  virtual bool message_port_is_hier_out(pmt::pmt_t port_id) { (void) port_id; std::cout << "is_hier_out\n"; return false; }
148 
149  /*!
150  * \brief Get input message port names.
151  *
152  * Returns the available input message ports for a block. The
153  * return object is a PMT vector that is filled with PMT symbols.
154  */
155  pmt::pmt_t message_ports_in();
156 
157  /*!
158  * \brief Get output message port names.
159  *
160  * Returns the available output message ports for a block. The
161  * return object is a PMT vector that is filled with PMT symbols.
162  */
163  pmt::pmt_t message_ports_out();
164 
165  /*!
166  * Accept msg, place in queue, arrange for thread to be awakened if it's not already.
167  */
168  void _post(pmt::pmt_t which_port, pmt::pmt_t msg);
169 
170  //! is the queue empty?
171  //bool empty_p(const pmt::pmt_t &which_port) const { return msg_queue[which_port].empty(); }
172  bool empty_p(pmt::pmt_t which_port) {
173  if(msg_queue.find(which_port) == msg_queue.end())
174  throw std::runtime_error("port does not exist!");
175  return msg_queue[which_port].empty();
176  }
177  bool empty_p() {
178  bool rv = true;
179  BOOST_FOREACH(msg_queue_map_t::value_type &i, msg_queue){ rv &= msg_queue[i.first].empty(); }
180  return rv;
181  }
182 
183  //| Acquires and release the mutex
184  void insert_tail( pmt::pmt_t which_port, pmt::pmt_t msg);
185  /*!
186  * \returns returns pmt at head of queue or pmt_t() if empty.
187  */
188  pmt::pmt_t delete_head_nowait( pmt::pmt_t which_port);
189 
190  /*!
191  * \returns returns pmt at head of queue or pmt_t() if empty.
192  */
193  pmt::pmt_t delete_head_blocking( pmt::pmt_t which_port);
194 
195  msg_queue_t::iterator get_iterator(pmt::pmt_t which_port){
196  return msg_queue[which_port].begin();
197  }
198 
199  void erase_msg(pmt::pmt_t which_port, msg_queue_t::iterator it){
200  msg_queue[which_port].erase(it);
201  }
202 
203  virtual bool has_msg_port(pmt::pmt_t which_port){
204  if(msg_queue.find(which_port) != msg_queue.end()){
205  return true;
206  }
207  if(pmt::pmt_dict_has_key(message_subscribers, which_port)){
208  return true;
209  }
210  return false;
211  }
212 
213 
214  /*!
215  * \brief Confirm that ninputs and noutputs is an acceptable combination.
216  *
217  * \param ninputs number of input streams connected
218  * \param noutputs number of output streams connected
219  *
220  * \returns true if this is a valid configuration for this block.
221  *
222  * This function is called by the runtime system whenever the
223  * topology changes. Most classes do not need to override this.
224  * This check is in addition to the constraints specified by the input
225  * and output gr_io_signatures.
226  */
227  virtual bool check_topology(int ninputs, int noutputs) { (void) ninputs; (void) noutputs; return true; }
228 
229  /*!
230  * \brief Set the callback that is fired when messages are available.
231  *
232  * \p msg_handler can be any kind of function pointer or function object
233  * that has the signature:
234  * <pre>
235  * void msg_handler(pmt::pmt msg);
236  * </pre>
237  *
238  * (You may want to use boost::bind to massage your callable into the
239  * correct form. See gr_nop.{h,cc} for an example that sets up a class
240  * method as the callback.)
241  *
242  * Blocks that desire to handle messages must call this method in their
243  * constructors to register the handler that will be invoked when messages
244  * are available.
245  *
246  * If the block inherits from gr_block, the runtime system will ensure that
247  * msg_handler is called in a thread-safe manner, such that work and
248  * msg_handler will never be called concurrently. This allows msg_handler
249  * to update state variables without having to worry about thread-safety
250  * issues with work, general_work or another invocation of msg_handler.
251  *
252  * If the block inherits from gr_hier_block2, the runtime system will
253  * ensure that no reentrant calls are made to msg_handler.
254  */
255  //template <typename T> void set_msg_handler(T msg_handler){
256  // d_msg_handler = msg_handler_t(msg_handler);
257  //}
258  template <typename T> void set_msg_handler(pmt::pmt_t which_port, T msg_handler){
259  if(msg_queue.find(which_port) == msg_queue.end()){
260  throw std::runtime_error("attempt to set_msg_handler() on bad input message port!"); }
261  d_msg_handlers[which_port] = msg_handler_t(msg_handler);
262  }
263 };
264 
266 {
267  return lhs->unique_id() < rhs->unique_id();
268 }
269 
270 typedef std::vector<gr_basic_block_sptr> gr_basic_block_vector_t;
271 typedef std::vector<gr_basic_block_sptr>::iterator gr_basic_block_viter_t;
272 
274 
275 inline std::ostream &operator << (std::ostream &os, gr_basic_block_sptr basic_block)
276 {
277  os << basic_block->name() << "(" << basic_block->unique_id() << ")";
278  return os;
279 }
280 
281 #endif /* INCLUDED_GR_BASIC_BLOCK_H */