summaryrefslogtreecommitdiff
path: root/i/marvin/src/socket
diff options
context:
space:
mode:
Diffstat (limited to 'i/marvin/src/socket')
-rw-r--r--i/marvin/src/socket/Makefile.defs9
-rw-r--r--i/marvin/src/socket/address.cc93
-rw-r--r--i/marvin/src/socket/address.hh76
-rw-r--r--i/marvin/src/socket/server_socket.cc86
-rw-r--r--i/marvin/src/socket/server_socket.hh47
-rw-r--r--i/marvin/src/socket/socket_text.cc236
-rw-r--r--i/marvin/src/socket/socket_text.hh80
-rw-r--r--i/marvin/src/socket/test_server.cc91
-rw-r--r--i/marvin/src/socket/test_socket.cc76
9 files changed, 794 insertions, 0 deletions
diff --git a/i/marvin/src/socket/Makefile.defs b/i/marvin/src/socket/Makefile.defs
new file mode 100644
index 0000000..c125e61
--- /dev/null
+++ b/i/marvin/src/socket/Makefile.defs
@@ -0,0 +1,9 @@
+PROGRAMS += test_socket test_server
+
+net_OBJECTS = address.o server_socket.o socket_text.o
+
+test_socket_OBJECTS = test_socket.o $(net_OBJECTS) $(data_OBJECTS)
+test_server_OBJECTS = test_server.o $(net_OBJECTS) $(data_OBJECTS) $(image_OBJECTS)
+
+test_socket: $(test_socket_OBJECTS)
+test_server: $(test_server_OBJECTS)
diff --git a/i/marvin/src/socket/address.cc b/i/marvin/src/socket/address.cc
new file mode 100644
index 0000000..4567e2e
--- /dev/null
+++ b/i/marvin/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: <dufourj@efrei.fr>
+// }}}
+#include "address.hh"
+
+#include <netdb.h>
+#include <stdexcept>
+
+/// 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<const sockaddr_in*> (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<char *> (&(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/marvin/src/socket/address.hh b/i/marvin/src/socket/address.hh
new file mode 100644
index 0000000..53fca07
--- /dev/null
+++ b/i/marvin/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: <dufourj@efrei.fr>
+// }}}
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+
+#include <string>
+#include <ostream>
+
+/// 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<const sockaddr *> (&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/marvin/src/socket/server_socket.cc b/i/marvin/src/socket/server_socket.cc
new file mode 100644
index 0000000..539b94e
--- /dev/null
+++ b/i/marvin/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: <dufourj@efrei.fr>
+// }}}
+#include "server_socket.hh"
+
+#include "address.hh"
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <stdexcept>
+
+/// 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<sockaddr *> (&sa),
+ &sl);
+ if (s == -1)
+ {
+ throw std::runtime_error ("Erreur d'accept nouvelle connexion");
+ }
+ // Récupération de l'adresse
+ a = Address (reinterpret_cast<const sockaddr *> (&sa), sl);
+ return s;
+}
diff --git a/i/marvin/src/socket/server_socket.hh b/i/marvin/src/socket/server_socket.hh
new file mode 100644
index 0000000..995ad01
--- /dev/null
+++ b/i/marvin/src/socket/server_socket.hh
@@ -0,0 +1,47 @@
+#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: <dufourj@efrei.fr>
+// }}}
+
+#include <unistd.h>
+class Address;
+
+/// Créer un server TCP pour le transfert des données.
+class ServerSocket {
+ private:
+ /// L'identificateur du socket.
+ int socket_;
+ /// Bind le serveur sur un port d'écoute.
+ void bind (int port);
+ 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;
+};
+#endif // server_socket_hh
diff --git a/i/marvin/src/socket/socket_text.cc b/i/marvin/src/socket/socket_text.cc
new file mode 100644
index 0000000..da8a23b
--- /dev/null
+++ b/i/marvin/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: <dufourj@efrei.fr>
+// }}}
+
+#include "socket_text.hh"
+
+#include "server_socket.hh"
+#include "address.hh"
+
+#include <sstream>
+#include <stdexcept>
+#include <iterator>
+#include <fcntl.h>
+
+/// 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<char> 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 <char> 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/marvin/src/socket/socket_text.hh b/i/marvin/src/socket/socket_text.hh
new file mode 100644
index 0000000..b734753
--- /dev/null
+++ b/i/marvin/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: <dufourj@efrei.fr>
+// }}}
+
+class ServerSocket;
+class Address;
+#include "data/data_buffer.hh"
+
+#include <vector>
+
+class SocketText {
+ /// Structure pour stocker les informations d'un message.
+ typedef struct {
+ unsigned int size;
+ DataBuffer::dataType_e type;
+ } mess_t;
+ std::vector <mess_t> messList_;
+ /// L'identificateur du socket.
+ int socket_;
+ /// Buffer pour le header.
+ std::vector<char> headBuf_;
+ /// Buffer de reception de données.
+ std::vector<char> recvBuf_;
+ /// Buffer d'émission de données.
+ std::vector<char> 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/marvin/src/socket/test_server.cc b/i/marvin/src/socket/test_server.cc
new file mode 100644
index 0000000..81399ec
--- /dev/null
+++ b/i/marvin/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: <dufourj@efrei.fr>
+// }}}
+#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 <iostream>
+#include <fstream>
+#include <stdexcept>
+
+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 <char> 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/marvin/src/socket/test_socket.cc b/i/marvin/src/socket/test_socket.cc
new file mode 100644
index 0000000..f2beb51
--- /dev/null
+++ b/i/marvin/src/socket/test_socket.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: <dufourj@efrei.fr>
+// }}}
+
+#include "server_socket.hh"
+#include "address.hh"
+#include "socket_text.hh"
+#include "data/data_input_file.hh"
+#include "data/data_buffer.hh"
+
+#include <iostream>
+#include <cstdlib>
+#include <stdexcept>
+#include <string>
+#include <vector>
+
+int
+main (int argc, char **argv)
+{
+ if (argc == 4)
+ {
+ try
+ {
+ // Récupération des données du fichier
+ DataInputFile df (argv[3]);
+ std::vector <unsigned char> 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;
+ }
+}