From 8f486613be58ced269db1d437e560c16558604e8 Mon Sep 17 00:00:00 2001 From: becquet Date: Thu, 10 May 2007 18:49:20 +0000 Subject: Création de chuck, le programme du robot 2007. --- i/chuck/src/socket/Makefile.defs | 12 ++ i/chuck/src/socket/address.cc | 93 +++++++++++ i/chuck/src/socket/address.hh | 76 +++++++++ i/chuck/src/socket/server_socket.cc | 86 ++++++++++ i/chuck/src/socket/server_socket.hh | 48 ++++++ i/chuck/src/socket/socket_client.cc | 115 +++++++++++++ i/chuck/src/socket/socket_client.hh | 63 +++++++ i/chuck/src/socket/socket_databuffer.cc | 97 +++++++++++ i/chuck/src/socket/socket_databuffer.hh | 52 ++++++ i/chuck/src/socket/socket_server.cc | 90 ++++++++++ i/chuck/src/socket/socket_server.hh | 56 +++++++ i/chuck/src/socket/socket_text.cc | 236 +++++++++++++++++++++++++++ i/chuck/src/socket/socket_text.hh | 80 +++++++++ i/chuck/src/socket/test_server.cc | 91 +++++++++++ i/chuck/src/socket/test_socket.cc | 156 ++++++++++++++++++ i/chuck/src/socket/test_socket_databuffer.cc | 125 ++++++++++++++ i/chuck/src/socket/test_socket_old.cc | 76 +++++++++ 17 files changed, 1552 insertions(+) create mode 100644 i/chuck/src/socket/Makefile.defs create mode 100644 i/chuck/src/socket/address.cc create mode 100644 i/chuck/src/socket/address.hh create mode 100644 i/chuck/src/socket/server_socket.cc create mode 100644 i/chuck/src/socket/server_socket.hh create mode 100644 i/chuck/src/socket/socket_client.cc create mode 100644 i/chuck/src/socket/socket_client.hh create mode 100644 i/chuck/src/socket/socket_databuffer.cc create mode 100644 i/chuck/src/socket/socket_databuffer.hh create mode 100644 i/chuck/src/socket/socket_server.cc create mode 100644 i/chuck/src/socket/socket_server.hh create mode 100644 i/chuck/src/socket/socket_text.cc create mode 100644 i/chuck/src/socket/socket_text.hh create mode 100644 i/chuck/src/socket/test_server.cc create mode 100644 i/chuck/src/socket/test_socket.cc create mode 100644 i/chuck/src/socket/test_socket_databuffer.cc create mode 100644 i/chuck/src/socket/test_socket_old.cc (limited to 'i/chuck/src/socket') diff --git a/i/chuck/src/socket/Makefile.defs b/i/chuck/src/socket/Makefile.defs new file mode 100644 index 0000000..1ff9efc --- /dev/null +++ b/i/chuck/src/socket/Makefile.defs @@ -0,0 +1,12 @@ +PROGRAMS += test_socket test_socket_databuffer #test_server + +socket_basic_OBJECTS = address.o socket_server.o socket_client.o +socket_databuffer_OBJECTS = socket_databuffer.o $(socket_basic_OBJECTS) \ + $(data_OBJECTS) + +test_socket_OBJECTS = test_socket.o $(socket_basic_OBJECTS) $(data_OBJECTS) +test_socket_databuffer_OBJECTS = test_socket_databuffer.o \ + $(socket_databuffer_OBJECTS) $(data_OBJECTS) \ + $(socket_basic_OBJECTS) +#test_server_OBJECTS = test_server.o $(net_OBJECTS) $(data_OBJECTS) \ + $(image_OBJECTS) diff --git a/i/chuck/src/socket/address.cc b/i/chuck/src/socket/address.cc new file mode 100644 index 0000000..4567e2e --- /dev/null +++ b/i/chuck/src/socket/address.cc @@ -0,0 +1,93 @@ +// address.cc +// robert - programme du robot 2005. {{{ +// +// Copyright (C) 2005 Dufour Jérémy +// +// 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. +// +// Contact : +// Web: %WEB% +// Email: +// }}} +#include "address.hh" + +#include +#include + +/// Constructeur par défaut. +Address::Address (void) + : port_ (0) +{ + resolv (); +} + +/// Constructeur avec le port (serveur) +Address::Address (const int port) + : port_ (port) +{ + resolv (); +} +/// Constructeur avec hôte et port. +Address::Address (const std::string &host, const int port) + : host_ (host), port_ (port) +{ + resolv (); +} + +/// Constructeur à partir de la strcuture d'adresse. +Address::Address (const sockaddr *sa, socklen_t sl) +{ + // On vérifie que c'est vraiment un struct sockaddr_in + if (sl != sizeof (sockaddr_in)) + throw std::invalid_argument ("Invalide Adresse"); + sa_ = * reinterpret_cast (sa); +} + +/// Résolution de la structure d'adresse depuis l'hôte/port +void +Address::resolv (void) +{ + // Cas du serveur, pas d'hôte + if (host_.empty ()) + sa_.sin_addr.s_addr = htonl (INADDR_ANY); + else + { + // Convertion de l'hôte en structure adresse + if (!inet_pton (AF_INET, host_.c_str (), &sa_.sin_addr)) + { + hostent *host_info = gethostbyname (host_.c_str ()); + if (!host_info) + throw std::runtime_error (host_ + " : Hôte inconnue"); + } + } + sa_.sin_family = AF_INET; + // Convertion pour le passage sur le réseau + sa_.sin_port = htons (port_); +} + +/// Résolution de l'hôte/port depuis la structure d'adresse +void +Address::unresolv (void) +{ + hostent *host_info; + // Recherche de l'hôte + host_info = gethostbyaddr (reinterpret_cast (&(sa_.sin_addr)), sizeof sa_.sin_addr, AF_INET); + if (!host_info) + host_ = inet_ntoa (sa_.sin_addr); + else + host_ = host_info->h_name; + // Recherche du port + port_ = ntohs (sa_.sin_port); +} diff --git a/i/chuck/src/socket/address.hh b/i/chuck/src/socket/address.hh new file mode 100644 index 0000000..53fca07 --- /dev/null +++ b/i/chuck/src/socket/address.hh @@ -0,0 +1,76 @@ +#ifndef address_hh +#define address_hh +// address.hh +// robert - programme du robot 2005. {{{ +// +// Copyright (C) 2005 Dufour Jérémy +// +// 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. +// +// Contact : +// Web: %WEB% +// Email: +// }}} + +#include +#include +#include +#include + +#include +#include + +/// S'occupe de gérer les adresses. +class Address { + /// La structure sockaddr_in de type réseau/AF_INET. + sockaddr_in sa_; + /// Le nom d'hôte de l'adresse. + std::string host_; + /// Le numéro de port de l'adresse. + int port_; + + public: + /// Constructeur par défaut. + Address (void); + /// Constructeur pour le serveur. (port seulement) + Address (const int port); + /// Constructeur avec hôte et port. + Address (const std::string &host, const int port); + /// Constructeur à partir de la structure d'adresse. + Address (const sockaddr *sa, socklen_t sl); + /// Geter sockaddr. + const sockaddr *getSockaddr (void) const { return + reinterpret_cast (&sa_); } + /// Geter hôte. + const std::string &getHost (void) const { return host_; } + /// Geter port. + const int getPort (void) const { return port_; } + + private: + /// Résolution adresse vers hôte/port + void resolv (void); + /// Résolution hôte/port vers adresse + void unresolv (void); + +}; + +/// Surcharge de l'opérateur << pour l'affichage. +inline std::ostream & +operator<< (std::ostream &s, const Address &a) +{ + return s << a.getHost () << ':' << a.getPort (); +} + +#endif // address_hh diff --git a/i/chuck/src/socket/server_socket.cc b/i/chuck/src/socket/server_socket.cc new file mode 100644 index 0000000..539b94e --- /dev/null +++ b/i/chuck/src/socket/server_socket.cc @@ -0,0 +1,86 @@ +// server_socket.cc +// robert - programme du robot 2005. {{{ +// +// Copyright (C) 2005 Dufour Jérémy +// +// 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. +// +// Contact : +// Web: %WEB% +// Email: +// }}} +#include "server_socket.hh" + +#include "address.hh" + +#include +#include +#include + +/// Constructeur par défaut. +ServerSocket::ServerSocket (int port) +{ + bind (port); +} + +/// Bind le serveur sur un port d'écoute. +void +ServerSocket::bind (int port) +{ + static const int reuse_s = 1; + // Adresse sur un port (pas d'hote, écoutera de partout) + Address a (port); + // Création du socket IPv4 - TCP + socket_ = socket (PF_INET, SOCK_STREAM, 0); + if (socket_ == -1) + throw std::runtime_error ("Erreur de création du socket"); + // Socket réutilisable en cas d'arret "brutale" + if (setsockopt (socket_, SOL_SOCKET, SO_REUSEADDR, + &reuse_s, sizeof (int)) == -1) + throw std::runtime_error ("Erreur setsockopt : reusable"); + // Bindage du socket sur le port et l'adresse + if (::bind (socket_, a.getSockaddr (), sizeof (struct sockaddr_in)) == -1) + throw std::runtime_error ("Erreur de bind du socket"); + // On met le socket en attente de connexion + if (listen (socket_, 0) == -1) + throw std::runtime_error ("Erreur de listen"); +} + +// Accepte une nouvelle connexion. +int +ServerSocket::accept (void) const +{ + int s = ::accept (socket_, 0, 0); + if (s == -1) + throw std::runtime_error ("Erreur d'accept de nouvelles connexions"); + return s; +} + +// Accepte une nouvelle connexion et remplie Address. +int +ServerSocket::accept (Address &a) const +{ + sockaddr_in sa; + socklen_t sl = sizeof sa; + int s = ::accept (socket_, reinterpret_cast (&sa), + &sl); + if (s == -1) + { + throw std::runtime_error ("Erreur d'accept nouvelle connexion"); + } + // Récupération de l'adresse + a = Address (reinterpret_cast (&sa), sl); + return s; +} diff --git a/i/chuck/src/socket/server_socket.hh b/i/chuck/src/socket/server_socket.hh new file mode 100644 index 0000000..159aa65 --- /dev/null +++ b/i/chuck/src/socket/server_socket.hh @@ -0,0 +1,48 @@ +#ifndef server_socket_hh +#define server_socket_hh +// server_socket.hh +// robert - programme du robot 2005. {{{ +// +// Copyright (C) 2005 Dufour Jérémy +// +// 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. +// +// Contact : +// Web: %WEB% +// Email: +// }}} + +#include +class Address; + +/// Créer un server TCP pour le transfert des données. +class ServerSocket { + private: + /// L'identificateur du socket. + int socket_; + public: + /// Constructeur par défaut. + ServerSocket (int port); + /// Destructeur. + ~ServerSocket () { close (socket_); } + /// Accepte une nouvelle connexion et remplie Address. + int accept (Address &a) const; + /// Accepte une nouvelle connexion. + int accept (void) const; + private: + /// Bind le serveur sur un port d'écoute. + void bind (int port); +}; +#endif // server_socket_hh diff --git a/i/chuck/src/socket/socket_client.cc b/i/chuck/src/socket/socket_client.cc new file mode 100644 index 0000000..951ea6a --- /dev/null +++ b/i/chuck/src/socket/socket_client.cc @@ -0,0 +1,115 @@ +/* socket_client.cc - Définition de la classe des sockets client */ +/* 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_client.hh" +#include "socket/socket_server.hh" +#include "socket/address.hh" +#include "utils/errno_exception.hh" + +#include +#include + +SocketClient::SocketClient (void) +{ + // Crée le file descriptor du socket + socket_ = socket (PF_INET, SOCK_STREAM, 0); + if (socket_ < 0) + throw errno_exception ("Socket: La chaussette n'a pas pu être tricoté", errno); +} + +SocketClient::SocketClient (SocketServer & socketServer) +{ + socket_ = socketServer.accept (); + if (socket_ < 0) + throw errno_exception ("Socket: Problème lors de la construction d'une chaussette cliente par accept", errno); +} + +SocketClient::~SocketClient (void) +{ + if (socket_ >= 0) + ::close (socket_); +} + +void +SocketClient::connect (const Address & address) +{ + if (::connect (socket_, address.getSockaddr (), sizeof (sockaddr_in)) < 0) + throw errno_exception ("Socket: Impossible de mettre la chaussette", errno); +} + +void +SocketClient::connect (const std::string & address, int port) +{ + connect (Address(address, port)); +} + +bool +SocketClient::read (std::string & strReaded, bool bloquant) +{ + int charReaded; + char buffer[BUFFER_SIZE]; + // Vérifie si il y a quelque chose à lire et retourne si non + if (!bloquant) + { + fd_set fds; + timeval tv = {0, 0}; + FD_ZERO(&fds); + FD_SET(socket_, &fds); + if(select(socket_ + 1, &fds, NULL, NULL, &tv) == 0) + return false; + } + strReaded.clear(); + do + { + charReaded = ::read (socket_, buffer, BUFFER_SIZE - 1); + if (charReaded < 0) + throw errno_exception ("SocketClient: problème de lecture", errno); + //buffer[charReaded] = 0; + //str += buffer; + strReaded.append(buffer, charReaded); + }while (charReaded == BUFFER_SIZE - 1); + return true; +} + +void +SocketClient::write (const std::string & str) +{ + write(reinterpret_cast(str.data()), str.size()); +} + +void +SocketClient::write (const uint8_t * buffer, unsigned size) +{ + int retval; + + retval = ::write (socket_, buffer, size); + if (retval < 0) + throw errno_exception ("SocketClient: erreur d'écriture (write()) ", errno); +} + +//int +//SocketClient::getFD(void) +//{ +// return socket_; +//} diff --git a/i/chuck/src/socket/socket_client.hh b/i/chuck/src/socket/socket_client.hh new file mode 100644 index 0000000..ad8d1ea --- /dev/null +++ b/i/chuck/src/socket/socket_client.hh @@ -0,0 +1,63 @@ +#ifndef socket_client_hh +#define socket_client_hh +/* socket_client.hh - Classe des socket client */ +/* 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 "utils/non_copyable.hh" + +#include +#include + +class Address; +class SocketServer; + +class SocketClient :public NonCopyable +{ + private: + /// Ze socket + int socket_; + /// constantes + /// Taille max du buffer de lecture/ecriture + static const int BUFFER_SIZE = 256; + public: + /// Constructeur + SocketClient (void); + /// Constructeur accept + SocketClient (SocketServer & socketServer); + /// Destructeur + ~SocketClient (void); + /// Connect le client au serveur + void connect (const Address & address); + /// Connect le client au serveur en se chargeant de créer Address + void connect (const std::string & address, int port); + /// lit du socket + bool read (std::string & strReaded, bool bloquant); + /// écrit dans le socket + void write (const std::string & str); + /// écrit dans le socket + void write (const uint8_t * buffer, unsigned size); +}; + +#endif //socket_client_hh diff --git a/i/chuck/src/socket/socket_databuffer.cc b/i/chuck/src/socket/socket_databuffer.cc new file mode 100644 index 0000000..1b4258c --- /dev/null +++ b/i/chuck/src/socket/socket_databuffer.cc @@ -0,0 +1,97 @@ +// socket_databuffer.cc +// marvin - programme du robot 2006. {{{ +// +// Copyright (C) 2006 Nicolas Haller +// +// 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. +// +// Contact : +// Web: http://perso.efrei.fr/~haller/ +// Email: +// }}} +#include "socket_databuffer.hh" + +#include +#include + +// Constructeur +SocketDataBuffer::SocketDataBuffer(void) + :SocketClient(), dbSize_(0) // TODO Ça sert à quelque chose où il le fait implicitement? +{ +} + +// Constructeur accept +SocketDataBuffer::SocketDataBuffer(SocketServer & socketServer) + :SocketClient(socketServer), dbSize_(0) +{ +} + +// Récupère un DataBuffer +// false en valeur de retour avec bloquant = true est un motif de licenciement +bool +SocketDataBuffer::read(DataBuffer & dbReaded, bool bloquant) +{ + std::string strReceive; + do + { + SocketClient::read(strReceive, bloquant); + strBuffer_.append(strReceive); + // On regarde si on a assez de donnée pour regarder si on a une en-tête + if(strBuffer_.size() > sizeof(unsigned)) + { + dbSize_ = 0; + dbSize_ = static_cast(strBuffer_[0]) << 24 + | static_cast(strBuffer_[1]) << 16 + | static_cast(strBuffer_[2]) << 8 + | static_cast(strBuffer_[3]); + // On regarde si on a suffisemment de données pour constituer le DB + if(dbSize_ <= strBuffer_.size() - 4) + { + dbBuffer_.write(reinterpret_cast(strBuffer_.c_str() + 4) + , dbSize_); + dbReaded.swap(dbBuffer_); + dbBuffer_.clear(); + strBuffer_.erase(0, dbSize_ + 4); + return true; + } + } + }while(bloquant); + return false; +} + +// Ecrit un DB dans le socket +void +SocketDataBuffer::write(DataBuffer & db) +{ + // On met une en-tête de 4 octects déterminant la taille de la DB + unsigned dbSize = db.size(); + char entete[4]; + entete[0] = static_cast (dbSize >> 24); + entete[1] = static_cast (dbSize >> 16); + entete[2] = static_cast (dbSize >> 8); + entete[3] = static_cast (dbSize); + + SocketClient::write(std::string(entete,4)); + // On récupère les données de la dataBuffer + uint8_t * donnees = new uint8_t[dbSize]; + db.read(donnees, dbSize); + if (db.size() != 0) + throw std::runtime_error("ERREUR DE CODAGE dans SocketDataBuffer" + "::Write"); + // On envoie le tout et on nettoie l'allocation + SocketClient::write(std::string(reinterpret_cast(donnees), + dbSize)); + delete [] donnees; +} diff --git a/i/chuck/src/socket/socket_databuffer.hh b/i/chuck/src/socket/socket_databuffer.hh new file mode 100644 index 0000000..c385db5 --- /dev/null +++ b/i/chuck/src/socket/socket_databuffer.hh @@ -0,0 +1,52 @@ +#ifndef socket_databuffer_hh +#define socket_databuffer_hh +// socket_databuffer.hh +// marvin - programme du robot 2006. {{{ +// +// Copyright (C) 2006 Nicolas Haller +// +// 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. +// +// Contact : +// Web: http://perso.efrei.fr/~haller/ +// Email: +// }}} + +#include "socket_client.hh" +#include "data/data_buffer.hh" + +/// Class s'occupant en particulier de récupérer les DataBuffer via le réseau +/// TCP/IP +class SocketDataBuffer : public SocketClient +{ + private: + /// Taille de la DB à recevoir + unsigned dbSize_; + /// Tampon provisoire + std::string strBuffer_; + /// DataBuffer Provisoire + DataBuffer dbBuffer_; + public: + /// Constructeur + SocketDataBuffer(void); + /// Constructeur accept + SocketDataBuffer(SocketServer & socketServer); + /// Récupère un DataBuffer + bool read (DataBuffer & dbReaded, bool bloquant); + /// Ecrit un DB dans le socket + void write (DataBuffer & db); +}; + +#endif // socket_databuffer_hh diff --git a/i/chuck/src/socket/socket_server.cc b/i/chuck/src/socket/socket_server.cc new file mode 100644 index 0000000..1a9cdb9 --- /dev/null +++ b/i/chuck/src/socket/socket_server.cc @@ -0,0 +1,90 @@ +/* socket_serveur.cc - Définition de la classe des sockets client */ +/* 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_server.hh" +#include "utils/errno_exception.hh" +#include "address.hh" + +#include +#include + +SocketServer::SocketServer (const Address & address) +{ + initSocket(address); +} + +SocketServer::SocketServer (const std::string & address, int port) +{ + if(address.empty()) + initSocket(Address(port)); + else + initSocket(Address(address, port)); +} + +SocketServer::~SocketServer (void) +{ + if (socket_ >= 0) + ::close (socket_); +} + + void +SocketServer::listen (int maxQueue) +{ + if (::listen (socket_, maxQueue) < 0 ) + throw errno_exception ("Socket: Impossible d'écouter la mer dans la chaussette", errno); +} + + int +SocketServer::accept (void) +{ + int socket = ::accept (socket_, 0, 0); + if (socket < 0) + throw errno_exception ("Socket: Chaussette bloqué à la douane", errno); + + return socket; +} + +int +SocketServer::getFD (void) const +{ + return socket_; +} + +void +SocketServer::initSocket(const Address & address) +{ + static const int reuse_s = 1; + // Crée le file descriptor du socket + socket_ = socket (PF_INET, SOCK_STREAM, 0); + if (socket_ < 0) + throw errno_exception ("Socket: La chaussette n'a pas pu être tricoté", errno); + // On règle la réusabilité du socket + if (setsockopt (socket_, SOL_SOCKET, SO_REUSEADDR, + &reuse_s, sizeof (int)) == -1) + throw errno_exception ("Socket: Erreur setsockopt : reusable", errno); + // On bind le socket + if (::bind (socket_, address.getSockaddr (), sizeof (*address.getSockaddr ())) < 0) + throw errno_exception ("Socket: Impossible d'assigner la chausette", errno); +} diff --git a/i/chuck/src/socket/socket_server.hh b/i/chuck/src/socket/socket_server.hh new file mode 100644 index 0000000..f2a022e --- /dev/null +++ b/i/chuck/src/socket/socket_server.hh @@ -0,0 +1,56 @@ +#ifndef socket_server_hh +#define socket_server_hh +/* socket_server.hh - Classe des sockets serveur */ +/* 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 "utils/non_copyable.hh" + +#include +class Address; + +class SocketServer : public NonCopyable +{ + private: + /// Ze socket + int socket_; + public: + /// Constructeur + SocketServer (const Address & address); + /// Constructeur s'occupant de Address + SocketServer (const std::string & address, int port); + /// Destructeur + ~SocketServer (void); + /// Ecoute le port + void listen (int maxQueue); + /// accepte une connection et renvoie le fd + int accept (void); + /// récupère le FD du socket + int getFD (void) const; + private: + /// Initialise le socket + void initSocket(const Address & address); +}; + +#endif //socket_server_hh diff --git a/i/chuck/src/socket/socket_text.cc b/i/chuck/src/socket/socket_text.cc new file mode 100644 index 0000000..da8a23b --- /dev/null +++ b/i/chuck/src/socket/socket_text.cc @@ -0,0 +1,236 @@ +// socket_text.cc +// robert - programme du robot 2005. {{{ +// +// Copyright (C) 2005 Dufour Jérémy +// +// 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. +// +// Contact : +// Web: %WEB% +// Email: +// }}} + +#include "socket_text.hh" + +#include "server_socket.hh" +#include "address.hh" + +#include +#include +#include +#include + +/// Taille maximum du header +static const unsigned max_header_size = 10; + +/// Constructeur du serveur +SocketText::SocketText (ServerSocket &ss) +: recvBufPos_ (0), numMessageBuffer_ (0) +{ + headBuf_.resize (max_header_size); + socket_ = ss.accept (); +} + +/// Constructeur du serveur avec addresse +SocketText::SocketText (ServerSocket &ss, Address &a) +: recvBufPos_ (0), numMessageBuffer_ (0) +{ + headBuf_.resize (max_header_size); + socket_ = ss.accept (a); +} + +/// Constructeur du client +SocketText::SocketText (const Address &a) +: recvBufPos_ (0), numMessageBuffer_ (0) +{ + headBuf_.resize (max_header_size); + // Création du socket + socket_ = socket (AF_INET, SOCK_STREAM, 0); + if (socket_ == -1) + throw std::runtime_error ("Erreur de création de socket"); + // Connexion au serveur + if (connect (socket_, a.getSockaddr (), sizeof (sockaddr_in)) == -1) + throw std::runtime_error ("Erreur de connexion au serveur"); +} + +/// Envoie d'un buffer de données. +SocketText & +SocketText::operator<< (const DataBuffer &d) +{ + // Copie du buffer + DataBuffer db (d); + // Récupération de la taille du message + unsigned size = db.size (); + // Données du buffer + std::vector data (size); + // On y place les données dedans + db.read ((uint8_t*) &data[0], size); + // Encode les données et le places dans le buffer d'envoie + encode (&data[0], size, db.type ()); + return *this; +} + +/// Reception d'un buffer de données. +SocketText & +SocketText::operator>> (DataBuffer &d) +{ + if (numMessageBuffer_ > 0) + { + // Récupération des informations du message qu'on veut retirer + unsigned int size = messList_.begin ()->size; + d.setType (messList_.begin ()->type); + // Récupération du message depuis le buffer + std::vector v (size); + std::memcpy (&v[0], &recvBuf_[0], size); + // Ecriture des données dans le buffer + d.write ((uint8_t*) &v[0], v.size ()); + // Suppression du message du buffer + recvBuf_.erase (recvBuf_.begin (), recvBuf_.begin () + size); + messList_.erase (messList_.begin ()); + bufferOldSize_ -= size; + bufferCurSize_ -= size; + recvBufPos_ -= size; + numMessageBuffer_--; + } + return *this; +} + +/// Envoie des données du buffer. +int +SocketText::send (void) +{ + int status; + status = ::send (socket_, &sendBuf_[0], sendBuf_.size (), MSG_NOSIGNAL); + if (status == -1 && errno != EWOULDBLOCK) + { + throw std::runtime_error ("Erreur de send de socket"); + } + if (status >= 0) + { + if (status != 0) + sendBuf_.erase (sendBuf_.begin (), sendBuf_.begin () + status); + return status; + } + return -1; +} + +/// Encodage du message (rajout du header) +void +SocketText::encode (const char *data, const int size, + const DataBuffer::dataType_e type) +{ + // Création du header (1H4242H) + std::ostringstream o; + // Rajout du type + if (!(o << type)) + throw std::runtime_error ("Erreur de convertion int -> string"); + o << "H"; + if (!(o << size )) + throw std::runtime_error ("Erreur de convertion int -> string"); + o << "H"; + // Recopie du header dans le buffer + std::string s = o.str (); + std::string::const_iterator sEnd = s.end (); + unsigned int compt = 0; + std::string::const_iterator i = s.begin (); + // Ecriture d'un header de taille fixe + while (compt < max_header_size) + { + if (i != sEnd) + { + sendBuf_.push_back (*i); + i++; + } + else + sendBuf_.push_back ('H'); + compt++; + } + // Rajout des données dans le buffer + for (int i = 0; i < size; i++) + sendBuf_.push_back (data[i]); +} + +/// Décodage du header. +int +SocketText::decode (void) +{ + mess_t header; + header.type = (DataBuffer::dataType_e) std::atoi (&headBuf_[0]); + header.size = std::atoi (&headBuf_[2]); + messList_.push_back (header); + return header.size; +} + +/// Reception de données dans un vecteur. +bool +SocketText::recv () +{ + // FIXME C'est KK + int status; + static unsigned int headerPos = 0; + + // On n'a pas reçu encore de quoi faire un header + if (headerPos < max_header_size) + { + status = ::recv (socket_, &headBuf_[headerPos], + max_header_size - headerPos, MSG_NOSIGNAL); + if (status == -1 && errno != EWOULDBLOCK) + throw std::runtime_error ("Erreur de send de socket"); + if (status > 0) + headerPos += status; + } + // Décodage du header + if (headerPos == max_header_size) + { + bufferOldSize_ = recvBuf_.size (); + bufferCurSize_ = bufferOldSize_ + decode (); + recvBuf_.resize (bufferCurSize_); + headerPos ++; + } + // Récupération du message lui-même + if (headerPos > max_header_size) + { + status = ::recv (socket_, &recvBuf_[recvBufPos_], + bufferCurSize_ - recvBufPos_, MSG_NOSIGNAL); + if (status == -1 && errno != EWOULDBLOCK) + throw std::runtime_error ("Erreur de send de socket"); + if (status > 0) + recvBufPos_ += status; + if (recvBufPos_ == bufferCurSize_) + { + headerPos = 0; + numMessageBuffer_++; + } + } + return numMessageBuffer_ > 0; +} + +/// Changement du bloquant non/bloquant. +void +SocketText::nonblock (bool flag) +{ + // Récupération de l'ancien flag. + int old = fcntl (socket_, F_GETFL, 0); + if (old == -1) + throw std::runtime_error ("Erreur de fcntl"); + // Change le flag. + if (flag) + old |= O_NONBLOCK; + else + old &= ~O_NONBLOCK; + // Ecrit le nouveau flag. + if (fcntl (socket_, F_SETFL, old) == -1) + throw std::runtime_error ("Erreur de fcntl"); +} diff --git a/i/chuck/src/socket/socket_text.hh b/i/chuck/src/socket/socket_text.hh new file mode 100644 index 0000000..b734753 --- /dev/null +++ b/i/chuck/src/socket/socket_text.hh @@ -0,0 +1,80 @@ +#ifndef socket_text_hh +#define socket_text_hh +// socket_text.hh +// robert - programme du robot 2005. {{{ +// +// Copyright (C) 2005 Dufour Jérémy +// +// 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. +// +// Contact : +// Web: %WEB% +// Email: +// }}} + +class ServerSocket; +class Address; +#include "data/data_buffer.hh" + +#include + +class SocketText { + /// Structure pour stocker les informations d'un message. + typedef struct { + unsigned int size; + DataBuffer::dataType_e type; + } mess_t; + std::vector messList_; + /// L'identificateur du socket. + int socket_; + /// Buffer pour le header. + std::vector headBuf_; + /// Buffer de reception de données. + std::vector recvBuf_; + /// Buffer d'émission de données. + std::vector sendBuf_; + /// Taille des buffers d'E/S XXX + unsigned int recvBufPos_; + unsigned int bufferOldSize_; + unsigned int bufferCurSize_; + /// Nombre de message dans le buffer + int numMessageBuffer_; + DataBuffer::dataType_e type_; + /// Encodage du message (rajout du header) + void encode (const char *data, const int size, + const DataBuffer::dataType_e type); + /// Décodage du header. + int decode (void); + + public: + /// Constructeur du serveur. + SocketText (ServerSocket &ss); + /// Constructeur du serveur avec addresse. + SocketText (ServerSocket &ss, Address &a); + /// Constructeur du client. + SocketText (const Address &a); + /// Envoie d'un buffer de données. + SocketText &operator<< (const DataBuffer &d); + /// Reception d'un buffer de données. + SocketText &operator>> (DataBuffer &d); + /// Envoie des données du buffer. + int send (void); + /// Reception de données dans un vecteur. + bool recv (void); + /// Changement du bloquant non/bloquant. + void nonblock (bool flag = true); +}; + +#endif // socket_text_hh diff --git a/i/chuck/src/socket/test_server.cc b/i/chuck/src/socket/test_server.cc new file mode 100644 index 0000000..81399ec --- /dev/null +++ b/i/chuck/src/socket/test_server.cc @@ -0,0 +1,91 @@ +// test_socket.cc +// robert - programme du robot 2005. {{{ +// +// Copyright (C) 2005 Dufour Jérémy +// +// 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. +// +// Contact : +// Web: %WEB% +// Email: +// }}} +#include "server_socket.hh" +#include "address.hh" +#include "socket_text.hh" +#include "data/data_input_file.hh" +#include "data/data_buffer.hh" +#include "image/raw_reader.hh" +#include "image/image.hh" + +#include +#include +#include + +int +main (int argc, char **argv) +{ + if (argc == 3) + { + try + { + // Serveur + ServerSocket ss (atoi (argv[1])); + Address a; + SocketText st (ss, a); + st.nonblock (); + // DataBuffer de reception + DataBuffer db; + uint8_t c = 'a'; + DataBuffer dbAskImg (&c, 1, 1, DataBuffer::AskImage); + st << dbAskImg; + st.send (); + // Reception des données + while (!st.recv ()); + st >> db; + if (db.type () != DataBuffer::Image) + throw std::runtime_error ("Echec de la transmission"); + RawReader reader (db, 360, 296, Image::rgb); + Image img (360, 296, Image::rgb); + img.read (reader); + std::ofstream file (argv[2]); + file.write ((char*) img.getBuf (), img.getBufSize ()); + +// // Récupération des données du DataBuffer +// std::vector v (db.size ()); +// db.read ((uint8_t*) &v[0], db.size ()); +// // Ecriture dans le fichier +// std::ofstream file (argv[2]); +// file.write (&v[0], v.size ()); +// DataBuffer db1; +// // Reception des données +// while (!st.recv ()); +// st >> db1; +// std::cout << ">> Reçu " << db1.size () << " bits." << std::endl; +// if (db1.type () != DataBuffer::Image) +// throw std::runtime_error ("Echec de la transmission"); + } + catch (const std::runtime_error &r) + { + std::cerr << argv[0] << ": " << r.what () << std::endl; + return 1; + } + } + else + { + std::cerr << "Syntaxe: " << argv[0] << " port out_file" << std::endl; + return 1; + } + return 0; +} diff --git a/i/chuck/src/socket/test_socket.cc b/i/chuck/src/socket/test_socket.cc new file mode 100644 index 0000000..81b39a0 --- /dev/null +++ b/i/chuck/src/socket/test_socket.cc @@ -0,0 +1,156 @@ +/* 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_server.hh" +#include "socket_client.hh" +#include "utils/errno_exception.hh" + +#include +#include +#include + +//Chaine et char de test +const std::string strDepart = "J'aime la prog système."; +std::string strDest; +const char a = 'f'; +char b; + +int testSockServ(SocketServer & sockServ); +int testSockClient(int pauseMode); + +int main(void) +{ + pid_t pid; + int resultServer = 0, resultClient; + // Pause mode pour débugger le fork + int pauseMode = 0; + + // Création de la socket serveur; + SocketServer sockServ("",4242); + // Mise en écoute du socket serveur + sockServ.listen(12); + + // on fork pour traiter le client et le serveur + pid = fork(); + if(pid == 0) //Processus fils + testSockClient(pauseMode); + else //Processus parent + resultServer = testSockServ(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 testSockServ(SocketServer & sockServ) +{ + try + { + // Acceptation d'une connection + SocketClient sockFork(sockServ); + // Reception d'un message + sockFork.read(strDest, true); + 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 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 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 1; + } + return 0; +} + +int testSockClient(int pauseMode) +{ + // Chance pour debbuger le fork + while(pauseMode) + sleep(10); + try + { + // Création de la socket client + SocketClient sockClient; + // Demande de connection + sockClient.connect(std::string("127.0.0.1"), 4242); + // Ecriture du client dans le serveur + sockClient.write(strDepart); + // Reception du message serveur + sockClient.read(strDest, true); + 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; + exit (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; + // exit (1); + //} + } + catch(errno_exception & chier) + { + std::cout << "CHIER !! Une exception a été lancé coté client!!" << std::endl; + std::cout << chier.what() << std::endl; + exit (1); + } + exit (0); +} diff --git a/i/chuck/src/socket/test_socket_databuffer.cc b/i/chuck/src/socket/test_socket_databuffer.cc new file mode 100644 index 0000000..fb220a8 --- /dev/null +++ b/i/chuck/src/socket/test_socket_databuffer.cc @@ -0,0 +1,125 @@ +/* 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_server.hh" +#include "socket_databuffer.hh" +#include "data/data_buffer.hh" +#include "utils/errno_exception.hh" + +#include +#include + +//Chaine et char de test +const std::string strDepart = "J'aime la prog système."; +const char a = 'f'; +DataBuffer db1, db2; + +int testSockServ(SocketServer & sockServ); +int testSockClient(int pauseMode); + +int main(void) +{ + pid_t pid; + int resultServer = 0, resultClient; + // Pause mode pour débugger le fork + int pauseMode = 0; + + // Création de la socket serveur; + SocketServer sockServ("",4242); + // Mise en écoute du socket serveur + sockServ.listen(12); + + // on fork pour traiter le client et le serveur + pid = fork(); + if(pid == 0) //Processus fils + testSockClient(pauseMode); + else //Processus parent + resultServer = testSockServ(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 testSockServ(SocketServer & sockServ) +{ + try + { + // On crée le DataBuffer + db1.write(reinterpret_cast(strDepart.c_str()), + strDepart.size()); + db1.write(reinterpret_cast(&a), sizeof(const char)); + // Acceptation d'une connection + SocketDataBuffer sockFork(sockServ); + // On écrit la DataBuffer dans la socket + sockFork.write(db1); + } + catch(errno_exception & chier) + { + std::cout << "CHIER !! Une exception a été lancé coté serveur!!" << std::endl; + std::cout << chier.what() << std::endl; + return 1; + } + return 0; +} + +int testSockClient(int pauseMode) +{ + // Chance pour debbuger le fork + while(pauseMode) + sleep(10); + try + { + // Création de la socket client + SocketDataBuffer sockDataBuffer; + // Demande de connection + sockDataBuffer.connect(std::string("127.0.0.1"), 4242); + // Reception du message serveur + sockDataBuffer.read(db2, true); + // Paye ta comparaison de DataBuffer + if(db1.size() != db2.size()) + { + std::cout << "CHIER!! Les DB ne font pas la même size" << std::endl; + exit (1); + } + } + catch(errno_exception & chier) + { + std::cout << "CHIER !! Une exception a été lancé coté client!!" << std::endl; + std::cout << chier.what() << std::endl; + exit (1); + } + exit (0); +} diff --git a/i/chuck/src/socket/test_socket_old.cc b/i/chuck/src/socket/test_socket_old.cc new file mode 100644 index 0000000..f2beb51 --- /dev/null +++ b/i/chuck/src/socket/test_socket_old.cc @@ -0,0 +1,76 @@ +// test_socket.cc +// robert - programme du robot 2005. {{{ +// +// Copyright (C) 2005 Dufour Jérémy +// +// 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. +// +// Contact : +// Web: %WEB% +// Email: +// }}} + +#include "server_socket.hh" +#include "address.hh" +#include "socket_text.hh" +#include "data/data_input_file.hh" +#include "data/data_buffer.hh" + +#include +#include +#include +#include +#include + +int +main (int argc, char **argv) +{ + if (argc == 4) + { + try + { + // Récupération des données du fichier + DataInputFile df (argv[3]); + std::vector img (319680); + df.read (&img[0], 319680); + // Ecriture des données du fichier dans le buffer + DataBuffer db (&img[0], img.size (), img.size (), + DataBuffer::Image); + DataBuffer dbAnswer; + // Création du socket + char *h = argv[1]; + SocketText st (Address (h, atoi (argv[2]))); + st.nonblock (); + st >> dbAnswer; + if (dbAnswer.type () == DataBuffer::AskImage) + { + // Envoie des données + st << db; + while (st.send () != 0); + } + } + catch (const std::runtime_error &r) + { + std::cerr << argv[0] << ": " << r.what () << std::endl; + return 1; + } + } + else + { + std::cerr << "Syntaxe: " << argv[0] << " host port in_file" << + std::endl; + return 1; + } +} -- cgit v1.2.3