From 5779237e7edadc6e179424652a2f938153f31d30 Mon Sep 17 00:00:00 2001 From: Paul Garver Date: Thu, 21 Apr 2016 16:02:56 -0400 Subject: [PATCH] Added random periodic message source with seed intialization --- grc/CMakeLists.txt | 2 +- grc/foo_random_periodic_msg_source.xml | 67 +++++++++ include/foo/CMakeLists.txt | 2 +- include/foo/random_periodic_msg_source.h | 49 +++++++ lib/CMakeLists.txt | 1 + lib/random_periodic_msg_source_impl.cc | 174 +++++++++++++++++++++++ lib/random_periodic_msg_source_impl.h | 67 +++++++++ swig/foo_swig.i | 4 +- 8 files changed, 363 insertions(+), 3 deletions(-) create mode 100644 grc/foo_random_periodic_msg_source.xml create mode 100644 include/foo/random_periodic_msg_source.h create mode 100644 lib/random_periodic_msg_source_impl.cc create mode 100644 lib/random_periodic_msg_source_impl.h diff --git a/grc/CMakeLists.txt b/grc/CMakeLists.txt index 0aed470..dc56521 100644 --- a/grc/CMakeLists.txt +++ b/grc/CMakeLists.txt @@ -25,5 +25,5 @@ install(FILES foo_periodic_msg_source.xml foo_rtt_measure.xml foo_wireshark_connector.xml - DESTINATION share/gnuradio/grc/blocks + foo_random_periodic_msg_source.xml DESTINATION share/gnuradio/grc/blocks ) diff --git a/grc/foo_random_periodic_msg_source.xml b/grc/foo_random_periodic_msg_source.xml new file mode 100644 index 0000000..15e2ce9 --- /dev/null +++ b/grc/foo_random_periodic_msg_source.xml @@ -0,0 +1,67 @@ + + + + Random Periodic Message Source + foo_random_periodic_msg_source + Foo + import foo + import pmt + foo.random_periodic_msg_source($msg_len, $interval, $num, $quit, $debug, $seed) + + Message Length + msg_len + 64 + int + + + Period (ms) + interval + 1000 + real + + + Quit + quit + True + bool + + + + + Debug + debug + False + bool + + + + + Num Messages + num + 1 + int + + + Random Seed + seed + 1 + int + + + out + message + 1 + + diff --git a/include/foo/CMakeLists.txt b/include/foo/CMakeLists.txt index adee36c..ef576d2 100644 --- a/include/foo/CMakeLists.txt +++ b/include/foo/CMakeLists.txt @@ -29,5 +29,5 @@ install(FILES periodic_msg_source.h wireshark_connector.h rtt_measure.h - DESTINATION include/foo + random_periodic_msg_source.h DESTINATION include/foo ) diff --git a/include/foo/random_periodic_msg_source.h b/include/foo/random_periodic_msg_source.h new file mode 100644 index 0000000..d599653 --- /dev/null +++ b/include/foo/random_periodic_msg_source.h @@ -0,0 +1,49 @@ +/* + * Copyright (C) 2016 Bastian Bloessl + * 2016 Paul Garver + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#ifndef INCLUDED_FOO_RANDOM_PERIODIC_MSG_SOURCE_H +#define INCLUDED_FOO_RANDOM_PERIODIC_MSG_SOURCE_H + +#include +#include + +namespace gr { +namespace foo { + +class FOO_API random_periodic_msg_source : virtual public gr::block +{ +public: + + typedef boost::shared_ptr sptr; + static sptr make(int msg_len, float interval, int num_msg = 1, + bool quit = true, bool debug = false, int seed = 0); + + virtual void set_nmsg(int nmsg) = 0; + virtual int get_nmsg() = 0; + + virtual void set_delay(float delay) = 0; + virtual float get_delay() = 0; + + virtual void start_tx() = 0; + virtual void stop_tx() = 0; + virtual bool is_running() = 0; +}; + +} // namespace foo +} // namespace gr + +#endif /* INCLUDED_FOO_RANDOM_PERIODIC_MSG_SOURCE */ diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index deb5a00..d6b8e5c 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -39,6 +39,7 @@ list(APPEND foo_sources periodic_msg_source_impl.cc wireshark_connector_impl.cc rtt_measure_impl.cc + random_periodic_msg_source_impl.cc ) set(PACKAGE_FIND_VERSION_MAJOR 0) diff --git a/lib/random_periodic_msg_source_impl.cc b/lib/random_periodic_msg_source_impl.cc new file mode 100644 index 0000000..3689b14 --- /dev/null +++ b/lib/random_periodic_msg_source_impl.cc @@ -0,0 +1,174 @@ +/* + * Copyright (C) 2013 Bastian Bloessl + * Copyright (C) 2016 Paul Garver + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +// Just like periodic_msg_source but random data for every message +#include "random_periodic_msg_source_impl.h" + +#include +#include +#include + +using namespace gr::foo; + +#define dout d_debug && std::cout + +random_periodic_msg_source_impl::random_periodic_msg_source_impl(int msg_len, + float interval, int num_msg, bool quit, bool debug, int seed): + block("random_periodic_msg_source", + gr::io_signature::make(0, 0, 0), + gr::io_signature::make(0, 0, 0)), + d_msg_len(msg_len), + d_nmsg_total(num_msg), + d_nmsg_left(num_msg), + d_interval(interval), + d_debug(debug), + d_finished(false), + d_quit(quit), + d_seed(static_cast(seed)), + d_brange(0,255), + d_rng(seed), + d_randbytes(d_rng,d_brange) +{ + message_port_register_out(pmt::mp("out")); + + d_thread = new boost::thread(boost::bind(&random_periodic_msg_source_impl::run, this, this)); +} + +random_periodic_msg_source_impl::~random_periodic_msg_source_impl() { + gr::thread::scoped_lock(d_mutex); + d_finished = true; + d_thread->interrupt(); + d_thread->join(); + delete d_thread; +} + +void +random_periodic_msg_source_impl::run(random_periodic_msg_source_impl *instance) { + + try { + + // flow graph startup delay + boost::this_thread::sleep(boost::posix_time::milliseconds(500)); + + while(1) { + float delay; + { + gr::thread::scoped_lock(d_mutex); + if(d_finished || !d_nmsg_left) { + d_finished = true; + if(d_quit) { + boost::this_thread::sleep(boost::posix_time::milliseconds(500)); + message_port_pub( pmt::mp("out"), pmt::PMT_EOF); + post(pmt::mp("system"), pmt::cons(pmt::mp("done"), pmt::from_long(1))); + } + break; + } + + dout << "PMS: number of messages left: " << d_nmsg_left << std::endl; + // Generate a random message + pmt::pmt_t msg = generate_msg(); + message_port_pub( pmt::mp("out"), msg); + + if(d_nmsg_left > 0) { + d_nmsg_left--; + } + + delay = d_interval; + } + boost::this_thread::sleep(boost::posix_time::milliseconds(delay)); + } + + } catch(boost::thread_interrupted) { + gr::thread::scoped_lock(d_mutex); + dout << "PMS: thread interrupted" << std::endl; + d_finished = true; + if(d_quit) { + boost::this_thread::sleep(boost::posix_time::milliseconds(500)); + message_port_pub( pmt::mp("out"), pmt::PMT_EOF); + post(pmt::mp("system"), pmt::cons(pmt::mp("done"), pmt::from_long(1))); + } + } +} + +// Generate a random message +pmt::pmt_t +random_periodic_msg_source_impl::generate_msg(){ + // Construct random vector of specified length + std::vector vec(d_msg_len); + // Fill w/random bytes + for( int i=0; ijoin(); + delete d_thread; + + d_thread = new boost::thread(boost::bind(&random_periodic_msg_source_impl::run, this, this)); +} + +void +random_periodic_msg_source_impl::stop_tx() { + d_thread->interrupt(); +} + +bool +random_periodic_msg_source_impl::is_running() { + return !d_finished; +} + +random_periodic_msg_source::sptr +random_periodic_msg_source::make(int msg_len, float interval, int num_msg, + bool quit, bool debug,int seed) { + return gnuradio::get_initial_sptr(new random_periodic_msg_source_impl(msg_len, interval, num_msg, quit, debug, seed)); +} + diff --git a/lib/random_periodic_msg_source_impl.h b/lib/random_periodic_msg_source_impl.h new file mode 100644 index 0000000..dfbd706 --- /dev/null +++ b/lib/random_periodic_msg_source_impl.h @@ -0,0 +1,67 @@ +/* + * Copyright (C) 2013 Bastian Bloessl + * Copyright (C) 2016 Paul Garver + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#ifndef INCLUDED_FOO_RANDOM_PERIODIC_MSG_SOURCE_IMPL_H +#define INCLUDED_FOO_RANDOM_PERIODIC_MSG_SOURCE_IMPL_H + +#include +#include +#include + +namespace gr { +namespace foo { + + class random_periodic_msg_source_impl : public random_periodic_msg_source { + private: + void run(random_periodic_msg_source_impl *instance); + int d_msg_len; + int d_nmsg_total; + int d_nmsg_left; + unsigned int d_seed; + bool d_debug; + bool d_quit; + bool d_finished; + float d_interval; + boost::thread *d_thread; + gr::thread::mutex d_mutex; + boost::mt19937 d_rng; + boost::uniform_int<> d_brange; + boost::variate_generator< boost::mt19937, boost::uniform_int<> > d_randbytes; + + public: + random_periodic_msg_source_impl(int msg_len, + float interval, int num_msg, + bool quit, bool debug, int seed); + virtual ~random_periodic_msg_source_impl(); + + void set_nmsg(int nmsg); + int get_nmsg(); + + void set_delay(float delay); + float get_delay(); + + void start_tx(); + void stop_tx(); + pmt::pmt_t generate_msg(); + bool is_running(); + + }; + +} // namespace foo +} // namespace gr + +#endif /* INCLUDED_FOO_RANDOM_PERIODIC_MSG_SOURCE_IMPL_H */ diff --git a/swig/foo_swig.i b/swig/foo_swig.i index d781fd0..4841b67 100644 --- a/swig/foo_swig.i +++ b/swig/foo_swig.i @@ -13,6 +13,7 @@ #include "foo/periodic_msg_source.h" #include "foo/wireshark_connector.h" #include "foo/rtt_measure.h" +#include "foo/random_periodic_msg_source.h" %} @@ -23,7 +24,7 @@ %include "foo/periodic_msg_source.h" %include "foo/wireshark_connector.h" %include "foo/rtt_measure.h" - +%include "foo/random_periodic_msg_source.h" GR_SWIG_BLOCK_MAGIC2(foo, burst_tagger); GR_SWIG_BLOCK_MAGIC2(foo, packet_dropper); @@ -32,3 +33,4 @@ GR_SWIG_BLOCK_MAGIC2(foo, packet_pad2); GR_SWIG_BLOCK_MAGIC2(foo, periodic_msg_source); GR_SWIG_BLOCK_MAGIC2(foo, wireshark_connector); GR_SWIG_BLOCK_MAGIC2(foo, rtt_measure); +GR_SWIG_BLOCK_MAGIC2(foo, random_periodic_msg_source);