summaryrefslogtreecommitdiff
path: root/i/simulotron
diff options
context:
space:
mode:
authorhaller2005-12-07 16:10:55 +0000
committerhaller2005-12-07 16:10:55 +0000
commit6dd92c230db1e1c27b02f54a86fb710c1d7269fc (patch)
treef5da92c96ae4ddbb7343ed3e2083a4dd6b16792d /i/simulotron
parent3dc1f65dbcfa73a0b82887d5a6dd800334bfa327 (diff)
* Ajout d'une fonction membre clear dans GSMessage
* Debugage des GS (passe le test) * Améliorations sur les GS comming soon...
Diffstat (limited to 'i/simulotron')
-rw-r--r--i/simulotron/src/gs/gs_message.cc17
-rw-r--r--i/simulotron/src/gs/gs_message.hh2
-rw-r--r--i/simulotron/src/gs/gs_transmitter.cc20
-rw-r--r--i/simulotron/src/gs/test_gs.cc44
4 files changed, 41 insertions, 42 deletions
diff --git a/i/simulotron/src/gs/gs_message.cc b/i/simulotron/src/gs/gs_message.cc
index b971cd9..818c433 100644
--- a/i/simulotron/src/gs/gs_message.cc
+++ b/i/simulotron/src/gs/gs_message.cc
@@ -51,19 +51,15 @@ GSMessage::readGS(void * data, size_t size)
str.assign(igs_, gs_.end());
if (size > str.size())
throw std::runtime_error("CHIER!!! La size dans readGS est trop grande!!");
- for (unsigned int i = 0; i < size; i++)
- {
- static_cast<char *> (data)[i] = *igs_;
- ++igs_;
- }
+ memcpy(data, str.data(), size);
+ igs_ += size;
}
void
GSMessage::writeGS (const void * data, size_t size)
{
+ gs_.append(static_cast<const char *> (data), size);
setIteratorBegin();
- for (unsigned int i = 0; i < size; ++i)
- gs_.push_back(static_cast<const char *> (data)[i]);
}
void
@@ -71,3 +67,10 @@ GSMessage::setIteratorBegin(void)
{
igs_ = gs_.begin();
}
+
+void
+GSMessage::clear(void)
+{
+ gs_.clear();
+ setIteratorBegin();
+}
diff --git a/i/simulotron/src/gs/gs_message.hh b/i/simulotron/src/gs/gs_message.hh
index 4a9b64e..b96491c 100644
--- a/i/simulotron/src/gs/gs_message.hh
+++ b/i/simulotron/src/gs/gs_message.hh
@@ -48,6 +48,8 @@ class GSMessage
void writeGS (const void * data, size_t size);
/// Remise à zéro de l'itérateur de lecture
void setIteratorBegin(void);
+ /// Efface la GS
+ void clear(void);
};
#endif //gs_message_hh
diff --git a/i/simulotron/src/gs/gs_transmitter.cc b/i/simulotron/src/gs/gs_transmitter.cc
index a72f812..e9b6648 100644
--- a/i/simulotron/src/gs/gs_transmitter.cc
+++ b/i/simulotron/src/gs/gs_transmitter.cc
@@ -36,24 +36,26 @@ GSTransmitter::GSTransmitter(SocketClient & socket)
int
GSTransmitter::getGS(GSMessage & gsm)
{
+ ///BUG Pas portable le code, suppose des int à 4 octect
unsigned int size = 0;
std::string strRead (socket_.read ());
strBrut_ += strRead;
- size = static_cast<int> (strRead[0]) << 24
- | static_cast<int> (strRead[1]) << 16
- | static_cast<int> (strRead[2]) << 8
- | static_cast<int> (strRead[3]);
+ 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;
- strRead = strBrut_.substr (4, size -1);
- strBrut_.erase (4, size - 1);
- gsm.writeGS (strRead.c_str (), size);
+ strRead = strBrut_.substr (4, size);
+ strBrut_.erase (0, size + 4);
+ gsm.writeGS (strRead.data (), size);
return 0;
}
void
GSTransmitter::putGS(const GSMessage & gsm)
{
+ ///BUG Pas portable le code, suppose des int à 4 octect
std::string gs(gsm.getGS ());
char bsize[4];
int size = gs.size();
@@ -61,6 +63,6 @@ GSTransmitter::putGS(const GSMessage & gsm)
bsize[1] = static_cast<char> (size >> 16);
bsize[2] = static_cast<char> (size >> 8);
bsize[3] = static_cast<char> (size);
- gs.insert(0, bsize);
- socket_.write (gsm.getGS ());
+ gs.insert(0, bsize, 4);
+ socket_.write (gs);
}
diff --git a/i/simulotron/src/gs/test_gs.cc b/i/simulotron/src/gs/test_gs.cc
index 0dc557a..a2d8724 100644
--- a/i/simulotron/src/gs/test_gs.cc
+++ b/i/simulotron/src/gs/test_gs.cc
@@ -89,7 +89,7 @@ int main(void)
<< std::endl;
return 1;
}
- std::cout << "String OK" << std::endl;
+ gsm.clear();
// Un char
if (gst.getGS(gsm) != 0)
throw std::runtime_error ("CHIER!! Reception mal passé");
@@ -102,11 +102,11 @@ int main(void)
<< std::endl;
return 1;
}
- std::cout << "Char OK" << std::endl;
+ gsm.clear();
// Un short
if (gst.getGS(gsm) != 0)
throw std::runtime_error ("CHIER!! Reception mal passé");
- gsm.readGS(&s2, sizeof(char));
+ gsm.readGS(&s2, sizeof(short));
if ( s1 != s2)
{
std::cout << "CHIER Short altéré\n"
@@ -115,11 +115,11 @@ int main(void)
<< std::endl;
return 1;
}
- std::cout << "Short OK" << std::endl;
+ gsm.clear();
// Un int positif
if (gst.getGS(gsm) != 0)
throw std::runtime_error ("CHIER!! Reception mal passé");
- gsm.readGS(&i2, sizeof(char));
+ gsm.readGS(&i2, sizeof(int));
if ( i1 != i2)
{
std::cout << "CHIER int positif altéré\n"
@@ -128,11 +128,11 @@ int main(void)
<< std::endl;
return 1;
}
- std::cout << "int positif OK" << std::endl;
+ gsm.clear();
// Un int negatif
if (gst.getGS(gsm) != 0)
throw std::runtime_error ("CHIER!! Reception mal passé");
- gsm.readGS(&i4, sizeof(char));
+ gsm.readGS(&i4, sizeof(int));
if ( i3 != i4)
{
std::cout << "CHIER int negatif altéré\n"
@@ -141,11 +141,11 @@ int main(void)
<< std::endl;
return 1;
}
- std::cout << "int negatif OK" << std::endl;
+ gsm.clear();
// Un Long
if (gst.getGS(gsm) != 0)
throw std::runtime_error ("CHIER!! Reception mal passé");
- gsm.readGS(&l2, sizeof(char));
+ gsm.readGS(&l2, sizeof(long));
if ( l1 != l2)
{
std::cout << "CHIER long altéré\n"
@@ -154,7 +154,7 @@ int main(void)
<< std::endl;
return 1;
}
- std::cout << "int positif OK" << std::endl;
+ gsm.clear();
// Un gros message
if (gst.getGS(gsm) != 0)
throw std::runtime_error ("CHIER!! Reception mal passé");
@@ -185,7 +185,6 @@ int main(void)
<< std::endl;
return 1;
}
- std::cout << "Gros message OK" << std::endl;
//On attend le thread client
pthread_join(threadId, &retour);
stateTh = (int) retour;
@@ -208,28 +207,21 @@ void * testGSClient(void * CaSertARien)
GSMessage g1,g2,g3,g4,g5,g6,g7;
g1.writeGS(strDepart.data(), strDepart.size());
gst.putGS(g1);
- g2.writeGS(&s1, sizeof(short));
+ g2.writeGS(&a, sizeof(char));
gst.putGS(g2);
- g3.writeGS(&i1, sizeof(int));
+ g3.writeGS(&s1, sizeof(short));
gst.putGS(g3);
- g4.writeGS(&i3, sizeof(int));
+ g4.writeGS(&i1, sizeof(int));
gst.putGS(g4);
- g5.writeGS(&l1, sizeof(long));
+ g5.writeGS(&i3, sizeof(int));
gst.putGS(g5);
-
- g6.writeGS(&s1, sizeof(short));
g6.writeGS(&l1, sizeof(long));
- g6.writeGS(&i3, sizeof(int));
gst.putGS(g6);
- std::cout << "Et pouf" << std::endl;
-
- /*//tv
- timeval tv;
- tv.tv_sec = 20;
- tv.tv_usec = 0;
- select(1,NULL,NULL,NULL,&tv);
- */
+ g7.writeGS(&s1, sizeof(short));
+ g7.writeGS(&l1, sizeof(long));
+ g7.writeGS(&i3, sizeof(int));
+ gst.putGS(g7);
}
catch(std::exception & chier)
{