diff --git a/include/Connection.hpp b/include/Connection.hpp index 2effdad..6f994ed 100644 --- a/include/Connection.hpp +++ b/include/Connection.hpp @@ -5,27 +5,26 @@ #include "Common.hpp" #include "Socket.hpp" +#include "Message.hpp" #include /** @todo make connection an iface and this class shall be a TcpConnection, * inherited from connection */ -template class Connection { public: Connection ( const int socket, - void *msgParam = 0, + Message *message, const size_t bufferLength = 1024 ) : m_socket(socket) , m_host() , m_port() - , m_message(this, msgParam) + , m_message(message) , m_buffer(0) , m_bufferLength(bufferLength) - , m_msgParam(msgParam) { TRACE; @@ -35,15 +34,14 @@ public: Connection ( const std::string host, const std::string port, - void *msgParam = 0, + Message *message, const size_t bufferLength = 1024 ) : m_socket(AF_INET, SOCK_STREAM) , m_host(host) , m_port(port) - , m_message(this, msgParam) + , m_message(message) , m_buffer(0) , m_bufferLength(bufferLength) - , m_msgParam(msgParam) { TRACE; m_socket.createSocket(); @@ -57,6 +55,16 @@ public: delete[] m_buffer; } + Connection* create(const int socket) + { + TRACE; + Connection *conn = new Connection( socket, + m_message->clone(), + m_bufferLength); + conn->m_message->setConnection(conn); + return conn; + } + bool connectToHost() { TRACE; @@ -107,7 +115,7 @@ public: append(TToStr(length)).append(" bytes from: "). append(m_host).append(":").append(m_port).c_str() ); - return m_message.buildMessage( (void*)m_buffer, (size_t)length); + return m_message->buildMessage( (void*)m_buffer, (size_t)length); } @@ -129,12 +137,6 @@ public: return m_port; } - void* getMsgParam() const - { - TRACE; - return m_msgParam; - } - private: @@ -144,11 +146,10 @@ private: Socket m_socket; std::string m_host; std::string m_port; - T m_message; + Message *m_message; unsigned char *m_buffer; size_t m_bufferLength; - void *m_msgParam; }; diff --git a/include/Message.hpp b/include/Message.hpp index b3c73a7..40a70d5 100644 --- a/include/Message.hpp +++ b/include/Message.hpp @@ -1,7 +1,9 @@ #ifndef MESSAGE_HPP #define MESSAGE_HPP -#include "Connection.hpp" +#include "Logger.hpp" + +// #include "Connection.hpp" #include #include // size_t @@ -11,37 +13,56 @@ * getExpectedLength(). */ -template +class Connection; + + class Message { public: - Message( Connection *connection, + Message( Connection *connection, void *msgParam = 0 ) : m_connection(connection) , m_param(msgParam) , m_buffer() - {}; + { + TRACE; + }; + + Message( void *msgParam = 0 ) + : m_connection(0) + , m_param(msgParam) + , m_buffer() + { + TRACE; + }; virtual ~Message() {}; + virtual Message* clone() = 0; virtual bool buildMessage( const void *msgPart, const size_t msgLen ) = 0; virtual void onMessageReady() = 0; + void setConnection(Connection* conn ) + { + TRACE; + m_connection = conn; + } + protected: virtual size_t getExpectedLength() = 0; - Connection *m_connection; + Connection *m_connection; void *m_param; std::string m_buffer; private: - Message(const Message &); - Message& operator=(const Message &); + Message(const Message &); + Message& operator=(const Message &); }; diff --git a/include/Poll.hpp b/include/Poll.hpp index 30b8764..c0260c6 100644 --- a/include/Poll.hpp +++ b/include/Poll.hpp @@ -10,12 +10,11 @@ -template class Poll { public: - Poll( Connection *connection, + Poll( Connection *connection, const nfds_t maxClient = 10 ) : m_connection(connection) , m_polling(false) @@ -95,9 +94,7 @@ protected: return; } - Connection *connection = new Connection( - client_socket, - m_connection->getMsgParam() ); + Connection *connection = m_connection->create(client_socket); LOG( Logger::INFO, std::string("New client connected: "). append(connection->getHost()).append(":"). @@ -170,9 +167,9 @@ private: } - typedef typename std::map< int, Connection* > ConnectionPool; + typedef typename std::map< int, Connection* > ConnectionPool; - Connection *m_connection; + Connection *m_connection; volatile bool m_polling; ConnectionPool m_connectionPool; diff --git a/include/TcpClient.hpp b/include/TcpClient.hpp index 4e789f3..c2bc1f1 100644 --- a/include/TcpClient.hpp +++ b/include/TcpClient.hpp @@ -12,18 +12,17 @@ #include // size_t -template class TcpClient { private: - template class PollerThread : public Thread - , public Poll + , public Poll { public: - PollerThread( TcpClient &data ) - : Poll( &(data.m_connection) ) + + PollerThread( TcpClient* data ) + : Poll( &(data->m_connection) ) , m_tcpClient(data) { TRACE; @@ -32,39 +31,42 @@ private: void stopPoller() { TRACE; - Poll::stopPolling(); + stopPolling(); stop(); } protected: - // overridig poll's behaviour - virtual void acceptClient() - { - TRACE; + // overridig poll's behaviour + virtual void acceptClient() + { + TRACE; - m_tcpClient.m_connection.receive(); - Poll::stopPolling(); - } + m_tcpClient->m_connection.receive(); + stopPolling(); + } - // overridig poll's behaviour - virtual void handleClient( const int ) - { - TRACE; - LOG( Logger::DEBUG, "Server closed the connection." ); - Poll::stopPolling(); - } + // overridig poll's behaviour + virtual void handleClient( const int ) + { + TRACE; + LOG( Logger::DEBUG, "Server closed the connection." ); + stopPolling(); + } private: + PollerThread(const PollerThread&); + PollerThread& operator=(const PollerThread&); + void* run() { TRACE; - Poll::startPolling(); + startPolling(); return 0; } - TcpClient &m_tcpClient; + TcpClient *m_tcpClient; }; // class PollerThread @@ -73,11 +75,13 @@ public: TcpClient ( const std::string host, const std::string port, - void *msgParam = 0 ) - : m_connection (host, port, msgParam) - , m_watcher(*this) + Message *message ) + : m_connection (host, port, message) + , m_watcher(this) { TRACE; + + message->setConnection(&m_connection); } virtual ~TcpClient() @@ -127,8 +131,8 @@ private: TcpClient& operator=(const TcpClient& ); - Connection m_connection; - PollerThread m_watcher; + Connection m_connection; + PollerThread m_watcher; }; diff --git a/include/TcpServer.hpp b/include/TcpServer.hpp index f1099cf..7c33b8b 100644 --- a/include/TcpServer.hpp +++ b/include/TcpServer.hpp @@ -5,26 +5,27 @@ #include "Connection.hpp" #include "Poll.hpp" - +#include "Message.hpp" #include -template class TcpServer { public: TcpServer ( const std::string host, const std::string port, - void *msgParam = 0, + Message *message, const int maxClients = 5, const int maxPendingQueueLen = 10 ) - : m_connection(host, port, msgParam) + : m_connection(host, port, message) , m_poll( &m_connection, maxClients) , m_maxPendingQueueLen(maxPendingQueueLen) { TRACE; + + message->setConnection(&m_connection); } virtual ~TcpServer() @@ -60,8 +61,8 @@ private: TcpServer(const TcpServer&); TcpServer& operator=(const TcpServer&); - Connection m_connection; - Poll m_poll; + Connection m_connection; + Poll m_poll; const int m_maxPendingQueueLen; }; diff --git a/other/tcpclient_main.cpp b/other/tcpclient_main.cpp index f25c4b2..7ef8d09 100644 --- a/other/tcpclient_main.cpp +++ b/other/tcpclient_main.cpp @@ -15,13 +15,12 @@ -class SimpleMessage : public Message +class SimpleMessage : public Message { public: - SimpleMessage( Connection *connection, - void *msgParam = 0) - : Message(connection, msgParam) + SimpleMessage( void *msgParam = 0) + : Message(msgParam) { TRACE; } @@ -45,6 +44,12 @@ public: *( static_cast(m_param) ) = true; } + Message* clone() + { + TRACE; + return new SimpleMessage(m_param); + } + protected: size_t getExpectedLength() @@ -58,7 +63,7 @@ protected: int main(int argc, char* argv[] ) { if ( argc != 4 ) { - std::cerr << "Usage: client " << std::endl; + std::cerr << "Usage: " << argv[0] << " " << std::endl; return 1; } @@ -68,7 +73,11 @@ int main(int argc, char* argv[] ) bool finished = false; - TcpClient tcpclient(argv[1], argv[2], &finished); + SimpleMessage msg(&finished); + + TcpClient tcpclient( argv[1], + argv[2], + &msg); if ( !tcpclient.connect() ) { LOG( Logger::ERR, "Couldn't connect to server, exiting..." ); diff --git a/other/tcpserver_main.cpp b/other/tcpserver_main.cpp index 2675bc5..55077ab 100644 --- a/other/tcpserver_main.cpp +++ b/other/tcpserver_main.cpp @@ -10,13 +10,12 @@ #include #include -class EchoMessage : public Message +class EchoMessage : public Message { public: - EchoMessage( Connection *connection, - void *msgParam = 0) - : Message(connection, msgParam) + EchoMessage( void *msgParam = 0) + : Message(msgParam) { TRACE; } @@ -47,6 +46,12 @@ public: m_connection->send( reply.c_str(), reply.length() ); } + Message* clone() + { + TRACE; + return new EchoMessage(m_param); + } + protected: size_t getExpectedLength() @@ -57,14 +62,23 @@ protected: }; -int main() +int main(int argc, char* argv[] ) { + if ( argc != 3 ) { + std::cerr << "Usage: " << argv[0] << " " << std::endl; + return 1; + } + Logger::createInstance(); Logger::init(std::cout); - Logger::setLogLevel(Logger::INFO); - Logger::setNoPrefix(); + Logger::setLogLevel(Logger::FINEST); +// Logger::setNoPrefix(); + + EchoMessage msg; - TcpServer tcpServer("localhost", "4455"); + TcpServer tcpServer( argv[1], + argv[2], + &msg ); if ( !tcpServer.start() ) { LOG( Logger::ERR, "Failed to start TCP server, exiting...");