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
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
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-23 09:24:35 +00:00
|
|
|
#include <utils/logger_manager.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-25 13:42:58 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Thread function started at creation of the receiver object.
|
|
|
|
*
|
2005-12-06 16:36:42 +00:00
|
|
|
* @param this calling object
|
2005-11-25 13:42:58 +00:00
|
|
|
*/
|
|
|
|
void (*receive_packets) (private_receiver_t *this);
|
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
|
|
|
|
2005-11-21 11:45:56 +00:00
|
|
|
/**
|
2005-12-06 16:36:42 +00:00
|
|
|
* A logger for the receiver_t object.
|
2005-11-21 11:45:56 +00:00
|
|
|
*/
|
|
|
|
logger_t *logger;
|
2005-11-07 13:20:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
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-07-04 13:30:49 +00:00
|
|
|
this->logger->log(this->logger, CONTROL, "receiver thread running, thread_ID: %06u", (int)pthread_self());
|
2005-11-26 15:44:40 +00:00
|
|
|
|
2005-11-07 13:20:11 +00:00
|
|
|
while (1)
|
|
|
|
{
|
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
|
|
|
{
|
2005-12-07 07:57:18 +00:00
|
|
|
this->logger->log(this->logger, CONTROL | LEVEL1, "Creating job from packet");
|
2005-11-16 14:44:25 +00:00
|
|
|
current_job = (job_t *) incoming_packet_job_create(current_packet);
|
2005-11-07 13:20:11 +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
|
|
|
|
|
|
|
}
|
2005-11-21 11:45:56 +00:00
|
|
|
/* bad bad, rebuild the socket ? */
|
2005-12-06 16:36:42 +00:00
|
|
|
this->logger->log(this->logger, ERROR, "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
|
|
|
{
|
2005-12-07 07:57:18 +00:00
|
|
|
this->logger->log(this->logger, CONTROL | LEVEL1, "Going to terminate receiver thread");
|
2005-11-07 13:20:11 +00:00
|
|
|
pthread_cancel(this->assigned_thread);
|
2005-11-09 09:11:06 +00:00
|
|
|
|
2005-11-07 13:20:11 +00:00
|
|
|
pthread_join(this->assigned_thread, NULL);
|
2005-12-07 07:57:18 +00:00
|
|
|
this->logger->log(this->logger, CONTROL | LEVEL1, "Receiver thread terminated");
|
2005-11-07 13:20:11 +00:00
|
|
|
|
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-25 13:42:58 +00:00
|
|
|
this->receive_packets = receive_packets;
|
2005-11-21 11:45:56 +00:00
|
|
|
|
2006-04-10 08:07:38 +00:00
|
|
|
this->logger = logger_manager->get_logger(logger_manager, RECEIVER);
|
2005-11-21 11:45:56 +00:00
|
|
|
|
2005-11-25 13:42:58 +00:00
|
|
|
if (pthread_create(&(this->assigned_thread), NULL, (void*(*)(void*))this->receive_packets, this) != 0)
|
2005-11-07 13:20:11 +00:00
|
|
|
{
|
2005-11-25 13:42:58 +00:00
|
|
|
this->logger->log(this->logger, ERROR, "Receiver thread could not be started");
|
2006-04-10 08:07:38 +00:00
|
|
|
free(this);
|
2005-12-05 16:09:42 +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);
|
|
|
|
}
|