From 13471bef8422b02117b0f1ec2a839f5cb804f1a4 Mon Sep 17 00:00:00 2001 From: Christoph Leitner Date: Tue, 27 Aug 2013 14:36:35 +0200 Subject: [PATCH] added block for measuring round trip times. --- grc/CMakeLists.txt | 1 + grc/foo_rtt_measure.xml | 30 ++++++++++++ include/foo/CMakeLists.txt | 1 + include/foo/rtt_measure.h | 39 ++++++++++++++++ lib/CMakeLists.txt | 1 + lib/rtt_measure_impl.cc | 94 ++++++++++++++++++++++++++++++++++++++ lib/rtt_measure_impl.h | 45 ++++++++++++++++++ swig/foo_swig.i | 3 ++ 8 files changed, 214 insertions(+) create mode 100644 grc/foo_rtt_measure.xml create mode 100644 include/foo/rtt_measure.h create mode 100644 lib/rtt_measure_impl.cc create mode 100644 lib/rtt_measure_impl.h diff --git a/grc/CMakeLists.txt b/grc/CMakeLists.txt index da7bdf9..468f051 100644 --- a/grc/CMakeLists.txt +++ b/grc/CMakeLists.txt @@ -22,6 +22,7 @@ install(FILES foo_packet_dropper.xml foo_packet_pad.xml foo_periodic_msg_source.xml + foo_rtt_measure.xml foo_wireshark_connector.xml DESTINATION share/gnuradio/grc/blocks ) diff --git a/grc/foo_rtt_measure.xml b/grc/foo_rtt_measure.xml new file mode 100644 index 0000000..b4d45fe --- /dev/null +++ b/grc/foo_rtt_measure.xml @@ -0,0 +1,30 @@ + + + RTT measure + foo_rtt_measure + Foo + import foo + foo.rtt_measure($interval) + + + Send interval + interval + 500 + int + + + + in + message + + + + dout + byte + + + + out + message + + diff --git a/include/foo/CMakeLists.txt b/include/foo/CMakeLists.txt index 166431e..ab5f182 100644 --- a/include/foo/CMakeLists.txt +++ b/include/foo/CMakeLists.txt @@ -27,5 +27,6 @@ install(FILES packet_pad.h periodic_msg_source.h wireshark_connector.h + rtt_measure.h DESTINATION include/foo ) diff --git a/include/foo/rtt_measure.h b/include/foo/rtt_measure.h new file mode 100644 index 0000000..d90197d --- /dev/null +++ b/include/foo/rtt_measure.h @@ -0,0 +1,39 @@ +/* + * Copyright (C) 2013 Christoph Leitner + * + * 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_RTT_MEASURE_H +#define INCLUDED_FOO_RTT_MEASURE_H + +#include +#include + +namespace gr { +namespace foo { + + class FOO_API rtt_measure : virtual public gr::block + { + public: + typedef boost::shared_ptr sptr; + static sptr make(unsigned long interval); + }; +} // namespace foo +} // namespace gr + + + + +#endif diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index 6d57f98..018c5db 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -31,6 +31,7 @@ list(APPEND foo_sources packet_pad_impl.cc periodic_msg_source_impl.cc wireshark_connector_impl.cc + rtt_measure_impl.cc ) add_library(gnuradio-foo SHARED ${foo_sources}) diff --git a/lib/rtt_measure_impl.cc b/lib/rtt_measure_impl.cc new file mode 100644 index 0000000..9900407 --- /dev/null +++ b/lib/rtt_measure_impl.cc @@ -0,0 +1,94 @@ +/* + * Copyright (C) 2013 Christoph Leitner + * + * 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 . + */ + +#include "rtt_measure_impl.h" +#include + +using namespace gr::foo; + +rtt_measure_impl::rtt_measure_impl(unsigned long interval) + : block("rtt_measure", + gr::io_signature::make(0, 0, 0), + gr::io_signature::make(1, 1, sizeof(uint8_t))), + d_interval(interval), d_thread(&rtt_measure_impl::run, this), + d_stop(false) +{ + message_port_register_in(pmt::mp("in")); + message_port_register_out(pmt::mp("out")); +} + +rtt_measure_impl::~rtt_measure_impl() { + d_stop.store(true); + d_thread.interrupt(); //if thread was sleeping + d_thread.join(); +} + +void +rtt_measure_impl::run() +{ + // flow graph startup delay + boost::this_thread::sleep(boost::posix_time::milliseconds(500)); + std::string text("fnord"); + pmt::pmt_t msg = pmt::make_blob(text.data(), text.length()); + while(!d_stop.load()){ + gr::thread::scoped_lock lock(d_mutex); + message_port_pub(pmt::mp("out"), pmt::cons(pmt::PMT_NIL, msg)); + d_start_time = boost::posix_time::microsec_clock::local_time(); + lock.unlock(); + boost::this_thread::sleep(boost::posix_time::milliseconds(d_interval)); + } +} + +int +rtt_measure_impl::general_work(int noutput, gr_vector_int& ninput_items, + gr_vector_const_void_star& input_items, + gr_vector_void_star& output_items ) +{ + gr_complex *out = (gr_complex*)output_items[0]; + + pmt::pmt_t msg(delete_head_nowait(pmt::mp("in"))); + if(!msg.get()){ + return 0; + } + boost::posix_time::time_duration dur = + boost::posix_time::microsec_clock::local_time() - start_time(); + std::string time_string(std::to_string(dur.total_microseconds()) + "\n"); + std::cout << time_string; + + if(pmt::is_eof_object(msg)) { + return -1; + } else if(!pmt::is_pair(msg)) { + throw std::invalid_argument("only PDU messages allowed"); + } + int to_copy = time_string.length(); + std::memcpy(out, time_string.data(), to_copy); + + return to_copy; +} + +boost::posix_time::ptime +rtt_measure_impl::start_time() +{ + gr::thread::scoped_lock(d_mutex); + return d_start_time; +} + +rtt_measure::sptr +rtt_measure::make(unsigned long interval) +{ + return gnuradio::get_initial_sptr(new rtt_measure_impl(interval)); +} \ No newline at end of file diff --git a/lib/rtt_measure_impl.h b/lib/rtt_measure_impl.h new file mode 100644 index 0000000..953212e --- /dev/null +++ b/lib/rtt_measure_impl.h @@ -0,0 +1,45 @@ +/* + * Copyright (C) 2013 Christoph Leitner + * + * 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_RTT_MEASURE_IMPL_H +#define INCLUDED_FOO_RTT_MEASURE_IMPL_H + +#include +#include +namespace gr { +namespace foo { + class rtt_measure_impl : public rtt_measure { + private: + unsigned long d_interval; + std::atomic_bool d_stop; + boost::posix_time::ptime d_start_time; + gr::thread::mutex d_mutex; + gr::thread::thread d_thread; + gr::thread::condition_variable d_msg_received; + public: + rtt_measure_impl(unsigned long interval); + ~rtt_measure_impl(); + void run(); + int general_work(int noutput, gr_vector_int& ninput_items, + gr_vector_const_void_star& input_items, + gr_vector_void_star& output_items ); + boost::posix_time::ptime start_time(); + }; +} +} + +#endif diff --git a/swig/foo_swig.i b/swig/foo_swig.i index b6d6bb5..b0e19bd 100644 --- a/swig/foo_swig.i +++ b/swig/foo_swig.i @@ -11,6 +11,7 @@ #include "foo/packet_pad.h" #include "foo/periodic_msg_source.h" #include "foo/wireshark_connector.h" +#include "foo/rtt_measure.h" %} @@ -19,6 +20,7 @@ %include "foo/packet_pad.h" %include "foo/periodic_msg_source.h" %include "foo/wireshark_connector.h" +%include "foo/rtt_measure.h" GR_SWIG_BLOCK_MAGIC2(foo, burst_tagger); @@ -26,3 +28,4 @@ GR_SWIG_BLOCK_MAGIC2(foo, packet_dropper); GR_SWIG_BLOCK_MAGIC2(foo, packet_pad); GR_SWIG_BLOCK_MAGIC2(foo, periodic_msg_source); GR_SWIG_BLOCK_MAGIC2(foo, wireshark_connector); +GR_SWIG_BLOCK_MAGIC2(foo, rtt_measure);