Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
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
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)
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)
00078 {
00079 ROS_ERROR("Failed to open foreign_relay to %s", ip);
00080 }
00081 else
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
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
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
00163