/* test_gs.cc - Programme de test des gs. */ /* Simulotron - Programme de simulation de robot {{{ * * Copyright (C) 2005 Nicolas Haller * * Robot APB Team/Efrei 2005. * Web: http://assos.efrei.fr/robot/ * Email: robot AT efrei DOT fr * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * * }}} */ #include "socket/address.hh" #include "socket/socket_server.hh" #include "socket/socket_client.hh" #include "gs/gs_transmitter.hh" #include "gs/gs_message.hh" #include "utils/errno_exception.hh" #include #include // Adress useful Address adr; Address adr2; //Chaine et char de test const std::string strDepart = "J'aime la prog système."; std::string strDest; const char a = 'f'; char b; int i1 = 4568; int i2; int i3 = -45687; int i4; short s1 = 23; short s2; long l1 = 465676; long l2; //threads pthread_t threadId[2]; void * retour; int stateTh[2]; void * testGSServ(void * CaSertARien); void * testGSClient(void * CaSertARien); int main(void) { int ret; ret = pthread_create(& threadId[0], NULL, testGSServ, NULL); if(ret != 0) { std::cout << "CHIER: Echec thread serveur" << std::endl; return EXIT_FAILURE; } ret = pthread_create(& threadId[1], NULL, testGSClient, NULL); if(ret != 0) { std::cout << "CHIER: Echec thread client" << std::endl; return EXIT_FAILURE; } pthread_join(threadId[0], &retour); stateTh[0] = (int) retour; pthread_join(threadId[1], &retour); stateTh[1] = (int) retour; if(stateTh[0] == 0 && stateTh[1] == 0) return 0; else return 1; } void * testGSServ(void * CaSertARien) { try { // Création de la socket serveur; SocketServer sockServ(Address(4242)); // Mise en écoute du socket serveur sockServ.listen(12); // Acceptation d'une connection SocketClient sockFork(sockServ); // Création du transmitter GSTransmitter gst(sockFork); // Reception des messages // une string GSMessage gsm; if (gst.getGS(gsm) != 0) throw std::runtime_error ("CHIER!! Reception mal passé"); if (strDepart != gsm.getGS()) { std::cout << "CHIER String altéré\n" << "Message d'origine: " << strDepart << "\n" << "Message d'arrivé: " << gsm.getGS() << std::endl; return (void *)1; } // Un char if (gst.getGS(gsm) != 0) throw std::runtime_error ("CHIER!! Reception mal passé"); gsm.readGS(&b, sizeof(char)); if ( a != b) { std::cout << "CHIER Char altéré\n" << "Message d'origine: " << a << "\n" << "Message d'arrivé: " << b << std::endl; return (void *)1; } // Un short if (gst.getGS(gsm) != 0) throw std::runtime_error ("CHIER!! Reception mal passé"); gsm.readGS(&s2, sizeof(char)); if ( s1 != s2) { std::cout << "CHIER Short altéré\n" << "Message d'origine: " << s1 << "\n" << "Message d'arrivé: " << s2 << std::endl; return (void *)1; } // Un int positif if (gst.getGS(gsm) != 0) throw std::runtime_error ("CHIER!! Reception mal passé"); gsm.readGS(&i2, sizeof(char)); if ( i1 != i2) { std::cout << "CHIER int positif altéré\n" << "Message d'origine: " << i1 << "\n" << "Message d'arrivé: " << i2 << std::endl; return (void *)1; } // Un int negatif if (gst.getGS(gsm) != 0) throw std::runtime_error ("CHIER!! Reception mal passé"); gsm.readGS(&i4, sizeof(char)); if ( i3 != i4) { std::cout << "CHIER int negatif altéré\n" << "Message d'origine: " << i3 << "\n" << "Message d'arrivé: " << i4 << std::endl; return (void *)1; } // Un Long if (gst.getGS(gsm) != 0) throw std::runtime_error ("CHIER!! Reception mal passé"); gsm.readGS(&l2, sizeof(char)); if ( l1 != l2) { std::cout << "CHIER int positif altéré\n" << "Message d'origine: " << l1 << "\n" << "Message d'arrivé: " << l2 << std::endl; return (void *)1; } // Un gros message if (gst.getGS(gsm) != 0) throw std::runtime_error ("CHIER!! Reception mal passé"); gsm.readGS(&s2,sizeof(short)); gsm.readGS(&l2,sizeof(long)); gsm.readGS(&i4,sizeof(int)); if ( s1 != s2) { std::cout << "CHIER Short altéré\n" << "Message d'origine: " << s1 << "\n" << "Message d'arrivé: " << s2 << std::endl; return (void *)1; } if ( l1 != l2) { std::cout << "CHIER int positif altéré\n" << "Message d'origine: " << l1 << "\n" << "Message d'arrivé: " << l2 << std::endl; return (void *)1; } if ( i3 != i4) { std::cout << "CHIER int negatif altéré\n" << "Message d'origine: " << i3 << "\n" << "Message d'arrivé: " << i4 << std::endl; return (void *)1; } } catch(errno_exception & chier) { std::cout << "CHIER !! Une exception a été lancé coté serveur!!" << std::endl; std::cout << chier.what() << std::endl; return (void *) 1; } return (void *) 0; } void * testGSClient(void * CaSertARien) { try { // Création de la socket client SocketClient sockClient(adr); // Demande de connection sockClient.connect(Address(std::string("localhost"), 4242)); // Création du transmitter GSTransmitter gst(sockClient); GSMessage g1,g2,g3,g4,g5,g6,g7; g1.writeGS(strDepart.c_str(), strDepart.size()); g2.writeGS(&s1, sizeof(short)); g3.writeGS(&i1, sizeof(int)); g4.writeGS(&i3, sizeof(int)); g5.writeGS(&l1, sizeof(long)); g6.writeGS(&s1, sizeof(short)); g6.writeGS(&l1, sizeof(long)); g6.writeGS(&i3, sizeof(int)); } catch(errno_exception & chier) { std::cout << "CHIER !! Une exception a été lancé coté client!!" << std::endl; std::cout << chier.what() << std::endl; return (void *) 1; } return (void *) 0; }