/*************************************************************************** * Copyright (C) 2007 by Rick L. Vinyard, Jr. * * rvinyard@cs.nmsu.edu * * * * This file is part of the conexus library. * * * * The conexus library is free software; you can redistribute it and/or * * modify it under the terms of the GNU General Public License version 3 * * as published by the Free Software Foundation. * * * * The conexus library 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 software. If not see <http://www.gnu.org/licenses/>. * ***************************************************************************/ #include <conexus.h> #include <sys/types.h> #include <unistd.h> #include <iostream> #include <glibmm.h> // prototypes of the three print callback functions void print_data1(const Conexus::Data d); void print_data2(const Conexus::Data d); void print_data3(const Conexus::Data d); int main(int argc, char* argv[]) { Glib::ustring interface; int port = 1500; int run_time = 20; Glib::OptionContext option_context( "- IPv4 UDP Server" ); Glib::OptionGroup option_group( "Options", "" ); Glib::OptionEntry option_interface; option_interface.set_long_name( "if" ); option_interface.set_description( "Interface address to receive data on [ default = any ]" ); option_group.add_entry( option_interface, interface ); Glib::OptionEntry option_port; option_port.set_long_name( "port" ); option_port.set_description( "Port to receive data on [default=1500]" ); option_group.add_entry( option_port, port ); Glib::OptionEntry option_run_time; option_run_time.set_long_name( "run" ); option_run_time.set_description( "Running time in seconds [default=20]" ); option_group.add_entry( option_run_time, run_time ); option_context.set_main_group( option_group ); option_context.parse( argc, argv ); // Initialize the Conexus library // This is needed because we will use threaded servers and this // will do all the behind the scenes work to initialize pthreads Conexus::init(); // Declare the udp object Conexus::IPv4::UDP::pointer udp; Conexus::Endpoint::pointer ptr = Conexus::default_factory().create_endpoint( "IPv4::UDP" ); udp = conexus_dynamic_pointer_cast<Conexus::IPv4::UDP>(ptr); udp->local_address().set_port( port ); if ( interface.size() > 0 ) udp->local_address().set_host(interface); // The server connect method connects a provided sigc++ slot that will be called // back when the server receives any data. ptr->signal_data().connect(sigc::ptr_fun(&print_data1)); ptr->signal_data().connect(sigc::ptr_fun(&print_data2)); ptr->signal_data().connect(sigc::ptr_fun(&print_data3)); // Start the server. The server will spawn a separate thread to service // received data, so this call will immediately return after the thread // is spawned. ptr->start(); std::cout << "Main thread pid: " << pthread_self() << std::endl; // Set up a loop that will run and print the time every 5 // seconds. Since the server is threaded, the sleep(1) call will not effect // the servicing thread. std::cout << "Starting..." << std::endl; std::cout << "Run time: " << run_time << std::endl; for (int i=0; i <= run_time; i++) { if (i%5 == 0) std::cout << "Time: " << i << std::endl; sleep(1); } std::cout << "Time: " << run_time << std::endl; // Stop the server and prepare for shutdown ptr->stop(); return 0; } // These are the three callback functions. Each simply prints out the data received. void print_data1( const Conexus::Data d) { std::cout << "Responding thread pid: " << pthread_self() << std::endl; std::cout << "<1> Received " << d.size() << " bytes of data [" << d.hex_string() << "]\n"; } void print_data2( const Conexus::Data d) { std::cout << "Responding thread pid: " << pthread_self() << std::endl; std::cout << "<2> Received " << d.size() << " bytes of data [" << d.hex_string() << "]\n"; } void print_data3( const Conexus::Data d) { std::cout << "Responding thread pid: " << pthread_self() << std::endl; std::cout << "<3> Received " << d.size() << " bytes of data [" << d.hex_string() << "]\n"; }