/*************************************************************************** * Copyright (C) 2001 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( "- IPv6 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::IPv6::UDP::pointer udp; if ( interface.size() == 0 ) udp = Conexus::IPv6::UDP::create(port); else udp = Conexus::IPv6::UDP::create(interface, port); // The server connect method connects a provided sigc++ slot that will be called // back when the server receives any data. udp->signal_data().connect(sigc::ptr_fun(&print_data1)); udp->signal_data().connect(sigc::ptr_fun(&print_data2)); udp->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. udp->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. 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 udp->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 (" << (uint64_t)(d.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 (" << (uint64_t)(d.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 (" << (uint64_t)(d.data()) << ") [" << d.hex_string() << "]\n"; }