summaryrefslogtreecommitdiff
path: root/i/simulotron
diff options
context:
space:
mode:
Diffstat (limited to 'i/simulotron')
-rw-r--r--i/simulotron/src/Makefile.am4
-rw-r--r--i/simulotron/src/socket/address.cc93
-rw-r--r--i/simulotron/src/socket/address.hh76
-rw-r--r--i/simulotron/src/socket/socket.cc21
-rw-r--r--i/simulotron/src/socket/socket.hh10
-rw-r--r--i/simulotron/src/utils/errno_exception.hh48
-rw-r--r--i/simulotron/test/socket/Makefile.am4
7 files changed, 244 insertions, 12 deletions
diff --git a/i/simulotron/src/Makefile.am b/i/simulotron/src/Makefile.am
index 2970e77..7302a68 100644
--- a/i/simulotron/src/Makefile.am
+++ b/i/simulotron/src/Makefile.am
@@ -1,6 +1,8 @@
bin_PROGRAMS = simulotron
simulotron_SOURCES = main.cc \
- socket/socket.cc socket/socket.hh
+ socket/socket.cc socket/socket.hh \
+ socket/address.cc socket/address.hh \
+ utils/errno_exception.hh
simulotron_CXXFLAGS = -g -Wall -fmessage-length=0
simulotron_LDADD =
diff --git a/i/simulotron/src/socket/address.cc b/i/simulotron/src/socket/address.cc
new file mode 100644
index 0000000..4567e2e
--- /dev/null
+++ b/i/simulotron/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/simulotron/src/socket/address.hh b/i/simulotron/src/socket/address.hh
new file mode 100644
index 0000000..53fca07
--- /dev/null
+++ b/i/simulotron/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/simulotron/src/socket/socket.cc b/i/simulotron/src/socket/socket.cc
index 2f7139c..f231cf5 100644
--- a/i/simulotron/src/socket/socket.cc
+++ b/i/simulotron/src/socket/socket.cc
@@ -1,6 +1,10 @@
#include <sys/socket.h>
+#include <cerrno>
+#include "socket/socket.hh"
+#include "utils/errno_exception.hh"
+#include "socket/address.hh"
-Socket::Socket(const std::string & address, int port, bool block)
+Socket::Socket(const Address & address)
{
// Crée le file descriptor du socket
socket_ = socket (PF_INET, SOCK_STREAM, 0);
@@ -8,19 +12,24 @@ Socket::Socket(const std::string & address, int port, bool block)
throw errno_exception ("La chaussette n'a pas pu être tricoté", errno);
// On bind le socket
- if(bind (socket_, address.getSockaddr(), sizeof(*address.getSockaddr())) < 0)
+ if(::bind (socket_, address.getSockaddr(), sizeof(*address.getSockaddr())) < 0)
throw errno_exception("Impossible d'assigner la chausette", errno);
}
+Socket::Socket(int socket)
+{
+ socket_ = socket;
+}
+
void Socket::connect(const Address & address)
{
- if (connect (socket_, address.getSockaddr(), sizeof(*address.getSockaddr()) < 0))
+ if (::connect (socket_, address.getSockaddr(), sizeof(*address.getSockaddr()) < 0))
throw errno_exception("Impossible de mettre la chaussette", errno);
}
void Socket::listen(int maxQueue)
{
- if( listen(socket_, maxQueue) < 0 )
+ if(::listen(socket_, maxQueue) < 0 )
throw errno_exception("Impossible d'écouter la mer dans la chaussette", errno);
}
@@ -29,11 +38,11 @@ Socket Socket::accept(Address & address)
sockaddr addr;
socklen_t length;
- int socket = accept(socket_, &addr, &length);
+ int socket = ::accept(socket_, &addr, &length);
if (socket < 0)
throw errno_exception("Chaussette bloqué à la douane", errno);
- address = Address(addr, length);
+ address = Address(&addr, length);
return Socket(socket);
}
diff --git a/i/simulotron/src/socket/socket.hh b/i/simulotron/src/socket/socket.hh
index c1b47df..27f3f5d 100644
--- a/i/simulotron/src/socket/socket.hh
+++ b/i/simulotron/src/socket/socket.hh
@@ -1,3 +1,5 @@
+class Address;
+
class Socket
{
private:
@@ -5,15 +7,15 @@ class Socket
int socket_;
public:
/// Constructeur
- Socket(const Address & adress, bool block = false);
+ Socket(const Address & address);
/// Constructeur à partir d'un fd
Socket(int socket);
/// Connect le client au serveur
- void connect(void);
+ void connect(const Address & address);
/// Ecoute le port
- void listen(void);
+ void listen(int maxQueue);
/// accepte une connection
- void accept(void);
+ Socket accept(Address & adress);
/// lit du socket
void read(void);
/// écrit dans le socket
diff --git a/i/simulotron/src/utils/errno_exception.hh b/i/simulotron/src/utils/errno_exception.hh
new file mode 100644
index 0000000..58a636e
--- /dev/null
+++ b/i/simulotron/src/utils/errno_exception.hh
@@ -0,0 +1,48 @@
+#ifndef errno_exception_hh
+#define errno_exception_hh
+// errno_exception.hh
+// robert - programme du robot 2005. {{{
+//
+// Copyright (C) 2005 Nicolas Schodet
+//
+// 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 <string>
+#include <exception>
+#include <cstring>
+#include <errno.h>
+
+/// Exception lancé lorsqu'une erreur est retournée par la libc.
+class errno_exception : public std::exception
+{
+ int errno_;
+ std::string what_;
+ public:
+ errno_exception (const std::string &desc, int errno__)
+ : errno_ (errno__), what_ (desc + ": " + strerror (errno__)) { }
+ errno_exception (int errno__)
+ : errno_ (errno__), what_ (strerror (errno__)) { }
+ ~errno_exception (void) throw () { }
+ virtual const char* what () const throw () { return what_.c_str (); }
+ int getErrno (void) const { return errno_; }
+};
+
+#endif // errno_exception_hh
diff --git a/i/simulotron/test/socket/Makefile.am b/i/simulotron/test/socket/Makefile.am
index 344d20c..f1c3892 100644
--- a/i/simulotron/test/socket/Makefile.am
+++ b/i/simulotron/test/socket/Makefile.am
@@ -1,5 +1,7 @@
check_PROGRAMS = testsocket
testsocket_SOURCES = testsocket.cc \
- ../../src/date/diffdate.cc ../../src/date/diffdate.hh
+ ../../src/socket/socket.cc ../../src/socket/socket.hh \
+ ../../src/socket/address.cc ../../src/socket/address.hh \
+ ../../src/utils/errno_exception.hh
AM_CXXFLAGS=-g -W -Wall -fmessage-length=0
INCLUDES= -I../../src