tcpclient_main.cpp waits for reply, log fixes

master
Denes Matetelki 13 years ago
parent d3a28084af
commit 60d6757975

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

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

@ -90,7 +90,7 @@ protected:
Connection<T> *connection = new Connection<T>(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() );

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

@ -10,6 +10,10 @@
#include <iostream>
#include <string>
#include <time.h> // nanosleep
class SimpleMessage : public Message<SimpleMessage>
{
@ -37,6 +41,8 @@ public:
LOG( Logger::INFO, std::string("Got reply from server: ").
append(m_buffer).c_str() );
*( static_cast<bool*>(m_param) ) = true;
}
protected:
@ -49,32 +55,34 @@ protected:
};
int main()
int main(int argc, char* argv[] )
{
if ( argc != 4 ) {
std::cerr << "Usage: client <HOST> <PORT> <MSG>" << std::endl;
return 1;
}
Logger::createInstance();
Logger::init(std::cout);
Logger::setLogLevel(Logger::DEBUG);
int *a = new int;
*a=2;
bool finished = false;
TcpClient<SimpleMessage> tcpclient("127.0.0.1", "4455");
TcpClient<SimpleMessage> 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;
}

@ -36,10 +36,12 @@ 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()).
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<EchoMessage> tcpServer("localhost", "4455");

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

@ -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() );
}

@ -1,11 +1,9 @@
#include "Thread.hpp"
#include "Common.hpp"
#include <signal.h>
#include <iostream>
#include <exception> // std::runtime_error
#include "Logger.hpp"
#include <signal.h> // pthread_kill
#include <sched.h> // sched_param
Thread::Thread()

Loading…
Cancel
Save