/* aiguillage.cc - Classe rajoutant les info de la source et de la dest d'un packet */ /* 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. * * }}} */ #include "aiguillage/aiguillage.hh" #include "gs/gs_message.hh" #include "utils/simulotron_exception.hh" #include /// Constructeur client Aiguillage::Aiguillage(const std::string & address, int port, const std::string & name) :gst_(address, port) { setName(name); } /// Constructeur serveur Aiguillage::Aiguillage(SocketServer & socket, const std::string & name) :gst_(socket) { setName(name); } /// Envoie un paquet en rajoutant les infos de sources et de provenance void Aiguillage::send(const GSMessage & gsm, const std::string & source, const std::string & dest) { ///\todo Voir pour optimiser ca GSMessage g = gsm; g.insertFrontGS(dest.data(), MAX_SIZE_NAME); // Si source est vide, mettre name_ if(source.empty()) g.insertFrontGS(name_.data(), MAX_SIZE_NAME); else g.insertFrontGS(source.data(), MAX_SIZE_NAME); gst_.putGS(g); } /// Reçoie un paquet du réseau et supprime les infos src et dest du GSM bool Aiguillage::receive(GSMessage & gsm, std::string & source, std::string & dest, bool bloquant) { std::string tmp; if(!gst_.getGS(gsm, bloquant)) return false; gsm.getString(tmp, 8); source = tmp.c_str(); gsm.getString(tmp, 8); dest = tmp.c_str(); gsm.delBeforeIt(); return true; } void Aiguillage::setName(const std::string & name) { if(name.size() > MAX_SIZE_NAME) throw simulotron_exception("Aiguillage", "Le nom est trop long!!"); name_ = name; name_.append(MAX_SIZE_NAME - name.size(), '\0'); }