You can use this generic queue implementation if you need a FIFO buffer in your code. To use it, you should #include
"queue.h"
in your .c file.
All data (message_t
units) passed to functions in
client.c and server.c is first buffered in a
message queue (aka FIFO buffer). When the code that
implements the transport layer protocols (ABP or GBN) is
ready for more input then data can be extracted from the queue one unit
at a time and passed to the routines that need it. You don't need to
touch the message queue between the upper layers or your protocol layers.
queue_t new_queue(void)
The new_queue()
function must be called to create a
empty queue for you to manipulate. It returns a pointer to the
queue structure, or NULL
if the queue could not be
created.
void destory_queue (queue_t q)
The destory_queue()
function clears the queue and
all items in the queue. After destory_queue()
function
is called all memory taken by the objects stored in the queue will
also be released. So those objects should not be used again.
void queue_init(queue_t q)
The queue_init()
function initializes a queue. It
will be called automatically in the new_queue()
function. So you need not to call it while creating a new
queue.
It should be used to reset an existing queue.
void queue_clear(queue_t q)
The queue_clear()
function clears the queue, however
none of the items in the queue are destroyed.
void* queue_top(queue_t q )
The queue_top()
function returns the object stored in
the head (first) item. The returned void*
pointer should be
cast to a proper object type pointer for use.
int queue_size(queue_t q)
The queue_size()
function returns the length of the
queue.
int queue_empty(queue_t q)
The queue_empty()
function returns 1
(true)
for an empty queue, otherwise it returns 0
(false).
void queue_push(queue_t q, void * data)
The queue_push()
function inserts a new item which
contains the specified data
object at the end
of the queue q
.
void queue_pop(queue_t q)
The queue_pop()
function removes the head (first) item
in the queue. The object stored in the head item is not released.