summaryrefslogtreecommitdiff
path: root/i/simulotron/src/hub/hub.cc
diff options
context:
space:
mode:
Diffstat (limited to 'i/simulotron/src/hub/hub.cc')
-rw-r--r--i/simulotron/src/hub/hub.cc139
1 files changed, 139 insertions, 0 deletions
diff --git a/i/simulotron/src/hub/hub.cc b/i/simulotron/src/hub/hub.cc
new file mode 100644
index 0000000..13d3e23
--- /dev/null
+++ b/i/simulotron/src/hub/hub.cc
@@ -0,0 +1,139 @@
+/* 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 <iostream>
+
+Hub::Hub (std::string & address, int port)
+ :timeh_(0), socket_(address, port), isRunning(false)
+{
+ init();
+}
+
+void
+Hub::start(void)
+{
+ waitConnection();
+ startLoop();
+}
+
+void
+Hub::init(void)
+{
+ // Créer la liste des modules attendus
+ //dfqsdf
+
+}
+
+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(ComH(socket_));
+ // On verifie que le module est attendu
+ std::list<std::string>::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
+ 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<ComH>::iterator it = modules_.begin(); it != modules_.end(); it++)
+ {
+ processModule(it);
+ }
+}
+
+void
+Hub::processModule(const std::list<ComH>::iterator & it)
+{
+ std::string source, dest;
+ int msgId;
+ GSMessage gsm;
+ while (it->receiveMsg(gsm, source, dest, msgId))
+ laPoste(gsm, source, dest, msgId);
+}
+
+void
+Hub::processWaits(void)
+{
+ int timeToSet = 0;
+ long long int tmp;
+ for(std::list<ComH>::iterator it = modules_.begin(); it != modules_.end(); it++)
+ {
+ if(!it->isWaiting())
+ return;
+ tmp = it->getDLWait();
+ if(tmp != -1 && tmp < timeToSet)
+ timeToSet = tmp;
+ }
+}
+
+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<ComH>::iterator it = modules_.begin(); it != modules_.end(); it++)
+ {
+ if(dest == it->getModuleName())
+ it->send(gsm, source, dest, msgId);
+ }
+ }
+}
+