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