diff --git a/include/Message.hpp b/include/Message.hpp new file mode 100644 index 0000000..c298046 --- /dev/null +++ b/include/Message.hpp @@ -0,0 +1,33 @@ +#ifndef MESSAGE_HPP +#define MESSAGE_HPP + +#include + + + /** Append messageParts with buildMessage() to m_buffer. + * Call onMessageReady() if the length of the buffer equals the value from + * getExpectedLength(). + */ + +class Message +{ +public: + + Message() : m_buffer() {}; + virtual ~Message() {}; + + virtual bool buildMessage( const unsigned char* messagePart, + const int length ) = 0; + virtual void onMessageReady() = 0; + +protected: + + virtual int getExpectedLength() = 0; + + /// @todo shall i use dinamic array? + std::string m_buffer; + +}; + + +#endif // MESSAGE_HPP diff --git a/include/MessageBuilder.cpp b/include/MessageBuilder.cpp deleted file mode 100644 index 855ef1e..0000000 --- a/include/MessageBuilder.cpp +++ /dev/null @@ -1,23 +0,0 @@ -#ifndef MESSAGE_BUILDER_HPP -#define MESSAGE_BUILDER_HPP - -class MessageBuilder -{ -public: - - bool buildMessage( const unsigned char* message, unsigned int length ) ; - -protected: - - MessageBuilder( const int bufferLength ) ; - virtual ~MessageBuilder() ; - virtual void onMessageReady( const unsigned char* message, unsigned int length ) = 0 ; - -private: - - unsigned char *m_buffer; - int m_bufferLength; - int m_bufferUsed; -}; - -#endif // MESSAGE_BUILDER_HPP diff --git a/include/MessageBuilder.hpp b/include/MessageBuilder.hpp deleted file mode 100644 index 0d3c597..0000000 --- a/include/MessageBuilder.hpp +++ /dev/null @@ -1,32 +0,0 @@ -#ifndef MESSAGE_BUILDER_HPP -#define MESSAGE_BUILDER_HPP - -class TcpConnection; - - -class MessageBuilder -{ -public: - - MessageBuilder( TcpConnection *connection, - const int bufferLength = 1024 ); - - virtual ~MessageBuilder(); - - bool buildMessage( const unsigned char* message, - const int length ) ; - - -private: - - MessageBuilder(const MessageBuilder&); - MessageBuilder& operator=(const MessageBuilder&); - - - TcpConnection *m_connection; - unsigned char *m_buffer; - int m_bufferLength; - int m_bufferUsed; -}; - -#endif // MESSAGE_BUILDER_HPP diff --git a/include/TcpClient.hpp b/include/TcpClient.hpp index b1752bf..6ac9656 100644 --- a/include/TcpClient.hpp +++ b/include/TcpClient.hpp @@ -2,7 +2,6 @@ #define TCP_CLIENT_HPP #include "TcpConnection.hpp" -#include "MessageBuilder.hpp" #include "Thread.hpp" #include "Poll.hpp" @@ -34,8 +33,7 @@ private: public: TcpClient ( const std::string host, - const std::string port, - MessageBuilder *buidler ); + const std::string port ); virtual ~TcpClient(); @@ -46,7 +44,7 @@ public: private: - virtual void onDisconnect() = 0; +// virtual void onDisconnect() = 0; TcpConnection m_connection; WatcherThread m_watcher; @@ -54,3 +52,5 @@ private: }; #endif // TCP_CLIENT_HPP + + diff --git a/include/TcpConnection.hpp b/include/TcpConnection.hpp index fb66c57..1097cc3 100644 --- a/include/TcpConnection.hpp +++ b/include/TcpConnection.hpp @@ -1,7 +1,6 @@ #ifndef TCP_CONNECTION_HPP #define TCP_CONNECTION_HPP -#include "MessageBuilder.hpp" #include "Socket.hpp" #include @@ -16,12 +15,10 @@ public: }; TcpConnection ( const int socket, - const MessageBuilder *m_builder, const int bufferLenght = 1024 ); TcpConnection ( const std::string host, const std::string port, - MessageBuilder *builder, const int bufferLenght = 1024 ); virtual ~TcpConnection(); @@ -33,7 +30,8 @@ public: bool sendMessage( const void* message, const int length ); bool readFromSocket(); - virtual void onMessageReady ( const unsigned char * message, const int length ) = 0; + virtual void onMessageReady ( const unsigned char * message, + const int length ) = 0; int getSocket() const; @@ -47,7 +45,6 @@ private: Socket m_socket; std::string m_host; std::string m_port; - MessageBuilder *m_builder; Status m_status; unsigned char *m_buffer; diff --git a/other/tcpclient_main.cpp b/other/tcpclient_main.cpp index 5b9f431..e881639 100644 --- a/other/tcpclient_main.cpp +++ b/other/tcpclient_main.cpp @@ -3,66 +3,47 @@ #include "Logger.hpp" #include "TcpClient.hpp" -#include "MessageBuilder.hpp" +#include "Message.hpp" #include #include - -class DummyBuilder : public MessageBuilder +class SimpleMessage : public Message { private: - void messageBuilt( const unsigned char* message, + bool buildMessage( const unsigned char* messagePart, const int length ) { TRACE; - - std::string reply((char *)message, length); - LOG( Logger::INFO, std::string("Got reply from server: "). - append(reply).c_str() ); + m_buffer = std::string( (const char*) messagePart, length ); + onMessageReady(); } -}; - - -class PrinterTcpClient : public TcpClient -{ -public: - PrinterTcpClient ( const std::string host, - const std::string port, - MessageBuilder *builder - ) - : TcpClient(host, port, builder) + void onMessageReady() { TRACE; + LOG( Logger::INFO, std::string("Got reply from server: "). + append(m_buffer).c_str() ); } -private: +protected: - void msgArrived( const std::string msg) + int getExpectedLength() { TRACE; - LOG( Logger::DEBUG, std::string("Got msg: ").append(msg).c_str() ); + return 0; } - - void onDisconnect() - { - TRACE; - LOG( Logger::DEBUG, "What shall I do..." ); - } - }; + int main( int argc, char * argv[] ) { Logger::createInstance(); Logger::init(std::cout); Logger::setLogLevel(Logger::FINEST); - MessageBuilder *builder = new DummyBuilder; - - PrinterTcpClient tcpclient("localhost", "4455", builder); + TcpClient tcpclient("localhost", "4455"); tcpclient.connect(); @@ -74,14 +55,9 @@ int main( int argc, char * argv[] ) tcpclient.send( msg2.c_str(), msg2.length()); sleep(2); -// std::string reply; -// tcpclient.receive(reply); -// std::cout << "got reply \"" << reply << "\"" << std::endl; tcpclient.disconnect(); - - delete builder; Logger::destroy(); return 0; } \ No newline at end of file diff --git a/src/TcpClient.cpp b/src/TcpClient.cpp index bcc446c..6118bb4 100644 --- a/src/TcpClient.cpp +++ b/src/TcpClient.cpp @@ -5,9 +5,8 @@ TcpClient::TcpClient( const std::string host, - const std::string port, - MessageBuilder *builder ) - : m_connection (host, port, builder) + const std::string port ) + : m_connection (host, port) , m_watcher(*this) { TRACE;