- implemented event_queue
- defined tests for the event_queue
This commit is contained in:
parent
db5dc98603
commit
ae758c7934
9 changed files with 584 additions and 1 deletions
|
@ -44,7 +44,7 @@ int main()
|
|||
|
||||
job_queue = job_queue_create();
|
||||
|
||||
tester_t *tester = tester_create(test_output, FALSE);
|
||||
tester_t *tester = tester_create(test_output, TRUE);
|
||||
|
||||
tester->test_all(tester,tests);
|
||||
|
||||
|
|
370
Source/charon/event_queue.c
Normal file
370
Source/charon/event_queue.c
Normal file
|
@ -0,0 +1,370 @@
|
|||
/**
|
||||
* @file event_queue.c
|
||||
*
|
||||
* @brief Event-Queue based on linked_list_t
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (C) 2005 Jan Hutter, Martin Willi
|
||||
* Hochschule fuer Technik Rapperswil
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation; either version 2 of the License, or (at your
|
||||
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* for more details.
|
||||
*/
|
||||
|
||||
#include <freeswan.h>
|
||||
#include <pluto/constants.h>
|
||||
#include <pluto/defs.h>
|
||||
#include <pthread.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
|
||||
#include "types.h"
|
||||
#include "event_queue.h"
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @brief represents an event as it is stored in the event queue
|
||||
*
|
||||
* A event consists of a event time and an assigned job object
|
||||
*
|
||||
*/
|
||||
typedef struct event_s event_t;
|
||||
|
||||
struct event_s{
|
||||
/**
|
||||
* Time to fire the event
|
||||
*/
|
||||
timeval_t time;
|
||||
|
||||
/**
|
||||
* Every event has its assigned job
|
||||
*/
|
||||
job_t * job;
|
||||
|
||||
/**
|
||||
* @brief Destroys a event_t object
|
||||
*
|
||||
* @param event_t calling object
|
||||
* @returns SUCCESS if succeeded, FAILED otherwise
|
||||
*/
|
||||
status_t (*destroy) (event_t *event);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @brief implements function destroy of event_t
|
||||
*/
|
||||
static status_t event_destroy(event_t *event)
|
||||
{
|
||||
if (event == NULL)
|
||||
{
|
||||
return FAILED;
|
||||
}
|
||||
pfree(event);
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Creates a event for a specific time
|
||||
*
|
||||
* @param time to fire the event
|
||||
* @param job job to add to job-queue at specific time
|
||||
*
|
||||
* @return event_t event object
|
||||
*/
|
||||
static event_t *event_create(timeval_t time, job_t *job)
|
||||
{
|
||||
event_t *this = alloc_thing(event_t, "event_t");
|
||||
|
||||
this->destroy = event_destroy;
|
||||
|
||||
this->time = time;
|
||||
this->job = job;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief Private Variables and Functions of event_queue class
|
||||
*
|
||||
*/
|
||||
typedef struct private_event_queue_s private_event_queue_t;
|
||||
|
||||
|
||||
struct private_event_queue_s {
|
||||
event_queue_t public;
|
||||
|
||||
/**
|
||||
* The events are stored in a linked list
|
||||
*/
|
||||
linked_list_t *list;
|
||||
|
||||
/**
|
||||
* access to linked_list is locked through this mutex
|
||||
*/
|
||||
pthread_mutex_t mutex;
|
||||
|
||||
/**
|
||||
* If the queue is empty or an event has not to be fired
|
||||
* a thread has to wait
|
||||
* This condvar is used to wake up such a thread
|
||||
*/
|
||||
pthread_cond_t condvar;
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns the difference of to timeval structs in microseconds
|
||||
*
|
||||
* @param end_time end time
|
||||
* @param start_time start time
|
||||
*
|
||||
* @warning this function is also defined in the tester class
|
||||
* In later improvements, this function can be added to a general
|
||||
* class type!
|
||||
*
|
||||
* @return difference in microseconds
|
||||
*/
|
||||
static long time_difference(struct timeval *end_time, struct timeval *start_time)
|
||||
{
|
||||
long seconds, microseconds;
|
||||
|
||||
seconds = (end_time->tv_sec - start_time->tv_sec);
|
||||
microseconds = (end_time->tv_usec - start_time->tv_usec);
|
||||
return ((seconds * 1000000) + microseconds);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief implements function get_count of event_queue_t
|
||||
*/
|
||||
static status_t get_count (private_event_queue_t *this, int *count)
|
||||
{
|
||||
pthread_mutex_lock(&(this->mutex));
|
||||
status_t status = this->list->get_count(this->list,count);
|
||||
pthread_mutex_unlock(&(this->mutex));
|
||||
return status;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief implements function get of event_queue_t
|
||||
*/
|
||||
static status_t get(private_event_queue_t *this, job_t **job)
|
||||
{
|
||||
timespec_t timeout;
|
||||
timeval_t current_time;
|
||||
event_t * next_event;
|
||||
int count;
|
||||
|
||||
pthread_mutex_lock(&(this->mutex));
|
||||
|
||||
while (1)
|
||||
{
|
||||
this->list->get_count(this->list,&count);
|
||||
while(count == 0)
|
||||
{
|
||||
pthread_cond_wait( &(this->condvar), &(this->mutex));
|
||||
this->list->get_count(this->list,&count);
|
||||
}
|
||||
|
||||
this->list->get_first(this->list,(void **) &next_event);
|
||||
gettimeofday(¤t_time,NULL);
|
||||
long difference = time_difference(¤t_time,&(next_event->time));
|
||||
if (difference <= 0)
|
||||
{
|
||||
timeout.tv_sec = next_event->time.tv_sec;
|
||||
timeout.tv_nsec = next_event->time.tv_usec * 1000;
|
||||
|
||||
pthread_cond_timedwait( &(this->condvar), &(this->mutex),&timeout);
|
||||
}
|
||||
else
|
||||
{
|
||||
/* event available */
|
||||
this->list->remove_first(this->list,(void **) &next_event);
|
||||
|
||||
*job = next_event->job;
|
||||
|
||||
next_event->destroy(next_event);
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
pthread_cond_signal( &(this->condvar));
|
||||
|
||||
pthread_mutex_unlock(&(this->mutex));
|
||||
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief implements function add of event_queue_t
|
||||
*/
|
||||
static status_t add(private_event_queue_t *this, job_t *job, timeval_t time)
|
||||
{
|
||||
event_t *event = event_create(time,job);
|
||||
linked_list_element_t * current_list_element;
|
||||
event_t *current_event;
|
||||
status_t status;
|
||||
bool has_next;
|
||||
int count;
|
||||
|
||||
if (event == NULL)
|
||||
{
|
||||
return FAILED;
|
||||
}
|
||||
pthread_mutex_lock(&(this->mutex));
|
||||
|
||||
/* while just used to break out */
|
||||
while(1)
|
||||
{
|
||||
this->list->get_count(this->list,&count);
|
||||
if (count == 0)
|
||||
{
|
||||
status = this->list->insert_first(this->list,event);
|
||||
break;
|
||||
}
|
||||
|
||||
/* check last entry */
|
||||
this->list->get_last(this->list,(void **) ¤t_event);
|
||||
|
||||
if (time_difference(&(event->time), &(current_event->time)) >= 0)
|
||||
{
|
||||
/* my event has to be fired after the last event in list */
|
||||
status = this->list->insert_last(this->list,event);
|
||||
break;
|
||||
}
|
||||
|
||||
/* check first entry */
|
||||
this->list->get_first(this->list,(void **) ¤t_event);
|
||||
|
||||
if (time_difference(&(event->time), &(current_event->time)) < 0)
|
||||
{
|
||||
/* my event has to be fired before the first event in list */
|
||||
status = this->list->insert_first(this->list,event);
|
||||
break;
|
||||
}
|
||||
|
||||
linked_list_iterator_t * iterator;
|
||||
|
||||
status = this->list->create_iterator(this->list,&iterator,TRUE);
|
||||
if (status != SUCCESS)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
status = iterator->has_next(iterator,&has_next);
|
||||
/* first element has not to be checked (already done) */
|
||||
status = iterator->has_next(iterator,&has_next);
|
||||
if (status != SUCCESS)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
while(has_next)
|
||||
{
|
||||
status = iterator->current(iterator,¤t_list_element);
|
||||
if (status != SUCCESS)
|
||||
{
|
||||
break;
|
||||
}
|
||||
current_event = (event_t *) current_list_element->value;
|
||||
|
||||
if (time_difference(&(event->time), &(current_event->time)) <= 0)
|
||||
{
|
||||
/* my event has to be fired before the current event in list */
|
||||
status = this->list->insert_before(this->list,current_list_element,event);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
iterator->has_next(iterator,&has_next);
|
||||
if (status != SUCCESS)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
pthread_cond_signal( &(this->condvar));
|
||||
pthread_mutex_unlock(&(this->mutex));
|
||||
|
||||
if (status != SUCCESS)
|
||||
{
|
||||
event->destroy(event);
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief implements function destroy of event_queue_t
|
||||
*/
|
||||
static status_t event_queue_destroy(private_event_queue_t *this)
|
||||
{
|
||||
int count;
|
||||
this->list->get_count(this->list,&count);
|
||||
while (count > 0)
|
||||
{
|
||||
event_t *event;
|
||||
|
||||
if (this->list->remove_first(this->list,(void *) &event) != SUCCESS)
|
||||
{
|
||||
this->list->destroy(this->list);
|
||||
break;
|
||||
}
|
||||
event->job->destroy(event->job);
|
||||
event->destroy(event);
|
||||
this->list->get_count(this->list,&count);
|
||||
}
|
||||
this->list->destroy(this->list);
|
||||
|
||||
pthread_mutex_destroy(&(this->mutex));
|
||||
|
||||
pthread_cond_destroy(&(this->condvar));
|
||||
|
||||
pfree(this);
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
/*
|
||||
*
|
||||
* Documented in header
|
||||
*/
|
||||
event_queue_t *event_queue_create()
|
||||
{
|
||||
linked_list_t *linked_list = linked_list_create();
|
||||
if (linked_list == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
private_event_queue_t *this = alloc_thing(private_event_queue_t, "private_event_queue_t");
|
||||
if (this == NULL)
|
||||
{
|
||||
linked_list->destroy(linked_list);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
this->public.get_count = (status_t (*) (event_queue_t *event_queue, int *count)) get_count;
|
||||
this->public.get = (status_t (*) (event_queue_t *event_queue, job_t **job)) get;
|
||||
this->public.add = (status_t (*) (event_queue_t *event_queue, job_t *job, timeval_t time)) add;
|
||||
this->public.destroy = (status_t (*) (event_queue_t *event_queue)) event_queue_destroy;
|
||||
|
||||
this->list = linked_list;
|
||||
pthread_mutex_init(&(this->mutex), NULL);
|
||||
pthread_cond_init(&(this->condvar), NULL);
|
||||
|
||||
return (&this->public);
|
||||
}
|
94
Source/charon/event_queue.h
Normal file
94
Source/charon/event_queue.h
Normal file
|
@ -0,0 +1,94 @@
|
|||
/**
|
||||
* @file event_queue.h
|
||||
*
|
||||
* @brief Event-Queue based on linked_list_t
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (C) 2005 Jan Hutter, Martin Willi
|
||||
* Hochschule fuer Technik Rapperswil
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation; either version 2 of the License, or (at your
|
||||
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* for more details.
|
||||
*/
|
||||
|
||||
#ifndef EVENT_QUEUE_H_
|
||||
#define EVENT_QUEUE_H_
|
||||
|
||||
#include <sys/time.h>
|
||||
|
||||
#include "linked_list.h"
|
||||
#include "job_queue.h"
|
||||
|
||||
/**
|
||||
* @brief Event-Queue
|
||||
*
|
||||
* Despite the event-queue is based on a linked_list_t
|
||||
* all access functions are thread-save implemented
|
||||
*/
|
||||
typedef struct event_queue_s event_queue_t;
|
||||
|
||||
struct event_queue_s {
|
||||
|
||||
/**
|
||||
* @brief returns number of events in queue
|
||||
*
|
||||
* @param event_queue calling object
|
||||
* @param[out] count integer pointer to store the event count in
|
||||
* @returns SUCCESS if succeeded, FAILED otherwise
|
||||
*/
|
||||
status_t (*get_count) (event_queue_t *event_queue, int *count);
|
||||
|
||||
/**
|
||||
* @brief get the next job from the event-queue
|
||||
*
|
||||
* If no event is pending, this function blocks until a job can be returned.
|
||||
*
|
||||
* @param event_queue calling object
|
||||
* @param[out] job pointer to a job pointer where to job is returned to
|
||||
* @returns SUCCESS if succeeded, FAILED otherwise
|
||||
*/
|
||||
status_t (*get) (event_queue_t *event_queue, job_t **job);
|
||||
|
||||
/**
|
||||
* @brief adds a event to the queue
|
||||
*
|
||||
* This function is non blocking and adds a job_t at a specific time to the list.
|
||||
* The specific job-object has to get destroyed by the thread which
|
||||
* removes the job.
|
||||
*
|
||||
* @param event_queue calling object
|
||||
* @param[in] job job to add to the queue (job is not copied)
|
||||
* @param[in] time time, when the event has to get fired
|
||||
* @returns SUCCESS if succeeded, FAILED otherwise
|
||||
*/
|
||||
status_t (*add) (event_queue_t *event_queue, job_t *job, timeval_t time);
|
||||
|
||||
/**
|
||||
* @brief destroys a event_queue object
|
||||
*
|
||||
* @warning The caller of this function has to make sure
|
||||
* that no thread is going to add or get an event from the event_queue
|
||||
* after calling this function.
|
||||
*
|
||||
* @param event_queue calling object
|
||||
* @returns SUCCESS if succeeded, FAILED otherwise
|
||||
*/
|
||||
status_t (*destroy) (event_queue_t *event_queue);
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Creates an empty event_queue
|
||||
*
|
||||
* @return empty event_queue object
|
||||
*/
|
||||
event_queue_t *event_queue_create();
|
||||
#endif /*EVENT_QUEUE_H_*/
|
67
Source/charon/tests/event_queue_test.c
Normal file
67
Source/charon/tests/event_queue_test.c
Normal file
|
@ -0,0 +1,67 @@
|
|||
/**
|
||||
* @file event_queue_test.h
|
||||
*
|
||||
* @brief Tests to test the Event-Queue type event_queue_t
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (C) 2005 Jan Hutter, Martin Willi
|
||||
* Hochschule fuer Technik Rapperswil
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation; either version 2 of the License, or (at your
|
||||
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* for more details.
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include "../tester.h"
|
||||
#include "../event_queue.h"
|
||||
|
||||
void test_event_queue(tester_t *tester)
|
||||
{
|
||||
event_queue_t * event_queue = event_queue_create();
|
||||
timeval_t current_time;
|
||||
timeval_t time1, time2, time3;
|
||||
job_t * current_job;
|
||||
int count;
|
||||
job_t * job1 = job_create(INCOMING_PACKET,"incoming packet");
|
||||
job_t * job2 = job_create(RETRANSMIT_REQUEST,"retransmit request");
|
||||
job_t * job3 = job_create(ESTABLISH_IKE_SA,"establish ike sa");
|
||||
|
||||
gettimeofday(¤t_time,NULL);
|
||||
time1.tv_usec = 0;
|
||||
time1.tv_sec = current_time.tv_sec + 3;
|
||||
time2.tv_usec = 0;
|
||||
time2.tv_sec = current_time.tv_sec + 12;
|
||||
time3.tv_usec = 0;
|
||||
time3.tv_sec = current_time.tv_sec + 12;
|
||||
|
||||
tester->assert_true(tester,(event_queue->add(event_queue,job1,time1) == SUCCESS), "add call check");
|
||||
tester->assert_true(tester,(event_queue->get_count(event_queue,&count) == SUCCESS), "get_count call check");
|
||||
tester->assert_true(tester,(count == 1), "count value check");
|
||||
|
||||
tester->assert_true(tester,(event_queue->add(event_queue,job2,time2) == SUCCESS), "add call check");
|
||||
tester->assert_true(tester,(event_queue->get_count(event_queue,&count) == SUCCESS), "get_count call check");
|
||||
tester->assert_true(tester,(count == 2), "count value check");
|
||||
|
||||
tester->assert_true(tester,(event_queue->add(event_queue,job3,time3) == SUCCESS), "add call check");
|
||||
tester->assert_true(tester,(event_queue->get_count(event_queue,&count) == SUCCESS), "get_count call check");
|
||||
tester->assert_true(tester,(count == 3), "count value check");
|
||||
|
||||
tester->assert_true(tester,(event_queue->get(event_queue,¤t_job) == SUCCESS), "get call check");
|
||||
fprintf(stderr,"%s\n",(char *) current_job->assigned_data);
|
||||
tester->assert_true(tester,(event_queue->get(event_queue,¤t_job) == SUCCESS), "get call check");
|
||||
fprintf(stderr,"%s\n",(char *) current_job->assigned_data);
|
||||
tester->assert_true(tester,(event_queue->get(event_queue,¤t_job) == SUCCESS), "get call check");
|
||||
fprintf(stderr,"%s\n",(char *) current_job->assigned_data);
|
||||
|
||||
tester->assert_true(tester,(event_queue->destroy(event_queue) == SUCCESS), "destroy call check");
|
||||
}
|
42
Source/charon/tests/event_queue_test.h
Normal file
42
Source/charon/tests/event_queue_test.h
Normal file
|
@ -0,0 +1,42 @@
|
|||
/**
|
||||
* @file event_queue_test.h
|
||||
*
|
||||
* @brief Tests to test the Event-Queue type event_queue_t
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (C) 2005 Jan Hutter, Martin Willi
|
||||
* Hochschule fuer Technik Rapperswil
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation; either version 2 of the License, or (at your
|
||||
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* for more details.
|
||||
*/
|
||||
|
||||
#ifndef EVENT_QUEUE_TEST_H_
|
||||
#define EVENT_QUEUE_TEST_H_
|
||||
|
||||
#include "../tester.h"
|
||||
|
||||
/**
|
||||
* @brief Test function used to test the event_queue functionality
|
||||
*
|
||||
* Tests are performed using one thread
|
||||
*
|
||||
* @param tester associated tester object
|
||||
*/
|
||||
void test_event_queue(tester_t *tester);
|
||||
|
||||
/**
|
||||
* Test for event_queue_t
|
||||
*/
|
||||
test_t event_queue_test = {test_event_queue,"Event-Queue Test"};
|
||||
|
||||
#endif /*EVENT_QUEUE_TEST_H_*/
|
|
@ -23,6 +23,8 @@
|
|||
#ifndef JOB_QUEUE_TEST_H_
|
||||
#define JOB_QUEUE_TEST_H_
|
||||
|
||||
#include "../tester.h"
|
||||
|
||||
/**
|
||||
* @brief Test function used to test the job_queue functionality
|
||||
*
|
||||
|
|
|
@ -23,6 +23,8 @@
|
|||
#ifndef LINKED_LIST_TEST_H_
|
||||
#define LINKED_LIST_TEST_H_
|
||||
|
||||
#include "../tester.h"
|
||||
|
||||
/**
|
||||
* @brief Test function for the type linked_list_t
|
||||
*
|
||||
|
|
|
@ -29,6 +29,7 @@
|
|||
#include "linked_list_test.h"
|
||||
#include "thread_pool_test.h"
|
||||
#include "job_queue_test.h"
|
||||
#include "event_queue_test.h"
|
||||
|
||||
|
||||
/**
|
||||
|
@ -40,6 +41,7 @@ test_t *tests[] ={
|
|||
&linked_list_insert_and_remove_test,
|
||||
&thread_pool_test,
|
||||
&job_queue_test1,
|
||||
&event_queue_test,
|
||||
NULL
|
||||
};
|
||||
|
||||
|
|
|
@ -31,4 +31,8 @@ typedef enum status_e {
|
|||
ALREADY_DONE
|
||||
} status_t;
|
||||
|
||||
typedef struct timeval timeval_t;
|
||||
|
||||
typedef struct timespec timespec_t;
|
||||
|
||||
#endif /*TYPES_H_*/
|
||||
|
|
Loading…
Reference in a new issue