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

@ -1,7 +1,6 @@
#ifndef TCP_CONNECTION_HPP
#define TCP_CONNECTION_HPP
#include "MessageBuilder.hpp"
#include "Socket.hpp"
#include <string>
@ -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;

@ -3,66 +3,47 @@
#include "Logger.hpp"
#include "TcpClient.hpp"
#include "MessageBuilder.hpp"
#include "Message.hpp"
#include <iostream>
#include <string>
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:
void msgArrived( const std::string msg)
{
TRACE;
LOG( Logger::DEBUG, std::string("Got msg: ").append(msg).c_str() );
}
protected:
void onDisconnect()
int getExpectedLength()
{
TRACE;
LOG( Logger::DEBUG, "What shall I do..." );
return 0;
}
};
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<SimpleMessage> 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;
}

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

Loading…
Cancel
Save