summaryrefslogtreecommitdiff
path: root/i/simulotron/src/comc
diff options
context:
space:
mode:
Diffstat (limited to 'i/simulotron/src/comc')
-rw-r--r--i/simulotron/src/comc/Makefile.incl.am12
-rw-r--r--i/simulotron/src/comc/comc.cc (renamed from i/simulotron/src/comc/comc.tcc)39
-rw-r--r--i/simulotron/src/comc/comc.hh20
-rw-r--r--i/simulotron/src/comc/send.tcc40
-rw-r--r--i/simulotron/src/comc/struct_message.hh79
-rw-r--r--i/simulotron/src/comc/test_comc.cc107
-rw-r--r--i/simulotron/src/comc/test_comc_struct.hh37
7 files changed, 211 insertions, 123 deletions
diff --git a/i/simulotron/src/comc/Makefile.incl.am b/i/simulotron/src/comc/Makefile.incl.am
index 93bf1ee..d1ca797 100644
--- a/i/simulotron/src/comc/Makefile.incl.am
+++ b/i/simulotron/src/comc/Makefile.incl.am
@@ -1,9 +1,9 @@
-comc_S = comc/comc.tcc comc/comc.hh
+comc_S = comc/comc.cc comc/comc.hh comc/struct_message.hh
check_PROGRAMS += test_comc
test_comc_SOURCES = comc/test_comc.cc \
- $(comc_S) \
- $(socket_S) \
- $(aiguillage_S) \
- $(gs_message_S) \
- $(gs_transmitter_S)
+ $(comc_S) \
+ $(gs_message_S) \
+ $(gs_transmitter_S) \
+ $(socket_S) \
+ $(aiguillage_S)
diff --git a/i/simulotron/src/comc/comc.tcc b/i/simulotron/src/comc/comc.cc
index 6e7bef7..cbfb474 100644
--- a/i/simulotron/src/comc/comc.tcc
+++ b/i/simulotron/src/comc/comc.cc
@@ -23,36 +23,39 @@
*
* }}} */
+#include "gs/gs_message.hh"
#include "comc/comc.hh"
#include <stdexcept>
-/// Constructeur général
-template <class C>
-ComC<C>::ComC(const std::string & address, int port, const std::string & name)
- :C(),aiguillage_(address, port, name)
+/// Constructeur client
+ComC::ComC(const std::string & address, int port, const std::string & name)
+ :aiguillage_(address, port, name)
{
}
-/// Fonction send générale qui DOIT planter
-template <class C> template <class T>
-void
-ComC<C>::send(const T & message, const std::string & destname)
+/// Constructeur serveur
+ComC::ComC(SocketServer & socket, const std::string & name)
+ :aiguillage_(socket, name)
{
- throw std::runtime_error("ComC: Utilisation de send avec une struct indéfini!!");
}
-/// Recoie quelque chose et appelle une fonction callback
-template <class C>
void
-ComC<C>::receive(void)
+ComC::send(const msgX & message, const std::string & destname)
{
- throw std::runtime_error("ComC: Utilisation de receive avec une class indefini!!!");
+ GSMessage gsm;
+ message.msgToGs(gsm);
+ aiguillage_.send(gsm, std::string(), destname);
}
-class TuSersARien{};
+/// Récupération de la GS
+int
+ComC::receiveGS(GSMessage & gsm, std::string & source, std::string & dest, int & msgId)
+{
+ if(aiguillage_.receive(gsm, source, dest) != 0)
+ return 1;
+ gsm.readGS(&msgId, sizeof(int));
+ gsm.setIteratorBegin();
+ return 0;
+}
-template<>
-void
-ComC<TuSersARien>::receive(void)
-{}
diff --git a/i/simulotron/src/comc/comc.hh b/i/simulotron/src/comc/comc.hh
index 3edd2c1..a7e0076 100644
--- a/i/simulotron/src/comc/comc.hh
+++ b/i/simulotron/src/comc/comc.hh
@@ -26,27 +26,23 @@
* }}} */
#include "aiguillage/aiguillage.hh"
+#include "comc/struct_message.hh"
-template <class C>
-class ComC : public C
+class ComC
{
private:
Aiguillage aiguillage_;
public:
- /// Constructeur
+ /// Constructeur client
ComC(const std::string & address, int port, const std::string & name);
+ /// Constructeur serveur
+ ComC(SocketServer & socket, const std::string & name);
/// Initialise la communication avec le hub
void InitHub(void);
/// Envoie un truc dans le réseau
- template <class T>
- void send(const T & message, const std::string & destname);
- // Recoie quelque chose et appelle une fonction callback
- // void receive(void);
+ void send(const msgX & message, const std::string & destname);
+ /// Récupération de la GS
+ int receiveGS(GSMessage & gsm, std::string & source, std::string & dest, int & msgId);
};
-/// Contient les fonction de base de la classe
-#include "comc/comc.tcc"
-/// Contient les fonctions send spécialisé.
-#include "comc/send.tcc"
-
#endif //COMC_HH
diff --git a/i/simulotron/src/comc/send.tcc b/i/simulotron/src/comc/send.tcc
deleted file mode 100644
index 6c5554a..0000000
--- a/i/simulotron/src/comc/send.tcc
+++ /dev/null
@@ -1,40 +0,0 @@
-/* send.tcc - Contient toute les fcts send pour les structures gs */
-/* Simulotron - Programme de simulation de robot {{{
- *
- * Copyright (C) 2005 Nicolas Haller
- *
- * Robot APB Team/Efrei 2006.
- * 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.
- *
- * }}} */
-
-// Structure 0 - Structure de test
-// int, double, string(8),short, string, long
-template <class C> template <>
-void
-ComC<C>::send(const msg0 & message, const std::string & destname)
-{
- GSMessage gsm;
- gsm appendGS(&i, sizeof(int));
- gsm appendGS(&j, sizeof(double));
- gsm appendGS(&dfsdf, sizeof(int));
- gsm appendGS(&i, sizeof(int));
- gsm appendGS(&i, sizeof(int));
- gsm appendGS(&i, sizeof(int));
-}
-
diff --git a/i/simulotron/src/comc/struct_message.hh b/i/simulotron/src/comc/struct_message.hh
new file mode 100644
index 0000000..598ac40
--- /dev/null
+++ b/i/simulotron/src/comc/struct_message.hh
@@ -0,0 +1,79 @@
+#ifndef STRUCT_MESSAGE_HH
+#define STRUCT_MESSAGE_HH
+/* struct_message.hh - Fichier contenant les structures des messages réseau */
+/* Simulotron - Programme de simulation de robot {{{
+ *
+ * Copyright (C) 2006 Nicolas Haller
+ *
+ * Robot APB Team/Efrei 2006.
+ * 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 <string>
+#include <stdexcept>
+
+/// Message mère de toute les structures de données réseau
+struct msgX
+{
+ virtual ~msgX(void) {}
+ virtual void msgToGs(GSMessage & gsm) const = 0;
+ virtual void gsToMsg(GSMessage & gsm) = 0;
+};
+
+struct msg0 : public msgX
+{
+ int i;
+ double d;
+ std::string strh; //8 char
+ short s;
+ std::string str;
+ long l;
+
+ virtual void msgToGs(GSMessage & gsm) const
+ {
+ int msgId = 0;
+ gsm.clear();
+ gsm.appendGS(&msgId, sizeof(int));
+ gsm.appendGS(&i, sizeof(int));
+ gsm.appendGS(&d, sizeof(double));
+ gsm.appendGS(strh.data(), 8);
+ gsm.appendGS(&s, sizeof(short));
+ gsm.appendGS(str.c_str(), str.size() + 1);
+ gsm.appendGS(&l, sizeof(long));
+ }
+
+ virtual void gsToMsg(GSMessage & gsm)
+ {
+ int msgId = 0;
+ int tmp;
+ gsm.readGS(&tmp, sizeof(int));
+ if(tmp != msgId)
+ throw std::runtime_error("la gs ne correspond pas à la structure");
+ gsm.readGS(&i, sizeof(int));
+ gsm.readGS(&d, sizeof(double));
+ gsm.getString(strh, 8);
+ gsm.readGS(&s, sizeof(short));
+ gsm.getString(str);
+ gsm.readGS(&l, sizeof(long));
+ }
+};
+
+#endif //STRUCT_MESSAGE_HH
diff --git a/i/simulotron/src/comc/test_comc.cc b/i/simulotron/src/comc/test_comc.cc
index 1fe2aa7..7182df1 100644
--- a/i/simulotron/src/comc/test_comc.cc
+++ b/i/simulotron/src/comc/test_comc.cc
@@ -1,7 +1,7 @@
-/* test_comc.cc - Programme de test pour comc */
+/* test_comc.cc - Programme de test du comc. */
/* Simulotron - Programme de simulation de robot {{{
*
- * Copyright (C) 2005 Nicolas Haller
+ * Copyright (C) 2006 Nicolas Haller
*
* Robot APB Team/Efrei 2005.
* Web: http://assos.efrei.fr/robot/
@@ -23,24 +23,111 @@
*
* }}} */
+#include "comc/struct_message.hh"
#include "comc/comc.hh"
+#include "socket/socket_server.hh"
+#include <sys/wait.h>
#include <iostream>
+// message de test
+msg0 ms;
-int
-main (void)
+int testGSServer(SocketServer & sockServ);
+void testGSClient(int pauseMode);
+
+int main(void)
{
- try
+ //pauseMode pour debbuger le fork
+ int pauseMode = 0;
+ int pid;
+ //retour des test
+ int resultClient, resultServer;
+ //Initialisation des valeurs de la struct de test
+ ms.i = 42;
+ ms.d = 23.23;
+ ms.strh = "chier!!!";
+ ms.s = 4;
+ ms.str = "Ho!! Une structure de test, comme c'est amusant!!";
+ ms.l = 3432;
+ // 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))
{
- ComC<TuSersARien> ccTSAR("127.0.0.1", 4080, "chier");
- ccTSAR.receive();
+ std::cerr << "ECHEC: Problème coté client" << std::endl;
+ exit (-1);
}
- catch (std::runtime_error & chier)
+ if (resultServer != 0)
{
- std::cout << chier.what() << std::endl;
- exit(1);
+ std::cerr << "ECHEC: Problème coté serveur" << std::endl;
+ exit (-1);
}
return 0;
}
+
+int testGSServer(SocketServer & sockServ)
+{
+ try
+ {
+ std::string source, dest;
+ GSMessage gsm;
+ msg0 mscom;
+ int msgId;
+ // Création de la comc
+ ComC comc(sockServ,"serveur");
+ while(comc.receiveGS(gsm,source,dest, msgId)){}
+ mscom.gsToMsg(gsm);
+ if(!(ms.i == mscom.i &&
+ ms.d == mscom.d &&
+ ms.strh == mscom.strh &&
+ ms.s == mscom.s &&
+ ms.str == mscom.str &&
+ ms.l == mscom.l))
+ {
+ std::cout << "CHIER!! Le message recu n'est pas identique !!" << std::endl;
+ std::cout << "m d'origine: " << ms.i << "\nm d'arrivée: " << mscom.i << "result: " << (ms.i == mscom.i) << std::endl;
+ std::cout << "m d'origine: " << ms.d << "\nm d'arrivée: " << mscom.d << "result: " << (ms.d == mscom.d) << std::endl;
+ std::cout << "m d'origine: " << ms.strh << "\nm d'arrivée: " << mscom.strh << "result: " << (ms.strh == mscom.strh) << std::endl;
+ std::cout << "m d'origine: " << ms.s << "\nm d'arrivée: " << mscom.s << "result: " << (ms.s == mscom.s) << std::endl;
+ std::cout << "m d'origine: " << ms.str << "\nm d'arrivée: " << mscom.str << "result: " << (ms.str == mscom.str) << std::endl;
+ std::cout << "m d'origine: " << ms.l << "\nm d'arrivée: " << mscom.l << "result: " << (ms.l == mscom.l) << std::endl;
+ return 1;
+ }
+ }
+ catch (std::exception & c)
+ {
+ std::cout << "Oops, exeption dans le serveur" << std::endl;
+ }
+ return 0;
+}
+
+void testGSClient(int pauseMode)
+{
+ while(pauseMode)
+ sleep (10);
+ try
+ {
+ // Création du comc
+ ComC comc(std::string("127.0.0.1"), 4242, "client");
+ comc.send(ms, std::string("serveur"));
+ }
+ 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);
+}
diff --git a/i/simulotron/src/comc/test_comc_struct.hh b/i/simulotron/src/comc/test_comc_struct.hh
deleted file mode 100644
index fa1fc63..0000000
--- a/i/simulotron/src/comc/test_comc_struct.hh
+++ /dev/null
@@ -1,37 +0,0 @@
-#ifndef TCS_HH
-#define TCS_HH
-/* test_comc_struct.hh - message de test 0 */
-/* Simulotron - Programme de simulation de robot {{{
- *
- * Copyright (C) 2006 Nicolas Haller
- *
- * Robot APB Team/Efrei 2006.
- * 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.
- *
- * }}} */
-
-struct msg0
-{
- int i;
- double j;
- std::string str1; // Limité à 8 octets
- short s;
- std::string str2; // string infini
- long l;
-}
-#endif //TCS_HH