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