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 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26 
27 #include "message_strobe_random_impl.h"
28 #include <gnuradio/io_signature.h>
29 #include <errno.h>
30 #include <fcntl.h>
31 #include <string.h>
32 #include <sys/stat.h>
33 #include <sys/types.h>
34 #include <cstdio>
35 #include <iostream>
36 #include <stdexcept>
37 
38 namespace gr {
39 namespace blocks {
40 
41 message_strobe_random::sptr
make(pmt::pmt_t msg,message_strobe_random_distribution_t dist,float mean_ms,float std_ms)42 message_strobe_random::make(pmt::pmt_t msg,
43                             message_strobe_random_distribution_t dist,
44                             float mean_ms,
45                             float std_ms)
46 {
47     return gnuradio::get_initial_sptr(
48         new message_strobe_random_impl(msg, dist, mean_ms, std_ms));
49 }
50 
51 
message_strobe_random_impl(pmt::pmt_t msg,message_strobe_random_distribution_t dist,float mean_ms,float std_ms)52 message_strobe_random_impl::message_strobe_random_impl(
53     pmt::pmt_t msg,
54     message_strobe_random_distribution_t dist,
55     float mean_ms,
56     float std_ms)
57     : block("message_strobe_random",
58             io_signature::make(0, 0, 0),
59             io_signature::make(0, 0, 0)),
60       d_finished(false),
61       d_mean_ms(mean_ms),
62       d_std_ms(std_ms),
63       d_dist(dist),
64       d_msg(msg),
65       d_rng(),
66       d_port(pmt::mp("strobe"))
67 {
68     // allocate RNGs
69     update_dist();
70 
71     // set up ports
72     message_port_register_out(d_port);
73     d_thread = boost::shared_ptr<gr::thread::thread>(
74         new gr::thread::thread(boost::bind(&message_strobe_random_impl::run, this)));
75 
76     message_port_register_in(pmt::mp("set_msg"));
77     set_msg_handler(pmt::mp("set_msg"), [this](pmt::pmt_t msg) { this->set_msg(msg); });
78 }
79 
next_delay()80 long message_strobe_random_impl::next_delay()
81 {
82     switch (d_dist) {
83     case STROBE_POISSON:
84         // return d_variate_poisson->operator()();
85         return static_cast<long>(d_variate_poisson->operator()());
86     case STROBE_GAUSSIAN:
87         return static_cast<long>(d_variate_normal->operator()());
88     case STROBE_UNIFORM:
89         return static_cast<long>(d_variate_uniform->operator()());
90     default:
91         throw std::runtime_error(
92             "message_strobe_random_impl::d_distribution is very unhappy with you");
93     }
94 }
95 
update_dist()96 void message_strobe_random_impl::update_dist()
97 {
98     boost::poisson_distribution<> pd(d_mean_ms);
99     d_variate_poisson = boost::shared_ptr<
100         boost::variate_generator<boost::mt19937, boost::poisson_distribution<>>>(
101         new boost::variate_generator<boost::mt19937, boost::poisson_distribution<>>(d_rng,
102                                                                                     pd));
103 
104     boost::normal_distribution<> nd(d_mean_ms, d_std_ms);
105     d_variate_normal = boost::shared_ptr<
106         boost::variate_generator<boost::mt19937, boost::normal_distribution<>>>(
107         new boost::variate_generator<boost::mt19937, boost::normal_distribution<>>(d_rng,
108                                                                                    nd));
109 
110     boost::uniform_real<> ud(d_mean_ms - d_std_ms, d_mean_ms + d_std_ms);
111     d_variate_uniform = boost::shared_ptr<
112         boost::variate_generator<boost::mt19937, boost::uniform_real<>>>(
113         new boost::variate_generator<boost::mt19937, boost::uniform_real<>>(d_rng, ud));
114 }
115 
116 
~message_strobe_random_impl()117 message_strobe_random_impl::~message_strobe_random_impl()
118 {
119     d_finished = true;
120     d_thread->interrupt();
121     d_thread->join();
122 }
123 
run()124 void message_strobe_random_impl::run()
125 {
126     while (!d_finished) {
127         boost::this_thread::sleep(
128             boost::posix_time::milliseconds(std::max(0L, next_delay())));
129         if (d_finished) {
130             return;
131         }
132 
133         message_port_pub(d_port, d_msg);
134     }
135 }
136 
137 } /* namespace blocks */
138 } /* namespace gr */
139