1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
/**** POSIX *******************************************************************/
#include <pthread.h>
#include <string.h>
/**** RELABSD *****************************************************************/
#include <relabsd/debug.h>
#include <relabsd/server.h>
/******************************************************************************/
/**** LOCAL FUNCTIONS *********************************************************/
/******************************************************************************/
static void main_loop (struct relabsd_server server [const static 1])
{
int communication_socket, current_client_socket;
int interrupt_fd, highest_fd;
int ready_fds;
fd_set ready_to_read;
if
(
relabsd_server_create_communication_node
(
relabsd_parameters_get_communication_node_name(&(server->parameters)),
&communication_socket
)
< 0
)
{
relabsd_server_interrupt();
return;
}
interrupt_fd = relabsd_server_get_interruption_file_descriptor();
if (interrupt_fd > communication_socket)
{
highest_fd = (interrupt_fd + 1);
}
else
{
highest_fd = (communication_socket + 1);
}
for (;;)
{
FD_ZERO(&ready_to_read);
FD_SET(communication_socket, &ready_to_read);
FD_SET(interrupt_fd, &ready_to_read);
ready_fds =
select
(
highest_fd,
&ready_to_read,
(fd_set *) NULL,
(fd_set *) NULL,
(struct timeval *) NULL
);
/* TODO: select error handling. */
if (!relabsd_server_keep_running())
{
return;
}
errno = 0;
current_client_socket =
accept
(
communication_socket,
(struct sockaddr *) NULL,
(socklen_t *) NULL
);
if (current_client_socket == -1)
{
RELABSD_ERROR
(
"Unable to accept on the server's socket: %s.",
strerror(errno)
);
return -1;
}
(void) handle_client(current_client_socket, server);
}
}
static void * posix_main_loop (void * params)
{
main_loop((struct relabsd_server *) params);
return NULL;
}
/******************************************************************************/
/**** EXPORTED FUNCTIONS ******************************************************/
/******************************************************************************/
int relabsd_server_create_communication_thread
(
struct relabsd_server server [const static 1]
)
{
int err;
err =
pthread_create
(
&(server->communication_thread),
(const pthread_attr_t *) NULL,
posix_main_loop,
(void *) server
);
if (err != 0)
{
RELABSD_FATAL
(
"Unable to create the communication thread: %s",
strerror(err)
);
return -1;
}
return 0;
}
int relabsd_server_join_communication_thread
(
struct relabsd_server server [const static 1]
)
{
int err;
err = pthread_join(server->communication_thread, (void **) NULL);
if (err != 0)
{
RELABSD_FATAL
(
"Unable to join with the communication thread: %s",
strerror(err)
);
return -1;
}
return 0;
}
|