Convert gr-audio-portaudio to Boost via gruel
[debian/gnuradio] / mblock / src / lib / mb_mblock_impl.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2006,2008,2009 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
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25 #include <mb_mblock_impl.h>
26 #include <mblock/mblock.h>
27 #include <mblock/protocol_class.h>
28 #include <mblock/port.h>
29 #include <mb_port_simple.h>
30 #include <mblock/exception.h>
31 #include <mb_util.h>
32 #include <mb_msg_accepter_smp.h>
33 #include <mbi_runtime_lock.h>
34 #include <iostream>
35
36 using namespace pmt;
37
38 static pmt_t s_self = pmt_intern("self");
39
40 ////////////////////////////////////////////////////////////////////////
41
42 bool 
43 mb_mblock_impl::port_is_defined(const std::string &name)
44 {
45   return d_port_map.count(name) != 0;
46 }
47
48 bool
49 mb_mblock_impl::comp_is_defined(const std::string &name)
50 {
51   return name == "self" || d_comp_map.count(name) != 0;
52 }
53
54 ////////////////////////////////////////////////////////////////////////
55
56 mb_mblock_impl::mb_mblock_impl(mb_runtime_base *runtime, mb_mblock *mb,
57                                const std::string &instance_name)
58   : d_runtime(runtime), d_mb(mb), d_mb_parent(0), 
59     d_instance_name(instance_name), d_class_name("mblock")
60 {
61 }
62
63 mb_mblock_impl::~mb_mblock_impl()
64 {
65   d_mb = 0;     // we don't own it
66 }
67
68
69 mb_port_sptr
70 mb_mblock_impl::define_port(const std::string &port_name,
71                             const std::string &protocol_class_name,
72                             bool conjugated,
73                             mb_port::port_type_t port_type)
74 {
75   mbi_runtime_lock      l(this);
76
77   if (port_is_defined(port_name))
78     throw mbe_duplicate_port(d_mb, port_name);
79
80   mb_port_sptr p =
81     mb_port_sptr(new mb_port_simple(d_mb,
82                                     port_name, protocol_class_name,
83                                     conjugated, port_type));
84   d_port_map[port_name] = p;
85   return p;
86 }
87
88 void
89 mb_mblock_impl::define_component(const std::string &name,
90                                  const std::string &class_name,
91                                  pmt_t user_arg)
92 {
93   {
94     mbi_runtime_lock    l(this);
95
96     if (comp_is_defined(name))  // check for duplicate name
97       throw mbe_duplicate_component(d_mb, name);
98   }
99
100   // We ask the runtime to create the component so that it can worry about
101   // mblock placement on a NUMA machine or on a distributed multicomputer
102
103   mb_mblock_sptr component =
104     d_runtime->create_component(instance_name() + "/" + name,
105                                 class_name, user_arg);
106   {
107     mbi_runtime_lock    l(this);
108
109     component->d_impl->d_mb_parent = d_mb;     // set component's parent link
110     d_comp_map[name] = component;
111   }
112 }
113
114 void
115 mb_mblock_impl::connect(const std::string &comp_name1,
116                         const std::string &port_name1,
117                         const std::string &comp_name2,
118                         const std::string &port_name2)
119 {
120   mbi_runtime_lock      l(this);
121
122   mb_endpoint   ep0 = check_and_resolve_endpoint(comp_name1, port_name1);
123   mb_endpoint   ep1 = check_and_resolve_endpoint(comp_name2, port_name2);
124
125   if (!endpoints_are_compatible(ep0, ep1))
126     throw mbe_incompatible_ports(d_mb,
127                                  comp_name1, port_name1,
128                                  comp_name2, port_name2);
129   // FIXME more checks?
130
131   d_conn_table.create_conn(ep0, ep1);
132 }
133
134 void
135 mb_mblock_impl::disconnect(const std::string &comp_name1,
136                            const std::string &port_name1,
137                            const std::string &comp_name2,
138                            const std::string &port_name2)
139 {
140   mbi_runtime_lock      l(this);
141
142   d_conn_table.disconnect(comp_name1, port_name1, comp_name2, port_name2);
143   invalidate_all_port_caches();
144 }
145
146 void
147 mb_mblock_impl::disconnect_component(const std::string component_name)
148 {
149   mbi_runtime_lock      l(this);
150
151   d_conn_table.disconnect_component(component_name);
152   invalidate_all_port_caches();
153 }
154
155 void
156 mb_mblock_impl::disconnect_all()
157 {
158   mbi_runtime_lock      l(this);
159
160   d_conn_table.disconnect_all();
161   invalidate_all_port_caches();
162 }
163
164 int
165 mb_mblock_impl::nconnections()
166 {
167   mbi_runtime_lock      l(this);
168
169   return d_conn_table.nconnections();
170 }
171
172 ////////////////////////////////////////////////////////////////////////
173
174 mb_endpoint
175 mb_mblock_impl::check_and_resolve_endpoint(const std::string &comp_name,
176                                            const std::string &port_name)
177 {
178   mb_conn_iter  it;
179   int           which_ep;
180   mb_port_sptr  port = resolve_port(comp_name, port_name);
181
182   // Confirm that we're not trying to connect to the inside of one of
183   // our EXTERNAL ports.  Connections that include "self" as the
184   // component name must be either INTERNAL or RELAY.
185
186   if (comp_name == "self" && port->port_type() == mb_port::EXTERNAL)
187     throw mbe_invalid_port_type(d_mb, comp_name, port_name);
188
189   // Is this endpoint already connected?
190   if (d_conn_table.lookup_conn_by_name(comp_name, port_name, &it, &which_ep))
191     throw mbe_already_connected(d_mb, comp_name, port_name);
192
193   return mb_endpoint(comp_name, port_name, port);
194 }
195
196 mb_port_sptr
197 mb_mblock_impl::resolve_port(const std::string &comp_name,
198                              const std::string &port_name)
199 {
200   if (comp_name == "self"){
201     // Look through our ports.
202     if (!port_is_defined(port_name))
203       throw mbe_no_such_port(d_mb, mb_util::join_names("self", port_name));
204     return d_port_map[port_name];
205   }
206   else {
207     // Look through the specified child's ports.
208     if (!comp_is_defined(comp_name))
209       throw mbe_no_such_component(d_mb, comp_name);
210     
211     mb_mblock_impl_sptr  c_impl = d_comp_map[comp_name]->d_impl;  // childs impl pointer
212     if (!c_impl->port_is_defined(port_name))
213       throw mbe_no_such_port(d_mb, mb_util::join_names(comp_name, port_name));
214
215     mb_port_sptr c_port = c_impl->d_port_map[port_name];
216
217     if (c_port->port_type() == mb_port::INTERNAL) // can't "see" a child's internal ports
218       throw mbe_no_such_port(d_mb, mb_util::join_names(comp_name, port_name));
219
220     return c_port;
221   }
222 }
223
224
225
226 bool
227 mb_mblock_impl::endpoints_are_compatible(const mb_endpoint &ep0,
228                                          const mb_endpoint &ep1)
229 {
230   pmt_t p0_outgoing = ep0.outgoing_message_set();
231   pmt_t p0_incoming = ep0.incoming_message_set();
232
233   pmt_t p1_outgoing = ep1.outgoing_message_set();
234   pmt_t p1_incoming = ep1.incoming_message_set();
235
236   return (pmt_subsetp(p0_outgoing, p1_incoming)
237           && pmt_subsetp(p1_outgoing, p0_incoming));
238 }
239
240 bool
241 mb_mblock_impl::walk_tree(mb_visitor *visitor)
242 {
243   if (!(*visitor)(d_mb))
244     return false;
245
246   mb_comp_map_t::iterator it;
247   for (it = d_comp_map.begin(); it != d_comp_map.end(); ++it)
248     if (!(it->second->walk_tree(visitor)))
249       return false;
250
251   return true;
252 }
253
254 mb_msg_accepter_sptr
255 mb_mblock_impl::make_accepter(pmt_t port_name)
256 {
257   // FIXME this should probably use some kind of configurable factory
258   mb_msg_accepter *ma =
259     new mb_msg_accepter_smp(d_mb->shared_from_this(), port_name);
260
261   return mb_msg_accepter_sptr(ma);
262 }
263
264 bool
265 mb_mblock_impl::lookup_other_endpoint(const mb_port *port, mb_endpoint *ep)
266 {
267   mb_conn_iter  it;
268   int           which_ep = 0;
269
270   if (!d_conn_table.lookup_conn_by_port(port, &it, &which_ep))
271     return false;
272   
273   *ep = it->d_ep[which_ep^1];
274   return true;
275 }
276
277 mb_mblock_sptr
278 mb_mblock_impl::component(const std::string &comp_name)
279 {
280   if (comp_name == "self")
281     return d_mb->shared_from_this();
282
283   if (d_comp_map.count(comp_name) == 0)
284     return mb_mblock_sptr();    // null pointer
285
286   return d_comp_map[comp_name];
287 }
288
289 void
290 mb_mblock_impl::set_instance_name(const std::string &name)
291 {
292   d_instance_name = name;
293 }
294
295 void
296 mb_mblock_impl::set_class_name(const std::string &name)
297 {
298   d_class_name = name;
299 }
300
301 /*
302  * This is the "Big Hammer" port cache invalidator.
303  * It invalidates _all_ of the port caches in the entire mblock tree.
304  * It's overkill, but was simple to code.
305  */
306 void
307 mb_mblock_impl::invalidate_all_port_caches()
308 {
309   class invalidator : public mb_visitor
310   {
311   public:
312     bool operator()(mb_mblock *mblock)
313     {
314       mb_mblock_impl_sptr impl = mblock->impl();
315       mb_port_map_t::iterator it = impl->d_port_map.begin();
316       mb_port_map_t::iterator end = impl->d_port_map.end();
317       for (; it != end; ++it)
318         it->second->invalidate_cache();
319       return true;
320     }
321   };
322
323   invalidator visitor;
324
325   // Always true, except in early QA code
326   if (runtime()->top())
327     runtime()->top()->walk_tree(&visitor);
328 }