Added random periodic message source with seed intialization

This commit is contained in:
Paul Garver
2016-04-21 16:02:56 -04:00
parent 396086a6e5
commit 5779237e7e
8 changed files with 363 additions and 3 deletions
+1 -1
View File
@@ -25,5 +25,5 @@ install(FILES
foo_periodic_msg_source.xml foo_periodic_msg_source.xml
foo_rtt_measure.xml foo_rtt_measure.xml
foo_wireshark_connector.xml foo_wireshark_connector.xml
DESTINATION share/gnuradio/grc/blocks foo_random_periodic_msg_source.xml DESTINATION share/gnuradio/grc/blocks
) )
+67
View File
@@ -0,0 +1,67 @@
<?xml version="1.0"?>
<block>
<name>Random Periodic Message Source</name>
<key>foo_random_periodic_msg_source</key>
<category>Foo</category>
<import>import foo</import>
<import>import pmt</import>
<make>foo.random_periodic_msg_source($msg_len, $interval, $num, $quit, $debug, $seed)</make>
<param>
<name>Message Length</name>
<key>msg_len</key>
<value>64</value>
<type>int</type>
</param>
<param>
<name>Period (ms)</name>
<key>interval</key>
<value>1000</value>
<type>real</type>
</param>
<param>
<name>Quit</name>
<key>quit</key>
<value>True</value>
<type>bool</type>
<option>
<name>Enable</name>
<key>True</key>
</option>
<option>
<name>Disable</name>
<key>False</key>
</option>
</param>
<param>
<name>Debug</name>
<key>debug</key>
<value>False</value>
<type>bool</type>
<option>
<name>Enable</name>
<key>True</key>
</option>
<option>
<name>Disable</name>
<key>False</key>
</option>
</param>
<param>
<name>Num Messages</name>
<key>num</key>
<value>1</value>
<type>int</type>
</param>
<param>
<name>Random Seed</name>
<key>seed</key>
<value>1</value>
<type>int</type>
</param>
<source>
<name>out</name>
<type>message</type>
<optional>1</optional>
</source>
</block>
+1 -1
View File
@@ -29,5 +29,5 @@ install(FILES
periodic_msg_source.h periodic_msg_source.h
wireshark_connector.h wireshark_connector.h
rtt_measure.h rtt_measure.h
DESTINATION include/foo random_periodic_msg_source.h DESTINATION include/foo
) )
+49
View File
@@ -0,0 +1,49 @@
/*
* Copyright (C) 2016 Bastian Bloessl <bloessl@ccs-labs.org>
* 2016 Paul Garver <garverp@gatech.edu>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#ifndef INCLUDED_FOO_RANDOM_PERIODIC_MSG_SOURCE_H
#define INCLUDED_FOO_RANDOM_PERIODIC_MSG_SOURCE_H
#include <foo/api.h>
#include <gnuradio/block.h>
namespace gr {
namespace foo {
class FOO_API random_periodic_msg_source : virtual public gr::block
{
public:
typedef boost::shared_ptr<random_periodic_msg_source> 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 */
+1
View File
@@ -39,6 +39,7 @@ list(APPEND foo_sources
periodic_msg_source_impl.cc periodic_msg_source_impl.cc
wireshark_connector_impl.cc wireshark_connector_impl.cc
rtt_measure_impl.cc rtt_measure_impl.cc
random_periodic_msg_source_impl.cc
) )
set(PACKAGE_FIND_VERSION_MAJOR 0) set(PACKAGE_FIND_VERSION_MAJOR 0)
+174
View File
@@ -0,0 +1,174 @@
/*
* Copyright (C) 2013 Bastian Bloessl <bloessl@ccs-labs.org>
* Copyright (C) 2016 Paul Garver <garverp@gatech.edu>
*
* 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 <http://www.gnu.org/licenses/>.
*/
// Just like periodic_msg_source but random data for every message
#include "random_periodic_msg_source_impl.h"
#include <gnuradio/io_signature.h>
#include <gnuradio/block_detail.h>
#include <string.h>
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<unsigned int>(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<unsigned char> vec(d_msg_len);
// Fill w/random bytes
for( int i=0; i<d_msg_len; i++ ){
vec[i] = ((unsigned char) d_randbytes());
}
// Construct the PDU
pmt::pmt_t vecpmt(pmt::make_blob(&vec[0], d_msg_len));
return pmt::cons(pmt::PMT_NIL, vecpmt);
}
void
random_periodic_msg_source_impl::set_nmsg(int nmsg) {
gr::thread::scoped_lock(d_mutex);
d_nmsg_total = nmsg;
}
int
random_periodic_msg_source_impl::get_nmsg() {
gr::thread::scoped_lock(d_mutex);
return d_nmsg_total;
}
void
random_periodic_msg_source_impl::set_delay(float delay) {
gr::thread::scoped_lock(d_mutex);
d_interval = delay;
}
float
random_periodic_msg_source_impl::get_delay() {
gr::thread::scoped_lock(d_mutex);
return d_interval;
}
void
random_periodic_msg_source_impl::start_tx() {
gr::thread::scoped_lock(d_mutex);
if(is_running()) return;
d_nmsg_left = d_nmsg_total;
d_finished = false;
d_thread->join();
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));
}
+67
View File
@@ -0,0 +1,67 @@
/*
* Copyright (C) 2013 Bastian Bloessl <bloessl@ccs-labs.org>
* Copyright (C) 2016 Paul Garver <garverp@gatech.edu>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#ifndef INCLUDED_FOO_RANDOM_PERIODIC_MSG_SOURCE_IMPL_H
#define INCLUDED_FOO_RANDOM_PERIODIC_MSG_SOURCE_IMPL_H
#include <foo/random_periodic_msg_source.h>
#include <boost/random.hpp>
#include <boost/generator_iterator.hpp>
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 */
+3 -1
View File
@@ -13,6 +13,7 @@
#include "foo/periodic_msg_source.h" #include "foo/periodic_msg_source.h"
#include "foo/wireshark_connector.h" #include "foo/wireshark_connector.h"
#include "foo/rtt_measure.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/periodic_msg_source.h"
%include "foo/wireshark_connector.h" %include "foo/wireshark_connector.h"
%include "foo/rtt_measure.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, burst_tagger);
GR_SWIG_BLOCK_MAGIC2(foo, packet_dropper); 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, periodic_msg_source);
GR_SWIG_BLOCK_MAGIC2(foo, wireshark_connector); GR_SWIG_BLOCK_MAGIC2(foo, wireshark_connector);
GR_SWIG_BLOCK_MAGIC2(foo, rtt_measure); GR_SWIG_BLOCK_MAGIC2(foo, rtt_measure);
GR_SWIG_BLOCK_MAGIC2(foo, random_periodic_msg_source);