PrintMessage can signal when ready with conditional variable

master
dmatetelki 9 years ago
parent fab6d0b6e6
commit 576a5af701

@ -3,13 +3,25 @@
#include <cpp_utils/Message.hpp>
/// @brief prints the received message
#include <mutex>
#include <condition_variable>
/// @brief prints the received message, can signal when ready
class PrintMessage : public Message
{
struct WaitForFlag {
bool m_isReady;
std::condition_variable m_condition_variable;
std::mutex m_mutex;
WaitForFlag() : m_isReady(false), m_condition_variable(), m_mutex() {}
};
public:
PrintMessage( void *msgParam = 0)
: Message(msgParam)
, m_flag()
{
TRACE;
}
@ -35,10 +47,10 @@ public:
LOG_PROP("port", m_connection->getPort())
LOG_END("Got message.");
// threat m_params as "isready" flag
*( static_cast<bool*>(m_param) ) = true;
m_buffer.clear();
std::unique_lock<std::mutex> lock(m_flag.m_mutex);
m_flag.m_isReady = true;
m_flag.m_condition_variable.notify_one();
}
Message* clone()
@ -53,6 +65,12 @@ public:
return m_buffer;
}
void waitForReady() {
std::unique_lock<std::mutex> lock(m_flag.m_mutex);
while (!m_flag.m_isReady)
m_flag.m_condition_variable.wait(lock);
}
protected:
size_t getExpectedLength()
@ -61,6 +79,9 @@ protected:
return 0;
}
private:
WaitForFlag m_flag;
};
#endif // PRINT_MESSAGE_HPP

@ -4,12 +4,9 @@
#include "PrintMessage.hpp"
#include <unistd.h>
#include <iostream>
#include <string>
#include <time.h> // nanosleep
int main(int argc, char* argv[] )
{
@ -23,13 +20,10 @@ int main(int argc, char* argv[] )
Logger::setLogLevel(Logger::FINEST);
SslConnection::init();
bool finished = false;
PrintMessage msg(&finished);
PrintMessage msg;
SslConnection conn(argv[1], argv[2], &msg);
conn.initClientContext();
SocketClient socketClient(&conn);
if ( !socketClient.connect() ) {
LOG_STATIC( Logger::ERR, "Couldn't connect to server, exiting..." );
SslConnection::destroy();
@ -37,9 +31,6 @@ int main(int argc, char* argv[] )
return 1;
}
// wait for thread creation
sleep(1);
// send message to server
std::string msg1(argv[3]);
if ( !socketClient.send( msg1.c_str(), msg1.length()) ) {
@ -48,11 +39,7 @@ int main(int argc, char* argv[] )
Logger::destroy();
return 1;
}
// wait for the complate &handled reply
struct timespec tm = {0,1000};
while ( !finished && socketClient.isPolling() )
nanosleep(&tm, &tm) ;
msg.waitForReady();
socketClient.disconnect();
SslConnection::destroy();

@ -8,9 +8,6 @@
#include <iostream>
#include <string>
#include <time.h> // nanosleep
#include <unistd.h> // sleep
int main(int argc, char* argv[] )
{
@ -24,9 +21,7 @@ int main(int argc, char* argv[] )
Logger::init(std::cout);
Logger::setLogLevel(Logger::FINEST);
bool finished = false;
PrintMessage msg(&finished);
PrintMessage msg;
TcpConnection conn(argv[1], argv[2], &msg);
SocketClient socketClient(&conn);
@ -36,9 +31,6 @@ int main(int argc, char* argv[] )
return 1;
}
// wait for thread creation
sleep(1);
// send message to server
std::string msg1(argv[3]);
if ( !socketClient.send( msg1.c_str(), msg1.length()) ) {
@ -46,11 +38,7 @@ int main(int argc, char* argv[] )
Logger::destroy();
return 1;
}
// wait for the complate &handled reply
struct timespec tm = {0,1000};
while ( !finished && socketClient.isPolling() )
nanosleep(&tm, &tm) ;
msg.waitForReady();
socketClient.disconnect();
Logger::destroy();

Loading…
Cancel
Save