source: roaraudio/roard/clients.c @ 1834:3335435e4427

Last change on this file since 1834:3335435e4427 was 1834:3335435e4427, checked in by phi, 15 years ago

do not start to read data in the same cycle as a stream is execed

File size: 10.7 KB
Line 
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
27int 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
36int 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
46int 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
84int 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
111int 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
120int 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
130int clients_get_fh    (int id) {
131 if ( g_clients[id] == NULL )
132  return -1;
133
134 return g_clients[id]->fh;
135}
136
137int 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
146int 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
155int 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
166int 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]->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
322int 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
371int 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  if ( g_clients[i]->execed == -1 ) {
386   // TODO: add some code to send a message to the client insetd of the raw data.
387   for (j = 0; j < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; j++) {
388    //if ( (fh = streams_get_fh(g_clients[i]->streams[j])) != -1 ) {
389    ROAR_DBG("clients_send_mon(*): client=%i, stream=%i -> %i", i, j, g_clients[i]->streams[j]);
390    if ( g_clients[i]->streams[j] != -1 )
391     streams_send_mon(g_clients[i]->streams[j]);
392   }
393  } else {
394//   streams_check(g_clients[i]->execed);
395   streams_send_mon(g_clients[i]->execed);
396//   if ( streams_send_mon(g_clients[i]->execed) == -1 )
397//    clients_delete(i); // delete client in case we could not write
398  }
399 }
400
401 return -1;
402}
403
404int clients_send_filter(struct roar_audio_info * sa, uint32_t pos) {
405 int i;
406 int fh;
407
408 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
409  if ( g_clients[i] == NULL )
410   continue;
411
412  if ( (fh = g_clients[i]->fh) == -1 )
413   continue;
414
415  if ( g_clients[i]->execed == -1 ) {
416   // TODO: add some code to send a message to the client insetd of the raw data.
417  } else {
418//   streams_check(g_clients[i]->execed);
419   streams_send_filter(g_clients[i]->execed);
420//   if ( streams_send_mon(g_clients[i]->execed) == -1 )
421//    clients_delete(i); // delete client in case we could not write
422  }
423 }
424
425 return -1;
426}
427
428int client_stream_exec   (int client, int stream) {
429 int i;
430
431 if ( g_clients[client] == NULL )
432  return -1;
433
434 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
435  if ( g_clients[client]->streams[i] == stream ) {
436   g_clients[client]->execed = stream;
437   streams_set_fh(stream, g_clients[client]->fh);
438   streams_set_socktype(stream, ROAR_SOCKET_TYPE_GENSTR);
439   return 0;
440  }
441 }
442
443 return -1;
444}
445
446int client_stream_set_fh (int client, int stream, int fh) {
447 int i;
448
449 if ( g_clients[client] == NULL )
450  return -1;
451
452 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
453  if ( g_clients[client]->streams[i] == stream ) {
454   streams_set_fh(stream, fh);
455   return 0;
456  }
457 }
458
459 return -1;
460}
461
462int client_stream_add    (int client, int stream) {
463 int i;
464
465 if ( g_clients[client] == NULL )
466  return -1;
467
468 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
469  if ( g_clients[client]->streams[i] == -1 ) {
470   g_clients[client]->streams[i] = stream;
471   streams_set_client(stream, client);
472   return 0;
473  }
474 }
475
476 return -1;
477}
478
479int client_stream_delete (int client, int stream) {
480 int i;
481
482 if ( g_clients[client] == NULL )
483  return -1;
484
485 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
486  if ( g_clients[client]->streams[i] == stream ) {
487   g_clients[client]->streams[i] = -1;
488
489   if ( stream == g_clients[client]->execed ) {
490    ROAR_DBG("client_stream_delete(client=%i, stream=%i): stream is execed one, deleting client!", client, stream);
491    clients_delete(client);
492   }
493
494   ROAR_DBG("client_stream_delete(client=%i, stream=%i) = 0", client, stream);
495   return 0;
496  }
497 }
498
499 ROAR_DBG("client_stream_delete(client=%i, stream=%i) = -1", client, stream);
500 return -1;
501}
502
503int client_stream_move   (int client, int stream) {
504 int old_client = streams_get_client(stream);
505
506 ROAR_DBG("client_stream_move(client=%i, stream=%i): old_client = %i", client, stream, old_client);
507
508 if ( old_client != -1 )
509  if ( client_stream_delete(old_client, stream) == -1 )
510   return -1;
511
512 return client_stream_add(client, stream);
513}
514
515//ll
Note: See TracBrowser for help on using the repository browser.