Playing with connection through ana, stats gathering is bugged.

This commit is contained in:
Guillermo Biset 2010-07-01 21:07:07 +00:00
parent c415483459
commit 7fb4dff412
5 changed files with 58 additions and 46 deletions

View File

@ -58,9 +58,9 @@ namespace ana
virtual size_t bytes_in() const = 0;
virtual size_t bytes_out() const = 0;
};
namespace detail
{
{
class stats_logger : public stats
{
public:
@ -82,13 +82,13 @@ namespace ana
++packets_out_;
bytes_out_ += buffer->size() + HEADER_LENGTH;
}
void log_receive( detail::read_buffer buffer )
{
++packets_in_;
bytes_in_ += buffer->size() + HEADER_LENGTH;
}
private:
void reset(boost::system::error_code& /*ec*/)
{
@ -96,26 +96,26 @@ namespace ana
packets_out_ = 0;
bytes_in_ = 0;
bytes_out_ = 0;
if (ms_to_reset_ > 0 )
timer_.wait(ms_to_reset_, boost::bind( &stats_logger::reset, this, boost::asio::placeholders::error ) );
}
virtual size_t uptime() const
{
return 0;
}
virtual size_t packets_in() const
{
return packets_in_;
}
virtual size_t packets_out() const
{
return packets_out_;
}
virtual size_t bytes_in() const
{
return bytes_in_;
@ -125,21 +125,20 @@ namespace ana
{
return bytes_out_;
}
size_t ms_to_reset_;
timer timer_;
std::time_t start_time_;
size_t packets_in_;
size_t packets_out_;
size_t bytes_in_;
size_t bytes_out_;
};
}
class stats_collector
{
public:

View File

@ -31,6 +31,7 @@
*/
#include "buffers.hpp"
#include <boost/thread.hpp>
#ifndef ANA_TIMERS_HPP
#define ANA_TIMERS_HPP
@ -123,6 +124,7 @@ namespace ana
public:
/** Standard constructor. */
timer() :
mutex_(),
io_service_(),
timer_(io_service_)
{
@ -156,27 +158,35 @@ namespace ana
/** Cancel the timer if running. */
void cancel()
{
timer_.cancel();
if ( ! mutex_.try_lock() ) // handler wasn't called, unlock to delete this
{
timer_.cancel(); // it's a cancel, handler wasn't called
//wait for running thread to finish
mutex_.lock();
}
mutex_.unlock();
}
/** Standard destructor, cancels pending operations and stops the I/O service. */
/** Standard destructor, cancels pending operations if handler wasn't called. */
~timer()
{
timer_.cancel();
io_service_.stop();
if ( ! mutex_.try_lock() ) // handler wasn't called, unlock to delete this
{
timer_.cancel(); // it's a cancel, handler wasn't called
//wait for running thread to finish
mutex_.lock();
}
mutex_.unlock();
}
private:
void run()
{
try
{
io_service_.run_one();
}
catch(const std::exception& e)
{
// Timer was cancelled. Don't propagate exception
}
mutex_.lock();
io_service_.run_one();
mutex_.unlock();
}
/** Private class providing traits for the timer type. */
@ -224,6 +234,8 @@ namespace ana
}
};
boost::mutex mutex_;
boost::asio::io_service io_service_;
boost::asio::basic_deadline_timer<std::time_t,time_t_traits> timer_;

View File

@ -114,11 +114,11 @@ class ChatClient : public ana::listener_handler,
else if (msg[1] == 's')
{
const ana::stats* acum_stats = client_->get_stats( ana::ACCUMULATED );
const ana::stats* sec_stats = client_->get_stats( ana::SECONDS );
const ana::stats* min_stats = client_->get_stats( ana::MINUTES );
const ana::stats* sec_stats = client_->get_stats( ana::SECONDS );
const ana::stats* min_stats = client_->get_stats( ana::MINUTES );
const ana::stats* hour_stats = client_->get_stats( ana::HOURS );
const ana::stats* day_stats = client_->get_stats( ana::DAYS );
const ana::stats* day_stats = client_->get_stats( ana::DAYS );
std::cout << "Network Statistics:\n"
<< "\tPackets Out:\n"
<< "\t\tTotal: " << acum_stats->packets_out() << std::endl
@ -144,7 +144,7 @@ class ChatClient : public ana::listener_handler,
<< "\t\tLast Minute: " << min_stats->bytes_in() << std::endl
<< "\t\tLast Hour: " << hour_stats->bytes_in() << std::endl
<< "\t\tLast Day: " << day_stats->bytes_in() << std::endl;
}
}
}
void run_input()
@ -182,7 +182,7 @@ class ChatClient : public ana::listener_handler,
client_->set_listener_handler( this );
client_->run();
client_->start_logging();
// client_->start_logging();
std::cout << "Available commands: \n" <<
" '/quit' : Quit. \n"

View File

@ -58,7 +58,7 @@ class ChatServer : public listener_handler,
server_->run(pt);
server_->set_timeouts(ana::FixedTime, ana::time::milliseconds(1));
// server_->set_timeouts(ana::FixedTime, ana::time::milliseconds(1));
std::cout << "Server running, Enter to quit." << std::endl;

View File

@ -154,6 +154,7 @@ class ana_connect_handler : public ana::connection_handler
virtual void handle_connect(ana::error_code error_code, ana::net_id /*client*/)
{
connected_ = true;
timer_->cancel();
if (! error_code)
std::cout << "DEBUG: Connected.\n";
@ -177,19 +178,21 @@ class ana_component : public send_stats_logger
ana_component( ) :
base_( ana::server::create() ),
is_server_( true ),
id_( server()->id() ),
send_stats_(),
receive_stats_()
{
server()->start_logging();
// server()->start_logging();
}
ana_component( const std::string& host, const std::string& port) :
base_( ana::client::create(host,port) ),
is_server_( false ),
id_( client()->id() ),
send_stats_(),
receive_stats_()
{
client()->start_logging();
// client()->start_logging();
}
network::statistics get_send_stats() const
@ -230,12 +233,15 @@ class ana_component : public send_stats_logger
ana::net_id get_id() const
{
return listener()->id();
return id_;
}
const ana::stats* get_stats() const
{
return listener()->get_stats();
if ( is_server_)
return server()->get_stats();
else
return client()->get_stats();
}
void update_receive_stats( size_t buffer_size )
@ -246,21 +252,17 @@ class ana_component : public send_stats_logger
}
private:
virtual void update_send_stats( size_t buffer_size)
{
send_stats_.current_max = ( buffer_size > send_stats_.current_max) ? buffer_size : send_stats_.current_max;
send_stats_.total += buffer_size;
send_stats_.current = buffer_size;
}
const ana::detail::listener* listener() const
{
return boost::get<ana::detail::listener*>(base_);
}
boost::variant<ana::server*, ana::client*> base_;
bool is_server_;
bool is_server_;
ana::net_id id_;
network::statistics send_stats_;
network::statistics receive_stats_;
@ -318,7 +320,6 @@ class ana_network_manager : public ana::listener_handler
server->set_connection_handler( manager );
server->set_listener_handler( this );
server->start_logging();
return server->id();
}
@ -351,7 +352,7 @@ class ana_network_manager : public ana::listener_handler
client->set_listener_handler( this );
client->run();
client->start_logging();
// client->start_logging();
mutex.lock(); // just wait for handler to release it
mutex.unlock(); // unlock for destruction