Imported Upstream version 3.2.2
[debian/gnuradio] / mblock / src / lib / mb_mblock.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
26 #include <mblock/mblock.h>
27 #include <mb_mblock_impl.h>
28 #include <mblock/runtime.h>
29 #include <mblock/exception.h>
30 #include <iostream>
31
32
33 static pmt_t s_sys_port = pmt_intern("%sys-port");
34 static pmt_t s_halt = pmt_intern("%halt");
35
36 mb_visitor::~mb_visitor()
37 {
38   // nop base case for virtual destructor.
39 }
40
41
42 mb_mblock::mb_mblock(mb_runtime *runtime,
43                      const std::string &instance_name,
44                      pmt_t user_arg)
45   : d_impl(mb_mblock_impl_sptr(
46                new mb_mblock_impl(dynamic_cast<mb_runtime_base*>(runtime),
47                                   this, instance_name)))
48 {
49 }
50
51 mb_mblock::~mb_mblock()
52 {
53 }
54
55
56 void
57 mb_mblock::initial_transition()
58 {
59   // default implementation does nothing
60 }
61
62 void
63 mb_mblock::handle_message(mb_message_sptr msg)
64 {
65   // default implementation does nothing
66 }
67
68
69 void
70 mb_mblock::main_loop()
71 {
72   while (1){
73     mb_message_sptr msg;
74     try {
75       while (1){
76         msg = impl()->msgq().get_highest_pri_msg();
77
78         // check for %halt from %sys-port
79         if (pmt_eq(msg->port_id(), s_sys_port) && pmt_eq(msg->signal(), s_halt))
80           exit();
81
82         handle_message(msg);
83       }
84     }
85     catch (pmt_exception e){
86       std::cerr << "\nmb_mblock::main_loop: ignored pmt_exception: "
87                 << e.what()
88                 << "\nin mblock instance \"" << instance_name()
89                 << "\" while handling message:"
90                 << "\n    port_id = " << msg->port_id()
91                 << "\n     signal = " << msg->signal()
92                 << "\n       data = " << msg->data()
93                 << "\n  metatdata = " << msg->metadata() << std::endl;
94     }
95   }
96 }
97
98 ////////////////////////////////////////////////////////////////////////
99 //           Forward other methods to implementation class            //
100 ////////////////////////////////////////////////////////////////////////
101
102 mb_port_sptr
103 mb_mblock::define_port(const std::string &port_name_string,
104                        const std::string &protocol_class_name,
105                        bool conjugated,
106                        mb_port::port_type_t port_type)
107 {
108   return d_impl->define_port(port_name_string, protocol_class_name,
109                              conjugated, port_type);
110 }
111
112 void
113 mb_mblock::define_component(const std::string &component_name,
114                             const std::string &class_name,
115                             pmt_t user_arg)
116                 
117 {
118   d_impl->define_component(component_name, class_name, user_arg);
119 }
120
121 void
122 mb_mblock::connect(const std::string &comp_name1, const std::string &port_name1,
123                    const std::string &comp_name2, const std::string &port_name2)
124 {
125   d_impl->connect(comp_name1, port_name1,
126                   comp_name2, port_name2);
127 }                               
128
129
130 void
131 mb_mblock::disconnect(const std::string &comp_name1, const std::string &port_name1,
132                       const std::string &comp_name2, const std::string &port_name2)
133 {
134   d_impl->disconnect(comp_name1, port_name1,
135                      comp_name2, port_name2);
136 }
137
138 void
139 mb_mblock::disconnect_component(const std::string &component_name)
140 {
141   d_impl->disconnect_component(component_name);
142 }
143
144 void
145 mb_mblock::disconnect_all()
146 {
147   d_impl->disconnect_all();
148 }
149
150 int
151 mb_mblock::nconnections() const
152 {
153   return d_impl->nconnections();
154 }
155
156 bool
157 mb_mblock::walk_tree(mb_visitor *visitor)
158 {
159   return d_impl->walk_tree(visitor);
160 }
161
162 std::string
163 mb_mblock::instance_name() const
164 {
165   return d_impl->instance_name();
166 }
167
168 void
169 mb_mblock::set_instance_name(const std::string &name)
170 {
171   d_impl->set_instance_name(name);
172 }
173
174 std::string
175 mb_mblock::class_name() const
176 {
177   return d_impl->class_name();
178 }
179
180 void
181 mb_mblock::set_class_name(const std::string &name)
182 {
183   d_impl->set_class_name(name);
184 }
185
186 mb_mblock *
187 mb_mblock::parent() const
188 {
189   return d_impl->mblock_parent();
190 }
191
192 void
193 mb_mblock::exit()
194 {
195   throw mbe_exit();     // adios...
196 }
197
198 void
199 mb_mblock::shutdown_all(pmt_t result)
200 {
201   d_impl->runtime()->request_shutdown(result);
202 }
203
204 pmt_t
205 mb_mblock::schedule_one_shot_timeout(const mb_time &abs_time, pmt_t user_data)
206 {
207   mb_msg_accepter_sptr accepter = impl()->make_accepter(s_sys_port);
208   return d_impl->runtime()->schedule_one_shot_timeout(abs_time, user_data,
209                                                       accepter);
210 }
211
212 pmt_t
213 mb_mblock::schedule_periodic_timeout(const mb_time &first_abs_time,
214                                      const mb_time &delta_time,
215                                      pmt_t user_data)
216 {
217   mb_msg_accepter_sptr accepter = impl()->make_accepter(s_sys_port);
218   return d_impl->runtime()->schedule_periodic_timeout(first_abs_time,
219                                                       delta_time,
220                                                       user_data,
221                                                       accepter);
222 }
223
224 void
225 mb_mblock::cancel_timeout(pmt_t handle)
226 {
227   d_impl->runtime()->cancel_timeout(handle);
228 }
229