No MessageBuilder, but abstract Message, which is a template type of TcpClient

master
Denes Matetelki 13 years ago
parent cff3093cad
commit ede802cc36

@ -0,0 +1,33 @@
#ifndef MESSAGE_HPP
#define MESSAGE_HPP
#include <string>
/** 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

@ -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

@ -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

@ -2,7 +2,6 @@
#define TCP_CLIENT_HPP #define TCP_CLIENT_HPP
#include "TcpConnection.hpp" #include "TcpConnection.hpp"
#include "MessageBuilder.hpp"
#include "Thread.hpp" #include "Thread.hpp"
#include "Poll.hpp" #include "Poll.hpp"
@ -34,8 +33,7 @@ private:
public: public:
TcpClient ( const std::string host, TcpClient ( const std::string host,
const std::string port, const std::string port );
MessageBuilder *buidler );
virtual ~TcpClient(); virtual ~TcpClient();
@ -46,7 +44,7 @@ public:
private: private:
virtual void onDisconnect() = 0; // virtual void onDisconnect() = 0;
TcpConnection m_connection; TcpConnection m_connection;
WatcherThread m_watcher; WatcherThread m_watcher;
@ -54,3 +52,5 @@ private:
}; };
#endif // TCP_CLIENT_HPP #endif // TCP_CLIENT_HPP

@ -1,7 +1,6 @@
#ifndef TCP_CONNECTION_HPP #ifndef TCP_CONNECTION_HPP
#define TCP_CONNECTION_HPP #define TCP_CONNECTION_HPP
#include "MessageBuilder.hpp"
#include "Socket.hpp" #include "Socket.hpp"
#include <string> #include <string>
@ -16,12 +15,10 @@ public:
}; };
TcpConnection ( const int socket, TcpConnection ( const int socket,
const MessageBuilder *m_builder,
const int bufferLenght = 1024 ); const int bufferLenght = 1024 );
TcpConnection ( const std::string host, TcpConnection ( const std::string host,
const std::string port, const std::string port,
MessageBuilder *builder,
const int bufferLenght = 1024 ); const int bufferLenght = 1024 );
virtual ~TcpConnection(); virtual ~TcpConnection();
@ -33,7 +30,8 @@ public:
bool sendMessage( const void* message, const int length ); bool sendMessage( const void* message, const int length );
bool readFromSocket(); 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; int getSocket() const;
@ -47,7 +45,6 @@ private:
Socket m_socket; Socket m_socket;
std::string m_host; std::string m_host;
std::string m_port; std::string m_port;
MessageBuilder *m_builder;
Status m_status; Status m_status;
unsigned char *m_buffer; unsigned char *m_buffer;

@ -3,66 +3,47 @@
#include "Logger.hpp" #include "Logger.hpp"
#include "TcpClient.hpp" #include "TcpClient.hpp"
#include "MessageBuilder.hpp" #include "Message.hpp"
#include <iostream> #include <iostream>
#include <string> #include <string>
class SimpleMessage : public Message
class DummyBuilder : public MessageBuilder
{ {
private: private:
void messageBuilt( const unsigned char* message, bool buildMessage( const unsigned char* messagePart,
const int length ) const int length )
{ {
TRACE; TRACE;
m_buffer = std::string( (const char*) messagePart, length );
std::string reply((char *)message, length); onMessageReady();
LOG( Logger::INFO, std::string("Got reply from server: ").
append(reply).c_str() );
} }
}; void onMessageReady()
class PrinterTcpClient : public TcpClient
{
public:
PrinterTcpClient ( const std::string host,
const std::string port,
MessageBuilder *builder
)
: TcpClient(host, port, builder)
{ {
TRACE; TRACE;
LOG( Logger::INFO, std::string("Got reply from server: ").
append(m_buffer).c_str() );
} }
private: protected:
void msgArrived( const std::string msg)
{
TRACE;
LOG( Logger::DEBUG, std::string("Got msg: ").append(msg).c_str() );
}
void onDisconnect() int getExpectedLength()
{ {
TRACE; TRACE;
LOG( Logger::DEBUG, "What shall I do..." ); return 0;
} }
}; };
int main( int argc, char * argv[] ) 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);
MessageBuilder *builder = new DummyBuilder; TcpClient<SimpleMessage> tcpclient("localhost", "4455");
PrinterTcpClient tcpclient("localhost", "4455", builder);
tcpclient.connect(); tcpclient.connect();
@ -74,14 +55,9 @@ int main( int argc, char * argv[] )
tcpclient.send( msg2.c_str(), msg2.length()); tcpclient.send( msg2.c_str(), msg2.length());
sleep(2); sleep(2);
// std::string reply;
// tcpclient.receive(reply);
// std::cout << "got reply \"" << reply << "\"" << std::endl;
tcpclient.disconnect(); tcpclient.disconnect();
delete builder;
Logger::destroy(); Logger::destroy();
return 0; return 0;
} }

@ -5,9 +5,8 @@
TcpClient::TcpClient( const std::string host, TcpClient::TcpClient( const std::string host,
const std::string port, const std::string port )
MessageBuilder *builder ) : m_connection (host, port)
: m_connection (host, port, builder)
, m_watcher(*this) , m_watcher(*this)
{ {
TRACE; TRACE;

Loading…
Cancel
Save