ros2udpProxy.cpp
Go to the documentation of this file.
00001 
00002 #include <string>
00003 #include <sstream>
00004 #include <iostream>
00005 #include <unistd.h>
00006 #include "ros/ros.h"
00007 #include <ros/transport_hints.h>
00008 #include <stdio.h>
00009 
00010 
00011 #include <SystemConfig.h>
00012 #include <Configuration.h>
00013 #include <exception>
00014 
00015 #include <boost/asio.hpp>
00016 #include <boost/thread.hpp> 
00017 
00018 #include <sys/ioctl.h>
00019 #include <net/if.h>
00020 #include <netinet/in.h>
00021 #include <arpa/inet.h>
00022 
00023 
00024 <?messageIncludes?>
00025 
00026 using namespace castor;
00027 
00028 
00029 
00030 using boost::asio::ip::udp;
00031 
00032 
00033 
00034 std::string ownRosName;
00035 udp::socket* insocket;
00036 udp::endpoint otherEndPoint;
00037 udp::endpoint destEndPoint;
00038 boost::asio::ip::address multiCastAddress;
00039 boost::asio::io_service io_service;
00040 void handleUdpPacket(const boost::system::error_code& error,   std::size_t bytes_transferred);
00041 void listenForPacket();
00042 
00043 
00044 
00045 <?rosMessageHandler?>
00046 
00047 <?rosPublisherDecl?>
00048 
00049 boost::array<char,64000> inBuffer;
00050 void listenForPacket() {
00051         insocket->async_receive_from(boost::asio::buffer(inBuffer), otherEndPoint,
00052         boost::bind(&handleUdpPacket, boost::asio::placeholders::error,boost::asio::placeholders::bytes_transferred));
00053 }
00054 void handleUdpPacket(const boost::system::error_code& error,   std::size_t bytes_transferred) {
00055         //std::cout << "From "<<otherEndPoint.address() << std::endl;
00056         if (!error) { // && otherEndPoint.address() != localIP) {
00057                 uint32_t id = *((uint32_t*)(inBuffer.data()));
00058                 //std::cout << "Got packet"<<std::endl;
00059                 try {   
00060                         ros::serialization::IStream stream(((uint8_t*)inBuffer.data())+4,bytes_transferred-4);
00061                         switch(id) {
00062                                 <?udpReception?>                        
00063                         
00064                                 default:
00065                                         std::cerr << "Cannot find Matching topic:" << id << std::endl;
00066                         }
00067                 }
00068                 catch(std::exception& e) {
00069                         ROS_ERROR_STREAM_THROTTLE(2,"Exception while receiving DDS message:"<<e.what()<< " Discarding message!");
00070                 }
00071                 
00072         }
00073         listenForPacket();
00074         return;
00075 }
00076 void run() {
00077         io_service.run();
00078 }
00079 
00080 int main (int argc, char *argv[])
00081 {
00082         
00083         SystemConfigPtr sc = SystemConfig::getInstance();
00084 
00085         Configuration *proxyconf = (*sc)["UdpProxy"];
00086         
00087         //std::string port = proxyconf->get<std::string>("UdpProxy","Port",NULL);
00088         
00089         std::string baddress = proxyconf->get<std::string>("UdpProxy","MulticastAddress",NULL);
00090         
00091         ushort port = proxyconf->get<ushort>("UdpProxy","Port",NULL);
00092         
00093         //udp::resolver resolver(io_service);
00094     //udp::resolver::query query(udp::v4(), baddress, port);
00095 
00096         //bcastEndPoint = *resolver.resolve(query);
00097         
00098         
00099         
00100         
00101         
00102   
00103         //myEndPoint = udp::endpoint(udp::v4(),bcastEndPoint.port());
00104         
00105         multiCastAddress = boost::asio::ip::address::from_string(baddress);
00106         destEndPoint = udp::endpoint(multiCastAddress,port);
00107         
00108         std::cout<<"Opening to "<<multiCastAddress <<std::endl;
00109         
00110         
00111         
00112         insocket = new udp::socket(io_service,udp::endpoint(multiCastAddress,port));
00113         
00114         insocket->set_option(boost::asio::ip::multicast::enable_loopback(false));
00115         insocket->set_option(boost::asio::ip::multicast::join_group(multiCastAddress));
00116         listenForPacket();
00117         
00118         
00119          
00120     ros::init(argc, argv, "udpProxy");
00121 
00122     ros::NodeHandle n;
00123     ownRosName = ros::this_node::getName();//n.getNamespace();//n.resolveName("ddsProxy",true);
00124     
00125     std::cout << ownRosName << std::endl;
00126     
00127         <?subscriptions?>
00128         
00129     <?advertisement?>
00130         
00131         boost::thread iothread(run);
00132     
00133     //ros::spin();
00134         ros::AsyncSpinner *spinner;
00135         
00136         spinner = new ros::AsyncSpinner(4);
00137         spinner->start();
00138         
00139         std::cout << "Ros2Udp Proxy running..." <<std::endl;
00140     while(n.ok()) {
00141             usleep(300000);
00142     }
00143     io_service.stop();
00144     iothread.join();
00145     return 0;
00146 }
00147 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends


udp_proxy_generator
Author(s): hendrik
autogenerated on Fri Nov 8 2013 11:05:33