3 * Copyright 2006,2008,2009 Free Software Foundation, Inc.
5 * This file is part of GNU Radio
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)
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.
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.
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>
32 #include <mb_msg_accepter_smp.h>
33 #include <mbi_runtime_lock.h>
38 static pmt_t s_self = pmt_intern("self");
40 ////////////////////////////////////////////////////////////////////////
43 mb_mblock_impl::port_is_defined(const std::string &name)
45 return d_port_map.count(name) != 0;
49 mb_mblock_impl::comp_is_defined(const std::string &name)
51 return name == "self" || d_comp_map.count(name) != 0;
54 ////////////////////////////////////////////////////////////////////////
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")
63 mb_mblock_impl::~mb_mblock_impl()
65 d_mb = 0; // we don't own it
70 mb_mblock_impl::define_port(const std::string &port_name,
71 const std::string &protocol_class_name,
73 mb_port::port_type_t port_type)
75 mbi_runtime_lock l(this);
77 if (port_is_defined(port_name))
78 throw mbe_duplicate_port(d_mb, port_name);
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;
89 mb_mblock_impl::define_component(const std::string &name,
90 const std::string &class_name,
94 mbi_runtime_lock l(this);
96 if (comp_is_defined(name)) // check for duplicate name
97 throw mbe_duplicate_component(d_mb, name);
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
103 mb_mblock_sptr component =
104 d_runtime->create_component(instance_name() + "/" + name,
105 class_name, user_arg);
107 mbi_runtime_lock l(this);
109 component->d_impl->d_mb_parent = d_mb; // set component's parent link
110 d_comp_map[name] = component;
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)
120 mbi_runtime_lock l(this);
122 mb_endpoint ep0 = check_and_resolve_endpoint(comp_name1, port_name1);
123 mb_endpoint ep1 = check_and_resolve_endpoint(comp_name2, port_name2);
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?
131 d_conn_table.create_conn(ep0, ep1);
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)
140 mbi_runtime_lock l(this);
142 d_conn_table.disconnect(comp_name1, port_name1, comp_name2, port_name2);
143 invalidate_all_port_caches();
147 mb_mblock_impl::disconnect_component(const std::string component_name)
149 mbi_runtime_lock l(this);
151 d_conn_table.disconnect_component(component_name);
152 invalidate_all_port_caches();
156 mb_mblock_impl::disconnect_all()
158 mbi_runtime_lock l(this);
160 d_conn_table.disconnect_all();
161 invalidate_all_port_caches();
165 mb_mblock_impl::nconnections()
167 mbi_runtime_lock l(this);
169 return d_conn_table.nconnections();
172 ////////////////////////////////////////////////////////////////////////
175 mb_mblock_impl::check_and_resolve_endpoint(const std::string &comp_name,
176 const std::string &port_name)
180 mb_port_sptr port = resolve_port(comp_name, port_name);
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.
186 if (comp_name == "self" && port->port_type() == mb_port::EXTERNAL)
187 throw mbe_invalid_port_type(d_mb, comp_name, port_name);
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);
193 return mb_endpoint(comp_name, port_name, port);
197 mb_mblock_impl::resolve_port(const std::string &comp_name,
198 const std::string &port_name)
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];
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);
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));
215 mb_port_sptr c_port = c_impl->d_port_map[port_name];
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));
227 mb_mblock_impl::endpoints_are_compatible(const mb_endpoint &ep0,
228 const mb_endpoint &ep1)
230 pmt_t p0_outgoing = ep0.outgoing_message_set();
231 pmt_t p0_incoming = ep0.incoming_message_set();
233 pmt_t p1_outgoing = ep1.outgoing_message_set();
234 pmt_t p1_incoming = ep1.incoming_message_set();
236 return (pmt_subsetp(p0_outgoing, p1_incoming)
237 && pmt_subsetp(p1_outgoing, p0_incoming));
241 mb_mblock_impl::walk_tree(mb_visitor *visitor)
243 if (!(*visitor)(d_mb))
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)))
255 mb_mblock_impl::make_accepter(pmt_t port_name)
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);
261 return mb_msg_accepter_sptr(ma);
265 mb_mblock_impl::lookup_other_endpoint(const mb_port *port, mb_endpoint *ep)
270 if (!d_conn_table.lookup_conn_by_port(port, &it, &which_ep))
273 *ep = it->d_ep[which_ep^1];
278 mb_mblock_impl::component(const std::string &comp_name)
280 if (comp_name == "self")
281 return d_mb->shared_from_this();
283 if (d_comp_map.count(comp_name) == 0)
284 return mb_mblock_sptr(); // null pointer
286 return d_comp_map[comp_name];
290 mb_mblock_impl::set_instance_name(const std::string &name)
292 d_instance_name = name;
296 mb_mblock_impl::set_class_name(const std::string &name)
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.
307 mb_mblock_impl::invalidate_all_port_caches()
309 class invalidator : public mb_visitor
312 bool operator()(mb_mblock *mblock)
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();
325 // Always true, except in early QA code
326 if (runtime()->top())
327 runtime()->top()->walk_tree(&visitor);