4 * @brief Implementation of sender_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/send_queue.h>
32 #include <utils/logger_manager.h>
35 typedef struct private_sender_t private_sender_t
;
38 * Private data of a sender_t object.
40 struct private_sender_t
{
42 * Public part of a sender_t object.
49 pthread_t assigned_thread
;
52 * @brief The thread function, sends out packets.
54 * @param this calling object
56 void (*send_packets
) (private_sender_t
* this);
59 * A logger for this sender_t object.
66 * Implementation of private_sender_t.send_packets.
68 static void send_packets(private_sender_t
* this)
70 packet_t
* current_packet
;
73 /* cancellation disabled by default */
74 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE
, NULL
);
76 this->logger
->log(this->logger
, CONTROL
, "Sender thread running, thread_id %u", (int)pthread_self());
80 current_packet
= charon
->send_queue
->get(charon
->send_queue
);
81 this->logger
->log(this->logger
, CONTROL
|LEVEL1
, "Got a packet, sending it");
82 status
= charon
->socket
->send(charon
->socket
,current_packet
);
83 if (status
!= SUCCESS
)
85 this->logger
->log(this->logger
, ERROR
, "Sending failed, socket returned %s",
86 mapping_find(status_m
, status
));
88 current_packet
->destroy(current_packet
);
93 * Implementation of sender_t.destroy.
95 static void destroy(private_sender_t
*this)
97 this->logger
->log(this->logger
, CONTROL
| LEVEL1
, "Going to terminate sender thread");
98 pthread_cancel(this->assigned_thread
);
100 pthread_join(this->assigned_thread
, NULL
);
101 this->logger
->log(this->logger
, CONTROL
| LEVEL1
, "Sender thread terminated");
107 * Described in header.
109 sender_t
* sender_create()
111 private_sender_t
*this = malloc_thing(private_sender_t
);
113 this->send_packets
= send_packets
;
114 this->public.destroy
= (void(*)(sender_t
*)) destroy
;
116 this->logger
= logger_manager
->get_logger(logger_manager
, SENDER
);
118 if (pthread_create(&(this->assigned_thread
), NULL
, (void*(*)(void*))this->send_packets
, this) != 0)
120 this->logger
->log(this->logger
, ERROR
, "Sender thread could not be created");
122 charon
->kill(charon
, "Unable to create sender thread");
125 return &(this->public);