@@ -57,39 +57,50 @@ std::string CommunicationSocket::read(void) const
57
57
if ((ret = recv (this ->_socket , buffer, BUFF_SIZE - 1 , 0 )) < 0 )
58
58
throw (" CommunicationSocket:get_datagram(): recv error\n " );
59
59
buffer[ret] = 0 ;
60
- std::cout << " I have read " << ret << " bytes [" << buffer << " ]\n " ;
60
+ // std::cout << "I have read " << ret << " bytes [" << buffer << "]\n";
61
61
return (std::string (buffer));
62
62
}
63
63
64
64
/* the private get_datagram has to be rewritten depending on the format of a datagram */
65
65
66
- void CommunicationSocket::get_datagram (void )
66
+
67
+ bool CommunicationSocket::get_datagram (void )
67
68
{
68
69
Datagram datagram;
69
70
std::size_t spliter;
70
71
std::string raw;
72
+ bool hasok = false ;
71
73
72
74
raw = this ->read ();
73
75
std::stringstream ss (raw);
74
-
75
76
for (std::string line; std::getline (ss, line);)
76
77
{
77
- std::cout << " raw_message : [" << line << " ]" << std::endl;
78
78
if ((spliter = line.find (" " )) == std::string::npos)
79
- throw (std::runtime_error (" CommunicationSocket(): get_datagram line.find error" ));
80
- std::cout << " spliter : " << spliter << std::endl;
79
+ {
80
+ std::cout << " CommunicationSocket(): get_datagram line.find error" << std::endl;
81
+ return false ;
82
+ }
81
83
datagram.setHeader (line.substr (0 , spliter));
82
84
datagram.setMessage (line.substr (spliter));
83
- std::cout << datagram.getHeader () << " | === |" << datagram.getMessage () << std::endl;
84
- try
85
+ // std::cout << datagram.getHeader() << "| === |" << datagram.getMessage() << std::endl;
86
+ if (datagram. getHeader () == " ok " )
85
87
{
86
- this ->_datagram_stack .push (datagram);
88
+ std::cout << KYEL << " OK " << KNRM << std::endl;
89
+ hasok = true ;
87
90
}
88
- catch (std:: exception &e)
91
+ else
89
92
{
90
- std::cout << " CommunicationSocket::getDatagram(): " << e.what () << std::endl;
93
+ try
94
+ {
95
+ this ->_datagram_stack .push (datagram);
96
+ }
97
+ catch (std::exception &e)
98
+ {
99
+ std::cout << " CommunicationSocket::getDatagram(): " << e.what () << std::endl;
100
+ }
91
101
}
92
102
}
103
+ return hasok;
93
104
}
94
105
95
106
void CommunicationSocket::get_peer (std::string data, Map &map) const
@@ -150,7 +161,7 @@ void CommunicationSocket::send_datagram(std::string header, std::string message
150
161
void CommunicationSocket::send_datagram (Datagram const & datagram) const
151
162
{
152
163
std::string data (datagram.getHeader () + datagram.getMessage ());
153
-
164
+ // std::cout << "sending datagram : [" << data << "]" << std::endl;;
154
165
if (send (this ->_socket , data.c_str (), data.length (), 0 ) < 0 )
155
166
throw (" CommunicationSocket:send_datagram(): send error\n " );
156
167
}
@@ -237,8 +248,6 @@ Map CommunicationSocket::get_first_info(Map &map, Position& start, Position &en
237
248
ss >> header >> id >> x >> y;
238
249
map[x][y].total_cars ++;
239
250
};
240
-
241
-
242
251
while (!done)
243
252
{
244
253
raw = this ->read ();
@@ -292,7 +301,7 @@ bool CommunicationSocket::get_datagram(Datagram & datagram)
292
301
{
293
302
if (!this ->_datagram_stack .empty ())
294
303
{
295
- this ->_datagram_stack .top ();
304
+ datagram = this ->_datagram_stack .top ();
296
305
this ->_datagram_stack .pop ();
297
306
return (true );
298
307
}
@@ -326,18 +335,11 @@ void CommunicationSocket::wait_for_move(void)
326
335
FD_COPY (&_rfds, ©);
327
336
328
337
/* Need to think about the structure, multithreaded or not */
329
- std::cout << " Entering move" << std::endl;
330
- while (!moved && select (_socket + 1 , &_rfds, NULL , NULL , &_tv) >= 0 )
338
+ while (!moved && select (_socket + 1 , &_rfds, NULL , NULL , NULL ) >= 0 )
331
339
{
332
340
if (FD_ISSET (_socket, ©))
333
341
{
334
- this ->get_datagram ();
335
- data = this ->_datagram_stack .top ();
336
- if (data.getHeader () == " ok" )
337
- {
338
- moved = true ;
339
- this ->_datagram_stack .pop ();
340
- }
342
+ moved = this ->get_datagram ();
341
343
FD_ZERO (&_rfds);
342
344
FD_COPY (©, &_rfds);
343
345
}
0 commit comments