4 * @brief Implementation of receiver_t.
9 * Copyright (C) 2005 Jan Hutter, Martin Willi
10 * Hochschule fuer Technik Rapperswil
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
29 #include <network/socket.h>
30 #include <network/packet.h>
31 #include <queues/job_queue.h>
32 #include <queues/jobs/job.h>
33 #include <queues/jobs/incoming_packet_job.h>
34 #include <utils/logger_manager.h>
37 typedef struct private_receiver_t private_receiver_t
;
40 * Private data of a receiver_t object.
42 struct private_receiver_t
{
44 * Public part of a receiver_t object.
49 * @brief Thread function started at creation of the receiver object.
51 * @param this calling object
53 void (*receive_packets
) (private_receiver_t
*this);
58 pthread_t assigned_thread
;
61 * A logger for the receiver_t object.
67 * Implementation of receiver_t.receive_packets.
69 static void receive_packets(private_receiver_t
* this)
71 packet_t
* current_packet
;
74 /* cancellation disabled by default */
75 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE
, NULL
);
77 this->logger
->log(this->logger
, CONTROL
, "Receiver thread running, thread_id %u", (int)pthread_self());
81 while (charon
->socket
->receive(charon
->socket
,¤t_packet
) == SUCCESS
)
83 this->logger
->log(this->logger
, CONTROL
| LEVEL1
, "Creating job from packet");
84 current_job
= (job_t
*) incoming_packet_job_create(current_packet
);
86 charon
->job_queue
->add(charon
->job_queue
,current_job
);
89 /* bad bad, rebuild the socket ? */
90 this->logger
->log(this->logger
, ERROR
, "Receiving from socket failed!");
95 * Implementation of receiver_t.destroy.
97 static void destroy(private_receiver_t
*this)
99 this->logger
->log(this->logger
, CONTROL
| LEVEL1
, "Going to terminate receiver thread");
100 pthread_cancel(this->assigned_thread
);
102 pthread_join(this->assigned_thread
, NULL
);
103 this->logger
->log(this->logger
, CONTROL
| LEVEL1
, "Receiver thread terminated");
109 * Described in header.
111 receiver_t
* receiver_create()
113 private_receiver_t
*this = malloc_thing(private_receiver_t
);
115 this->public.destroy
= (void(*)(receiver_t
*)) destroy
;
116 this->receive_packets
= receive_packets
;
118 this->logger
= logger_manager
->get_logger(logger_manager
, RECEIVER
);
120 if (pthread_create(&(this->assigned_thread
), NULL
, (void*(*)(void*))this->receive_packets
, this) != 0)
122 this->logger
->log(this->logger
, ERROR
, "Receiver thread could not be started");
124 charon
->kill(charon
, "Unable to create receiver thread");
127 return &(this->public);