summaryrefslogtreecommitdiff
path: root/i/simulotron/src/socket/test_socket.cc
blob: a2c729e2ffd78224b60e9d7e388633036b588ed5 (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
/* test_socket.cc - Programme de test des sockets. */
/* 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/address.hh"
#include "socket/socket_server.hh"
#include "socket/socket_client.hh"
#include "utils/errno_exception.hh"

#include <iostream>

// Adress useful
Address adr;
Address adr2;
//Chaine et char de test
const std::string strDepart = "J'aime la prog syst�me.";
std::string strDest;
const char a = 'f';
char b;
//threads
pthread_t threadId[2];
void * retour;
int stateTh[2];

void * testSockServ(void * CaSertARien);
void * testSockClient(void * CaSertARien);

int main(void)
{
    int ret;
    ret = pthread_create(& threadId[0], NULL, testSockServ, NULL);
    if(ret != 0)
    {
	std::cout << "CHIER: Echec thread serveur" << std::endl;
	return EXIT_FAILURE;
    }
    ret = pthread_create(& threadId[1], NULL, testSockClient, NULL);
    if(ret != 0)
    {
	std::cout << "CHIER: Echec thread client" << std::endl;
	return EXIT_FAILURE;
    }
    pthread_join(threadId[0], &retour);
    stateTh[0] = (int) retour;
    pthread_join(threadId[1], &retour);
    stateTh[1] = (int) retour;
    if(stateTh[0] == 0 && stateTh[1] == 0)
	return 0;
    else
	return 1;
}

void * testSockServ(void * CaSertARien)
{
    try
    {
	// Cr�ation de la socket serveur;
	SocketServer sockServ(Address(4242));
	// Mise en �coute du socket serveur
	sockServ.listen(12);
	// Acceptation d'une connection
	SocketClient sockFork(sockServ);
	// Reception d'un message
	strDest = sockFork.read();
	if(strDepart != strDest)
	{
	    std::cout << "CHIER!! Le message d'origine est alt�r� en ecrivant du client au serveur!!" << std::endl;
	    std::cout << "Le message d'origine: " << strDepart << std::endl;
	    std::cout << "Message re�u: " << std::endl;
	    return (void *) 1;
	}
	// Ecriture du serveur au client
	sockFork.write(strDepart);
	// Reception d'un char
	b = sockFork.getChar();
	if(a != b)
	{
	    std::cout << "CHIER!! Le char d'origine est alt�r� en ecrivant du client au serveur!!" << std::endl;
	    std::cout << "Le char d'origine: " << a << std::endl;
	    std::cout << "Le char d'arriv�: " << b << std::endl;
	    return (void *) 1;
	}
	// Ecriture du serveur vers le client
	sockFork.putChar(a);
    }
    catch(errno_exception & chier)
    {
	std::cout << "CHIER !! Une exception a �t� lanc� cot� serveur!!" << std::endl;
	std::cout << chier.what() << std::endl;
	return (void *) 1;
    }
    return (void *) 0;
}

void * testSockClient(void * CaSertARien)
{
    try
    {
	// Cr�ation de la socket client
	SocketClient sockClient(adr);
	// Demande de connection
	sockClient.connect(Address(std::string("localhost"), 4242));
	// Ecriture du client dans le serveur
	sockClient.write(strDepart);
	// Reception du message serveur
	strDest = sockClient.read();
	if(strDepart != strDest)
	{
	    std::cout << "CHIER!! Le message d'origine est alt�r� en ecrivant du serveur au client!!" << std::endl;
	    std::cout << "Le message d'origine: " << strDepart << std::endl;
	    std::cout << "Message re�u: " << std::endl;
	    return (void *) 1;
	}
	// Ecriture d'un char du client au serveur
	sockClient.putChar(a);
	// Reception d'un char
	b = sockClient.getChar();
	if(a != b)
	{
	    std::cout << "CHIER!! Le char d'origine est alt�r� en ecrivant du serveur au client!!" << std::endl;
	    std::cout << "Le char d'origine: " << a << std::endl;
	    std::cout << "Le char d'arriv�: " << b << std::endl;
	    return (void *) 1;
	}
    }
    catch(errno_exception & chier)
    {
	std::cout << "CHIER !! Une exception a �t� lanc� cot� client!!" << std::endl;
	std::cout << chier.what() << std::endl;
	return (void *) 1;
    }
    return (void *) 0;
}