1 | //clients.c: |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008 |
---|
5 | * |
---|
6 | * This file is part of roard a part of RoarAudio, |
---|
7 | * a cross-platform sound system for both, home and professional use. |
---|
8 | * See README for details. |
---|
9 | * |
---|
10 | * This file is free software; you can redistribute it and/or modify |
---|
11 | * it under the terms of the GNU General Public License version 3 |
---|
12 | * as published by the Free Software Foundation. |
---|
13 | * |
---|
14 | * RoarAudio is distributed in the hope that it will be useful, |
---|
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
17 | * GNU General Public License for more details. |
---|
18 | * |
---|
19 | * You should have received a copy of the GNU General Public License |
---|
20 | * along with this software; see the file COPYING. If not, write to |
---|
21 | * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
22 | * |
---|
23 | */ |
---|
24 | |
---|
25 | #include "roard.h" |
---|
26 | |
---|
27 | int clients_init (void) { |
---|
28 | int i; |
---|
29 | |
---|
30 | for (i = 0; i < ROAR_CLIENTS_MAX; i++) |
---|
31 | g_clients[i] = NULL; |
---|
32 | |
---|
33 | return 0; |
---|
34 | } |
---|
35 | |
---|
36 | int clients_free (void) { |
---|
37 | int i; |
---|
38 | |
---|
39 | for (i = 0; i < ROAR_CLIENTS_MAX; i++) |
---|
40 | if ( g_clients[i] ) |
---|
41 | clients_delete(i); |
---|
42 | |
---|
43 | return 0; |
---|
44 | } |
---|
45 | |
---|
46 | int clients_new (void) { |
---|
47 | int i; |
---|
48 | int s; |
---|
49 | struct roar_client * n; |
---|
50 | |
---|
51 | for (i = 0; i < ROAR_CLIENTS_MAX; i++) { |
---|
52 | if ( g_clients[i] == NULL ) { |
---|
53 | n = malloc(sizeof(struct roar_client)); |
---|
54 | if ( n != NULL ) { |
---|
55 | n->pid = -1; |
---|
56 | n->uid = -1; |
---|
57 | n->gid = -1; |
---|
58 | n->fh = -1; |
---|
59 | |
---|
60 | *n->name = 0; |
---|
61 | *n->host = 0; |
---|
62 | |
---|
63 | n->acl = NULL; |
---|
64 | |
---|
65 | n->execed = -1; |
---|
66 | for (s = 0; s < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; s++) |
---|
67 | n->streams[s] = -1; |
---|
68 | |
---|
69 | g_clients[i] = n; |
---|
70 | |
---|
71 | ROAR_DBG("clients_new(void) = %i", i); |
---|
72 | return i; |
---|
73 | } else { |
---|
74 | ROAR_ERR("clients_new(void): Can not alloc memory for new client: %s", strerror(errno)); |
---|
75 | ROAR_ERR("clients_new(void) = -1"); |
---|
76 | return -1; |
---|
77 | } |
---|
78 | } |
---|
79 | } |
---|
80 | |
---|
81 | return -1; |
---|
82 | } |
---|
83 | |
---|
84 | int clients_delete (int id) { |
---|
85 | int i; |
---|
86 | int close_client_fh = 1; |
---|
87 | |
---|
88 | if ( g_clients[id] == NULL ) |
---|
89 | return -1; |
---|
90 | |
---|
91 | if (g_clients[id]->execed != -1) { |
---|
92 | // return streams_delete(g_clients[id]->execed); |
---|
93 | g_clients[id]->execed = -1; |
---|
94 | close_client_fh = 0; |
---|
95 | } |
---|
96 | |
---|
97 | for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) { |
---|
98 | streams_delete(g_clients[id]->streams[i]); |
---|
99 | } |
---|
100 | |
---|
101 | if ( g_clients[id]->fh != -1 && close_client_fh ) |
---|
102 | close(g_clients[id]->fh); |
---|
103 | |
---|
104 | free(g_clients[id]); |
---|
105 | g_clients[id] = NULL; |
---|
106 | |
---|
107 | ROAR_DBG("clients_delete(id=%i) = 0", id); |
---|
108 | return 0; |
---|
109 | } |
---|
110 | |
---|
111 | int clients_get (int id, struct roar_client ** client) { |
---|
112 | *client = g_clients[id]; |
---|
113 | |
---|
114 | if ( *client == NULL ) |
---|
115 | return -1; |
---|
116 | |
---|
117 | return 0; |
---|
118 | } |
---|
119 | |
---|
120 | int clients_set_fh (int id, int fh) { |
---|
121 | |
---|
122 | if ( g_clients[id] == NULL ) |
---|
123 | return -1; |
---|
124 | |
---|
125 | g_clients[id]->fh = fh; |
---|
126 | |
---|
127 | return 0; |
---|
128 | } |
---|
129 | |
---|
130 | int clients_get_fh (int id) { |
---|
131 | if ( g_clients[id] == NULL ) |
---|
132 | return -1; |
---|
133 | |
---|
134 | return g_clients[id]->fh; |
---|
135 | } |
---|
136 | |
---|
137 | int clients_set_pid (int id, int pid) { |
---|
138 | if ( g_clients[id] == NULL ) |
---|
139 | return -1; |
---|
140 | |
---|
141 | g_clients[id]->pid = pid; |
---|
142 | |
---|
143 | return 0; |
---|
144 | } |
---|
145 | |
---|
146 | int clients_set_uid (int id, int uid) { |
---|
147 | if ( g_clients[id] == NULL ) |
---|
148 | return -1; |
---|
149 | |
---|
150 | g_clients[id]->uid = uid; |
---|
151 | |
---|
152 | return 0; |
---|
153 | } |
---|
154 | |
---|
155 | int clients_set_gid (int id, int gid) { |
---|
156 | if ( g_clients[id] == NULL ) |
---|
157 | return -1; |
---|
158 | |
---|
159 | g_clients[id]->gid = gid; |
---|
160 | |
---|
161 | return 0; |
---|
162 | } |
---|
163 | |
---|
164 | #define MAX_STREAMLESS 8 |
---|
165 | |
---|
166 | int clients_check_all (void) { |
---|
167 | #ifdef ROAR_HAVE_SELECT |
---|
168 | struct timeval tv; |
---|
169 | fd_set r, e; |
---|
170 | int i, j; |
---|
171 | int ret; |
---|
172 | int fh; |
---|
173 | int max_fh = -1; |
---|
174 | int have = 0; |
---|
175 | struct { |
---|
176 | int id; |
---|
177 | int fh; |
---|
178 | } streamless[MAX_STREAMLESS]; |
---|
179 | int have_streamless = 0; |
---|
180 | int have_stream; |
---|
181 | |
---|
182 | FD_ZERO(&r); |
---|
183 | FD_ZERO(&e); |
---|
184 | |
---|
185 | tv.tv_sec = 0; |
---|
186 | tv.tv_usec = 1; |
---|
187 | |
---|
188 | for (i = 0; i < ROAR_CLIENTS_MAX; i++) { |
---|
189 | if ( g_clients[i] == NULL ) |
---|
190 | continue; |
---|
191 | |
---|
192 | if ( (fh = g_clients[i]->fh) != -1 ) { |
---|
193 | have++; |
---|
194 | |
---|
195 | ROAR_DBG("clients_check_all(*): fh=%i", fh); |
---|
196 | |
---|
197 | FD_SET(fh, &r); |
---|
198 | FD_SET(fh, &e); |
---|
199 | |
---|
200 | if ( fh > max_fh ) |
---|
201 | max_fh = fh; |
---|
202 | } |
---|
203 | |
---|
204 | have_stream = 0; |
---|
205 | |
---|
206 | for (j = 0; j < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; j++) { |
---|
207 | if ( (fh = streams_get_fh(g_clients[i]->streams[j])) != -1 ) { |
---|
208 | if ( fh > -1 ) { |
---|
209 | FD_SET(fh, &r); |
---|
210 | |
---|
211 | if ( fh > max_fh ) |
---|
212 | max_fh = fh; |
---|
213 | } |
---|
214 | |
---|
215 | have_stream = 1; |
---|
216 | } |
---|
217 | //printf("D: client=%i, stream=%i, fh=%i\n", i, j, fh); |
---|
218 | } |
---|
219 | |
---|
220 | if ( !have_stream && have_streamless < MAX_STREAMLESS ) { |
---|
221 | streamless[have_streamless ].id = i; |
---|
222 | if ( (streamless[have_streamless++].fh = g_clients[i]->fh) == -1 ) |
---|
223 | have_streamless--; |
---|
224 | } |
---|
225 | } |
---|
226 | |
---|
227 | if ( max_fh == -1 ) |
---|
228 | return 0; |
---|
229 | |
---|
230 | if ( (ret = select(max_fh + 1, &r, NULL, &e, &tv)) < 1 ) { |
---|
231 | return ret < 0 ? ret : have; |
---|
232 | } |
---|
233 | |
---|
234 | for (i = 0; i < ROAR_CLIENTS_MAX; i++) { |
---|
235 | if ( g_clients[i] == NULL ) |
---|
236 | continue; |
---|
237 | |
---|
238 | if ( (fh = g_clients[i]->fh) != -1 ) { |
---|
239 | if ( FD_ISSET(fh, &r) ) { |
---|
240 | if ( g_clients[i]->execed == -1 ) { |
---|
241 | clients_check(i); |
---|
242 | if ( g_clients[i] != NULL && g_clients[i]->execed != -1 ) { |
---|
243 | FD_CLR(fh, &r); |
---|
244 | } |
---|
245 | /* |
---|
246 | } else { |
---|
247 | streams_check(g_clients[i]->execed); |
---|
248 | */ |
---|
249 | } |
---|
250 | } |
---|
251 | |
---|
252 | if ( FD_ISSET(fh, &e) ) { |
---|
253 | clients_delete(i); |
---|
254 | continue; |
---|
255 | } |
---|
256 | } |
---|
257 | |
---|
258 | if ( g_clients[i] == NULL ) |
---|
259 | continue; |
---|
260 | |
---|
261 | for (j = 0; j < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; j++) { |
---|
262 | ROAR_DBG("clients_check_all(*): D: client=%i, stream=%i, g_clients[i=%i] = %p", i, j, i, g_clients[i]); |
---|
263 | if ( g_clients[i] == NULL ) // streams_check() bellow can delete our client (why?) |
---|
264 | break; |
---|
265 | |
---|
266 | //ROAR_WARN("clients_check_all(*): client=%i: client exists", i); |
---|
267 | ROAR_DBG("clients_check_all(*): client=%i, stream=%i: id=%i", i, j, g_clients[i]->streams[j]); |
---|
268 | |
---|
269 | if ( (fh = streams_get_fh(g_clients[i]->streams[j])) != -1 ) { |
---|
270 | ROAR_DBG("clients_check_all(*): client=%i, stream=%i: fh=%i", i, j, fh); |
---|
271 | if ( fh == -2 ) { |
---|
272 | streams_check(g_clients[i]->streams[j]); |
---|
273 | } else if ( FD_ISSET(fh, &r) ) { |
---|
274 | streams_check(g_clients[i]->streams[j]); |
---|
275 | } |
---|
276 | } |
---|
277 | } |
---|
278 | } |
---|
279 | |
---|
280 | if ( have_streamless ) { |
---|
281 | FD_ZERO(&r); |
---|
282 | |
---|
283 | tv.tv_sec = 0; |
---|
284 | tv.tv_usec = 1; |
---|
285 | |
---|
286 | max_fh = -1; |
---|
287 | |
---|
288 | for (i = 0; i < have_streamless; i++) { |
---|
289 | if ( ! g_clients[j = streamless[i].id] ) |
---|
290 | continue; |
---|
291 | |
---|
292 | if ( g_clients[j]->execed != -1 ) |
---|
293 | continue; |
---|
294 | |
---|
295 | fh = streamless[i].fh; |
---|
296 | |
---|
297 | ROAR_DBG("clients_check_all(void): fh=%i", fh); |
---|
298 | FD_SET(fh, &r); |
---|
299 | |
---|
300 | if ( fh > max_fh ) |
---|
301 | max_fh = fh; |
---|
302 | } |
---|
303 | |
---|
304 | if ( (ret = select(max_fh + 1, &r, NULL, NULL, &tv)) < 0 ) { |
---|
305 | return ret; |
---|
306 | } |
---|
307 | |
---|
308 | for (i = 0; i < have_streamless; i++) { |
---|
309 | if ( FD_ISSET(streamless[i].fh, &r) ) { |
---|
310 | clients_check(streamless[i].id); |
---|
311 | } |
---|
312 | } |
---|
313 | } |
---|
314 | |
---|
315 | ROAR_DBG("clients_check_all(void) = %i // have value", have); |
---|
316 | return have; |
---|
317 | #else |
---|
318 | return -1; |
---|
319 | #endif |
---|
320 | } |
---|
321 | |
---|
322 | int clients_check (int id) { |
---|
323 | struct roar_message m; |
---|
324 | struct roar_connection con; |
---|
325 | char * data = NULL; |
---|
326 | int oldcmd; |
---|
327 | int r; |
---|
328 | int rv = 0; |
---|
329 | |
---|
330 | if ( g_clients[id] == NULL ) |
---|
331 | return -1; |
---|
332 | if ( g_clients[id]->fh == -1 ) |
---|
333 | return -1; |
---|
334 | |
---|
335 | roar_connect_fh(&con, g_clients[id]->fh); |
---|
336 | |
---|
337 | r = roar_recv_message(&con, &m, &data); |
---|
338 | |
---|
339 | if ( r == -1 ) { // should we drop the client? |
---|
340 | clients_delete(id); |
---|
341 | return -1; |
---|
342 | } |
---|
343 | |
---|
344 | roar_debug_message_print(&m); |
---|
345 | |
---|
346 | oldcmd = m.cmd; |
---|
347 | |
---|
348 | if ( (r = command_exec(id, &m, data)) == -1 ) { |
---|
349 | m.cmd = ROAR_CMD_ERROR; |
---|
350 | m.datalen = 0; |
---|
351 | ROAR_DBG("clients_check(*): Exec of command faild!"); |
---|
352 | } else { |
---|
353 | if ( m.cmd == oldcmd ) { |
---|
354 | m.cmd = ROAR_CMD_OK; |
---|
355 | m.datalen = 0; |
---|
356 | } else if ( m.cmd == ROAR_CMD_OK_STOP ) { |
---|
357 | m.cmd = ROAR_CMD_OK; |
---|
358 | rv = 1; |
---|
359 | } |
---|
360 | } |
---|
361 | |
---|
362 | roar_send_message(&con, &m, NULL); |
---|
363 | |
---|
364 | if ( data ) |
---|
365 | free(data); |
---|
366 | |
---|
367 | ROAR_DBG("clients_check(id=%i) = 0", id); |
---|
368 | return rv; |
---|
369 | } |
---|
370 | |
---|
371 | int clients_send_mon (struct roar_audio_info * sa, uint32_t pos) { |
---|
372 | int i; |
---|
373 | // int fh; |
---|
374 | int j; |
---|
375 | |
---|
376 | for (i = 0; i < ROAR_CLIENTS_MAX; i++) { |
---|
377 | if ( g_clients[i] == NULL ) |
---|
378 | continue; |
---|
379 | |
---|
380 | /* |
---|
381 | if ( (fh = g_clients[i]->fh) == -1 ) |
---|
382 | continue; |
---|
383 | */ |
---|
384 | |
---|
385 | ROAR_DBG("clients_send_mon(*): client=%i, execed=%i", i, g_clients[i]->execed); |
---|
386 | |
---|
387 | if ( g_clients[i]->execed == -1 ) { |
---|
388 | // TODO: add some code to send a message to the client insetd of the raw data. |
---|
389 | for (j = 0; j < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; j++) { |
---|
390 | //if ( (fh = streams_get_fh(g_clients[i]->streams[j])) != -1 ) { |
---|
391 | if ( g_clients[i]->streams[j] != -1 ) { |
---|
392 | ROAR_DBG("clients_send_mon(*): client=%i, stream=%i -> %i", i, j, g_clients[i]->streams[j]); |
---|
393 | streams_send_mon(g_clients[i]->streams[j]); |
---|
394 | } |
---|
395 | } |
---|
396 | } else { |
---|
397 | // streams_check(g_clients[i]->execed); |
---|
398 | streams_send_mon(g_clients[i]->execed); |
---|
399 | // if ( streams_send_mon(g_clients[i]->execed) == -1 ) |
---|
400 | // clients_delete(i); // delete client in case we could not write |
---|
401 | } |
---|
402 | } |
---|
403 | |
---|
404 | // TODO: FIXME: should this really be -1? |
---|
405 | return -1; |
---|
406 | } |
---|
407 | |
---|
408 | int clients_send_filter(struct roar_audio_info * sa, uint32_t pos) { |
---|
409 | int i; |
---|
410 | int fh; |
---|
411 | |
---|
412 | for (i = 0; i < ROAR_CLIENTS_MAX; i++) { |
---|
413 | if ( g_clients[i] == NULL ) |
---|
414 | continue; |
---|
415 | |
---|
416 | if ( (fh = g_clients[i]->fh) == -1 ) |
---|
417 | continue; |
---|
418 | |
---|
419 | if ( g_clients[i]->execed == -1 ) { |
---|
420 | // TODO: add some code to send a message to the client insetd of the raw data. |
---|
421 | } else { |
---|
422 | // streams_check(g_clients[i]->execed); |
---|
423 | streams_send_filter(g_clients[i]->execed); |
---|
424 | // if ( streams_send_mon(g_clients[i]->execed) == -1 ) |
---|
425 | // clients_delete(i); // delete client in case we could not write |
---|
426 | } |
---|
427 | } |
---|
428 | |
---|
429 | return -1; |
---|
430 | } |
---|
431 | |
---|
432 | int client_stream_exec (int client, int stream) { |
---|
433 | int i; |
---|
434 | |
---|
435 | if ( g_clients[client] == NULL ) { |
---|
436 | ROAR_WARN("client_stream_exec(client=%i, stream=%i) = -1 // client does not exist", client, stream); |
---|
437 | return -1; |
---|
438 | } |
---|
439 | |
---|
440 | for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) { |
---|
441 | if ( g_clients[client]->streams[i] == stream ) { |
---|
442 | g_clients[client]->execed = stream; |
---|
443 | streams_set_fh(stream, g_clients[client]->fh); |
---|
444 | streams_set_socktype(stream, ROAR_SOCKET_TYPE_GENSTR); |
---|
445 | return 0; |
---|
446 | } |
---|
447 | } |
---|
448 | |
---|
449 | ROAR_WARN("client_stream_exec(client=%i, stream=%i) = -1 // client does not own stream", client, stream); |
---|
450 | return -1; |
---|
451 | } |
---|
452 | |
---|
453 | int client_stream_set_fh (int client, int stream, int fh) { |
---|
454 | int i; |
---|
455 | |
---|
456 | if ( g_clients[client] == NULL ) |
---|
457 | return -1; |
---|
458 | |
---|
459 | for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) { |
---|
460 | if ( g_clients[client]->streams[i] == stream ) { |
---|
461 | streams_set_fh(stream, fh); |
---|
462 | return 0; |
---|
463 | } |
---|
464 | } |
---|
465 | |
---|
466 | return -1; |
---|
467 | } |
---|
468 | |
---|
469 | int client_stream_add (int client, int stream) { |
---|
470 | int i; |
---|
471 | |
---|
472 | if ( g_clients[client] == NULL ) |
---|
473 | return -1; |
---|
474 | |
---|
475 | for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) { |
---|
476 | if ( g_clients[client]->streams[i] == -1 ) { |
---|
477 | g_clients[client]->streams[i] = stream; |
---|
478 | streams_set_client(stream, client); |
---|
479 | return 0; |
---|
480 | } |
---|
481 | } |
---|
482 | |
---|
483 | return -1; |
---|
484 | } |
---|
485 | |
---|
486 | int client_stream_delete (int client, int stream) { |
---|
487 | int i; |
---|
488 | |
---|
489 | if ( g_clients[client] == NULL ) |
---|
490 | return -1; |
---|
491 | |
---|
492 | for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) { |
---|
493 | if ( g_clients[client]->streams[i] == stream ) { |
---|
494 | g_clients[client]->streams[i] = -1; |
---|
495 | |
---|
496 | if ( stream == g_clients[client]->execed ) { |
---|
497 | ROAR_DBG("client_stream_delete(client=%i, stream=%i): stream is execed one, deleting client!", client, stream); |
---|
498 | clients_delete(client); |
---|
499 | } |
---|
500 | |
---|
501 | ROAR_DBG("client_stream_delete(client=%i, stream=%i) = 0", client, stream); |
---|
502 | return 0; |
---|
503 | } |
---|
504 | } |
---|
505 | |
---|
506 | ROAR_DBG("client_stream_delete(client=%i, stream=%i) = -1", client, stream); |
---|
507 | return -1; |
---|
508 | } |
---|
509 | |
---|
510 | int client_stream_move (int client, int stream) { |
---|
511 | int old_client = streams_get_client(stream); |
---|
512 | |
---|
513 | ROAR_DBG("client_stream_move(client=%i, stream=%i): old_client = %i", client, stream, old_client); |
---|
514 | |
---|
515 | if ( old_client != -1 ) |
---|
516 | if ( client_stream_delete(old_client, stream) == -1 ) |
---|
517 | return -1; |
---|
518 | |
---|
519 | return client_stream_add(client, stream); |
---|
520 | } |
---|
521 | |
---|
522 | //ll |
---|