- began to clean code documentation
[strongswan.git] / Source / charon / threads / receiver.c
1 /**
2 * @file receiver.c
3 *
4 * @brief Implements the Receiver Thread encapsulated in the receiver_t object
5 *
6 */
7
8 /*
9 * Copyright (C) 2005 Jan Hutter, Martin Willi
10 * Hochschule fuer Technik Rapperswil
11 *
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>.
16 *
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
20 * for more details.
21 */
22
23 #include <stdlib.h>
24 #include <pthread.h>
25
26 #include "receiver.h"
27
28 #include <globals.h>
29 #include <network/socket.h>
30 #include <network/packet.h>
31 #include <queues/job_queue.h>
32 #include <queues/jobs/job.h>
33 #include <queues/jobs/incoming_packet_job.h>
34 #include <utils/allocator.h>
35 #include <utils/logger_manager.h>
36
37 typedef struct private_receiver_t private_receiver_t;
38
39 /**
40 * Private data of a receiver object
41 */
42 struct private_receiver_t {
43 /**
44 * Public part of a receiver object
45 */
46 receiver_t public;
47
48 /**
49 * Assigned thread to the receiver_t object
50 */
51 pthread_t assigned_thread;
52 /**
53 * logger for the receiver
54 */
55 logger_t *logger;
56
57
58 };
59
60 /**
61 * Thread function started at creation of the receiver object
62 *
63 * @param this assigned receiver object
64 * @return SUCCESS if thread_function ended successfully, FAILED otherwise
65 */
66 static void receiver_thread_function(private_receiver_t * this)
67 {
68 packet_t * current_packet;
69 job_t *current_job;
70 /* cancellation disabled by default */
71 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
72 while (1)
73 {
74 while (global_socket->receive(global_socket,&current_packet) == SUCCESS)
75 {
76 this->logger->log(this->logger, CONTROL, "creating job from packet");
77 current_job = (job_t *) incoming_packet_job_create(current_packet);
78 if (current_job == NULL)
79 {
80 this->logger->log(this->logger, ERROR, "job creation failed");
81 }
82
83 if (global_job_queue->add(global_job_queue,current_job) != SUCCESS)
84 {
85 this->logger->log(this->logger, ERROR, "job queueing failed");
86 }
87
88 }
89 /* bad bad, rebuild the socket ? */
90 this->logger->log(this->logger, ERROR, "receiving from socket failed!");
91 }
92 }
93
94 /**
95 * Implementation of receiver_t's destroy function
96 */
97 static status_t destroy(private_receiver_t *this)
98 {
99 this->logger->log(this->logger, CONTROL | MORE, "Going to terminate receiver thread");
100 pthread_cancel(this->assigned_thread);
101
102 pthread_join(this->assigned_thread, NULL);
103 this->logger->log(this->logger, CONTROL | MORE, "Receiver thread terminated");
104
105 global_logger_manager->destroy_logger(global_logger_manager, this->logger);
106
107 allocator_free(this);
108 return SUCCESS;
109 }
110
111
112 receiver_t * receiver_create()
113 {
114 private_receiver_t *this = allocator_alloc_thing(private_receiver_t);
115
116 this->public.destroy = (status_t(*)(receiver_t*)) destroy;
117
118 this->logger = global_logger_manager->create_logger(global_logger_manager, RECEIVER, NULL);
119 if (this->logger == NULL)
120 {
121 allocator_free(this);
122 }
123
124 if (pthread_create(&(this->assigned_thread), NULL, (void*(*)(void*))receiver_thread_function, this) != 0)
125 {
126 /* thread could not be created */
127 global_logger_manager->destroy_logger(global_logger_manager, this->logger);
128 allocator_free(this);
129 return NULL;
130 }
131
132 return &(this->public);
133 }