summaryrefslogtreecommitdiff
path: root/i/simulotron/src/gs
diff options
context:
space:
mode:
authorhaller2006-01-21 21:10:23 +0000
committerhaller2006-01-21 21:10:23 +0000
commita8cd31b9681938b1cea0d02e884962fd8f114208 (patch)
treeeeccb77227e02d919f75d9a97ea3c7557d2be2fc /i/simulotron/src/gs
parent270ede560382bbce481b18895276d9802ae931ee (diff)
- ajout de quelques structures de données
- importation de la class nonCopyable et utilisation dans quelques classes - création d'une class exception simulotron_exception - ajout d'un paramètre "bloquant" pour recevoir une GS - modification de la valeur de retour des fonction servant à récupérer une GS - Mise en place de la poignée de main lors de la connection d'un module au hub - Ajout d'un message template pour les message vide - suppression des fonction (get|put)Char et getFD de la classe socket - modification de la fonction GSMessage::getString à deux arguments - ajout d'un test_hub - ajout de la class ComH (un peu comme ComC mais coté hub)
Diffstat (limited to 'i/simulotron/src/gs')
-rw-r--r--i/simulotron/src/gs/gs_message.cc11
-rw-r--r--i/simulotron/src/gs/gs_message.hh2
-rw-r--r--i/simulotron/src/gs/gs_transmitter.cc72
-rw-r--r--i/simulotron/src/gs/gs_transmitter.hh6
-rw-r--r--i/simulotron/src/gs/test_gs.cc14
5 files changed, 66 insertions, 39 deletions
diff --git a/i/simulotron/src/gs/gs_message.cc b/i/simulotron/src/gs/gs_message.cc
index 0ab8bb8..df37ca0 100644
--- a/i/simulotron/src/gs/gs_message.cc
+++ b/i/simulotron/src/gs/gs_message.cc
@@ -25,6 +25,7 @@
#include "gs/gs_message.hh"
#include "gs/gs_transmitter.hh"
+#include "utils/simulotron_exception.hh"
#include <stdexcept>
@@ -33,9 +34,9 @@ GSMessage::GSMessage(void)
{
}
-GSMessage::GSMessage(GSTransmitter & gst)
+GSMessage::GSMessage(GSTransmitter & gst, bool bloquant)
{
- gst.getGS(*this);
+ gst.getGS(*this, bloquant);
}
const std::string &
@@ -50,7 +51,7 @@ GSMessage::readGS(void * data, size_t size)
std::string str;
str.assign(igs_, gs_.end());
if (size > str.size())
- throw std::runtime_error("CHIER!!! La size dans readGS est trop grande!!");
+ throw simulotron_exception("GSMessage:", "CHIER!!! La size dans readGS est trop grande!!");
memcpy(data, str.data(), size);
igs_ += size;
}
@@ -66,7 +67,11 @@ GSMessage::getString(std::string & str)
void
GSMessage::getString(std::string & str, size_t size)
{
+ unsigned int pos0;
str.assign(igs_, igs_ + size);
+ pos0 = str.find('\0');
+ if (pos0 != std::string::npos)
+ str.erase(pos0);
igs_ += size;
}
diff --git a/i/simulotron/src/gs/gs_message.hh b/i/simulotron/src/gs/gs_message.hh
index cfb0b41..b970571 100644
--- a/i/simulotron/src/gs/gs_message.hh
+++ b/i/simulotron/src/gs/gs_message.hh
@@ -39,7 +39,7 @@ class GSMessage
/// Constructeur sans argument
GSMessage (void);
/// Constructeur/récupérateur
- GSMessage (GSTransmitter & gst);
+ GSMessage (GSTransmitter & gst, bool bloquant);
/// Récupération de la string en entier
const std::string & getGS (void) const;
/// Récupération de données de la gs
diff --git a/i/simulotron/src/gs/gs_transmitter.cc b/i/simulotron/src/gs/gs_transmitter.cc
index 9f53963..abdfd0f 100644
--- a/i/simulotron/src/gs/gs_transmitter.cc
+++ b/i/simulotron/src/gs/gs_transmitter.cc
@@ -40,40 +40,60 @@ GSTransmitter::GSTransmitter(const std::string & address, int port)
socket_.connect(address, port);
}
-/// Renvoie 0 quand un message est récupéré, -1 sinon
-int
-GSTransmitter::getGS(GSMessage & gsm)
+/// Renvoie true quand un message est récupéré, false sinon
+bool
+GSTransmitter::getGS(GSMessage & gsm, bool bloquant)
{
///\bug Pas portable le code, suppose des int à 4 octect
- fd_set fds;
- timeval tv = {0, 0};
unsigned int size = 0;
+ bool GSCompleted;
- FD_ZERO(&fds);
- FD_SET(socket_.getFD(), &fds);
- //On lit le socket que si y'a quelque chose à lire
- ///\bug C'est top ca?
- if(select(socket_.getFD() + 1, &fds, NULL, NULL, &tv) > 0)
+ do
{
- std::string strRead (socket_.read ());
+ GSCompleted = true;
+ std::string strRead;
+ socket_.read(strRead, bloquant);
strBrut_ += strRead;
- }
- // Si la chaine à moins de 4 octect, y'a pas de message
- if (strBrut_.size() < 4)
- return -1;
- // On récupère la taille de la gs grace au 4 premier octect
- size = static_cast<int> (strBrut_[0]) << 24
- | static_cast<int> (strBrut_[1]) << 16
- | static_cast<int> (strBrut_[2]) << 8
- | static_cast<int> (strBrut_[3]);
- if(size > strBrut_.size() - 4)
- return -1;
- gsm.appendGS (strBrut_.substr(4, size).data(), size);
- strBrut_.erase (0, size + 4);
- return 0;
+ // Si la chaine à moins de 4 octect, y'a pas de message
+ if (strBrut_.size() < 4)
+ {
+ if(!bloquant)
+ return false;
+ else
+ GSCompleted = false;
+ }
+ }while (!GSCompleted);
+ do
+ {
+ GSCompleted = true;
+
+ // On récupère la taille de la gs grace au 4 premier octect
+ size = static_cast<int> (strBrut_[0]) << 24
+ | static_cast<int> (strBrut_[1]) << 16
+ | static_cast<int> (strBrut_[2]) << 8
+ | static_cast<int> (strBrut_[3]);
+ if(size > strBrut_.size() - 4)
+ {
+ if(!bloquant)
+ return false;
+ else
+ GSCompleted = false;
+ }
+ else
+ {
+ gsm.clear();
+ gsm.appendGS (strBrut_.substr(4, size).data(), size);
+ strBrut_.erase (0, size + 4);
+ return true;
+ }
+ std::string strRead;
+ socket_.read(strRead, bloquant);
+ strBrut_ += strRead;
+ }while (!GSCompleted);
+ return true;
}
-void
+ void
GSTransmitter::putGS(const GSMessage & gsm)
{
///\bug Pas portable le code, suppose des int à 4 octect
diff --git a/i/simulotron/src/gs/gs_transmitter.hh b/i/simulotron/src/gs/gs_transmitter.hh
index 86c0620..03b01e9 100644
--- a/i/simulotron/src/gs/gs_transmitter.hh
+++ b/i/simulotron/src/gs/gs_transmitter.hh
@@ -26,12 +26,14 @@
* }}} */
#include "socket/socket_client.hh"
+#include "utils/non_copyable.hh"
+
#include <string>
class SocketServer;
class GSMessage;
-class GSTransmitter
+class GSTransmitter : public NonCopyable
{
private:
/// Socket de transmission
@@ -44,7 +46,7 @@ class GSTransmitter
/// Constructeur prenant une adresse (cas d'un client)
GSTransmitter(const std::string & address, int port);
/// Récupère une grosse string
- int getGS(GSMessage & gsm);
+ bool getGS(GSMessage & gsm, bool bloquant);
/// envoie une grosse string
void putGS(const GSMessage & gsm);
};
diff --git a/i/simulotron/src/gs/test_gs.cc b/i/simulotron/src/gs/test_gs.cc
index acf47f9..c9e9ea5 100644
--- a/i/simulotron/src/gs/test_gs.cc
+++ b/i/simulotron/src/gs/test_gs.cc
@@ -89,7 +89,7 @@ int testGSServer(SocketServer & sockServ)
// Reception des messages
// une string
GSMessage gsm;
- while (gst.getGS(gsm) != 0);
+ while (!gst.getGS(gsm,true));
gsm.getString(strDest);
if (strDepart != strDest)
{
@@ -101,7 +101,7 @@ int testGSServer(SocketServer & sockServ)
}
gsm.clear();
// Un char
- while (gst.getGS(gsm) != 0);
+ while (!gst.getGS(gsm, true));
gsm.readGS(&b, sizeof(char));
if ( a != b)
{
@@ -113,7 +113,7 @@ int testGSServer(SocketServer & sockServ)
}
gsm.clear();
// Un short
- while (gst.getGS(gsm) != 0);
+ while (!gst.getGS(gsm, true));
gsm.readGS(&s2, sizeof(short));
if ( s1 != s2)
{
@@ -125,7 +125,7 @@ int testGSServer(SocketServer & sockServ)
}
gsm.clear();
// Un int positif
- while (gst.getGS(gsm) != 0);
+ while (!gst.getGS(gsm, true));
gsm.readGS(&i2, sizeof(int));
if ( i1 != i2)
{
@@ -137,7 +137,7 @@ int testGSServer(SocketServer & sockServ)
}
gsm.clear();
// Un int negatif
- while (gst.getGS(gsm) != 0);
+ while (!gst.getGS(gsm, true));
gsm.readGS(&i4, sizeof(int));
if ( i3 != i4)
{
@@ -149,7 +149,7 @@ int testGSServer(SocketServer & sockServ)
}
gsm.clear();
// Un Long
- while (gst.getGS(gsm) != 0);
+ while (!gst.getGS(gsm, true));
gsm.readGS(&l2, sizeof(long));
if ( l1 != l2)
{
@@ -161,7 +161,7 @@ int testGSServer(SocketServer & sockServ)
}
gsm.clear();
// Un gros message
- while (gst.getGS(gsm) != 0);
+ while (!gst.getGS(gsm, true));
gsm.readGS(&s2,sizeof(short));
gsm.readGS(&l2,sizeof(long));
gsm.getString(strDest);