From 576a5af701b6231d8dc96aae8bcf7deddcb9b4c9 Mon Sep 17 00:00:00 2001 From: dmatetelki Date: Wed, 18 May 2016 15:18:17 +0200 Subject: [PATCH] PrintMessage can signal when ready with conditional variable --- other/PrintMessage.hpp | 29 +++++++++++++++++++++++++---- other/sslclient_main.cpp | 17 ++--------------- other/tcpclient_main.cpp | 16 ++-------------- 3 files changed, 29 insertions(+), 33 deletions(-) diff --git a/other/PrintMessage.hpp b/other/PrintMessage.hpp index 17b54a8..fa65894 100644 --- a/other/PrintMessage.hpp +++ b/other/PrintMessage.hpp @@ -3,13 +3,25 @@ #include -/// @brief prints the received message +#include +#include + + +/// @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(m_param) ) = true; - m_buffer.clear(); + std::unique_lock 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 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 diff --git a/other/sslclient_main.cpp b/other/sslclient_main.cpp index fb52a6f..4c7d068 100644 --- a/other/sslclient_main.cpp +++ b/other/sslclient_main.cpp @@ -4,12 +4,9 @@ #include "PrintMessage.hpp" -#include #include #include -#include // 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(); diff --git a/other/tcpclient_main.cpp b/other/tcpclient_main.cpp index 5c21ff5..9df5dcc 100644 --- a/other/tcpclient_main.cpp +++ b/other/tcpclient_main.cpp @@ -8,9 +8,6 @@ #include #include -#include // nanosleep -#include // 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();