/* 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/socket_server.hh" #include "gs/gs_transmitter.hh" #include "gs/gs_message.hh" #include #include #include //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; int testGSServer(SocketServer & sockServ); void testGSClient(int pauseMode); int main(void) { //pauseMode pour debbuger le fork int pauseMode = 0; int pid; //retour des test int resultClient, resultServer; // Création de la socket serveur; SocketServer sockServ(std::string(),4242); // Mise en écoute du socket serveur sockServ.listen(12); // On crée le fork du client pid = fork(); if (pid == 0) // processus fils testGSClient(pauseMode); else //processus père resultServer = testGSServer(sockServ); // On analyse les résultats wait(&resultClient); if (!(WIFEXITED(resultClient) && WEXITSTATUS(resultClient) == 0)) { std::cerr << "ECHEC: Problème coté client" << std::endl; exit (-1); } if (resultServer != 0) { std::cerr << "ECHEC: Problème coté serveur" << std::endl; exit (-1); } return 0; } int testGSServer(SocketServer & sockServ) { // Création du transmitter GSTransmitter gst(sockServ); // Reception des messages // une string GSMessage gsm; while (!gst.getGS(gsm,true)); gsm.getString(strDest); if (strDepart != strDest) { std::cout << "CHIER String altéré\n" << "Message d'origine: " << strDepart << "\n" << "Message d'arrivé: " << gsm.getGS() << std::endl; return 1; } gsm.clear(); // Un char while (!gst.getGS(gsm, true)); 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 1; } gsm.clear(); // Un short while (!gst.getGS(gsm, true)); gsm.readGS(&s2, sizeof(short)); if ( s1 != s2) { std::cout << "CHIER Short altéré\n" << "Message d'origine: " << s1 << "\n" << "Message d'arrivé: " << s2 << std::endl; return 1; } gsm.clear(); // Un int positif while (!gst.getGS(gsm, true)); gsm.readGS(&i2, sizeof(int)); if ( i1 != i2) { std::cout << "CHIER int positif altéré\n" << "Message d'origine: " << i1 << "\n" << "Message d'arrivé: " << i2 << std::endl; return 1; } gsm.clear(); // Un int negatif while (!gst.getGS(gsm, true)); gsm.readGS(&i4, sizeof(int)); if ( i3 != i4) { std::cout << "CHIER int negatif altéré\n" << "Message d'origine: " << i3 << "\n" << "Message d'arrivé: " << i4 << std::endl; return 1; } gsm.clear(); // Un Long while (!gst.getGS(gsm, true)); gsm.readGS(&l2, sizeof(long)); if ( l1 != l2) { std::cout << "CHIER long altéré\n" << "Message d'origine: " << l1 << "\n" << "Message d'arrivé: " << l2 << std::endl; return 1; } gsm.clear(); // Un gros message while (!gst.getGS(gsm, true)); gsm.readGS(&s2,sizeof(short)); gsm.readGS(&l2,sizeof(long)); gsm.getString(strDest); 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 1; } if ( l1 != l2) { std::cout << "CHIER int positif altéré\n" << "Message d'origine: " << l1 << "\n" << "Message d'arrivé: " << l2 << std::endl; return 1; } if ( strDepart != strDest) { std::cout << "CHIER String altéré\n" << "Message d'origine: " << strDepart << "\n" << "Message d'arrivé: " << gsm.getGS() << std::endl; return 1; } if ( i3 != i4) { std::cout << "CHIER int negatif altéré\n" << "Message d'origine: " << i3 << "\n" << "Message d'arrivé: " << i4 << std::endl; return 1; } return 0; } void testGSClient(int pauseMode) { while(pauseMode) sleep (10); try { // Création du transmitter GSTransmitter gst(std::string("127.0.0.1"), 4242); GSMessage g1,g2,g3,g4,g5,g6,g7; g1.appendGS(strDepart.c_str(), strDepart.size() + 1); gst.putGS(g1); g2.appendGS(&a, sizeof(char)); gst.putGS(g2); g3.appendGS(&s1, sizeof(short)); gst.putGS(g3); g4.appendGS(&i1, sizeof(int)); gst.putGS(g4); g5.appendGS(&i3, sizeof(int)); gst.putGS(g5); g6.appendGS(&l1, sizeof(long)); gst.putGS(g6); g7.appendGS(&l1, sizeof(long)); g7.insertFrontGS(&s1, sizeof(short)); g7.appendGS(strDepart.c_str(), strDepart.size() + 1); g7.appendGS(&i3, sizeof(int)); gst.putGS(g7); } catch(std::exception & chier) { std::cout << "CHIER !! Une exception a été lancé coté client!!" << std::endl; std::cout << chier.what() << std::endl; exit (1); } exit (0); }