4 * @brief Implements the Sender Thread encapsulated in the sender_t object
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/allocator.h>
33 #include <utils/logger_manager.h>
36 * Private data of a sender object
38 typedef struct private_sender_s private_sender_t
;
40 struct private_sender_s
{
42 * Public part of a sender object
47 * Assigned thread to the sender_t object
49 pthread_t assigned_thread
;
52 * logger for this sender
59 * Thread function started at creation of the sender object
61 * @param this assigned sender object
62 * @return SUCCESS if thread_function ended successfully, FAILED otherwise
64 static void sender_thread_function(private_sender_t
* this)
66 packet_t
* current_packet
;
69 /* cancellation disabled by default */
70 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE
, NULL
);
74 while (global_send_queue
->get(global_send_queue
,¤t_packet
) == SUCCESS
)
76 this->logger
->log(this->logger
, CONTROL
|MORE
, "got a packet, sending it");
77 status
= global_socket
->send(global_socket
,current_packet
);
78 if (status
!= SUCCESS
)
80 this->logger
->log(this->logger
, ERROR
, "sending failed, socket returned %s",
81 mapping_find(status_m
, status
));
83 current_packet
->destroy(current_packet
);
89 * Implementation of sender_t's destroy function
91 static status_t
destroy(private_sender_t
*this)
93 this->logger
->log(this->logger
, CONTROL
| MORE
, "Going to terminate sender thread");
94 pthread_cancel(this->assigned_thread
);
96 pthread_join(this->assigned_thread
, NULL
);
97 this->logger
->log(this->logger
, CONTROL
| MORE
, "Sender thread terminated");
99 global_logger_manager
->destroy_logger(global_logger_manager
, this->logger
);
101 allocator_free(this);
106 sender_t
* sender_create()
108 private_sender_t
*this = allocator_alloc_thing(private_sender_t
);
110 this->public.destroy
= (status_t(*)(sender_t
*)) destroy
;
112 this->logger
= global_logger_manager
->create_logger(global_logger_manager
, SENDER
, NULL
);
113 if (this->logger
== NULL
)
115 allocator_free(this);
119 if (pthread_create(&(this->assigned_thread
), NULL
, (void*(*)(void*))sender_thread_function
, this) != 0)
121 /* thread could not be created */
122 allocator_free(this);
126 return &(this->public);