4 * @brief Implements the Receiver Thread encapsulated in the receiver_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/job_queue.h>
32 #include <queues/jobs/job.h>
33 #include <utils/allocator.h>
34 #include <utils/logger_manager.h>
37 * Private data of a receiver object
39 typedef struct private_receiver_s private_receiver_t
;
41 struct private_receiver_s
{
43 * Public part of a receiver object
48 * Assigned thread to the receiver_t object
50 pthread_t assigned_thread
;
52 * logger for the receiver
60 * Thread function started at creation of the receiver object
62 * @param this assigned receiver object
63 * @return SUCCESS if thread_function ended successfully, FAILED otherwise
65 static void receiver_thread_function(private_receiver_t
* this)
67 packet_t
* current_packet
;
69 /* cancellation disabled by default */
70 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE
, NULL
);
73 while (global_socket
->receive(global_socket
,¤t_packet
) == SUCCESS
)
75 this->logger
->log(this->logger
, CONTROL
, "creating job from packet");
76 current_job
= (job_t
*) incoming_packet_job_create(current_packet
);
77 if (current_job
== NULL
)
79 this->logger
->log(this->logger
, ERROR
, "job creation failed");
82 if (global_job_queue
->add(global_job_queue
,current_job
) != SUCCESS
)
84 this->logger
->log(this->logger
, ERROR
, "job queueing failed");
88 /* bad bad, rebuild the socket ? */
89 this->logger
->log(this->logger
, ERROR
, "receiving from socket failed!");
94 * Implementation of receiver_t's destroy function
96 static status_t
destroy(private_receiver_t
*this)
98 this->logger
->log(this->logger
, CONTROL
| MORE
, "Going to terminate receiver thread");
99 pthread_cancel(this->assigned_thread
);
101 pthread_join(this->assigned_thread
, NULL
);
102 this->logger
->log(this->logger
, CONTROL
| MORE
, "Receiver thread terminated");
104 global_logger_manager
->destroy_logger(global_logger_manager
, this->logger
);
106 allocator_free(this);
111 receiver_t
* receiver_create()
113 private_receiver_t
*this = allocator_alloc_thing(private_receiver_t
);
115 this->public.destroy
= (status_t(*)(receiver_t
*)) destroy
;
117 this->logger
= global_logger_manager
->create_logger(global_logger_manager
, RECEIVER
, NULL
);
118 if (this->logger
== NULL
)
120 allocator_free(this);
123 if (pthread_create(&(this->assigned_thread
), NULL
, (void*(*)(void*))receiver_thread_function
, this) != 0)
125 /* thread could not be created */
126 global_logger_manager
->destroy_logger(global_logger_manager
, this->logger
);
127 allocator_free(this);
131 return &(this->public);