summaryrefslogtreecommitdiff
path: root/i/simulotron/src/hub/comh.cc
blob: 124238e17dadab24ecdf6d2caa4cb26818ab558e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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;
}