-
Notifications
You must be signed in to change notification settings - Fork 0
/
meta_handler.c
304 lines (257 loc) · 7.06 KB
/
meta_handler.c
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/*
* Audio Scheduler - An audio clip scheduler for use in radio broadcasting
* Metadata request handler
*
* Copyright (C) 2017 Nick Kossifidis <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <netinet/ip.h> /* For IP stuff (also brings in socket etc) */
#include <arpa/inet.h> /* For inet_aton */
#include <stdlib.h> /* For malloc() / free() */
#include <string.h> /* For memset() */
#include <stdio.h> /* For snprintf() */
#include <unistd.h> /* For read/write */
#include <errno.h> /* For errno */
#include <time.h> /* For time() / gmtime() / strftime() */
#include "meta_handler.h"
#include "utils.h"
/*********\
* HELPERS *
\*********/
static int
meta_create_server_socket(uint16_t port, const char* ip4addr)
{
struct sockaddr_in name = {0};
int sockfd = 0;
int ret = 0;
/* Create the socket. */
sockfd = socket(PF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0);
if (sockfd < 0) {
utils_perr(META, "Could not create server socket");
return -errno;
}
/* Check if it's an ipv4 address */
if(ip4addr != NULL) {
ret = inet_aton(ip4addr, &name.sin_addr);
if(ret != 0)
return -EINVAL;
} else
name.sin_addr.s_addr = htonl(INADDR_ANY);
/* Give the socket a name. */
name.sin_family = AF_INET;
name.sin_port = htons(port);
ret = bind(sockfd, (struct sockaddr *) &name, sizeof (name));
if (ret < 0) {
utils_perr(META, "Could not bind server socket");
return -errno;
}
return sockfd;
}
static void
meta_server_thread_cleanup(void* arg)
{
struct meta_handler *mh = (struct meta_handler*) arg;
mh->active = 0;
pthread_mutex_unlock(&mh->state.proc_mutex);
close(mh->sockfd);
utils_dbg(META, "Server thread terminated\n");
}
/*****************\
* SERVER CALLBACK *
\*****************/
static int
meta_server_callback(struct meta_handler *mh, int sockfd)
{
struct current_state *st = &mh->state;
struct song_info *curr = &st->current;
struct song_info *next = &st->next;
time_t now = 0;
struct tm *tm = NULL;
char date_str[64] = {0};
int len = 0;
int ret = 0;
/* Ignore input */
while(recv(sockfd, NULL, 0, MSG_TRUNC | MSG_OOB) > 0);
/* Buffer freed */
if(mh->msg_buff == NULL)
return -1;
/* Create JSON message */
pthread_mutex_lock(&st->proc_mutex);
snprintf(mh->msg_buff, ST_STRING_LEN,
"{\n\t\"current_song\": {\n\t\t"
"\"Artist\": \"%s\",\n\t\t"
"\"Album\": \"%s\",\n\t\t"
"\"Title\": \"%s\",\n\t\t"
"\"Path\": \"%s\",\n\t\t"
"\"Duration\": \"%i\",\n\t\t"
"\"Elapsed\": \"%i\",\n\t\t"
"\"Zone\": \"%s\"\n\t\t},\n"
"\t\"next_song\": {\n\t\t"
"\"Artist\": \"%s\",\n\t\t"
"\"Album\": \"%s\",\n\t\t"
"\"Title\": \"%s\",\n\t\t"
"\"Path\": \"%s\",\n\t\t"
"\"Duration\": \"%i\",\n\t\t"
"\"Elapsed\": \"%i\",\n\t\t"
"\"Zone\": \"%s\"\n\t\t},\n"
"\t\"overlap\": \"%i\"\n}\r\n",
curr->artist,
curr->album,
curr->title,
curr->path,
curr->duration_sec,
curr->elapsed_sec,
curr->zone,
next->artist,
next->album,
next->title,
next->path,
next->duration_sec,
next->elapsed_sec,
next->zone,
st->overlap_sec);
pthread_mutex_unlock(&st->proc_mutex);
now = time(NULL);
tm = gmtime(&now);
strftime(date_str, 64, "%a, %d %b %Y %H:%M:%S %Z", tm);
len = strnlen(mh->msg_buff, ST_STRING_LEN);
dprintf(sockfd, "HTTP/1.1 200 OK\r\n"
"Server: audio-scheduler\r\n"
"Date: %s\r\n"
"Content-Length: %i\r\n"
"Content-type: application/json; charset=utf-8\r\n"
"Pragma: no-cache\r\n"
"Cache-Control: no-cache\r\n"
"Connection: Closed\r\n\r\n",
date_str, len);
ret = write(sockfd, mh->msg_buff, len);
if(ret < 0 || ret != len)
utils_pwrn(META, "Write error");
shutdown(sockfd, SHUT_WR);
return 0;
}
/***************\
* SERVER THREAD *
\***************/
static void*
meta_server_thread(void* arg)
{
struct meta_handler *mh = (struct meta_handler*) arg;
struct sockaddr_in clientname = {0};
fd_set active_set;
int client_sockfd = 0;
socklen_t size = 0;
int i = 0;
int ret = 0;
utils_info(META, "Waiting for connections...\n");
/* Register cleanup function */
pthread_cleanup_push(meta_server_thread_cleanup, arg);
/* Initialize the set of active sockets. */
FD_ZERO(&active_set);
FD_SET(mh->sockfd, &active_set);
while(mh->active) {
/* Block until input arrives on one or more active sockets. */
ret = select(FD_SETSIZE, &active_set, NULL, NULL, NULL);
if (ret < 0) {
utils_perr(META, "select() failed");
ret = -errno;
goto cleanup;
} if (!ret)
continue; /* No connection within timeout */
/* Loop on active sockets */
for (i = 0; i < FD_SETSIZE && mh->active; ++i) {
if (!FD_ISSET(i, &active_set))
continue;
/* Data on master socket: Move the connection to
* a new socket and add it to the set of sockets
* to monitor. */
if (i == mh->sockfd) {
size = sizeof(clientname);
client_sockfd = accept(mh->sockfd,
(struct sockaddr *) &clientname,
&size);
if (client_sockfd < 0) {
utils_perr(META, "accept() failed");
ret = -errno;
goto cleanup;
}
utils_info(META, "Connection from host %s.\n",
inet_ntoa(clientname.sin_addr),
ntohs(clientname.sin_port));
FD_SET(client_sockfd, &active_set);
/* Previously assigned socket */
} else {
ret = meta_server_callback(mh, i);
if (ret < 0)
goto cleanup;
close(i);
FD_CLR(i, &active_set);
}
}
}
cleanup:
pthread_cleanup_pop(arg);
return arg;
}
/**************\
* ENTRY POINTS *
\**************/
int
meta_handler_init(struct meta_handler *mh, uint16_t port, const char* ip4addr)
{
int ret = 0;
memset(mh, 0, sizeof(struct meta_handler));
mh->port = port;
mh->ipaddr = ip4addr;
pthread_mutex_init(&mh->state.proc_mutex, NULL);
/* Allocate output buffer */
mh->msg_buff = malloc(ST_STRING_LEN);
if(mh->msg_buff == NULL) {
utils_perr(META, "Could not allocate output buffer");
return -errno;
}
/* Create the socket and set it up to accept connections. */
mh->sockfd = meta_create_server_socket(port, ip4addr);
if(mh->sockfd < 0)
return mh->sockfd;
ret = listen(mh->sockfd, 1);
if (ret < 0) {
utils_perr(META, "Could not mark socket as passive");
return -errno;
}
/* Start server thread */
mh->active = 1;
ret = pthread_create(&mh->tid, NULL, meta_server_thread, (void*) mh);
if(ret < 0) {
utils_err(META, "Could not start server thread");
return -ret;
}
return 0;
}
void
meta_handler_destroy(struct meta_handler *mh)
{
if(mh->tid!=NULL){
pthread_cancel(mh->tid);
pthread_join(mh->tid, NULL);
}
free(mh->msg_buff);
mh->msg_buff = NULL;
}
struct current_state*
meta_get_state(struct meta_handler *mh)
{
return &mh->state;
}