1 /* -*- c++ -*- */
2 /*
3  * Copyright 2012-2013 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
18  * along with GNU Radio; see the file COPYING.  If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street,
20  * Boston, MA 02110-1301, USA.
21  */
22 
23 #include <gnuradio/basic_block.h>
24 #include <gnuradio/block.h>
25 #include <gnuradio/block_detail.h>
26 #include <gnuradio/block_registry.h>
27 #include <gnuradio/tpb_detail.h>
28 #include <stdio.h>
29 
30 gr::block_registry global_block_registry;
31 
32 namespace gr {
33 
block_registry()34 block_registry::block_registry() { d_ref_map = pmt::make_dict(); }
35 
block_register(basic_block * block)36 long block_registry::block_register(basic_block* block)
37 {
38     gr::thread::scoped_lock guard(d_mutex);
39 
40     if (d_map.find(block->name()) == d_map.end()) {
41         d_map[block->name()] = blocksubmap_t();
42         d_map[block->name()][0] = block;
43         return 0;
44     } else {
45         for (size_t i = 0; i <= d_map[block->name()].size(); i++) {
46             if (d_map[block->name()].find(i) == d_map[block->name()].end()) {
47                 d_map[block->name()][i] = block;
48                 return i;
49             }
50         }
51     }
52     throw std::runtime_error("should not reach this");
53 }
54 
block_unregister(basic_block * block)55 void block_registry::block_unregister(basic_block* block)
56 {
57     gr::thread::scoped_lock guard(d_mutex);
58 
59     d_map[block->name()].erase(d_map[block->name()].find(block->symbolic_id()));
60     d_ref_map = pmt::dict_delete(d_ref_map, pmt::intern(block->symbol_name()));
61     if (block->alias_set()) {
62         d_ref_map = pmt::dict_delete(d_ref_map, pmt::intern(block->alias()));
63     }
64 }
65 
register_symbolic_name(basic_block * block)66 std::string block_registry::register_symbolic_name(basic_block* block)
67 {
68     std::stringstream ss;
69     ss << block->name() << block->symbolic_id();
70     // std::cout << "register_symbolic_name: " << ss.str() << std::endl;
71     register_symbolic_name(block, ss.str());
72     return ss.str();
73 }
74 
register_symbolic_name(basic_block * block,std::string name)75 void block_registry::register_symbolic_name(basic_block* block, std::string name)
76 {
77     gr::thread::scoped_lock guard(d_mutex);
78 
79     if (pmt::dict_has_key(d_ref_map, pmt::intern(name))) {
80         throw std::runtime_error("symbol already exists, can not re-use!");
81     }
82     d_ref_map = pmt::dict_add(d_ref_map, pmt::intern(name), pmt::make_any(block));
83 }
84 
update_symbolic_name(basic_block * block,std::string name)85 void block_registry::update_symbolic_name(basic_block* block, std::string name)
86 {
87     gr::thread::scoped_lock guard(d_mutex);
88 
89     if (pmt::dict_has_key(d_ref_map, pmt::intern(name))) {
90         throw std::runtime_error("symbol already exists, can not re-use!");
91     }
92 
93     // If we don't already have an alias, don't try and delete it.
94     if (block->alias_set()) {
95         // And make sure that the registry has the alias key.
96         // We test both in case the block's and registry ever get out of sync.
97         if (pmt::dict_has_key(d_ref_map, block->alias_pmt())) {
98             d_ref_map = pmt::dict_delete(d_ref_map, block->alias_pmt());
99         }
100     }
101     d_ref_map = pmt::dict_add(d_ref_map, pmt::intern(name), pmt::make_any(block));
102 }
103 
block_lookup(pmt::pmt_t symbol)104 basic_block_sptr block_registry::block_lookup(pmt::pmt_t symbol)
105 {
106     gr::thread::scoped_lock guard(d_mutex);
107 
108     pmt::pmt_t ref = pmt::dict_ref(d_ref_map, symbol, pmt::PMT_NIL);
109     if (pmt::eq(ref, pmt::PMT_NIL)) {
110         throw std::runtime_error("block lookup failed! block not found!");
111     }
112     basic_block* blk = boost::any_cast<basic_block*>(pmt::any_ref(ref));
113     return blk->shared_from_this();
114 }
115 
register_primitive(std::string blk,block * ref)116 void block_registry::register_primitive(std::string blk, block* ref)
117 {
118     gr::thread::scoped_lock guard(d_mutex);
119 
120     primitive_map[blk] = ref;
121 }
122 
unregister_primitive(std::string blk)123 void block_registry::unregister_primitive(std::string blk)
124 {
125     gr::thread::scoped_lock guard(d_mutex);
126 
127     primitive_map.erase(primitive_map.find(blk));
128 }
129 
notify_blk(std::string blk)130 void block_registry::notify_blk(std::string blk)
131 {
132     gr::thread::scoped_lock guard(d_mutex);
133 
134     if (primitive_map.find(blk) == primitive_map.end()) {
135         /* In later GNU Radio versions, we throw here:
136          *
137          * throw std::runtime_error("block notify failed: block not found!");
138          *
139          * But since this is an API change that we'd like to avoid on a maintenance
140          * branch, we're not doing that on maint-3.8.
141          */
142         return;
143     }
144     if (primitive_map[blk]->detail().get()) {
145         primitive_map[blk]->detail()->d_tpb.notify_msg();
146     } else {
147         // not having block detail is not necessarily a problem; this will happen when
148         // publishing a message to a block that exists but has not yet been started
149     }
150 }
151 
152 } /* namespace gr */
153