moving EchoMessage and PrintMessage into a header

master
dmatetelki 9 years ago
parent cb4766eb62
commit fab6d0b6e6

@ -249,8 +249,16 @@ bool SslConnection::receive()
int ret = SSL_read(m_sslHandle, m_buffer, m_bufferLength); 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); return m_message->buildMessage( (void*)m_buffer, (size_t)ret);
}
unsigned long sslErrNo = ERR_get_error(); unsigned long sslErrNo = ERR_get_error();
if ( ret == 0 && (sslErrNo == SSL_ERROR_ZERO_RETURN || if ( ret == 0 && (sslErrNo == SSL_ERROR_ZERO_RETURN ||

@ -1,35 +1,64 @@
#ifndef ECHO_MESSAGE_HPP #ifndef EchoMessage_HPP
#define ECHO_MESSAGE_HPP #define EchoMessage_HPP
#include "Message.hpp" #include <cpp_utils/Message.hpp>
#include "Connection.hpp"
#include "ThreadPool.hpp"
#include "MysqlConnectionPool.hpp"
struct MsgParam /// @brief echoes back the message to the sender
class EchoMessage : public Message
{ {
MysqlConnectionPool *m_cp; public:
ThreadPool *m_tp;
MsgParam(MysqlConnectionPool *cp, ThreadPool *tp) : m_cp(cp), m_tp(tp) {};
};
EchoMessage( void *msgParam = 0)
: Message(msgParam)
{
TRACE;
}
class EchoMessage : public Message<EchoMessage> bool buildMessage( const void *msgPart,
const size_t msgLen )
{ {
public: TRACE;
m_buffer = std::string( (const char*) msgPart, msgLen );
EchoMessage( Connection<EchoMessage> *connection, // not using getExpectedLength
void *msgParam = 0); onMessageReady();
return true;
}
bool buildMessage( const void *msgPart, void onMessageReady()
const size_t msgLen ); {
TRACE;
void onMessageReady(); 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);
}
std::string getBuffer()
{
TRACE;
return m_buffer;
}
protected: protected:
size_t getExpectedLength(); size_t getExpectedLength()
{
TRACE;
return 0;
}
}; };
#endif // ECHO_MESSAGE_HPP #endif // EchoMessage_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<EchoMessage>
{
public:
EchoMessage( Connection<EchoMessage> *connection,
void *msgParam = 0);
bool buildMessage( const void *msgPart,
const size_t msgLen );
void onMessageReady();
protected:
size_t getExpectedLength();
};
#endif // ECHO_MESSAGE_HPP

@ -1,13 +1,14 @@
#ifndef SIMPLEMESSAGE_HPP #ifndef PRINT_MESSAGE_HPP
#define SIMPLEMESSAGE_HPP #define PRINT_MESSAGE_HPP
#include <cpp_utils/Message.hpp> #include <cpp_utils/Message.hpp>
class SimpleMessage : public Message /// @brief prints the received message
class PrintMessage : public Message
{ {
public: public:
SimpleMessage( void *msgParam = 0) PrintMessage( void *msgParam = 0)
: Message(msgParam) : Message(msgParam)
{ {
TRACE; TRACE;
@ -18,6 +19,8 @@ public:
{ {
TRACE; TRACE;
m_buffer = std::string( (const char*) msgPart, msgLen ); m_buffer = std::string( (const char*) msgPart, msgLen );
// not using getExpectedLength
onMessageReady(); onMessageReady();
return true; return true;
} }
@ -28,20 +31,20 @@ public:
LOG_BEGIN(Logger::INFO) LOG_BEGIN(Logger::INFO)
LOG_PROP("message", m_buffer) 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<bool*>(m_param) ) = true; *( static_cast<bool*>(m_param) ) = true;
if (m_connection != 0)
m_connection->send(m_buffer.c_str(), m_buffer.length());
m_buffer.clear(); m_buffer.clear();
} }
Message* clone() Message* clone()
{ {
TRACE; TRACE;
return new SimpleMessage(m_param); return new PrintMessage(m_param);
} }
std::string getBuffer() std::string getBuffer()
@ -60,4 +63,4 @@ protected:
}; };
#endif // SIMPLEMESSAGE_HPP #endif // PRINT_MESSAGE_HPP

@ -1,20 +1,15 @@
#include <cpp_utils/Logger.hpp> #include <cpp_utils/Logger.hpp>
#include <cpp_utils/Message.hpp>
#include <cpp_utils/SslConnection.hpp> #include <cpp_utils/SslConnection.hpp>
#include <cpp_utils/SocketClient.hpp> #include <cpp_utils/SocketClient.hpp>
#include "../test/cpp_utils/SimpleMessage.hpp" #include "PrintMessage.hpp"
#include <unistd.h> #include <unistd.h>
#include <iostream> #include <iostream>
#include <string> #include <string>
#include <time.h> // nanosleep #include <time.h> // nanosleep
// #include <Common.hpp>
int main(int argc, char* argv[] ) int main(int argc, char* argv[] )
{ {
@ -30,7 +25,7 @@ int main(int argc, char* argv[] )
bool finished = false; bool finished = false;
SimpleMessage msg(&finished); PrintMessage msg(&finished);
SslConnection conn(argv[1], argv[2], &msg); SslConnection conn(argv[1], argv[2], &msg);
conn.initClientContext(); conn.initClientContext();
SocketClient socketClient(&conn); SocketClient socketClient(&conn);
@ -59,7 +54,7 @@ int main(int argc, char* argv[] )
while ( !finished && socketClient.isPolling() ) while ( !finished && socketClient.isPolling() )
nanosleep(&tm, &tm) ; nanosleep(&tm, &tm) ;
// socketClient.disconnect(); socketClient.disconnect();
SslConnection::destroy(); SslConnection::destroy();
Logger::destroy(); Logger::destroy();
return 0; return 0;

@ -4,68 +4,16 @@
#include <cpp_utils/Logger.hpp> #include <cpp_utils/Logger.hpp>
#include <cpp_utils/Common.hpp> #include <cpp_utils/Common.hpp>
#include <cpp_utils/Message.hpp>
#include <cpp_utils/SslConnection.hpp> #include <cpp_utils/SslConnection.hpp>
#include <cpp_utils/SocketServer.hpp> #include <cpp_utils/SocketServer.hpp>
#include "EchoMessage.hpp"
#include <iostream> #include <iostream>
#include <string> #include <string>
#include <signal.h> #include <signal.h>
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; SocketServer *socketServer;

@ -1,10 +1,9 @@
#include <cpp_utils/Logger.hpp> #include <cpp_utils/Logger.hpp>
#include <cpp_utils/Common.hpp> #include <cpp_utils/Common.hpp>
#include <cpp_utils/Message.hpp>
#include <cpp_utils/TcpConnection.hpp> #include <cpp_utils/TcpConnection.hpp>
#include <cpp_utils/SocketClient.hpp> #include <cpp_utils/SocketClient.hpp>
#include "PrintMessage.hpp"
#include <iostream> #include <iostream>
#include <string> #include <string>
@ -13,53 +12,6 @@
#include <unistd.h> // sleep #include <unistd.h> // 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<bool*>(m_param) ) = true;
}
Message* clone()
{
TRACE;
return new SimpleMessage(m_param);
}
protected:
size_t getExpectedLength()
{
TRACE;
return 0;
}
};
int main(int argc, char* argv[] ) int main(int argc, char* argv[] )
{ {
if ( argc != 4 ) { if ( argc != 4 ) {
@ -73,7 +25,7 @@ int main(int argc, char* argv[] )
Logger::setLogLevel(Logger::FINEST); Logger::setLogLevel(Logger::FINEST);
bool finished = false; bool finished = false;
SimpleMessage msg(&finished); PrintMessage msg(&finished);
TcpConnection conn(argv[1], argv[2], &msg); TcpConnection conn(argv[1], argv[2], &msg);
SocketClient socketClient(&conn); SocketClient socketClient(&conn);

@ -1,69 +1,15 @@
#include <cpp_utils/Logger.hpp> #include <cpp_utils/Logger.hpp>
#include <cpp_utils/Common.hpp> #include <cpp_utils/Common.hpp>
#include <cpp_utils/Message.hpp>
#include <cpp_utils/TcpConnection.hpp> #include <cpp_utils/TcpConnection.hpp>
#include <cpp_utils/SocketServer.hpp> #include <cpp_utils/SocketServer.hpp>
#include "EchoMessage.hpp"
#include <iostream> #include <iostream>
#include <string> #include <string>
#include <unistd.h> // sleep #include <unistd.h> // 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[] ) int main(int argc, char* argv[] )
{ {
@ -75,7 +21,6 @@ int main(int argc, char* argv[] )
Logger::createInstance(); Logger::createInstance();
Logger::init(std::cout); Logger::init(std::cout);
Logger::setLogLevel(Logger::FINEST); Logger::setLogLevel(Logger::FINEST);
// Logger::setNoPrefix();
EchoMessage msg; EchoMessage msg;
TcpConnection conn(argv[1], argv[2], &msg); TcpConnection conn(argv[1], argv[2], &msg);

Loading…
Cancel
Save