- sender_t written, but not tested!

This commit is contained in:
Jan Hutter 2005-11-07 12:07:56 +00:00
parent 3a093c8018
commit da6f775610
3 changed files with 198 additions and 4 deletions

117
Source/charon/sender.c Normal file
View file

@ -0,0 +1,117 @@
/**
* @file sender.d
*
* @brief Implements the Sender Thread encapsulated in the sender_t-object
*
*/
/*
* 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 <stdlib.h>
#include <pthread.h>
#include <freeswan.h>
#include <pluto/constants.h>
#include <pluto/defs.h>
#include "sender.h"
#include "socket.h"
#include "packet.h"
#include "send_queue.h"
extern send_queue_t *global_send_queue;
extern socket_t *global_socket;
/**
* Private data of a sender object
*/
typedef struct private_sender_s private_sender_t;
struct private_sender_s {
/**
* Public part of a sender object
*/
sender_t public;
/**
* Assigned thread to the sender_t-object
*/
pthread_t assigned_thread;
};
/**
* Thread function started at creation of the sender object
*
* @param this assigned sender object
* @return SUCCESS if thread_function ended successfully, FAILED otherwise
*/
static void sender_thread_function(private_sender_t * this)
{
/* cancellation disabled by default */
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
packet_t * current_packet;
while (1)
{
while (global_send_queue->get(global_send_queue,&current_packet) == SUCCESS)
{
if ( global_socket->send(global_socket,current_packet) == SUCCESS)
{
current_packet->destroy(current_packet);
}
else
{
/* Packet could not be sent */
/* TODO LOG it */
}
}
/* NOT GOOD !!!!!! */
/* TODO LOG it */
}
}
/**
* Implementation of sender_t's destroy function
*/
static status_t destroy(private_sender_t *this)
{
pthread_cancel(this->assigned_thread);
pthread_join(this->assigned_thread, NULL);
pfree(this);
return SUCCESS;
}
sender_t * sender_create()
{
private_sender_t *this = alloc_thing(private_sender_t,"private_sender_t");
this->public.destroy = (status_t(*)(sender_t*)) destroy;
if (pthread_create(&(this->assigned_thread), NULL, (void*(*)(void*))sender_thread_function, this) != 0)
{
/* thread could not be created */
pfree(this);
return NULL;
}
return &(this->public);
}

47
Source/charon/sender.h Normal file
View file

@ -0,0 +1,47 @@
/**
* @file sender.h
*
* @brief Implements the Sender Thread encapsulated in the sender_t-object
*
*/
/*
* 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 SENDER_H_
#define SENDER_H_
#include "types.h"
/**
* @brief A Sender object which sends packets on the socket
*/
typedef struct sender_s sender_t;
struct sender_s {
/**
* @brief Destroys a sender object
*
* @param sender sender object
* @return SUCCESSFUL if succeeded, FAILED otherwise
*/
status_t (*destroy) (sender_t *sender);
};
sender_t * sender_create();
#endif /*SENDER_H_*/

View file

@ -25,6 +25,9 @@
#
#include "tester.h"
#include "job_queue.h"
#include "event_queue.h"
#include "send_queue.h"
#include "socket.h"
#include "tests/linked_list_test.h"
#include "tests/thread_pool_test.h"
#include "tests/job_queue_test.h"
@ -36,7 +39,6 @@
/* output for test messages */
extern FILE * stderr;
/**
* Test for linked_list_t
*/
@ -77,8 +79,27 @@ test_t socket_test = {test_socket,"Socket"};
*/
test_t thread_pool_test = {test_thread_pool,"Thread Pool"};
job_queue_t *job_queue;
/**
* Global job-queue
*/
job_queue_t *global_job_queue;
/**
* Global event-queue
*/
event_queue_t *global_event_queue;
/**
* Global send-queue
*/
send_queue_t *global_send_queue;
/**
* Global socket
*/
socket_t *global_socket;
int main()
{
FILE * test_output = stderr;
@ -95,16 +116,25 @@ job_queue_t *job_queue;
NULL
};
job_queue = job_queue_create();
global_socket = socket_create(4600);
global_job_queue = job_queue_create();
global_event_queue = event_queue_create();
global_send_queue = send_queue_create();
tester_t *tester = tester_create(test_output, FALSE);
tester->perform_tests(tester,all_tests);
// tester->perform_test(tester,&event_queue_test);
tester->destroy(tester);
/* Destroy all queues */
global_job_queue->destroy(global_job_queue);
global_event_queue->destroy(global_event_queue);
global_send_queue->destroy(global_send_queue);
job_queue->destroy(job_queue);
global_socket->destroy(global_socket);
#ifdef LEAK_DETECTIVE
/* Leaks are reported in log file */