summaryrefslogtreecommitdiff
path: root/i/simulotron/src/aiguillage/test_aiguillage.cc
blob: bb65af05e6ae8e13bf7c494e06955cdd11ab014f (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/* test_aiguillage.cc - Programme de test d'aiguillage. */
/* Simulotron - Programme de simulation de robot {{{
 *
 * Copyright (C) 2005 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 "socket/socket_server.hh"
#include "aiguillage/aiguillage.hh"
#include "gs/gs_message.hh"

#include <sys/wait.h>
#include <iostream>

//Chaine et char de test
int i1 = 1;
int i2;
std::string name1 = "nono";
std::string name2 = "robert";
std::string name3 = "marvin";

int testGSServer(SocketServer & sockServ);
void testGSClient(int pauseMode);

int main(void)
{
    //pauseMode pour debbuger le fork
    int pauseMode = 0;
    int pid;
    //retour des test
    int resultClient, resultServer;
    // Cr�ation de la socket serveur;
    SocketServer sockServ(std::string(),4242);
    // Mise en �coute du socket serveur
    sockServ.listen(12);
    // On cr�e le fork du client
    pid = fork();
    if (pid == 0) // processus fils
	testGSClient(pauseMode);
    else //processus p�re
	resultServer = testGSServer(sockServ);

    // On analyse les r�sultats
    wait(&resultClient);
    if (!(WIFEXITED(resultClient) && WEXITSTATUS(resultClient) == 0))
    {
	std::cerr << "ECHEC: Probl�me cot� client" << std::endl;
	exit (-1);
    }
    if (resultServer != 0)
    {
	std::cerr << "ECHEC: Probl�me cot� serveur" << std::endl;
	exit (-1);
    }

    return 0;
}

int testGSServer(SocketServer & sockServ)
{
    // Cr�ation de l'aiguillage
    Aiguillage aiSNCF(sockServ,name2);
    std::string source, dest;
    // Reception des messages
    GSMessage gsm;
    // Message 1
    while (!aiSNCF.receive(gsm, source, dest, true));
    gsm.readGS(&i2, sizeof(int));
    if (i1 != i2)
    {
	std::cout << "CHIER int alt�r�\n"
	    << "Message d'origine: " << i1 << "\n"
	    << "Message d'arriv�: "  << i2
	    << std::endl;
	return 1;
    }
    if (source != name3)
    {
	std::cout << "CHIER source alt�r�\n"
	    << "Source attendu: " << name3 << "\n"
	    << "Source re�u: " << source
	    << std::endl;
	return 1;
    }
    if (dest != name1)
    {
	std::cout << "Chier dest attendu\n"
	    << "Dest attendu: " << name1 << "\n"
	    << "Dest recu: " << dest << "\n"
	    << std::endl;
	return 1;
    }
    gsm.clear();
    // Message 2
    while (!aiSNCF.receive(gsm, source, dest, true));
    gsm.readGS(&i2, sizeof(int));
    if (i1 != i2)
    {
	std::cout << "CHIER int alt�r�\n"
	    << "Message d'origine: " << i1 << "\n"
	    << "Message d'arriv�: "  << i2
	    << std::endl;
	return 1;
    }
    if (source != name2)
    {
	std::cout << "CHIER source alt�r�\n"
	    << "Source attendu: " << name2 << "\n"
	    << "Source re�u: " << source
	    << std::endl;
	return 1;
    }
    if (dest != name3)
    {
	std::cout << "Chier dest attendu\n"
	    << "Dest attendu: " << name3 << "\n"
	    << "Dest recu: " << dest << "\n"
	    << std::endl;
	return 1;
    }
    gsm.clear();
    // Message 3
    while (!aiSNCF.receive(gsm, source, dest, true));
    gsm.readGS(&i2, sizeof(int));
    if (i1 != i2)
    {
	std::cout << "CHIER int alt�r�\n"
	    << "Message d'origine: " << i1 << "\n"
	    << "Message d'arriv�: "  << i2
	    << std::endl;
	return 1;
    }
    if (source != name1)
    {
	std::cout << "CHIER source alt�r�\n"
	    << "Source attendu: " << name1 << "\n"
	    << "Source re�u: " << source
	    << std::endl;
	return 1;
    }
    if (dest != name2)
    {
	std::cout << "Chier dest attendu\n"
	    << "Dest attendu: " << name2 << "\n"
	    << "Dest recu: " << dest << "\n"
	    << std::endl;
	return 1;
    }
    gsm.clear();
    return 0;
}

void testGSClient(int pauseMode)
{
    while(pauseMode)
	sleep (10);
    try
    {
	// Cr�ation de l'aiguillage
	Aiguillage aiSNCF(std::string("127.0.0.1"), 4242,name3);
	GSMessage g1;
	g1.appendGS(&i1, sizeof(int));
	aiSNCF.send(g1, name3, name1);
	aiSNCF.send(g1, name2, name3);
	aiSNCF.send(g1, name1, name2);
    }
    catch(std::exception & chier)
    {
	std::cout << "CHIER !! Une exception a �t� lanc� cot� client!!" << std::endl;
	std::cout << chier.what() << std::endl;
	exit (1);
    }
    exit (0);
}