summaryrefslogtreecommitdiff
path: root/i/simulotron/src/hub/test_hub.cc
blob: c9645d0c499f21e1a059550ea28ef36e3afdbe52 (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
/* test_hub.cc - Programme de test du 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 "comc/struct_message.hh"
#include "hub/hub.hh"
#include "socket/socket_server.hh"
#include "comc/comc.hh"

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

// message de test
msg0 ms1, ms2;

int testHub(Hub & hub);
void testModule1(int pauseMode1);
void testModule2(int pauseMode2);

int main(void)
{
    //pauseMode pour debbuger le fork
    int pauseMode1 = 0;
    int pauseMode2 = 0;
    pid_t pidModule1, pidModule2;
    //retour des test
    int resultHub, resultModule1, resultModule2;
    //Initialisation des valeurs de la struct de test
    ms1.i = 42;
    ms1.d = 23.23;
    ms1.strh = "chier!!!";
    ms1.s = 4;
    ms1.str = "Ho!! Une structure de test, comme c'est amusant!!";
    ms1.l = 3432;
    // Cr�ation du hub
    Hub hub(std::string(), 4242);
    // On cr�e le fork du module1
    pidModule1 = fork();
    if (pidModule1 == 0) // processus fils
	testModule1(pauseMode1);
    else //processus p�re
    {
	pidModule2 = fork();
	if (pidModule2 == 0)
	    testModule2(pauseMode2);
	else
	    resultHub = testHub(hub);

	// On analyse les r�sultats
	waitpid(pidModule1, &resultModule1, 0);
	waitpid(pidModule2, &resultModule2, 0);
	if (!(WIFEXITED(resultModule1) && WEXITSTATUS(resultModule1) == 0))
	{
	    std::cerr << "ECHEC: Probl�me cot� module 1" << std::endl;
	    exit (-1);
	}
	if (!(WIFEXITED(resultModule2) && WEXITSTATUS(resultModule2) == 0))
	{
	    std::cerr << "ECHEC: Probl�me cot� module 2" << std::endl;
	    exit (-1);
	}
	if (resultHub != 0)
	{
	    std::cerr << "ECHEC: Probl�me cot� hub" << std::endl;
	    exit (-1);
	}
    }
    return 0;
}

int testHub(Hub & hub)
{
    try
    {
	hub.start();
    }
    catch (std::exception & c)
    {
	std::cout << "Oops, exeption dans le hub" << std::endl;
	std::cout << c.what() << std::endl;
    }
    return 0;
}

void testModule1(int pauseMode)
{
    while(pauseMode)
	sleep (10);
    try
    {
	// Cr�ation du comc
	ComC comc(std::string("127.0.0.1"), 4242, "module1");
	// Initialisation de la com avec le hub
	comc.initHub();
	comc.send(ms1, std::string("module2"));
    }
    catch(std::exception & chier)
    {
	std::cout << "CHIER !! Une exception a �t� lanc� cot� module1!!" << std::endl;
	std::cout << chier.what() << std::endl;
	exit (1);
    }
    exit (0);
}

void testModule2(int pauseMode)
{
    while(pauseMode)
	sleep (10);
    try
    {
	GSMessage gsm;
	std::string source, dest;
	int msgId;
	// Cr�ation du comc
	ComC comc(std::string("127.0.0.1"), 4242, "module2");
	// Initialisation de la com avec le hub
	comc.initHub();
	// Reception du message
	comc.receiveGS(gsm, source, dest, msgId, true);
	ms2.gsToMsg(gsm);
	// Test dessus
	if (ms1.i != ms2.i ||
	    ms1.d != ms2.d ||
	    ms1.strh != ms2.strh ||
	    ms1.s != ms2.s ||
	    ms1.str != ms2.str ||
	    ms1.l != ms2.l)
	{
	    std::cerr << "CHIER!! Le message n'est pas identique � l'arriv�!!" << std::endl;
	}
    }
    catch(std::exception & chier)
    {
	std::cout << "CHIER !! Une exception a �t� lanc� cot� module2!!" << std::endl;
	std::cout << chier.what() << std::endl;
	exit (1);
    }
    exit (0);
}