From fab6d0b6e66eb14cff13b03fb0f893e4fb4f8b5c Mon Sep 17 00:00:00 2001 From: dmatetelki Date: Wed, 18 May 2016 14:22:51 +0200 Subject: [PATCH] moving EchoMessage and PrintMessage into a header --- lib/cpp_utils/SslConnection.cpp | 10 ++- other/EchoMessage.hpp | 71 +++++++++++++------ .../{EchoMessage.cpp => MySQLEchoMessage.cpp} | 0 other/MySQLEchoMessage.hpp | 35 +++++++++ .../PrintMessage.hpp | 23 +++--- other/sslclient_main.cpp | 11 +-- other/sslserver_main.cpp | 54 +------------- other/tcpclient_main.cpp | 52 +------------- other/tcpserver_main.cpp | 57 +-------------- 9 files changed, 114 insertions(+), 199 deletions(-) rename other/{EchoMessage.cpp => MySQLEchoMessage.cpp} (100%) create mode 100644 other/MySQLEchoMessage.hpp rename test/cpp_utils/SimpleMessage.hpp => other/PrintMessage.hpp (60%) diff --git a/lib/cpp_utils/SslConnection.cpp b/lib/cpp_utils/SslConnection.cpp index d01f6a0..1082ea7 100644 --- a/lib/cpp_utils/SslConnection.cpp +++ b/lib/cpp_utils/SslConnection.cpp @@ -249,8 +249,16 @@ bool SslConnection::receive() int ret = SSL_read(m_sslHandle, m_buffer, m_bufferLength); - if ( ret > 0 ) + if ( ret > 0 ) { + LOG_BEGIN(Logger::INFO) + LOG_PROP("Host", m_timedTcpConnection->getHost()) + LOG_PROP("Port", m_timedTcpConnection->getPort()) + LOG_PROP("Socket", m_timedTcpConnection->getSocket()) + LOG_PROP("Bytes", ret) + LOG_END("Received message from peer."); + return m_message->buildMessage( (void*)m_buffer, (size_t)ret); + } unsigned long sslErrNo = ERR_get_error(); if ( ret == 0 && (sslErrNo == SSL_ERROR_ZERO_RETURN || diff --git a/other/EchoMessage.hpp b/other/EchoMessage.hpp index 7d3e861..cdf849f 100644 --- a/other/EchoMessage.hpp +++ b/other/EchoMessage.hpp @@ -1,35 +1,64 @@ -#ifndef ECHO_MESSAGE_HPP -#define ECHO_MESSAGE_HPP +#ifndef EchoMessage_HPP +#define EchoMessage_HPP -#include "Message.hpp" -#include "Connection.hpp" -#include "ThreadPool.hpp" -#include "MysqlConnectionPool.hpp" +#include -struct MsgParam -{ - MysqlConnectionPool *m_cp; - ThreadPool *m_tp; - MsgParam(MysqlConnectionPool *cp, ThreadPool *tp) : m_cp(cp), m_tp(tp) {}; -}; - - -class EchoMessage : public Message +/// @brief echoes back the message to the sender +class EchoMessage : public Message { public: - EchoMessage( Connection *connection, - void *msgParam = 0); + EchoMessage( void *msgParam = 0) + : Message(msgParam) + { + TRACE; + } bool buildMessage( const void *msgPart, - const size_t msgLen ); + const size_t msgLen ) + { + TRACE; + m_buffer = std::string( (const char*) msgPart, msgLen ); + + // not using getExpectedLength + onMessageReady(); + return true; + } + + void onMessageReady() + { + TRACE; + + LOG_BEGIN(Logger::INFO) + LOG_PROP("message", m_buffer) + LOG_PROP("host", m_connection->getHost()) + LOG_PROP("port", m_connection->getPort()) + LOG_END("Got message."); + + m_connection->send(m_buffer.c_str(), m_buffer.length()); + m_buffer.clear(); + } + + Message* clone() + { + TRACE; + return new EchoMessage(m_param); + } - void onMessageReady(); + std::string getBuffer() + { + TRACE; + return m_buffer; + } protected: - size_t getExpectedLength(); + size_t getExpectedLength() + { + TRACE; + return 0; + } }; -#endif // ECHO_MESSAGE_HPP +#endif // EchoMessage_HPP diff --git a/other/EchoMessage.cpp b/other/MySQLEchoMessage.cpp similarity index 100% rename from other/EchoMessage.cpp rename to other/MySQLEchoMessage.cpp diff --git a/other/MySQLEchoMessage.hpp b/other/MySQLEchoMessage.hpp new file mode 100644 index 0000000..7d3e861 --- /dev/null +++ b/other/MySQLEchoMessage.hpp @@ -0,0 +1,35 @@ +#ifndef ECHO_MESSAGE_HPP +#define ECHO_MESSAGE_HPP + +#include "Message.hpp" +#include "Connection.hpp" +#include "ThreadPool.hpp" +#include "MysqlConnectionPool.hpp" + +struct MsgParam +{ + MysqlConnectionPool *m_cp; + ThreadPool *m_tp; + MsgParam(MysqlConnectionPool *cp, ThreadPool *tp) : m_cp(cp), m_tp(tp) {}; +}; + + +class EchoMessage : public Message +{ +public: + + EchoMessage( Connection *connection, + void *msgParam = 0); + + bool buildMessage( const void *msgPart, + const size_t msgLen ); + + void onMessageReady(); + +protected: + + size_t getExpectedLength(); + +}; + +#endif // ECHO_MESSAGE_HPP diff --git a/test/cpp_utils/SimpleMessage.hpp b/other/PrintMessage.hpp similarity index 60% rename from test/cpp_utils/SimpleMessage.hpp rename to other/PrintMessage.hpp index d696b25..17b54a8 100644 --- a/test/cpp_utils/SimpleMessage.hpp +++ b/other/PrintMessage.hpp @@ -1,13 +1,14 @@ -#ifndef SIMPLEMESSAGE_HPP -#define SIMPLEMESSAGE_HPP +#ifndef PRINT_MESSAGE_HPP +#define PRINT_MESSAGE_HPP #include -class SimpleMessage : public Message +/// @brief prints the received message +class PrintMessage : public Message { public: - SimpleMessage( void *msgParam = 0) + PrintMessage( void *msgParam = 0) : Message(msgParam) { TRACE; @@ -18,6 +19,8 @@ public: { TRACE; m_buffer = std::string( (const char*) msgPart, msgLen ); + + // not using getExpectedLength onMessageReady(); return true; } @@ -28,20 +31,20 @@ public: LOG_BEGIN(Logger::INFO) LOG_PROP("message", m_buffer) - LOG_END("Got reply from server."); + LOG_PROP("host", m_connection->getHost()) + LOG_PROP("port", m_connection->getPort()) + LOG_END("Got message."); + // threat m_params as "isready" flag *( static_cast(m_param) ) = true; - if (m_connection != 0) - m_connection->send(m_buffer.c_str(), m_buffer.length()); - m_buffer.clear(); } Message* clone() { TRACE; - return new SimpleMessage(m_param); + return new PrintMessage(m_param); } std::string getBuffer() @@ -60,4 +63,4 @@ protected: }; -#endif // SIMPLEMESSAGE_HPP +#endif // PRINT_MESSAGE_HPP diff --git a/other/sslclient_main.cpp b/other/sslclient_main.cpp index 152ae0e..fb52a6f 100644 --- a/other/sslclient_main.cpp +++ b/other/sslclient_main.cpp @@ -1,20 +1,15 @@ #include - -#include #include #include -#include "../test/cpp_utils/SimpleMessage.hpp" +#include "PrintMessage.hpp" #include - #include #include #include // nanosleep -// #include - int main(int argc, char* argv[] ) { @@ -30,7 +25,7 @@ int main(int argc, char* argv[] ) bool finished = false; - SimpleMessage msg(&finished); + PrintMessage msg(&finished); SslConnection conn(argv[1], argv[2], &msg); conn.initClientContext(); SocketClient socketClient(&conn); @@ -59,7 +54,7 @@ int main(int argc, char* argv[] ) while ( !finished && socketClient.isPolling() ) nanosleep(&tm, &tm) ; -// socketClient.disconnect(); + socketClient.disconnect(); SslConnection::destroy(); Logger::destroy(); return 0; diff --git a/other/sslserver_main.cpp b/other/sslserver_main.cpp index 8e517bf..752e9b3 100644 --- a/other/sslserver_main.cpp +++ b/other/sslserver_main.cpp @@ -4,68 +4,16 @@ #include #include - -#include #include #include +#include "EchoMessage.hpp" #include #include #include -class EchoMessage : public Message -{ -public: - - EchoMessage( void *msgParam = 0) - : Message(msgParam) - { - TRACE; - } - - bool buildMessage( const void *msgPart, - const size_t msgLen ) - { - TRACE; - m_buffer = std::string( (const char*) msgPart, msgLen ); - onMessageReady(); - return true; - } - - void onMessageReady() - { - TRACE; - - LOG( Logger::INFO, std::string("Got message: \""). - append(m_buffer).append("\" from: "). - append(m_connection->getHost().append(":"). - append(TToStr(m_connection->getPort())) ).c_str() ); - - std::string reply("Got your message, "); - reply.append(m_connection->getHost()).append(":"). - append(TToStr(m_connection->getPort())). - append(" \"").append(m_buffer).append("\""); - - m_connection->send( reply.c_str(), reply.length() ); - } - - Message* clone() - { - TRACE; - return new EchoMessage(m_param); - } - -protected: - - size_t getExpectedLength() - { - TRACE; - return 0; - } -}; - SocketServer *socketServer; diff --git a/other/tcpclient_main.cpp b/other/tcpclient_main.cpp index 7d55e02..5c21ff5 100644 --- a/other/tcpclient_main.cpp +++ b/other/tcpclient_main.cpp @@ -1,10 +1,9 @@ #include #include - -#include #include #include +#include "PrintMessage.hpp" #include #include @@ -13,53 +12,6 @@ #include // sleep - - -class SimpleMessage : public Message -{ -public: - - SimpleMessage( void *msgParam = 0) - : Message(msgParam) - { - TRACE; - } - - bool buildMessage( const void *msgPart, - const size_t msgLen ) - { - TRACE; - m_buffer = std::string( (const char*) msgPart, msgLen ); - onMessageReady(); - return true; - } - - void onMessageReady() - { - TRACE; - - LOG( Logger::INFO, std::string("Got reply from server: "). - append(m_buffer).c_str() ); - - *( static_cast(m_param) ) = true; - } - - Message* clone() - { - TRACE; - return new SimpleMessage(m_param); - } - -protected: - - size_t getExpectedLength() - { - TRACE; - return 0; - } -}; - - int main(int argc, char* argv[] ) { if ( argc != 4 ) { @@ -73,7 +25,7 @@ int main(int argc, char* argv[] ) Logger::setLogLevel(Logger::FINEST); bool finished = false; - SimpleMessage msg(&finished); + PrintMessage msg(&finished); TcpConnection conn(argv[1], argv[2], &msg); SocketClient socketClient(&conn); diff --git a/other/tcpserver_main.cpp b/other/tcpserver_main.cpp index 56adb82..e7a9580 100644 --- a/other/tcpserver_main.cpp +++ b/other/tcpserver_main.cpp @@ -1,69 +1,15 @@ #include #include - -#include #include #include +#include "EchoMessage.hpp" #include #include #include // sleep -class EchoMessage : public Message -{ -public: - - EchoMessage( void *msgParam = 0) - : Message(msgParam) - { - TRACE; - } - - bool buildMessage( const void *msgPart, - const size_t msgLen ) - { - TRACE; - m_buffer = std::string( (const char*) msgPart, msgLen ); - onMessageReady(); - return true; - } - - void onMessageReady() - { - TRACE; - - std::cout << "buffer: " << m_buffer << std::endl; - - LOG( Logger::INFO, std::string("Got message: \""). - append(m_buffer).append("\" from: "). - append(m_connection->getHost().append(":"). - append(TToStr(m_connection->getPort())) ).c_str() ); - - std::string reply("Got your message, "); - reply.append(m_connection->getHost()).append(":"). - append(TToStr(m_connection->getPort())). - append(" \"").append(m_buffer).append("\""); - - m_connection->send( reply.c_str(), reply.length() ); - } - - Message* clone() - { - TRACE; - return new EchoMessage(m_param); - } - -protected: - - size_t getExpectedLength() - { - TRACE; - return 0; - } -}; - int main(int argc, char* argv[] ) { @@ -75,7 +21,6 @@ int main(int argc, char* argv[] ) Logger::createInstance(); Logger::init(std::cout); Logger::setLogLevel(Logger::FINEST); -// Logger::setNoPrefix(); EchoMessage msg; TcpConnection conn(argv[1], argv[2], &msg);