diff --git a/include/Connection.hpp b/include/Connection.hpp index 22d9952..91022cf 100644 --- a/include/Connection.hpp +++ b/include/Connection.hpp @@ -110,7 +110,8 @@ public: } if (len == 0) { - LOG( Logger::DEBUG, "Connection closed by peer." ); + LOG( Logger::INFO, std::string("Connection closed by "). + append(m_host).append(":").append(m_port).c_str() ); return false; } diff --git a/include/Logger.hpp b/include/Logger.hpp index 77fd53f..21b47e6 100644 --- a/include/Logger.hpp +++ b/include/Logger.hpp @@ -31,6 +31,8 @@ public: static void init(std::ostream& log_stream ); static void setLogLevel ( const LogLevel loglevel ); + static void setNoPrefix (); + inline static LogLevel getLoglevel() { return m_logLevel; } static void log_pointer( const void* msg, @@ -54,6 +56,7 @@ private: static LogLevel m_logLevel; static std::ostream *m_ostream; + static bool m_usePrefix; }; diff --git a/include/Poll.hpp b/include/Poll.hpp index 8a0ed55..49e0f87 100644 --- a/include/Poll.hpp +++ b/include/Poll.hpp @@ -90,7 +90,7 @@ protected: Connection *connection = new Connection(client_socket); - LOG( Logger::DEBUG, std::string("New client connected: "). + LOG( Logger::INFO, std::string("New client connected: "). append(connection->getHost()).append(":"). append(connection->getPort()).c_str() ); diff --git a/other/client_caller.sh b/other/client_caller.sh new file mode 100644 index 0000000..1ff9c90 --- /dev/null +++ b/other/client_caller.sh @@ -0,0 +1,18 @@ +#!/bin/bash + +./client 127.0.0.1 4455 nekem & +./client 127.0.0.1 4455 nem & +./client 127.0.0.1 4455 hasznal & +./client 127.0.0.1 4455 a & +./client 127.0.0.1 4455 szo & +./client 127.0.0.1 4455 en & +./client 127.0.0.1 4455 nem & +./client 127.0.0.1 4455 tudom & +./client 127.0.0.1 4455 mi & +./client 127.0.0.1 4455 a & +./client 127.0.0.1 4455 jo & + +sleep 2 +./client 127.0.0.1 4455 "en hiaba megyek fejjel" & +./client 127.0.0.1 4455 "a falnak, nekem nem hasznal" & +./client 127.0.0.1 4455 "nem hasznal a szo" & diff --git a/other/tcpclient_main.cpp b/other/tcpclient_main.cpp index 86106b6..184ff27 100644 --- a/other/tcpclient_main.cpp +++ b/other/tcpclient_main.cpp @@ -10,6 +10,10 @@ #include #include +#include // nanosleep + + + class SimpleMessage : public Message { @@ -37,6 +41,8 @@ public: LOG( Logger::INFO, std::string("Got reply from server: "). append(m_buffer).c_str() ); + + *( static_cast(m_param) ) = true; } protected: @@ -49,32 +55,34 @@ protected: }; -int main() +int main(int argc, char* argv[] ) { + if ( argc != 4 ) { + std::cerr << "Usage: client " << std::endl; + return 1; + } + Logger::createInstance(); Logger::init(std::cout); Logger::setLogLevel(Logger::DEBUG); - int *a = new int; - *a=2; + bool finished = false; - TcpClient tcpclient("127.0.0.1", "4455"); + TcpClient tcpclient(argv[1], argv[2], &finished); tcpclient.connect(); + sleep(1); // wait for thread creation - sleep(2); - std::string msg1("madao"); + std::string msg1(argv[3]); tcpclient.send( msg1.c_str(), msg1.length()); - sleep(2); -// std::string msg2("this message is long. Cannot fit into one buffer"); -// tcpclient.send( msg2.c_str(), msg2.length()); -// sleep(2); + // wait for the complate &handled reply + struct timespec tm = {0,1000}; + while ( !finished ) + nanosleep(&tm, &tm) ; tcpclient.disconnect(); - delete a; - Logger::destroy(); return 0; } \ No newline at end of file diff --git a/other/tcpserver_main.cpp b/other/tcpserver_main.cpp index 362deb8..653a6b6 100644 --- a/other/tcpserver_main.cpp +++ b/other/tcpserver_main.cpp @@ -36,11 +36,13 @@ public: LOG( Logger::INFO, std::string("Got message: \""). append(m_buffer).append("\" from: "). - append(m_connection->getHost()).c_str() ); + append(m_connection->getHost().append(":"). + append(m_connection->getPort()) ).c_str() ); std::string reply("Got your message, "); - reply.append(m_connection->getHost()). - append(" \"").append(m_buffer).append("\""); + reply.append(m_connection->getHost()).append(":"). + append(m_connection->getPort()). + append(" \"").append(m_buffer).append("\""); m_connection->send( reply.c_str(), reply.length() ); } @@ -59,7 +61,8 @@ int main() { Logger::createInstance(); Logger::init(std::cout); - Logger::setLogLevel(Logger::FINEST); + Logger::setLogLevel(Logger::INFO); + Logger::setNoPrefix(); TcpServer tcpServer("localhost", "4455"); diff --git a/src/Logger.cpp b/src/Logger.cpp index 23b805f..0877094 100644 --- a/src/Logger.cpp +++ b/src/Logger.cpp @@ -18,12 +18,22 @@ void Logger::setLogLevel ( const LogLevel loglevel ) } +void Logger::setNoPrefix () +{ + m_usePrefix = false; +} + void Logger::log_pointer( const void* msg, const char* file, const int line, const char* function) { + if ( !m_usePrefix ) { + *m_ostream << msg << std::endl; + return; + } + *m_ostream << getTime() << " " << COLOR( FG_GREEN ) << extractFilename(file) << COLOR_RESET << ":" @@ -40,6 +50,11 @@ void Logger::log_string( const int level, const int line, const char* function) { + if ( !m_usePrefix ) { + *m_ostream << msg << std::endl; + return; + } + const char *color; if ( level <= WARNING ) { color = COLOR_F_FG( F_BOLD, FG_RED ); } else if ( level <= INFO ) { color = COLOR_F_FG( F_BOLD, FG_WHITE); } @@ -62,3 +77,4 @@ void Logger::msg(const char* text) Logger::LogLevel Logger::m_logLevel = Logger::FINEST; std::ostream* Logger::m_ostream = 0; +bool Logger::m_usePrefix = true; diff --git a/src/Socket.cpp b/src/Socket.cpp index 048cacd..4fdfa7f 100644 --- a/src/Socket.cpp +++ b/src/Socket.cpp @@ -146,7 +146,7 @@ bool Socket::connectToFirstAddress(struct addrinfo *servinfo) if (::connect(m_socket, it->ai_addr, it->ai_addrlen) != -1) { std::string address, service; if ( convertNameInfo( it->ai_addr, it->ai_addrlen, address, service) ) { - LOG( Logger::DEBUG, std::string("Connected to "). + LOG( Logger::INFO, std::string("Connected to "). append(address).append(":"). append(service).c_str() ); } @@ -169,7 +169,7 @@ bool Socket::bindToFirstAddress(struct addrinfo *servinfo ) std::string address, service; if ( Socket::convertNameInfo( &m_addr, m_addrLen, address, service) ) { - LOG( Logger::DEBUG, std::string("Binded to "). + LOG( Logger::INFO, std::string("Binded to "). append(address).append(":"). append(service).c_str() ); } diff --git a/src/Thread.cpp b/src/Thread.cpp index f4c6f75..18e8300 100644 --- a/src/Thread.cpp +++ b/src/Thread.cpp @@ -1,11 +1,9 @@ #include "Thread.hpp" -#include "Common.hpp" -#include -#include -#include // std::runtime_error +#include "Logger.hpp" + +#include // pthread_kill -#include // sched_param Thread::Thread()