summaryrefslogtreecommitdiffstats
path: root/host
diff options
context:
space:
mode:
authorJosh Blum <josh@joshknows.com>2011-12-20 14:37:34 -0800
committerJosh Blum <josh@joshknows.com>2011-12-20 14:37:34 -0800
commita6d3483bf9d98ea3a645b11c1f70ca01def794b3 (patch)
tree0f62b01b2212da17a2d721913cb0580f1b4d04fc /host
parent1fef438d8ba54d07760a21e8e8b50c05b7ddc051 (diff)
downloaduhd-a6d3483bf9d98ea3a645b11c1f70ca01def794b3.tar.gz
uhd-a6d3483bf9d98ea3a645b11c1f70ca01def794b3.tar.bz2
uhd-a6d3483bf9d98ea3a645b11c1f70ca01def794b3.zip
usrp2: created network relay example app
Diffstat (limited to 'host')
-rw-r--r--host/examples/CMakeLists.txt1
-rw-r--r--host/examples/network_relay.cpp194
2 files changed, 195 insertions, 0 deletions
diff --git a/host/examples/CMakeLists.txt b/host/examples/CMakeLists.txt
index bad7b72a6..4f3650e63 100644
--- a/host/examples/CMakeLists.txt
+++ b/host/examples/CMakeLists.txt
@@ -20,6 +20,7 @@
########################################################################
SET(example_sources
benchmark_rate.cpp
+ network_relay.cpp
rx_multi_samples.cpp
rx_samples_to_file.cpp
rx_samples_to_udp.cpp
diff --git a/host/examples/network_relay.cpp b/host/examples/network_relay.cpp
new file mode 100644
index 000000000..f9f2e33fc
--- /dev/null
+++ b/host/examples/network_relay.cpp
@@ -0,0 +1,194 @@
+//
+// Copyright 2010-2011 Ettus Research LLC
+//
+// 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/>.
+//
+
+#include <uhd/utils/thread_priority.hpp>
+#include <uhd/utils/safe_main.hpp>
+#include <boost/program_options.hpp>
+#include <boost/thread/thread.hpp>
+#include <boost/format.hpp>
+#include <boost/asio.hpp>
+#include <iostream>
+#include <csignal>
+#include <vector>
+
+namespace po = boost::program_options;
+namespace asio = boost::asio;
+
+typedef boost::shared_ptr<asio::ip::udp::socket> socket_type;
+
+static const size_t insane_mtu = 9000;
+
+boost::mutex spawn_mutex;
+
+/***********************************************************************
+ * Signal handlers
+ **********************************************************************/
+static bool stop_signal_called = false;
+void sig_int_handler(int){stop_signal_called = true;}
+
+static bool wait_for_recv_ready(int sock_fd){
+ //setup timeval for timeout
+ timeval tv;
+ tv.tv_sec = 0;
+ tv.tv_usec = 100000; //100ms
+
+ //setup rset for timeout
+ fd_set rset;
+ FD_ZERO(&rset);
+ FD_SET(sock_fd, &rset);
+
+ //call select with timeout on receive socket
+ return ::select(sock_fd+1, &rset, NULL, NULL, &tv) > 0;
+}
+
+/***********************************************************************
+ * Relay class
+ **********************************************************************/
+class udp_relay_type{
+public:
+ udp_relay_type(
+ const std::string &server_addr,
+ const std::string &client_addr,
+ const std::string &port
+ ):_port(port){
+ {
+ asio::ip::udp::resolver resolver(_io_service);
+ asio::ip::udp::resolver::query query(asio::ip::udp::v4(), server_addr, port);
+ asio::ip::udp::endpoint endpoint = *resolver.resolve(query);
+
+ _server_socket = boost::shared_ptr<asio::ip::udp::socket>(new asio::ip::udp::socket(_io_service, endpoint));
+ }
+ {
+ asio::ip::udp::resolver resolver(_io_service);
+ asio::ip::udp::resolver::query query(asio::ip::udp::v4(), client_addr, port);
+ asio::ip::udp::endpoint endpoint = *resolver.resolve(query);
+
+ _client_socket = boost::shared_ptr<asio::ip::udp::socket>(new asio::ip::udp::socket(_io_service));
+ _client_socket->open(asio::ip::udp::v4());
+ _client_socket->connect(endpoint);
+ }
+
+ std::cout << "spawning relay threads..." << std::endl;
+ _thread_group.create_thread(boost::bind(&udp_relay_type::server_thread, this));
+ spawn_mutex.lock();
+ spawn_mutex.lock();
+ spawn_mutex.unlock();
+ _thread_group.create_thread(boost::bind(&udp_relay_type::client_thread, this));
+ spawn_mutex.lock();
+ spawn_mutex.lock();
+ spawn_mutex.unlock();
+ std::cout << " done" << std::endl;
+ }
+
+ ~udp_relay_type(void){
+ std::cout << "killing relay threads..." << std::endl;
+ _thread_group.interrupt_all();
+ _thread_group.join_all();
+ std::cout << " done" << std::endl;
+ }
+
+private:
+
+ void server_thread(void){
+ std::cout << "entering server_thread... " << _port << std::endl;
+ spawn_mutex.unlock();
+ std::vector<char> buff(insane_mtu);
+ while (not boost::this_thread::interruption_requested()){
+ if (wait_for_recv_ready(_server_socket->native())){
+ boost::mutex::scoped_lock lock(_endpoint_mutex);
+ const size_t len = _server_socket->receive_from(asio::buffer(&buff.front(), buff.size()), _endpoint);
+ lock.unlock();
+ //std::cout << " len " << len << std::endl;
+ _client_socket->send(asio::buffer(&buff.front(), len));
+ }
+ }
+ std::cout << "exiting server_thread... " << _port << std::endl;
+ }
+
+ void client_thread(void){
+ std::cout << "entering client_thread... " << _port << std::endl;
+ spawn_mutex.unlock();
+ std::vector<char> buff(insane_mtu);
+ while (not boost::this_thread::interruption_requested()){
+ if (wait_for_recv_ready(_client_socket->native())){
+ const size_t len = _client_socket->receive(asio::buffer(&buff.front(), buff.size()));
+ //std::cout << " len " << len << std::endl;
+ boost::mutex::scoped_lock lock(_endpoint_mutex);
+ _server_socket->send_to(asio::buffer(&buff.front(), len), _endpoint);
+ }
+ }
+ std::cout << "exiting client_thread... " << _port << std::endl;
+ }
+
+ const std::string _port;
+ boost::thread_group _thread_group;
+ asio::io_service _io_service;
+ asio::ip::udp::endpoint _endpoint;
+ boost::mutex _endpoint_mutex;
+ socket_type _server_socket, _client_socket;
+};
+
+
+/***********************************************************************
+ * Main
+ **********************************************************************/
+int UHD_SAFE_MAIN(int argc, char *argv[]){
+ uhd::set_thread_priority_safe();
+
+ //variables to be set by po
+ std::string addr;
+
+ //setup the program options
+ po::options_description desc("Allowed options");
+ desc.add_options()
+ ("help", "help message")
+ ("addr", po::value<std::string>(&addr)->default_value(""), "the resolvable address of the usrp")
+ ;
+ po::variables_map vm;
+ po::store(po::parse_command_line(argc, argv, desc), vm);
+ po::notify(vm);
+
+ //print the help message
+ if (vm.count("help")){
+ std::cout
+ << boost::format("UHD Network Relay %s") % desc << std::endl
+ << "Runs a network relay between UHD on one computer and a USRP on the network.\n"
+ << "This example is basically for test purposes. Use at your own convenience.\n"
+ << std::endl;
+ return ~0;
+ }
+
+ {
+ boost::shared_ptr<udp_relay_type> ctrl(new udp_relay_type("0.0.0.0", addr, "49152"));
+ boost::shared_ptr<udp_relay_type> rxdsp0(new udp_relay_type("0.0.0.0", addr, "49156"));
+ boost::shared_ptr<udp_relay_type> txdsp0(new udp_relay_type("0.0.0.0", addr, "49157"));
+ boost::shared_ptr<udp_relay_type> rxdsp1(new udp_relay_type("0.0.0.0", addr, "49158"));
+ boost::shared_ptr<udp_relay_type> gps(new udp_relay_type("0.0.0.0", addr, "49172"));
+
+ std::signal(SIGINT, &sig_int_handler);
+ std::cout << "Press Ctrl + C to stop streaming..." << std::endl;
+
+ while (not stop_signal_called){
+ boost::this_thread::sleep(boost::posix_time::milliseconds(100));
+ }
+ }
+
+ //finished
+ std::cout << std::endl << "Done!" << std::endl << std::endl;
+
+ return 0;
+}