3 * Copyright 2006 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 <mb_mblock.h>
27 #include <mb_protocol_class.h>
29 #include <mb_port_simple.h>
30 #include <mb_exception.h>
32 #include <mb_msg_accepter_smp.h>
33 #include <mbi_runtime_lock.h>
37 static pmt_t s_self = pmt_intern("self");
39 ////////////////////////////////////////////////////////////////////////
42 mb_mblock_impl::port_is_defined(const std::string &name)
44 return d_port_map.count(name) != 0;
48 mb_mblock_impl::comp_is_defined(const std::string &name)
50 return name == "self" || d_comp_map.count(name) != 0;
53 ////////////////////////////////////////////////////////////////////////
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")
62 mb_mblock_impl::~mb_mblock_impl()
64 d_mb = 0; // we don't own it
69 mb_mblock_impl::define_port(const std::string &port_name,
70 const std::string &protocol_class_name,
72 mb_port::port_type_t port_type)
74 mbi_runtime_lock l(this);
76 if (port_is_defined(port_name))
77 throw mbe_duplicate_port(d_mb, port_name);
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;
88 mb_mblock_impl::define_component(const std::string &name,
89 const std::string &class_name,
93 mbi_runtime_lock l(this);
95 if (comp_is_defined(name)) // check for duplicate name
96 throw mbe_duplicate_component(d_mb, name);
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
102 mb_mblock_sptr component =
103 d_runtime->create_component(instance_name() + "/" + name,
104 class_name, user_arg);
106 mbi_runtime_lock l(this);
108 component->d_impl->d_mb_parent = d_mb; // set component's parent link
109 d_comp_map[name] = component;
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)
119 mbi_runtime_lock l(this);
121 mb_endpoint ep0 = check_and_resolve_endpoint(comp_name1, port_name1);
122 mb_endpoint ep1 = check_and_resolve_endpoint(comp_name2, port_name2);
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?
130 d_conn_table.create_conn(ep0, ep1);
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)
139 mbi_runtime_lock l(this);
141 d_conn_table.disconnect(comp_name1, port_name1, comp_name2, port_name2);
142 invalidate_all_port_caches();
146 mb_mblock_impl::disconnect_component(const std::string component_name)
148 mbi_runtime_lock l(this);
150 d_conn_table.disconnect_component(component_name);
151 invalidate_all_port_caches();
155 mb_mblock_impl::disconnect_all()
157 mbi_runtime_lock l(this);
159 d_conn_table.disconnect_all();
160 invalidate_all_port_caches();
164 mb_mblock_impl::nconnections()
166 mbi_runtime_lock l(this);
168 return d_conn_table.nconnections();
171 ////////////////////////////////////////////////////////////////////////
174 mb_mblock_impl::check_and_resolve_endpoint(const std::string &comp_name,
175 const std::string &port_name)
179 mb_port_sptr port = resolve_port(comp_name, port_name);
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.
185 if (comp_name == "self" && port->port_type() == mb_port::EXTERNAL)
186 throw mbe_invalid_port_type(d_mb, comp_name, port_name);
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);
192 return mb_endpoint(comp_name, port_name, port);
196 mb_mblock_impl::resolve_port(const std::string &comp_name,
197 const std::string &port_name)
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];
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);
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));
214 mb_port_sptr c_port = c_impl->d_port_map[port_name];
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));
226 mb_mblock_impl::endpoints_are_compatible(const mb_endpoint &ep0,
227 const mb_endpoint &ep1)
229 pmt_t p0_outgoing = ep0.outgoing_message_set();
230 pmt_t p0_incoming = ep0.incoming_message_set();
232 pmt_t p1_outgoing = ep1.outgoing_message_set();
233 pmt_t p1_incoming = ep1.incoming_message_set();
235 return (pmt_subsetp(p0_outgoing, p1_incoming)
236 && pmt_subsetp(p1_outgoing, p0_incoming));
240 mb_mblock_impl::walk_tree(mb_visitor *visitor)
242 if (!(*visitor)(d_mb))
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)))
254 mb_mblock_impl::make_accepter(pmt_t port_name)
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);
260 return mb_msg_accepter_sptr(ma);
264 mb_mblock_impl::lookup_other_endpoint(const mb_port *port, mb_endpoint *ep)
269 if (!d_conn_table.lookup_conn_by_port(port, &it, &which_ep))
272 *ep = it->d_ep[which_ep^1];
277 mb_mblock_impl::component(const std::string &comp_name)
279 if (comp_name == "self")
280 return d_mb->shared_from_this();
282 if (d_comp_map.count(comp_name) == 0)
283 return mb_mblock_sptr(); // null pointer
285 return d_comp_map[comp_name];
289 mb_mblock_impl::set_instance_name(const std::string &name)
291 d_instance_name = name;
295 mb_mblock_impl::set_class_name(const std::string &name)
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.
306 mb_mblock_impl::invalidate_all_port_caches()
308 class invalidator : public mb_visitor
311 bool operator()(mb_mblock *mblock)
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();
324 // Always true, except in early QA code
325 if (runtime()->top())
326 runtime()->top()->walk_tree(&visitor);