2005-11-07 13:20:11 +00:00
|
|
|
/**
|
|
|
|
* @file receiver.c
|
2005-11-09 09:11:06 +00:00
|
|
|
*
|
2005-11-25 13:42:58 +00:00
|
|
|
* @brief Implementation of receiver_t.
|
2005-11-09 09:11:06 +00:00
|
|
|
*
|
2005-11-07 13:20:11 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2006-07-07 08:49:06 +00:00
|
|
|
* Copyright (C) 2005-2006 Martin Willi
|
|
|
|
* Copyright (C) 2005 Jan Hutter
|
2005-11-07 13:20:11 +00:00
|
|
|
* 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.
|
|
|
|
*/
|
2005-11-09 09:11:06 +00:00
|
|
|
|
2005-11-07 13:20:11 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
|
|
|
|
#include "receiver.h"
|
2005-11-11 13:31:52 +00:00
|
|
|
|
2005-11-29 10:25:07 +00:00
|
|
|
#include <daemon.h>
|
2005-11-23 09:43:14 +00:00
|
|
|
#include <network/socket.h>
|
|
|
|
#include <network/packet.h>
|
2005-11-23 09:24:35 +00:00
|
|
|
#include <queues/job_queue.h>
|
2005-11-23 09:34:31 +00:00
|
|
|
#include <queues/jobs/job.h>
|
2005-11-24 16:22:04 +00:00
|
|
|
#include <queues/jobs/incoming_packet_job.h>
|
2005-11-07 13:20:11 +00:00
|
|
|
|
2005-12-06 16:36:42 +00:00
|
|
|
|
2005-11-24 11:30:19 +00:00
|
|
|
typedef struct private_receiver_t private_receiver_t;
|
|
|
|
|
2005-11-07 13:20:11 +00:00
|
|
|
/**
|
2005-12-06 16:36:42 +00:00
|
|
|
* Private data of a receiver_t object.
|
2005-11-07 13:20:11 +00:00
|
|
|
*/
|
2005-11-24 11:30:19 +00:00
|
|
|
struct private_receiver_t {
|
2005-11-07 13:20:11 +00:00
|
|
|
/**
|
2005-12-06 16:36:42 +00:00
|
|
|
* Public part of a receiver_t object.
|
2005-11-07 13:20:11 +00:00
|
|
|
*/
|
|
|
|
receiver_t public;
|
2005-11-09 09:11:06 +00:00
|
|
|
|
2005-11-07 13:20:11 +00:00
|
|
|
/**
|
2005-12-06 16:36:42 +00:00
|
|
|
* Assigned thread.
|
2005-11-07 13:20:11 +00:00
|
|
|
*/
|
|
|
|
pthread_t assigned_thread;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2005-12-06 16:36:42 +00:00
|
|
|
* Implementation of receiver_t.receive_packets.
|
2005-11-07 13:20:11 +00:00
|
|
|
*/
|
2005-11-25 13:42:58 +00:00
|
|
|
static void receive_packets(private_receiver_t * this)
|
2005-11-07 13:20:11 +00:00
|
|
|
{
|
|
|
|
packet_t * current_packet;
|
2005-11-16 14:44:25 +00:00
|
|
|
job_t *current_job;
|
2005-11-26 15:44:40 +00:00
|
|
|
|
2005-11-21 11:45:56 +00:00
|
|
|
/* cancellation disabled by default */
|
|
|
|
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
|
2005-11-26 15:44:40 +00:00
|
|
|
|
2006-10-18 11:46:13 +00:00
|
|
|
DBG1(SIG_DBG_NET, "receiver thread running, thread_ID: %06u",
|
|
|
|
(int)pthread_self());
|
2005-11-26 15:44:40 +00:00
|
|
|
|
2006-10-18 11:46:13 +00:00
|
|
|
while (TRUE)
|
2005-11-07 13:20:11 +00:00
|
|
|
{
|
2005-11-29 10:25:07 +00:00
|
|
|
while (charon->socket->receive(charon->socket,¤t_packet) == SUCCESS)
|
2005-11-07 13:20:11 +00:00
|
|
|
{
|
2006-10-18 11:46:13 +00:00
|
|
|
DBG2(SIG_DBG_NET, "creating job from packet");
|
2005-11-16 14:44:25 +00:00
|
|
|
current_job = (job_t *) incoming_packet_job_create(current_packet);
|
2006-10-18 11:46:13 +00:00
|
|
|
|
2005-11-29 10:25:07 +00:00
|
|
|
charon->job_queue->add(charon->job_queue,current_job);
|
2005-11-07 13:20:11 +00:00
|
|
|
}
|
2006-10-18 11:46:13 +00:00
|
|
|
/* bad bad, TODO: rebuild the socket ? */
|
|
|
|
DBG1(SIG_DBG_NET, "receiving from socket failed!");
|
2005-11-07 13:20:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2005-12-06 16:36:42 +00:00
|
|
|
* Implementation of receiver_t.destroy.
|
2005-11-07 13:20:11 +00:00
|
|
|
*/
|
2005-11-28 20:29:47 +00:00
|
|
|
static void destroy(private_receiver_t *this)
|
2005-11-07 13:20:11 +00:00
|
|
|
{
|
|
|
|
pthread_cancel(this->assigned_thread);
|
|
|
|
pthread_join(this->assigned_thread, NULL);
|
2006-04-10 08:07:38 +00:00
|
|
|
free(this);
|
2005-11-07 13:20:11 +00:00
|
|
|
}
|
|
|
|
|
2005-11-25 13:42:58 +00:00
|
|
|
/*
|
2005-12-06 16:36:42 +00:00
|
|
|
* Described in header.
|
2005-11-25 13:42:58 +00:00
|
|
|
*/
|
2005-11-07 13:20:11 +00:00
|
|
|
receiver_t * receiver_create()
|
|
|
|
{
|
2006-04-10 08:07:38 +00:00
|
|
|
private_receiver_t *this = malloc_thing(private_receiver_t);
|
2005-11-09 09:11:06 +00:00
|
|
|
|
2005-11-28 20:29:47 +00:00
|
|
|
this->public.destroy = (void(*)(receiver_t*)) destroy;
|
2005-11-21 11:45:56 +00:00
|
|
|
|
2006-07-10 14:24:04 +00:00
|
|
|
if (pthread_create(&(this->assigned_thread), NULL, (void*(*)(void*))receive_packets, this) != 0)
|
2005-11-07 13:20:11 +00:00
|
|
|
{
|
2006-04-10 08:07:38 +00:00
|
|
|
free(this);
|
2006-10-18 11:46:13 +00:00
|
|
|
charon->kill(charon, "unable to create receiver thread");
|
2005-11-07 13:20:11 +00:00
|
|
|
}
|
2005-11-09 09:11:06 +00:00
|
|
|
|
2005-11-07 13:20:11 +00:00
|
|
|
return &(this->public);
|
|
|
|
}
|