src/irc_queue.h
author Tero Marttila <terom@fixme.fi>
Thu, 28 May 2009 01:17:36 +0300
branchnew-lib-errors
changeset 219 cefec18b8268
parent 217 7728d6ec3abf
permissions -rw-r--r--
some of the lib/transport stuff compiles
90
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     1
#ifndef IRC_QUEUE_H
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     2
#define IRC_QUEUE_H
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     3
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     4
/**
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     5
 * @file
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     6
 *
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     7
 * Ratelimited queue of outgoing irc_line's for use with irc_conn.
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     8
 *
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     9
 * This implements the basic flood control algorithm as described in RFC1459 section 8.10.
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    10
 */
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    11
#include "irc_line.h"
219
cefec18b8268 some of the lib/transport stuff compiles
Tero Marttila <terom@fixme.fi>
parents: 217
diff changeset
    12
#include <lib/line_proto.h>
cefec18b8268 some of the lib/transport stuff compiles
Tero Marttila <terom@fixme.fi>
parents: 217
diff changeset
    13
#include <lib/error.h>
90
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    14
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    15
#include <sys/queue.h>
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    16
#include <event2/event.h>
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    17
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    18
/**
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    19
 * Number of seconds of penalty applied for each message
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    20
 */
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    21
#define IRC_QUEUE_PENALTY 2
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    22
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    23
/**
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    24
 * Maximum allowed burst, in seconds
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    25
 */
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    26
#define IRC_QUEUE_WINDOW 10
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    27
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    28
/**
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    29
 * An enqueued irc_line for later delivery
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    30
 */
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    31
struct irc_queue_entry {
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    32
    /** The formatted irc_line data, including terminating CRLF */
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    33
    char line_buf[IRC_LINE_MAX + 2];
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    34
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    35
    /** Our entry in the irc_queue list */
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    36
    TAILQ_ENTRY(irc_queue_entry) queue_list;
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    37
};
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    38
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    39
/**
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    40
 * The queue state and timing algorithm, as described in the RFC:
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    41
 *  * A line will be sent directly if the current timer is less than IRC_QUEUE_WINDOW seconds into the future, and
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    42
 *    IRC_QUEUE_PENALTY seconds are added on to the timer.
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    43
 *  * Otherwise, the line is stored as a irc_queue_entry and enqueued for later transmission, once the timer value
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    44
 *    drops below IRC_QUEUE_WINDOW seconds into the future.
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    45
 *  * The above is repeated for each dequeued line.
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    46
 *
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    47
 * Additionally, if sending a line fails due to the line_proto socket buffer being full, this also handles this case
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    48
 * by queueing the line for later sending.
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    49
 *
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    50
 * Note that the timing behaviour of this is rather unexact - the socket layer may introduce its own delays/jitter which
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    51
 * we can't measure or control here, but at least we make an effort. This should work OK as long as the outgoing lines
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    52
 * don't accumulate in the socket's write buffer too much. XXX: maybe tweak socket params to try and "disable" 
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    53
 * buffering on our end to improve accuracy?
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    54
 */
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    55
struct irc_queue {
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    56
    /** The line_proto that we can send the messages out on */
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    57
    struct line_proto *lp;
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    58
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    59
    /** Our timeout used for delaying messages */
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    60
    struct event *ev;
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    61
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    62
    /** Current "message timer" value, may be up to IRC_QUEUE_WINDOW seconds in the future */
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    63
    time_t timer;
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    64
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    65
    /** The actual queue of messages */
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    66
    TAILQ_HEAD(irc_queue_entry_list, irc_queue_entry) list;
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    67
};
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    68
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    69
/**
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    70
 * Create a new irc_queue for use with the given line_proto
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    71
 */
217
7728d6ec3abf nexus.c compiles
Tero Marttila <terom@fixme.fi>
parents: 155
diff changeset
    72
err_t irc_queue_create (struct irc_queue **queue_ptr, struct event_base *ev_base, struct line_proto *lp, error_t *err);
90
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    73
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    74
/**
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    75
 * Process a line, either sending it directly, or enqueueing it, based on the timer state.
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    76
 */
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    77
err_t irc_queue_process (struct irc_queue *queue, const struct irc_line *line);
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    78
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    79
/**
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    80
 * Destroy the irc_queue, releasing all queued lines
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    81
 */
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    82
void irc_queue_destroy (struct irc_queue *queue);
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    83
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    84
#endif /* IRC_QUEUE_H */