4 * @brief Implementation of sender_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/send_queue.h>
33 #include <utils/logger_manager.h>
36 typedef struct private_sender_t private_sender_t
;
39 * Private data of a sender_t object.
41 struct private_sender_t
{
43 * Public part of a sender_t object.
50 pthread_t assigned_thread
;
53 * @brief The thread function, sends out packets.
55 * @param this calling object
57 void (*send_packets
) (private_sender_t
* this);
60 * A logger for this sender_t object.
67 * Implementation of private_sender_t.send_packets.
69 static void send_packets(private_sender_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
, "sender thread running, thread_ID: %06u", (int)pthread_self());
81 current_packet
= charon
->send_queue
->get(charon
->send_queue
);
82 this->logger
->log(this->logger
, CONTROL
|LEVEL1
, "Got a packet, sending it");
83 status
= charon
->socket
->send(charon
->socket
,current_packet
);
84 if (status
!= SUCCESS
)
86 this->logger
->log(this->logger
, ERROR
, "Sending failed, socket returned %s",
87 mapping_find(status_m
, status
));
89 current_packet
->destroy(current_packet
);
94 * Implementation of sender_t.destroy.
96 static void destroy(private_sender_t
*this)
98 this->logger
->log(this->logger
, CONTROL
| LEVEL1
, "Going to terminate sender thread");
99 pthread_cancel(this->assigned_thread
);
101 pthread_join(this->assigned_thread
, NULL
);
102 this->logger
->log(this->logger
, CONTROL
| LEVEL1
, "Sender thread terminated");
108 * Described in header.
110 sender_t
* sender_create()
112 private_sender_t
*this = malloc_thing(private_sender_t
);
114 this->send_packets
= send_packets
;
115 this->public.destroy
= (void(*)(sender_t
*)) destroy
;
117 this->logger
= logger_manager
->get_logger(logger_manager
, SENDER
);
119 if (pthread_create(&(this->assigned_thread
), NULL
, (void*(*)(void*))this->send_packets
, this) != 0)
121 this->logger
->log(this->logger
, ERROR
, "Sender thread could not be created");
123 charon
->kill(charon
, "Unable to create sender thread");
126 return &(this->public);