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