/* hub.cc - Class hub du simulotron */ /* Simulotron - Programme de simulation de robot {{{ * * Copyright (C) 2006 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 "hub/hub.hh" #include "gs/gs_message.hh" #include Hub::Hub (const std::string & address, int port) :timeh_(0), socket_(address, port), isRunning(false) { init(); socket_.listen(32); } Hub::~Hub(void) { for (std::list::iterator it = modules_.begin(); it != modules_.end(); it++) delete *it; } void Hub::start(void) { waitConnection(); startLoop(); } void Hub::init(void) { // Créer la liste des modules attendus ///\todo faire l'init modAwaited_.push_back(std::string("module1")); modAwaited_.push_back(std::string("module2")); } void Hub::waitConnection(void) { //Tant que l'on attend des modules... while (!modAwaited_.empty()) { // On crée le système de com avec le module modules_.push_back(new ComH(socket_)); // On verifie que le module est attendu std::list::iterator it = find(modAwaited_.begin(), modAwaited_.end(), modules_.back()->getModuleName()); if(it == modAwaited_.end()) { // Ouille, on reporte l'incident std::cerr << "Un module inattendu a tenté de se connecter au Hub, nom du module: " << modules_.back()->getModuleName() << std::endl; // On vire le malotru delete modules_.back(); modules_.pop_back(); continue; } // On vire le modules de la liste des attendu modAwaited_.erase(it); } } void Hub::startLoop(void) { isRunning = true; while(isRunning) { processModules(); processWaits(); processStoppingRules(); } } void Hub::processModules(void) { for(std::list::iterator it = modules_.begin(); it != modules_.end(); it++) { processModule(it); } } void Hub::processModule(const std::list::iterator & it) { std::string source, dest; int msgId; GSMessage gsm; while ((*it)->receiveMsg(gsm, source, dest, msgId, false)) laPoste(gsm, source, dest, msgId); } void Hub::processWaits(void) { int timeToSet = 0; long long int tmp; for(std::list::iterator it = modules_.begin(); it != modules_.end(); it++) { if(!(*it)->isWaiting()) return; tmp = (*it)->getDLWait(); if(tmp != -1 && tmp < timeToSet) timeToSet = tmp; } } void Hub::processStoppingRules (void) { } void Hub::laPoste (const GSMessage & gsm, const std::string & source, const std::string & dest, int msgId) { //Si c'est un message particulier if(msgId == 0) { } // Si c'est un message banal else { ///\todo pas optimisé for(std::list::iterator it = modules_.begin(); it != modules_.end(); it++) { if(dest == (*it)->getModuleName()) (*it)->send(gsm, source, dest); } } }