summaryrefslogtreecommitdiff
path: root/i/simulotron/src/aiguillage/aiguillage.cc
blob: 3abd5abd5a90d9b8d60e9126a03b9aca8fe240f5 (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
/* 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 <stdexcept>

/// 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');
}