From 0515a080183ba3a36652e5aca73b1ecb1b63117c Mon Sep 17 00:00:00 2001 From: haller Date: Sat, 3 Dec 2005 23:55:02 +0000 Subject: * Codage des Grosses String (compile mais pas debuggué) --- i/simulotron/src/Makefile.am | 5 +- i/simulotron/src/gs/Makefile.incl.am | 11 ++ i/simulotron/src/gs/gs_message.cc | 73 +++++++++++ i/simulotron/src/gs/gs_message.hh | 53 ++++++++ i/simulotron/src/gs/gs_transmitter.cc | 66 ++++++++++ i/simulotron/src/gs/gs_transmitter.hh | 49 +++++++ i/simulotron/src/gs/test_gs.cc | 240 ++++++++++++++++++++++++++++++++++ 7 files changed, 496 insertions(+), 1 deletion(-) create mode 100644 i/simulotron/src/gs/Makefile.incl.am create mode 100644 i/simulotron/src/gs/gs_message.cc create mode 100644 i/simulotron/src/gs/gs_message.hh create mode 100644 i/simulotron/src/gs/gs_transmitter.cc create mode 100644 i/simulotron/src/gs/gs_transmitter.hh create mode 100644 i/simulotron/src/gs/test_gs.cc (limited to 'i/simulotron') diff --git a/i/simulotron/src/Makefile.am b/i/simulotron/src/Makefile.am index 5695d4d..7fe98a5 100644 --- a/i/simulotron/src/Makefile.am +++ b/i/simulotron/src/Makefile.am @@ -2,10 +2,13 @@ check_PROGRAMS = include socket/Makefile.incl.am include utils/Makefile.incl.am +include gs/Makefile.incl.am bin_PROGRAMS = simulotron simulotron_SOURCES = main.cc \ - $(socket_S) + $(socket_S) \ + $(gs_message_S) \ + $(gs_transmitter_S) AM_CXXFLAGS = -g -Wall -fmessage-length=0 simulotron_LDADD = diff --git a/i/simulotron/src/gs/Makefile.incl.am b/i/simulotron/src/gs/Makefile.incl.am new file mode 100644 index 0000000..e3f0026 --- /dev/null +++ b/i/simulotron/src/gs/Makefile.incl.am @@ -0,0 +1,11 @@ +gs_message_S = gs/gs_message.hh gs/gs_message.cc + +gs_transmitter_S = gs/gs_transmitter.hh gs/gs_transmitter.cc + +check_PROGRAMS += test_gs +test_gs_SOURCES = gs/test_gs.cc $(gs_message_S) \ + $(gs_transmitter_S) \ + $(socket_S) + +test_gs_LDADD = -lpthread +test_gs_CXXFLAGS = -W -Wall -g -pthread diff --git a/i/simulotron/src/gs/gs_message.cc b/i/simulotron/src/gs/gs_message.cc new file mode 100644 index 0000000..b971cd9 --- /dev/null +++ b/i/simulotron/src/gs/gs_message.cc @@ -0,0 +1,73 @@ +/* gs_message.cc - Définition de la classe des grosses string */ +/* 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 "gs/gs_message.hh" +#include "gs/gs_transmitter.hh" + +#include + +GSMessage::GSMessage(void) + :igs_(gs_.begin()) +{ +} + +GSMessage::GSMessage(GSTransmitter & gst) +{ + gst.getGS(*this); +} + +const std::string & +GSMessage::getGS(void) const +{ + return gs_; +} + +void +GSMessage::readGS(void * data, size_t size) +{ + std::string str; + str.assign(igs_, gs_.end()); + if (size > str.size()) + throw std::runtime_error("CHIER!!! La size dans readGS est trop grande!!"); + for (unsigned int i = 0; i < size; i++) + { + static_cast (data)[i] = *igs_; + ++igs_; + } +} + +void +GSMessage::writeGS (const void * data, size_t size) +{ + setIteratorBegin(); + for (unsigned int i = 0; i < size; ++i) + gs_.push_back(static_cast (data)[i]); +} + +void +GSMessage::setIteratorBegin(void) +{ + igs_ = gs_.begin(); +} diff --git a/i/simulotron/src/gs/gs_message.hh b/i/simulotron/src/gs/gs_message.hh new file mode 100644 index 0000000..4a9b64e --- /dev/null +++ b/i/simulotron/src/gs/gs_message.hh @@ -0,0 +1,53 @@ +#ifndef gs_message_hh +#define gs_message_hh +/* gs_message.hh - Déclaration de la classe des grosses string */ +/* 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 + +class GSTransmitter; + +class GSMessage +{ + private: + /// grosse string + std::string gs_; + std::string::iterator igs_; + public: + /// Constructeur sans argument + GSMessage (void); + /// Constructeur/récupérateur + GSMessage (GSTransmitter & gst); + /// Récupération de la string en entier + const std::string & getGS (void) const; + /// Récupération de données de la gs + void readGS (void * data, size_t size); + /// Ecriture de la gs + void writeGS (const void * data, size_t size); + /// Remise à zéro de l'itérateur de lecture + void setIteratorBegin(void); +}; + +#endif //gs_message_hh diff --git a/i/simulotron/src/gs/gs_transmitter.cc b/i/simulotron/src/gs/gs_transmitter.cc new file mode 100644 index 0000000..a72f812 --- /dev/null +++ b/i/simulotron/src/gs/gs_transmitter.cc @@ -0,0 +1,66 @@ +/* gs_transmitter.cc - Classe de transmission des grosses string */ +/* 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 "gs/gs_transmitter.hh" +#include "gs/gs_message.hh" +#include "socket/socket_client.hh" + +GSTransmitter::GSTransmitter(SocketClient & socket) + :socket_(socket) +{ +} + +/// Renvoie 0 quand un message est récupéré, -1 sinon +int +GSTransmitter::getGS(GSMessage & gsm) +{ + unsigned int size = 0; + std::string strRead (socket_.read ()); + strBrut_ += strRead; + size = static_cast (strRead[0]) << 24 + | static_cast (strRead[1]) << 16 + | static_cast (strRead[2]) << 8 + | static_cast (strRead[3]); + if(size > strBrut_.size() - 4) + return -1; + strRead = strBrut_.substr (4, size -1); + strBrut_.erase (4, size - 1); + gsm.writeGS (strRead.c_str (), size); + return 0; +} + +void +GSTransmitter::putGS(const GSMessage & gsm) +{ + std::string gs(gsm.getGS ()); + char bsize[4]; + int size = gs.size(); + bsize[0] = static_cast (size >> 24); + bsize[1] = static_cast (size >> 16); + bsize[2] = static_cast (size >> 8); + bsize[3] = static_cast (size); + gs.insert(0, bsize); + socket_.write (gsm.getGS ()); +} diff --git a/i/simulotron/src/gs/gs_transmitter.hh b/i/simulotron/src/gs/gs_transmitter.hh new file mode 100644 index 0000000..10ec27a --- /dev/null +++ b/i/simulotron/src/gs/gs_transmitter.hh @@ -0,0 +1,49 @@ +#ifndef gs_transmitter_hh +#define gs_transmitter_hh +/* gs_transmitter.hh - Classe de transmission des grosses string */ +/* 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 + +class SocketClient; +class GSMessage; + +class GSTransmitter +{ + private: + /// Socket de transmission + SocketClient & socket_; + /// String brute récupéré + std::string strBrut_; + public: + /// Constructeur + GSTransmitter(SocketClient & socket); + /// Récupère une grosse string + int getGS(GSMessage & gsm); + /// envoie une grosse string + void putGS(const GSMessage & gsm); +}; + +#endif //gs_transmitter_hh diff --git a/i/simulotron/src/gs/test_gs.cc b/i/simulotron/src/gs/test_gs.cc new file mode 100644 index 0000000..9aef410 --- /dev/null +++ b/i/simulotron/src/gs/test_gs.cc @@ -0,0 +1,240 @@ +/* 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; +} -- cgit v1.2.3