Imported Upstream version 3.2.2
[debian/gnuradio] / mblock / src / lib / mb_mblock_impl.h
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2006,2007,2008 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 along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21 #ifndef INCLUDED_MB_MBLOCK_IMPL_H
22 #define INCLUDED_MB_MBLOCK_IMPL_H
23
24 #include <mblock/mblock.h>
25 #include <mb_runtime_base.h>
26 #include <mb_connection.h>
27 #include <mblock/msg_queue.h>
28 #include <list>
29 #include <map>
30
31
32 typedef std::map<std::string, mb_port_sptr>   mb_port_map_t;
33 typedef std::map<std::string, mb_mblock_sptr> mb_comp_map_t;
34
35
36 /*!
37  * \brief The private implementation details of the mblock system.
38  */
39 class mb_mblock_impl : boost::noncopyable
40 {
41   mb_runtime_base              *d_runtime;      // pointer to runtime
42   mb_mblock                    *d_mb;           // pointer to our associated mblock
43   mb_mblock                    *d_mb_parent;    // pointer to our parent
44
45   std::string                   d_instance_name;    // hierarchical name
46   std::string                   d_class_name;       // name of this (derived) class
47
48   mb_port_map_t                 d_port_map;     // our ports
49   mb_comp_map_t                 d_comp_map;     // our components
50   mb_conn_table                 d_conn_table;   // our connections
51
52   mb_msg_queue                  d_msgq;         // incoming messages for us
53
54 public:
55   mb_mblock_impl(mb_runtime_base *runtime, mb_mblock *mb,
56                  const std::string &instance_name);
57   ~mb_mblock_impl();
58
59   /*!
60    * \brief Define a port.
61    *
62    * EXTERNAL and RELAY ports are part of our peer interface.
63    * INTERNAL ports are used to talk to sub-components.
64    *
65    * \param port_name    The name of the port (must be unique within this mblock).
66    * \param protocol_class_name The name of the protocol class associated with
67    *                            this port.  It must already be defined.
68    * \param conjugated   Are the incoming and outgoing message sets swapped?
69    * \param port_type    INTERNAL, EXTERNAL or RELAY.
70    */
71   mb_port_sptr
72   define_port(const std::string &port_name,
73               const std::string &protocol_class_name,
74               bool conjugated,
75               mb_port::port_type_t port_type);
76
77   /*!
78    * \brief Define a subcomponent by name.
79    *
80    * Called within the constructor to tell the system the
81    * names and identities of our sub-component mblocks.
82    *
83    * \param component_name  The name of the sub-component (must be unique with this mblock).
84    * \param class_name      The class of the instance that is to be created.
85    * \param user_arg The argument to pass to the constructor of the component.
86    */
87   void
88   define_component(const std::string &component_name,
89                    const std::string &class_name,
90                    pmt_t user_arg);
91
92   /*!
93    * \brief connect endpoint_1 to endpoint_2
94    *
95    * \param comp_name1  component on one end of the connection
96    * \param port_name1  the name of the port on comp1
97    * \param comp_name2  component on the other end of the connection
98    * \param port_name2  the name of the port on comp2
99    *
100    * An endpoint is specified by the component's local name (given as
101    * component_name in the call to register_component) and the name of
102    * the port on that component.
103    *
104    * To connect an internal or relay port, use "self" as the component name.
105    */
106   void
107   connect(const std::string &comp_name1, const std::string &port_name1,
108           const std::string &comp_name2, const std::string &port_name2);
109
110   /*!
111    * \brief disconnect endpoint_1 from endpoint_2
112    *
113    * \param comp_name1  component on one end of the connection
114    * \param port_name1  the name of the port on comp1
115    * \param comp_name2  component on the other end of the connection
116    * \param port_name2  the name of the port on comp2
117    *
118    * An endpoint is specified by the component's local name (given as
119    * component_name in the call to register_component) and the name of
120    * the port on that component.
121    *
122    * To disconnect an internal or relay port, use "self" as the component name.
123    */
124   void
125   disconnect(const std::string &comp_name1, const std::string &port_name1,
126              const std::string &comp_name2, const std::string &port_name2);
127
128   /*!
129    * \brief disconnect all connections to specified component
130    * \param component_name component to disconnect
131    */
132   void
133   disconnect_component(const std::string component_name);
134
135   /*!
136    * \brief disconnect all connections to all components
137    */
138   void
139   disconnect_all();
140
141   /*!
142    * \brief Return number of connections (QA mostly)
143    */
144   int
145   nconnections();
146
147   bool
148   walk_tree(mb_visitor *visitor);
149   
150   mb_msg_accepter_sptr
151   make_accepter(pmt_t port_name);
152
153   mb_msg_queue &
154   msgq() { return d_msgq; }
155
156   //! Return instance name of this block
157   std::string instance_name() const { return d_instance_name; }
158
159   //! Set the instance name of this block
160   void set_instance_name(const std::string &name);
161
162   //! Return the class name of this block
163   std::string class_name() const { return d_class_name; }
164
165   //! Set the class name
166   void set_class_name(const std::string &name);
167
168   /*!
169    * \brief If bound, store endpoint from the other end of the connection.
170    *
171    * \param port [in]  port the port that we're searching for.
172    * \param ep   [out] the other end point from the matching connection.
173    *
174    * \returns true iff there's a matching connection.
175    */
176   bool
177   lookup_other_endpoint(const mb_port *port, mb_endpoint *ep);
178
179
180   //! Return point to associated mblock
181   mb_mblock *mblock() const { return d_mb; }
182
183   //! Return pointer to the parent of our mblock
184   mb_mblock *mblock_parent() const { return d_mb_parent; }
185
186   //! Lookup a component by name
187   mb_mblock_sptr component(const std::string &comp_name);
188
189   //! Return the runtime instance
190   mb_runtime_base *runtime() { return d_runtime; }
191
192   //! Set the runtime instance
193   void set_runtime(mb_runtime_base *runtime) { d_runtime = runtime; }
194
195   /*
196    * Our implementation methods
197    */
198 private:
199   //bool port_is_defined(pmt_t name);
200   bool port_is_defined(const std::string &name);
201   //bool comp_is_defined(pmt_t name);
202   bool comp_is_defined(const std::string &name);
203
204   mb_endpoint 
205   check_and_resolve_endpoint(const std::string &comp_name,
206                              const std::string &port_name);
207
208
209   mb_port_sptr
210   resolve_port(const std::string &comp_name,
211                const std::string &port_name);
212
213   static bool
214   endpoints_are_compatible(const mb_endpoint &ep0,
215                            const mb_endpoint &ep1);
216
217   /*!
218    * \brief walk mblock tree and invalidate all port resolution caches.
219    * \internal
220    */
221   void
222   invalidate_all_port_caches();
223 };
224
225
226 #endif /* INCLUDED_MB_MBLOCK_IMPL_H */