| 1 | /**************************************************************************************
 | 
|---|
| 2 | ** Petit simulateur d'antenne
 | 
|---|
| 3 | ** BAORadio
 | 
|---|
| 4 | ** Franck RICHARD
 | 
|---|
| 5 | ***************************************************************************************/
 | 
|---|
| 6 | 
 | 
|---|
| 7 | #include "ClientSocket.h"
 | 
|---|
| 8 | #include "ServerSocket.h"
 | 
|---|
| 9 | #include "SocketException.h"
 | 
|---|
| 10 | #include <iostream>
 | 
|---|
| 11 | #include <string>
 | 
|---|
| 12 | #include <string.h>
 | 
|---|
| 13 | #include <stdio.h>
 | 
|---|
| 14 | #include <stdlib.h>
 | 
|---|
| 15 | #include <net/if.h>
 | 
|---|
| 16 | #include <sys/types.h>
 | 
|---|
| 17 | #include <sys/socket.h>
 | 
|---|
| 18 | #include <sys/ioctl.h>
 | 
|---|
| 19 | #include <netinet/in.h>
 | 
|---|
| 20 | 
 | 
|---|
| 21 | using namespace std;
 | 
|---|
| 22 | 
 | 
|---|
| 23 | 
 | 
|---|
| 24 | /**************************************************************************************
 | 
|---|
| 25 | ** Extraction de la position de l'antenne
 | 
|---|
| 26 | **
 | 
|---|
| 27 | ***************************************************************************************/
 | 
|---|
| 28 | 
 | 
|---|
| 29 | Position ExtractPosition(std::string str)
 | 
|---|
| 30 | {
 | 
|---|
| 31 |     Position result;
 | 
|---|
| 32 | 
 | 
|---|
| 33 |     std::string str2, str3;
 | 
|---|
| 34 | 
 | 
|---|
| 35 |     result.x=-1;
 | 
|---|
| 36 |     result.y=-1;
 | 
|---|
| 37 | 
 | 
|---|
| 38 |     int pos = str.find("#");
 | 
|---|
| 39 | 
 | 
|---|
| 40 |     if (pos!=string::npos)
 | 
|---|
| 41 |     {
 | 
|---|
| 42 |         str2 = str.substr(pos+2);
 | 
|---|
| 43 | 
 | 
|---|
| 44 |         pos = str2.find("Az");
 | 
|---|
| 45 | 
 | 
|---|
| 46 |         result.x=atoi(str2.substr(0, pos-1).c_str());
 | 
|---|
| 47 | 
 | 
|---|
| 48 |         pos = str2.find("#");
 | 
|---|
| 49 | 
 | 
|---|
| 50 |         if (pos!=string::npos)
 | 
|---|
| 51 |         {
 | 
|---|
| 52 |             str3 = str2.substr(pos+2);
 | 
|---|
| 53 | 
 | 
|---|
| 54 |             pos = str3.find("Alt");
 | 
|---|
| 55 | 
 | 
|---|
| 56 |             result.y=atoi(str3.substr(0, pos-1).c_str());;
 | 
|---|
| 57 | 
 | 
|---|
| 58 |         }
 | 
|---|
| 59 |     }
 | 
|---|
| 60 | 
 | 
|---|
| 61 |     return result;
 | 
|---|
| 62 | }
 | 
|---|
| 63 | 
 | 
|---|
| 64 | Position ExtractPosition2(std::string str)
 | 
|---|
| 65 | {
 | 
|---|
| 66 |     Position result;
 | 
|---|
| 67 | 
 | 
|---|
| 68 |     result.x=0;
 | 
|---|
| 69 |     result.y=0;
 | 
|---|
| 70 | 
 | 
|---|
| 71 |     if (str.length()>2)
 | 
|---|
| 72 |     {
 | 
|---|
| 73 |         result.x=atoi(str.substr(2, 4).c_str());
 | 
|---|
| 74 | 
 | 
|---|
| 75 |         result.y=atoi(str.substr(7, 4).c_str());
 | 
|---|
| 76 | 
 | 
|---|
| 77 |         if (str[1]=='f') result.x=-result.x;
 | 
|---|
| 78 | 
 | 
|---|
| 79 |         if (str[6]=='f') result.y=-result.y;
 | 
|---|
| 80 |     }
 | 
|---|
| 81 | 
 | 
|---|
| 82 |     return result;
 | 
|---|
| 83 | }
 | 
|---|
| 84 | 
 | 
|---|
| 85 | /**************************************************************************************
 | 
|---|
| 86 | ** CreÌation de l'identifiant de l'antenne aÌ partir de l'IP
 | 
|---|
| 87 | **
 | 
|---|
| 88 | ***************************************************************************************/
 | 
|---|
| 89 | /*
 | 
|---|
| 90 | int NumeroServeur()
 | 
|---|
| 91 | {
 | 
|---|
| 92 |     std::string str, str2;
 | 
|---|
| 93 | 
 | 
|---|
| 94 |     int fd;
 | 
|---|
| 95 |     struct ifreq ifr;
 | 
|---|
| 96 | 
 | 
|---|
| 97 |     fd=socket(AF_INET, SOCK_DGRAM, 0);
 | 
|---|
| 98 | 
 | 
|---|
| 99 |     ifr.ifr_addr.sa_family=AF_INET;
 | 
|---|
| 100 | 
 | 
|---|
| 101 |     strncpy(ifr.ifr_name, "eth0", IFNAMSIZ-1);
 | 
|---|
| 102 | 
 | 
|---|
| 103 |     ioctl(fd, SIOCGIFADDR, &ifr);
 | 
|---|
| 104 | 
 | 
|---|
| 105 |     close(fd);
 | 
|---|
| 106 | 
 | 
|---|
| 107 |     str=(std::string)inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr);
 | 
|---|
| 108 | 
 | 
|---|
| 109 |     int pos = str.find_last_of(".");
 | 
|---|
| 110 | 
 | 
|---|
| 111 |     if (pos!=string::npos)
 | 
|---|
| 112 |     {
 | 
|---|
| 113 |         str2 = str.substr(pos+1);
 | 
|---|
| 114 | 
 | 
|---|
| 115 |         return atoi(str2.c_str());
 | 
|---|
| 116 |     }
 | 
|---|
| 117 | 
 | 
|---|
| 118 |     return -1;
 | 
|---|
| 119 | }
 | 
|---|
| 120 | */
 | 
|---|
| 121 | 
 | 
|---|
| 122 | 
 | 
|---|
| 123 | /**************************************************************************************
 | 
|---|
| 124 | ** MAIN
 | 
|---|
| 125 | **
 | 
|---|
| 126 | ***************************************************************************************/
 | 
|---|
| 127 | 
 | 
|---|
| 128 | #define ERREURS 50
 | 
|---|
| 129 | 
 | 
|---|
| 130 | int main ( int argc, int argv[] )
 | 
|---|
| 131 | {
 | 
|---|
| 132 |     Position Pos, Pos2;
 | 
|---|
| 133 |     char cIP;
 | 
|---|
| 134 |     char IP[20];
 | 
|---|
| 135 |     bool Aleat;
 | 
|---|
| 136 | 
 | 
|---|
| 137 |     // Parametres sups
 | 
|---|
| 138 |     bool ErreursAleatoires=false;
 | 
|---|
| 139 |     bool Affichage=true;
 | 
|---|
| 140 | 
 | 
|---|
| 141 |     // Position PARK
 | 
|---|
| 142 |     Pos.x=0;
 | 
|---|
| 143 |     Pos.y=0;
 | 
|---|
| 144 | 
 | 
|---|
| 145 |     //init nbs aleÌatoires
 | 
|---|
| 146 |     if (ErreursAleatoires) srand(time(NULL));
 | 
|---|
| 147 | 
 | 
|---|
| 148 |     
 | 
|---|
| 149 | 
 | 
|---|
| 150 |     std::cout <<  "******************\n";
 | 
|---|
| 151 |     std::cout <<  "* Simulateur BAO *\n";
 | 
|---|
| 152 |     std::cout <<  "******************\n\n";
 | 
|---|
| 153 |     std::cout <<  "Adresse IP du serveur :\n";
 | 
|---|
| 154 |     std::cout <<  "1)-  192.168.0.1\n";
 | 
|---|
| 155 |     std::cout <<  "2)-  127.0.0.1\n\n? ";
 | 
|---|
| 156 |     std::cin >> cIP;
 | 
|---|
| 157 | 
 | 
|---|
| 158 |     if (cIP=='1') strcpy(IP, "192.168.0.1");
 | 
|---|
| 159 |     else strcpy(IP, "127.0.0.1");
 | 
|---|
| 160 | 
 | 
|---|
| 161 | 
 | 
|---|
| 162 |     try
 | 
|---|
| 163 |     {
 | 
|---|
| 164 |        ClientSocket client_socket ( IP, 8000 );
 | 
|---|
| 165 |         
 | 
|---|
| 166 |        // ServerSocket socket ( 8000 );        
 | 
|---|
| 167 |        // ServerSocket client_socket;
 | 
|---|
| 168 |        // socket.accept ( client_socket );
 | 
|---|
| 169 | 
 | 
|---|
| 170 |         std::string reply;
 | 
|---|
| 171 |         std::string asks,memasks;
 | 
|---|
| 172 |         char chaine[5000];
 | 
|---|
| 173 | 
 | 
|---|
| 174 |         while (true)
 | 
|---|
| 175 |         {
 | 
|---|
| 176 |             
 | 
|---|
| 177 |                 client_socket.recv(asks);
 | 
|---|
| 178 | 
 | 
|---|
| 179 |                 int pos=asks.find("\n");
 | 
|---|
| 180 |                 memasks=asks;
 | 
|---|
| 181 |                 
 | 
|---|
| 182 |                 while ((pos!=string::npos) && (asks.length()>1))
 | 
|---|
| 183 |                 {
 | 
|---|
| 184 |                     memasks=asks.substr(pos+1);
 | 
|---|
| 185 |                     asks=asks.substr(0,pos);
 | 
|---|
| 186 | 
 | 
|---|
| 187 |                     strcpy(chaine, "");
 | 
|---|
| 188 | 
 | 
|---|
| 189 |                    
 | 
|---|
| 190 |                         if (ErreursAleatoires) Aleat=rand()%ERREURS != 1; else Aleat=1;
 | 
|---|
| 191 | 
 | 
|---|
| 192 |                         if (Affichage) std::cout <<  "Pilote BAO : " << asks << "\n" ;
 | 
|---|
| 193 |          
 | 
|---|
| 194 | 
 | 
|---|
| 195 |                         if (asks.find("P")!=string::npos)
 | 
|---|
| 196 |                         {
 | 
|---|
| 197 | 
 | 
|---|
| 198 |                            strcpy(chaine,"ACK/POSITION\n");
 | 
|---|
| 199 |                            client_socket << chaine;
 | 
|---|
| 200 | //usleep(10000);
 | 
|---|
| 201 |                             if (Affichage) std::cout <<  "MicrocontroÌleur : " << chaine;
 | 
|---|
| 202 | 
 | 
|---|
| 203 |                             if (Aleat)
 | 
|---|
| 204 |                             {
 | 
|---|
| 205 |                                 sprintf(chaine, "POSITION/%04i/%04i/\n", Pos.x, Pos.y);
 | 
|---|
| 206 |                             }
 | 
|---|
| 207 |                             else
 | 
|---|
| 208 |                             {
 | 
|---|
| 209 |                                 strcpy(chaine, "NACK/HALT/POSITION/\n");   //erreur moteur !
 | 
|---|
| 210 |                             }
 | 
|---|
| 211 |                         }
 | 
|---|
| 212 | 
 | 
|---|
| 213 |                         if (asks.find("G")!=string::npos)
 | 
|---|
| 214 |                         {
 | 
|---|
| 215 |                         
 | 
|---|
| 216 |                             Pos2=ExtractPosition2(asks);
 | 
|---|
| 217 |                             
 | 
|---|
| 218 |                             Pos.x-=Pos2.x;
 | 
|---|
| 219 |                             Pos.y-=Pos2.y;
 | 
|---|
| 220 | 
 | 
|---|
| 221 |                             strcpy(chaine,"ACK/GOTO/\n");
 | 
|---|
| 222 |                             client_socket << chaine;
 | 
|---|
| 223 | 
 | 
|---|
| 224 |                             if (Affichage)
 | 
|---|
| 225 |                             { 
 | 
|---|
| 226 |                                std::cout <<  "MicrocontroÌleur : " << chaine;
 | 
|---|
| 227 | 
 | 
|---|
| 228 |                                std::cout <<  "Positionnement de l antenne...\n";
 | 
|---|
| 229 |                             }
 | 
|---|
| 230 | 
 | 
|---|
| 231 |                             // sleep(rand()%10);
 | 
|---|
| 232 | 
 | 
|---|
| 233 |                             if (Aleat)
 | 
|---|
| 234 |                             {
 | 
|---|
| 235 |                                 strcpy(chaine, "OK/GOTO/\n");
 | 
|---|
| 236 |                             }
 | 
|---|
| 237 |                             else
 | 
|---|
| 238 |                             {
 | 
|---|
| 239 |                                 strcpy(chaine, "NACK/HALT/GOTO/\n");   //erreur moteur !
 | 
|---|
| 240 |                             }
 | 
|---|
| 241 |                         }
 | 
|---|
| 242 | 
 | 
|---|
| 243 |                         if (asks.find("Z")!=string::npos)
 | 
|---|
| 244 |                         {
 | 
|---|
| 245 | 
 | 
|---|
| 246 |                             strcpy(chaine, "ACK/PARK\n");
 | 
|---|
| 247 |                             client_socket << chaine;
 | 
|---|
| 248 | 
 | 
|---|
| 249 |                             if (Affichage) std::cout <<  "MicrocontroÌleur : " << chaine;
 | 
|---|
| 250 | 
 | 
|---|
| 251 |                             if (Aleat)
 | 
|---|
| 252 |                             {
 | 
|---|
| 253 |                                 strcpy(chaine, "OK/PARK/\n");
 | 
|---|
| 254 |                             }
 | 
|---|
| 255 |                             else
 | 
|---|
| 256 |                             {
 | 
|---|
| 257 |                                 strcpy(chaine, "NACK/HALT/PARK/\n");   //erreur
 | 
|---|
| 258 |                             }
 | 
|---|
| 259 |                         }
 | 
|---|
| 260 | 
 | 
|---|
| 261 |                         if (asks.find("A")!=string::npos)
 | 
|---|
| 262 |                         {
 | 
|---|
| 263 | 
 | 
|---|
| 264 |                             strcpy(chaine,"ACK/ABORT\n");
 | 
|---|
| 265 |                             client_socket << chaine;
 | 
|---|
| 266 | 
 | 
|---|
| 267 |                             if (Affichage) std::cout <<  "MicrocontroÌleur : " << chaine;
 | 
|---|
| 268 | 
 | 
|---|
| 269 |                             if (Aleat)
 | 
|---|
| 270 |                             {
 | 
|---|
| 271 |                                 strcpy(chaine, "OK/ABORT/\n");
 | 
|---|
| 272 |                             }
 | 
|---|
| 273 |                             else
 | 
|---|
| 274 |                             {
 | 
|---|
| 275 |                                 strcpy(chaine, "NACK/HALT/ABORT/\n");   //erreur
 | 
|---|
| 276 |                             }
 | 
|---|
| 277 |                         }
 | 
|---|
| 278 | 
 | 
|---|
| 279 |                         if (strlen(chaine)>1)
 | 
|---|
| 280 |                         {
 | 
|---|
| 281 |                             //   usleep(200);
 | 
|---|
| 282 | 
 | 
|---|
| 283 |                             client_socket << chaine;
 | 
|---|
| 284 | 
 | 
|---|
| 285 | 
 | 
|---|
| 286 |                             if (Affichage)
 | 
|---|
| 287 |                             {
 | 
|---|
| 288 |                                 std::cout <<  "MicrocontroÌleur : " << chaine;
 | 
|---|
| 289 | 
 | 
|---|
| 290 |                                 std::cout <<  "---------\n";
 | 
|---|
| 291 |                             }
 | 
|---|
| 292 |                         }
 | 
|---|
| 293 |                     
 | 
|---|
| 294 | 
 | 
|---|
| 295 |                     asks=memasks;
 | 
|---|
| 296 |                     pos=asks.find("\n");
 | 
|---|
| 297 |                 }           
 | 
|---|
| 298 |             
 | 
|---|
| 299 |         }
 | 
|---|
| 300 |     }
 | 
|---|
| 301 |     catch ( SocketException& e )
 | 
|---|
| 302 |     {
 | 
|---|
| 303 |         std::cout << "Exception was caught:" << e.description() << "\n";
 | 
|---|
| 304 |     }
 | 
|---|
| 305 | 
 | 
|---|
| 306 |     return 0;
 | 
|---|
| 307 | }
 | 
|---|