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 2, 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>
35 static pmt_t s_self = pmt_intern("self");
37 ////////////////////////////////////////////////////////////////////////
40 mb_mblock_impl::port_is_defined(const std::string &name)
42 return d_port_map.count(name) != 0;
46 mb_mblock_impl::comp_is_defined(const std::string &name)
48 return name == "self" || d_comp_map.count(name) != 0;
51 ////////////////////////////////////////////////////////////////////////
53 mb_mblock_impl::mb_mblock_impl(mb_mblock *mb)
54 : d_mb(mb), d_mb_parent(0), d_fullname("<unknown>")
58 mb_mblock_impl::~mb_mblock_impl()
60 d_mb = 0; // we don't own it
65 mb_mblock_impl::define_port(const std::string &port_name,
66 const std::string &protocol_class_name,
68 mb_port::port_type_t port_type)
70 if (port_is_defined(port_name))
71 throw mbe_duplicate_port(d_mb, port_name);
74 mb_port_sptr(new mb_port_simple(d_mb,
75 port_name, protocol_class_name,
76 conjugated, port_type));
77 d_port_map[port_name] = p;
82 mb_mblock_impl::define_component(const std::string &name,
83 mb_mblock_sptr component)
85 if (comp_is_defined(name)) // check for duplicate name
86 throw mbe_duplicate_component(d_mb, name);
88 component->d_impl->d_mb_parent = d_mb; // set component's parent link
89 d_comp_map[name] = component;
93 mb_mblock_impl::connect(const std::string &comp_name1,
94 const std::string &port_name1,
95 const std::string &comp_name2,
96 const std::string &port_name2)
98 mb_endpoint ep0 = check_and_resolve_endpoint(comp_name1, port_name1);
99 mb_endpoint ep1 = check_and_resolve_endpoint(comp_name2, port_name2);
101 if (!endpoints_are_compatible(ep0, ep1))
102 throw mbe_incompatible_ports(d_mb,
103 comp_name1, port_name1,
104 comp_name2, port_name2);
105 // FIXME more checks?
107 d_conn_table.create_conn(ep0, ep1);
111 mb_mblock_impl::disconnect(const std::string &comp_name1,
112 const std::string &port_name1,
113 const std::string &comp_name2,
114 const std::string &port_name2)
116 d_conn_table.disconnect(comp_name1, port_name1, comp_name2, port_name2);
120 mb_mblock_impl::disconnect_component(const std::string component_name)
122 d_conn_table.disconnect_component(component_name);
126 mb_mblock_impl::disconnect_all()
128 d_conn_table.disconnect_all();
132 mb_mblock_impl::nconnections() const
134 return d_conn_table.nconnections();
137 ////////////////////////////////////////////////////////////////////////
140 mb_mblock_impl::check_and_resolve_endpoint(const std::string &comp_name,
141 const std::string &port_name)
145 mb_port_sptr port = resolve_port(comp_name, port_name);
147 // Confirm that we're not trying to connect to the inside of one of
148 // our EXTERNAL ports. Connections that include "self" as the
149 // component name must be either INTERNAL or RELAY.
151 if (comp_name == "self" && port->port_type() == mb_port::EXTERNAL)
152 throw mbe_invalid_port_type(d_mb, comp_name, port_name);
154 // Is this endpoint already connected?
155 if (d_conn_table.lookup_conn_by_name(comp_name, port_name, &it, &which_ep))
156 throw mbe_already_connected(d_mb, comp_name, port_name);
158 return mb_endpoint(comp_name, port_name, port);
162 mb_mblock_impl::resolve_port(const std::string &comp_name,
163 const std::string &port_name)
165 if (comp_name == "self"){
166 // Look through our ports.
167 if (!port_is_defined(port_name))
168 throw mbe_no_such_port(d_mb, mb_util::join_names("self", port_name));
169 return d_port_map[port_name];
172 // Look through the specified child's ports.
173 if (!comp_is_defined(comp_name))
174 throw mbe_no_such_component(d_mb, comp_name);
176 mb_mblock_impl_sptr c_impl = d_comp_map[comp_name]->d_impl; // childs impl pointer
177 if (!c_impl->port_is_defined(port_name))
178 throw mbe_no_such_port(d_mb, mb_util::join_names(comp_name, port_name));
180 mb_port_sptr c_port = c_impl->d_port_map[port_name];
182 if (c_port->port_type() == mb_port::INTERNAL) // can't "see" a child's internal ports
183 throw mbe_no_such_port(d_mb, mb_util::join_names(comp_name, port_name));
192 mb_mblock_impl::endpoints_are_compatible(const mb_endpoint &ep0,
193 const mb_endpoint &ep1)
195 pmt_t p0_outgoing = ep0.outgoing_message_set();
196 pmt_t p0_incoming = ep0.incoming_message_set();
198 pmt_t p1_outgoing = ep1.outgoing_message_set();
199 pmt_t p1_incoming = ep1.incoming_message_set();
201 return (pmt_subsetp(p0_outgoing, p1_incoming)
202 && pmt_subsetp(p1_outgoing, p0_incoming));
206 mb_mblock_impl::walk_tree(mb_visitor *visitor, const std::string &path)
208 if (!(*visitor)(d_mb, path))
211 mb_comp_map_t::iterator it;
212 for (it = d_comp_map.begin(); it != d_comp_map.end(); ++it)
213 if (!(it->second->walk_tree(visitor, path + "/" + it->first)))
220 mb_mblock_impl::make_accepter(const std::string port_name)
222 mb_msg_accepter *ma =
223 new mb_msg_accepter_smp(d_mb->shared_from_this(),
224 pmt_intern(port_name));
226 return mb_msg_accepter_sptr(ma);
230 mb_mblock_impl::lookup_other_endpoint(const mb_port *port, mb_endpoint *ep)
235 if (!d_conn_table.lookup_conn_by_port(port, &it, &which_ep))
238 *ep = it->d_ep[which_ep^1];
243 mb_mblock_impl::component(const std::string &comp_name)
245 if (comp_name == "self")
246 return d_mb->shared_from_this();
248 if (d_comp_map.count(comp_name) == 0)
249 return mb_mblock_sptr(); // null pointer
251 return d_comp_map[comp_name];
255 mb_mblock_impl::set_fullname(const std::string &name)