#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 "utils/simulotron_exception.hh" #include #include /// 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; }; /// Message 0: message de test, aucune utilité 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 simulotron_exception("msg0", "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)); } }; /// Message 1: initialisation de la com module/hub, premier message struct msg1 : public msgX { std::string moduleName; //8 char virtual void msgToGs(GSMessage & gsm) const { int msgId = 1; gsm.clear(); gsm.appendGS(&msgId, sizeof(int)); gsm.appendGS(moduleName.data(), 8); } virtual void gsToMsg(GSMessage & gsm) { int msgId = 1; int tmp; gsm.readGS(&tmp, sizeof(int)); if(tmp != msgId) throw simulotron_exception("msg1", "la gs ne correspond pas à la structure"); gsm.getString(moduleName, 8); } }; /// Message ?: template pour les messages vide template struct msg : public msgX { virtual void msgToGs(GSMessage & gsm) const { int msgId = MSGID; gsm.clear(); gsm.appendGS(&msgId, sizeof(int)); } virtual void gsToMsg(GSMessage & gsm) { int msgId = MSGID; int tmp; gsm.readGS(&tmp, sizeof(int)); if(tmp != msgId) throw simulotron_exception("msg" + MSGID, "la gs ne correspond pas à la structure"); } }; #endif //STRUCT_MESSAGE_HH