wifi_comm_node_deprecated.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002 *
00003 * Software License Agreement (BSD License)
00004 *
00005 *  Copyright (c) 2010, ISR University of Coimbra.
00006 *  All rights reserved.
00007 *
00008 *  Redistribution and use in source and binary forms, with or without
00009 *  modification, are permitted provided that the following conditions
00010 *  are met:
00011 *
00012 *   * Redistributions of source code must retain the above copyright
00013 *     notice, this list of conditions and the following disclaimer.
00014 *   * Redistributions in binary form must reproduce the above
00015 *     copyright notice, this list of conditions and the following
00016 *     disclaimer in the documentation and/or other materials provided
00017 *     with the distribution.
00018 *   * Neither the name of the ISR University of Coimbra nor the names of its
00019 *     contributors may be used to endorse or promote products derived
00020 *     from this software without specific prior written permission.
00021 *
00022 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00023 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00024 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00025 *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00026 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00027 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00028 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00029 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00030 *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031 *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00032 *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00033 *  POSSIBILITY OF SUCH DAMAGE.
00034 *
00035 * Author: Gonçalo Cabrita and Pedro Sousa
00036 *********************************************************************/
00037 #include <ros/ros.h>
00038 #include <stdlib.h>
00039 #include <stdio.h>
00040 #include <unistd.h>
00041 #include <vector>
00042 #include <string>
00043 #include <signal.h>
00044 #include "wifi_comm/WiFiNeighboursList.h"
00045 #include "wifi_comm/WiFiCommContainer.h"
00046 
00047 // defines
00048 #define NODE_VERSION 0.01
00049 
00050 using namespace wifi_comm;
00051 
00052 typedef struct
00053 {
00054         int pid;
00055         std::string ip;
00056 
00057 } ForeignRelay;
00058 
00059 std::vector<ForeignRelay> ForeignRelays;
00060 
00061 
00062 void openForeignRelay(char * ip)
00063 {
00064         ForeignRelay newRelay;
00065 
00066         int pid = fork();
00067         if(pid==0)      // Child
00068         {
00069                 char ros_master_uri[128];
00070                 sprintf(ros_master_uri, "http://%s:11311", ip);
00071 
00072                 execlp("rosrun", "rosrun", "foreign_relay", "foreign_relay", "adv", ros_master_uri, "/wifi_comm_rcv", "/wifi_comm_snd", (char*)0);
00073                 
00074                 ROS_WARN("Reached the end of foreign_relay to %s with pid %d", ip, getpid());
00075                 exit(0);
00076         }
00077         else if(pid<0)  // Error
00078         {
00079                 ROS_ERROR("Failed to open foreign_relay to %s", ip);
00080         }
00081         else            // Parent
00082         {
00083                 newRelay.pid = pid;
00084                 newRelay.ip = ip;
00085                 ForeignRelays.push_back(newRelay);
00086                 ROS_INFO("Successfully opened foreign_relay to %s with pid %d", ip, pid);
00087         }
00088         return;
00089 }
00090 
00091 void closeForeignRelay(char * ip)
00092 {
00093         unsigned int i;
00094 
00095         for(i=0 ; i<ForeignRelays.size() ; i++)
00096         {
00097                 if(ForeignRelays[i].ip.compare(ip)==0)
00098                 {
00099                         ROS_INFO("Killing foreign_relay to %s with pid %d", ip, ForeignRelays[i].pid);
00100                         kill(ForeignRelays[i].pid, SIGTERM);
00101                         ForeignRelays.erase(ForeignRelays.begin()+i);
00102                         return;
00103                 }
00104         }
00105         
00106         ROS_ERROR("Could not find foreign_relay to %s", ip);
00107         return;
00108 }
00109 
00110 void neighboursListUpdated(const wifi_comm::WiFiNeighboursListConstPtr& msg)
00111 {
00112         unsigned int i, j;
00113         bool found;
00114 
00115         // Now open and close foreign_relays!
00116         for(i=0 ; i<msg->neighbours.size() ; i++)
00117         {
00118                 found = false;
00119                 for(j=0 ; j<ForeignRelays.size() ; j++)
00120                 {
00121                         if(msg->neighbours[i].ip.compare(ForeignRelays[j].ip)==0)
00122                         {
00123                                 found = true;
00124                                 break;
00125                         }
00126                 }
00127                 if(!found) openForeignRelay((char*)msg->neighbours[i].ip.c_str());
00128         }
00129 
00130         for(i=0 ; i<ForeignRelays.size() ; i++)
00131         {
00132                 found = false;
00133                 for(j=0 ; j<msg->neighbours.size() ; j++)
00134                 {
00135                         if(ForeignRelays[i].ip.compare(msg->neighbours[j].ip)==0)
00136                         {
00137                                 found = true;
00138                                 break;
00139                         }
00140                 }
00141                 if(!found) closeForeignRelay((char*)ForeignRelays[i].ip.c_str());
00142         }
00143 }
00144 
00145 
00146 // *****************************************************************************
00147 // Main function for the multiRobotCom node
00148 int main( int argc, char** argv )
00149 {
00150         ros::init(argc, argv, "wifi_comm");
00151         ros::NodeHandle n;
00152         
00153         ROS_INFO("Wifi Comm Node for ROS %.2f", NODE_VERSION);
00154 
00155         ros::Publisher pub = n.advertise<wifi_comm::WiFiCommContainer>("/wifi_comm_snd", 10);
00156         ros::Subscriber list_sub = n.subscribe("wifi_neighbours_list", 10, neighboursListUpdated);
00157         ros::spin();
00158 
00159         return 0;
00160 }
00161 
00162 // EOF
00163 


wifi_comm
Author(s): Gonçalo Cabrita and Pedro Sousa
autogenerated on Mon Jan 6 2014 11:27:05