|
TOBI Interface A
0.1
|
00001 /* 00002 This file is part of the TOBI Interface A (TiA) library. 00003 00004 Commercial Usage 00005 Licensees holding valid Graz University of Technology Commercial 00006 licenses may use this file in accordance with the Graz University 00007 of Technology Commercial License Agreement provided with the 00008 Software or, alternatively, in accordance with the terms contained in 00009 a written agreement between you and Graz University of Technology. 00010 00011 -------------------------------------------------- 00012 00013 GNU Lesser General Public License Usage 00014 Alternatively, this file may be used under the terms of the GNU Lesser 00015 General Public License version 3.0 as published by the Free Software 00016 Foundation and appearing in the file lgpl.txt included in the 00017 packaging of this file. Please review the following information to 00018 ensure the GNU General Public License version 3.0 requirements will be 00019 met: http://www.gnu.org/copyleft/lgpl.html. 00020 00021 In case of GNU Lesser General Public License Usage ,the TiA library 00022 is distributed in the hope that it will be useful, 00023 but WITHOUT ANY WARRANTY; without even the implied warranty of 00024 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00025 GNU General Public License for more details. 00026 00027 You should have received a copy of the GNU Lesser General Public License 00028 along with the TiA library. If not, see <http://www.gnu.org/licenses/>. 00029 00030 Copyright 2010 Graz University of Technology 00031 Contact: TiA@tobi-project.org 00032 */ 00033 00039 // STL 00040 #include <sstream> 00041 00042 // boost 00043 #include <boost/bind.hpp> 00044 00045 // Standard 00046 #include <assert.h> 00047 #include <iostream> 00048 00049 // local 00050 #include "tia-private/network/udp_data_server.h" 00051 #include "tia/data_packet_interface.h" 00052 00053 namespace tia 00054 { 00055 00056 using std::cout; 00057 using std::cerr; 00058 using std::endl; 00059 using boost::uint16_t; 00060 using boost::uint32_t; 00061 00062 //----------------------------------------------------------------------------- 00063 00064 UDPDataServer::UDPDataServer(boost::asio::io_service& io_service) 00065 : io_service_(io_service), 00066 socket_(io_service, boost::asio::ip::udp::endpoint(boost::asio::ip::udp::v4(), 0)), 00067 num_clients_(0) 00068 { 00069 boost::asio::socket_base::broadcast bcast(true); 00070 socket_.set_option(bcast); 00071 } 00072 00073 //----------------------------------------------------------------------------- 00074 00075 void UDPDataServer::setDestination(const std::string& address, uint16_t port) 00076 { 00077 boost::asio::ip::udp::resolver resolver(io_service_); 00078 std::ostringstream ss; 00079 ss << port; 00080 boost::asio::ip::udp::resolver::query query(address, ss.str()); 00081 target_ = *resolver.resolve(query); 00082 } 00083 00084 //----------------------------------------------------------------------------- 00085 00086 void UDPDataServer::sendDataPacket(DataPacket& packet) 00087 { 00088 if (num_clients_ == 0) 00089 return; 00090 00091 void* data = packet.getRaw(); 00092 uint32_t size = packet.getRawMemorySize(); 00093 00094 assert(data != 0); 00095 assert(size != 0); 00096 #ifdef DEBUG 00097 cout << "UDPDataServer::sendDataPacket: broadcasting data packet at " 00098 << target_.address().to_string() << ":" << target_.port() << endl; 00099 #endif 00100 socket_.async_send_to(boost::asio::buffer(data, size), 00101 target_, 00102 boost::bind(&UDPDataServer::handleWrite, 00103 this, 00104 boost::asio::placeholders::error, 00105 boost::asio::placeholders::bytes_transferred)); 00106 } 00107 00108 //----------------------------------------------------------------------------- 00109 00110 void UDPDataServer::handleWrite(const boost::system::error_code& error, 00111 size_t /*bytes_transferred*/) 00112 { 00113 if (error) 00114 { 00115 cerr << "UDPDataServer::handleWrite: broadcasting data packet failed -- Error:" << endl; 00116 cerr << "--> " << error.message() << endl; 00117 return; 00118 } 00119 } 00120 00121 //----------------------------------------------------------------------------- 00122 00123 } // Namespace tobiss