ircproxy  The Ultimate Cyborg

Queue Class Reference

#include <Queue.h>

Inheritance diagram for Queue:

List of all members.


Detailed Description

A queue for messages to be sent to the server.

Definition at line 33 of file Queue.h.


Public Types

typedef std::priority_queue
< MessageOut, std::deque
< MessageOut > > 
priority_queue_type
 The type of the outgoing messages queue.

Public Member Functions

 Queue (ServerConnection &server_connection, float messages_per_second, unsigned int max_free_messages, unsigned int initial_free_messages)
 Construct a Queue object.
void queue_msg (MessageOut const &message_out)
 Send or queue an output message.

Private Member Functions

void flush (void)
 Flush output messages if possible.

Private Attributes

ServerConnectionM_server_connection
 Reference to the underlying server connection.
priority_queue_type M_priority_queue
 A queue for output messages, sorted by priority, that are waiting to be sent.
float const M_messages_per_second
 The maximum average number of messages per second that can be sent to the server.
unsigned int const M_max_free_messages
 The maximum number of message that can be sent in a single burst to the server.
float M_free_messages
 The current number of messages that can be sent in a single burst.
timeval M_time
 The last time that M_free_messages was updated.

Member Typedef Documentation

typedef std::priority_queue<MessageOut, std::deque<MessageOut> > Queue::priority_queue_type

The type of the outgoing messages queue.

Definition at line 36 of file Queue.h.


Constructor & Destructor Documentation

Queue::Queue ( ServerConnection server_connection,
float  messages_per_second,
unsigned int  max_free_messages,
unsigned int  initial_free_messages 
) [inline]

Construct a Queue object.

Definition at line 48 of file Queue.h.

00048                                                                                                                                               :
00049         event_client_ct(), M_server_connection(server_connection),
00050         M_messages_per_second(messages_per_second), M_max_free_messages(max_free_messages),
00051         M_free_messages(initial_free_messages), M_time(timer_event_server_ct::instance().get_now()) { }
    ~Queue() { cancel_all_requests(); }


Member Function Documentation

void Queue::flush ( void   )  [private]

Flush output messages if possible.

Definition at line 33 of file Queue.cc.

References DoutEntering, M_free_messages, M_max_free_messages, M_messages_per_second, M_priority_queue, M_server_connection, M_time, real_nick_sent, ServerConnection::server_session(), and ServerConnection::state().

Referenced by queue_msg().

00034 {
00035   DoutEntering(dc::debug, "Queue::flush() with M_free_messages = " << M_free_messages);
00036   timeval elapsed_time = timer_event_server_ct::instance().get_now() - M_time;
00037   float elapsed_seconds = elapsed_time.tv_sec + elapsed_time.tv_usec * 0.000001;
00038   // Calculate the number of free messages that we can send in a burst now.
00039   M_free_messages += elapsed_seconds * M_messages_per_second;
00040   if (M_free_messages > M_max_free_messages)
00041     M_free_messages = M_max_free_messages;
00042   while (!M_priority_queue.empty())
00043   {
00044     MessageOut const& msg(M_priority_queue.top());
00045     // We are still not hidden! Don't send any message yet.
00046     // However, allow to send WHO/WHOIS/WHOWAS and other safe queries.
00047     if (M_server_connection.state() < real_nick_sent && !msg.is_safe())
00048       M_free_messages = 0;
00049     if (M_free_messages < 1)    // FIXME: A more complex algorithm is needed.
00050       break;
00051     // Send this message to the server.
00052     msg.write_to(M_server_connection.server_session());
00053     M_free_messages -= 1;
00054     M_priority_queue.pop();
00055   }
00056   // Store the time at which M_free_messages was last updated.
00057   M_time = timer_event_server_ct::instance().get_now();
00058   // If there are still messages in the queue, set a timer to process them.
00059   if (!M_priority_queue.empty())
00060   {
00061     // Use 1.001 instead of 1 to avoid calling the function a micro second too soon due to rounding errors.
00062     float needed_seconds = (1.001 - M_free_messages) /  M_messages_per_second;
00063     timeval delay;
00064     delay.tv_sec = floorf(needed_seconds);
00065     delay.tv_usec = floorf((needed_seconds - delay.tv_sec) * 1000000);
00066     timerRequest(delay, *this, &Queue::send_next_message);
00067   }
00068 }

void Queue::queue_msg ( MessageOut const &  message_out  )  [inline]

Send or queue an output message.

Definition at line 61 of file Queue.h.

References flush(), and M_priority_queue.

Referenced by ServerSession::queue_msg().

00061 { M_priority_queue.push(message_out); this->flush(); }


Member Data Documentation

Reference to the underlying server connection.

Definition at line 39 of file Queue.h.

Referenced by flush().

A queue for output messages, sorted by priority, that are waiting to be sent.

Definition at line 40 of file Queue.h.

Referenced by flush(), and queue_msg().

float const Queue::M_messages_per_second [private]

The maximum average number of messages per second that can be sent to the server.

Definition at line 41 of file Queue.h.

Referenced by flush().

unsigned int const Queue::M_max_free_messages [private]

The maximum number of message that can be sent in a single burst to the server.

Definition at line 42 of file Queue.h.

Referenced by flush().

float Queue::M_free_messages [private]

The current number of messages that can be sent in a single burst.

Definition at line 43 of file Queue.h.

Referenced by flush().

timeval Queue::M_time [private]

The last time that M_free_messages was updated.

Definition at line 44 of file Queue.h.

Referenced by flush().


The documentation for this class was generated from the following files:

Copyright © 2005-2007 Carlo Wood.  All rights reserved.