4 * @brief Implementation of receiver_t.
9 * Copyright (C) 2005-2006 Martin Willi
10 * Copyright (C) 2005 Jan Hutter
11 * Hochschule fuer Technik Rapperswil
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
18 * This program is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
30 #include <network/socket.h>
31 #include <network/packet.h>
32 #include <queues/job_queue.h>
33 #include <queues/jobs/job.h>
34 #include <queues/jobs/incoming_packet_job.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.
51 pthread_t assigned_thread
;
55 * Implementation of receiver_t.receive_packets.
57 static void receive_packets(private_receiver_t
* this)
59 packet_t
* current_packet
;
62 /* cancellation disabled by default */
63 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE
, NULL
);
65 DBG1(DBG_NET
, "receiver thread running, thread_ID: %06u",
70 while (charon
->socket
->receive(charon
->socket
,¤t_packet
) == SUCCESS
)
72 DBG2(DBG_NET
, "creating job from packet");
73 current_job
= (job_t
*) incoming_packet_job_create(current_packet
);
75 charon
->job_queue
->add(charon
->job_queue
,current_job
);
77 /* bad bad, TODO: rebuild the socket ? */
78 DBG1(DBG_NET
, "receiving from socket failed!");
83 * Implementation of receiver_t.destroy.
85 static void destroy(private_receiver_t
*this)
87 pthread_cancel(this->assigned_thread
);
88 pthread_join(this->assigned_thread
, NULL
);
93 * Described in header.
95 receiver_t
* receiver_create()
97 private_receiver_t
*this = malloc_thing(private_receiver_t
);
99 this->public.destroy
= (void(*)(receiver_t
*)) destroy
;
101 if (pthread_create(&(this->assigned_thread
), NULL
, (void*(*)(void*))receive_packets
, this) != 0)
104 charon
->kill(charon
, "unable to create receiver thread");
107 return &(this->public);