summaryrefslogtreecommitdiff
path: root/i/simulotron/src/hub/comh.cc
diff options
context:
space:
mode:
Diffstat (limited to 'i/simulotron/src/hub/comh.cc')
-rw-r--r--i/simulotron/src/hub/comh.cc107
1 files changed, 107 insertions, 0 deletions
diff --git a/i/simulotron/src/hub/comh.cc b/i/simulotron/src/hub/comh.cc
new file mode 100644
index 0000000..124238e
--- /dev/null
+++ b/i/simulotron/src/hub/comh.cc
@@ -0,0 +1,107 @@
+/* comh.cc - Classe ComC speciale Hub */
+/* 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/comh.hh"
+#include "gs/gs_message.hh"
+#include "comc/struct_message.hh"
+
+#include <stdexcept>
+
+ComH::ComH (SocketServer & socket)
+ :aiguillage_(socket, std::string("hub")), waitingForMessage_(false)
+{
+ initCom();
+}
+
+const std::string &
+ComH::getModuleName(void)
+{
+ return moduleName_;
+}
+
+bool
+ComH::receiveMsg(GSMessage & gsm, std::string & source, std::string & dest, int & msgId, bool bloquant)
+{
+ if(aiguillage_.receive(gsm, source , dest, bloquant))
+ {
+ gsm.readGS(&msgId, sizeof(int));
+ gsm.setIteratorBegin();
+ return true;
+ }
+ return false;
+}
+
+void
+ComH::send(const GSMessage & gsm, const std::string & source, const std::string & dest)
+{
+ aiguillage_.send(gsm, source, dest);
+}
+
+void
+ComH::setWait(long long int DL, bool waitingForMessage)
+{
+ if(isWaiting())
+ throw simulotron_exception("ComH", "Le module est déjà en attente!!");
+ if(DL <= 0 && !waitingForMessage)
+ throw simulotron_exception("ComH", "On attend rien là!!");
+ dlWaiting_ = DL;
+ waitingForMessage_ = waitingForMessage;
+}
+
+bool
+ComH::isWaiting (void)
+{
+ if ( waitingForMessage_ || dlWaiting_ > 0)
+ return true;
+ return false;
+}
+
+long long int
+ComH::getDLWait(void)
+{
+ return dlWaiting_;
+}
+
+void
+ComH::unlockWait(void)
+{
+ waitingForMessage_ = false;
+ dlWaiting_ = 0;
+}
+
+void
+ComH::initCom(void)
+{
+ msg1 ms;
+ GSMessage gsm;
+ std::string source, dest;
+ int msgId;
+ // Reçoit le premier message
+ receiveMsg(gsm, source, dest, msgId, true);
+ if(msgId != 1)
+ throw simulotron_exception("ComH", "initCom: le message de reponse est incorrect!!");
+ ms.gsToMsg(gsm);
+ moduleName_ = ms.moduleName;
+}