source: roaraudio/roard/clients.c @ 2546:35ac878b7590

Last change on this file since 2546:35ac878b7590 was 2546:35ac878b7590, checked in by phi, 15 years ago

only check esd clienst in case we have esd emul support

File size: 11.5 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->proto = ROAR_PROTO_ROARAUDIO;
64
65    n->acl   = NULL;
66
67    n->execed = -1;
68    for (s = 0; s < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; s++)
69     n->streams[s] = -1;
70
71    g_clients[i] = n;
72
73    ROAR_DBG("clients_new(void) = %i", i);
74    return i;
75   } else {
76    ROAR_ERR("clients_new(void): Can not alloc memory for new client: %s", strerror(errno));
77    ROAR_ERR("clients_new(void) = -1");
78    return -1;
79   }
80  }
81 }
82
83 return -1;
84}
85
86int clients_delete (int id) {
87 int i;
88 int close_client_fh = 1;
89
90 if ( g_clients[id] == NULL )
91  return -1;
92
93 if (g_clients[id]->execed != -1) {
94//  return streams_delete(g_clients[id]->execed);
95  g_clients[id]->execed = -1;
96  close_client_fh = 0;
97 }
98
99 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
100  streams_delete(g_clients[id]->streams[i]);
101 }
102
103 if ( g_clients[id]->fh != -1 && close_client_fh )
104  close(g_clients[id]->fh);
105
106 free(g_clients[id]);
107 g_clients[id] = NULL;
108
109 ROAR_DBG("clients_delete(id=%i) = 0", id);
110 return 0;
111}
112
113int clients_get       (int id, struct roar_client ** client) {
114 *client = g_clients[id];
115
116 if ( *client == NULL )
117  return -1;
118
119 return 0;
120}
121
122int clients_set_fh    (int id, int    fh) {
123
124 if ( g_clients[id] == NULL )
125  return -1;
126
127 g_clients[id]->fh = fh;
128
129 return 0;
130}
131
132int clients_get_fh    (int id) {
133 if ( g_clients[id] == NULL )
134  return -1;
135
136 return g_clients[id]->fh;
137}
138
139int clients_set_pid   (int id, int    pid) {
140 if ( g_clients[id] == NULL )
141  return -1;
142
143 g_clients[id]->pid = pid;
144
145 return 0;
146}
147
148int clients_set_uid   (int id, int    uid) {
149 if ( g_clients[id] == NULL )
150  return -1;
151
152 g_clients[id]->uid = uid;
153
154 return 0;
155}
156
157int clients_set_gid   (int id, int    gid) {
158 if ( g_clients[id] == NULL )
159  return -1;
160
161 g_clients[id]->gid = gid;
162
163 return 0;
164}
165
166int clients_set_proto (int id, int    proto) {
167 if ( g_clients[id] == NULL )
168  return -1;
169
170 g_clients[id]->proto = proto;
171
172 return 0;
173}
174
175#define MAX_STREAMLESS 8
176
177int clients_check_all (void) {
178#ifdef ROAR_HAVE_SELECT
179 struct timeval tv;
180 fd_set r, e;
181 int i, j;
182 int ret;
183 int fh;
184 int max_fh = -1;
185 int have = 0;
186 struct {
187  int id;
188  int fh;
189 } streamless[MAX_STREAMLESS];
190 int have_streamless = 0;
191 int have_stream;
192
193 FD_ZERO(&r);
194 FD_ZERO(&e);
195
196 tv.tv_sec  = 0;
197 tv.tv_usec = 1;
198
199 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
200  if ( g_clients[i] == NULL )
201   continue;
202
203  if ( (fh = g_clients[i]->fh) != -1 ) {
204   have++;
205
206   ROAR_DBG("clients_check_all(*): fh=%i", fh);
207
208   FD_SET(fh, &r);
209   FD_SET(fh, &e);
210
211   if ( fh > max_fh )
212    max_fh = fh;
213  }
214
215  have_stream = 0;
216
217  for (j = 0; j < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; j++) {
218   if ( (fh = streams_get_fh(g_clients[i]->streams[j])) != -1 ) {
219    if ( fh > -1 ) {
220     FD_SET(fh, &r);
221
222     if ( fh > max_fh )
223      max_fh = fh;
224    }
225
226    have_stream = 1;
227   }
228   //printf("D: client=%i, stream=%i, fh=%i\n", i, j, fh);
229  }
230
231  if ( !have_stream && have_streamless < MAX_STREAMLESS ) {
232   streamless[have_streamless  ].id = i;
233   if ( (streamless[have_streamless++].fh = g_clients[i]->fh) == -1 )
234    have_streamless--;
235  }
236 }
237
238 if ( max_fh == -1 )
239  return 0;
240
241 if ( (ret = select(max_fh + 1, &r, NULL, &e, &tv)) < 1 ) {
242  return ret < 0 ? ret : have;
243 }
244
245 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
246  if ( g_clients[i] == NULL )
247   continue;
248
249  if ( (fh = g_clients[i]->fh) != -1 ) {
250   if ( FD_ISSET(fh, &r) ) {
251    if ( g_clients[i]->execed == -1 ) {
252     clients_check(i);
253     if ( g_clients[i] != NULL && g_clients[i]->execed != -1 ) {
254      FD_CLR(fh, &r);
255     }
256/*
257    } else {
258     streams_check(g_clients[i]->execed);
259*/
260    }
261   }
262
263   if ( FD_ISSET(fh, &e) ) {
264    clients_delete(i);
265    continue;
266   }
267  }
268
269  if ( g_clients[i] == NULL )
270   continue;
271
272  for (j = 0; j < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; j++) {
273   ROAR_DBG("clients_check_all(*): D: client=%i, stream=%i, g_clients[i=%i] = %p", i, j, i, g_clients[i]);
274   if ( g_clients[i] == NULL ) // streams_check() bellow can delete our client (why?)
275    break;
276
277   //ROAR_WARN("clients_check_all(*): client=%i: client exists", i);
278   ROAR_DBG("clients_check_all(*): client=%i, stream=%i: id=%i", i, j, g_clients[i]->streams[j]);
279
280   if ( (fh = streams_get_fh(g_clients[i]->streams[j])) != -1 ) {
281    ROAR_DBG("clients_check_all(*): client=%i, stream=%i: fh=%i", i, j, fh);
282    if ( fh == -2 ) {
283     streams_check(g_clients[i]->streams[j]);
284    } else if ( FD_ISSET(fh, &r) ) {
285     streams_check(g_clients[i]->streams[j]);
286    }
287   }
288  }
289 }
290
291 if ( have_streamless ) {
292   FD_ZERO(&r);
293
294   tv.tv_sec  = 0;
295   tv.tv_usec = 1;
296
297   max_fh = -1;
298
299   for (i = 0; i < have_streamless; i++) {
300    if ( ! g_clients[j = streamless[i].id] )
301     continue;
302
303    if ( g_clients[j]->execed != -1 )
304     continue;
305
306    fh = streamless[i].fh;
307
308    ROAR_DBG("clients_check_all(void): fh=%i", fh);
309    FD_SET(fh, &r);
310
311    if ( fh > max_fh )
312     max_fh = fh;
313   }
314
315   if ( (ret = select(max_fh + 1, &r, NULL, NULL, &tv)) < 0 ) {
316    return ret;
317   }
318
319   for (i = 0; i < have_streamless; i++) {
320    if ( FD_ISSET(streamless[i].fh, &r) ) {
321     clients_check(streamless[i].id);
322    }
323   }
324 }
325
326 ROAR_DBG("clients_check_all(void) = %i // have value", have);
327 return have;
328#else
329 return -1;
330#endif
331}
332
333int clients_check     (int id) {
334 struct roar_message    m;
335 struct roar_connection con;
336 char * data = NULL;
337 int oldcmd;
338 int r;
339 int rv = 0;
340
341 if ( g_clients[id] == NULL )
342  return -1;
343 if ( g_clients[id]->fh == -1 )
344  return -1;
345
346 roar_connect_fh(&con, g_clients[id]->fh);
347
348 switch (g_clients[id]->proto) {
349  case ROAR_PROTO_ROARAUDIO:
350    r = roar_recv_message(&con, &m, &data);
351
352    if ( r == -1 ) { // should we drop the client?
353     clients_delete(id);
354     return -1;
355    }
356
357    roar_debug_message_print(&m);
358
359    oldcmd = m.cmd;
360
361    if ( (r = command_exec(id, &m, data)) == -1 ) {
362     m.cmd     = ROAR_CMD_ERROR;
363     m.datalen = 0;
364     ROAR_DBG("clients_check(*): Exec of command faild!");
365    } else {
366     if ( m.cmd == oldcmd ) {
367      m.cmd     = ROAR_CMD_OK;
368      m.datalen = 0;
369     } else if ( m.cmd == ROAR_CMD_OK_STOP ) {
370      m.cmd     = ROAR_CMD_OK;
371      rv        = 1;
372     }
373    }
374
375    roar_send_message(&con, &m, NULL);
376   break;
377#ifndef ROAR_WITHOUT_DCOMP_EMUL_ESD
378#ifdef ROAR_HAVE_ESD
379  case ROAR_PROTO_ESOUND:
380    rv = emul_esd_check_client(id, NULL);
381   break;
382#endif
383#endif
384  default:
385    rv = -1;
386 }
387
388 if ( data )
389  free(data);
390
391 ROAR_DBG("clients_check(id=%i) = %i", id, rv);
392 return rv;
393}
394
395int clients_send_mon  (struct roar_audio_info * sa, uint32_t pos) {
396 int i;
397// int fh;
398 int j;
399
400 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
401  if ( g_clients[i] == NULL )
402   continue;
403
404/*
405  if ( (fh = g_clients[i]->fh) == -1 )
406   continue;
407*/
408
409  ROAR_DBG("clients_send_mon(*): client=%i, execed=%i", i, g_clients[i]->execed);
410
411  if ( g_clients[i]->execed == -1 ) {
412   // TODO: add some code to send a message to the client insetd of the raw data.
413   for (j = 0; j < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; j++) {
414    //if ( (fh = streams_get_fh(g_clients[i]->streams[j])) != -1 ) {
415    if ( g_clients[i]->streams[j] != -1 ) {
416     ROAR_DBG("clients_send_mon(*): client=%i, stream=%i -> %i", i, j, g_clients[i]->streams[j]);
417     streams_send_mon(g_clients[i]->streams[j]);
418    }
419   }
420  } else {
421//   streams_check(g_clients[i]->execed);
422   streams_send_mon(g_clients[i]->execed);
423//   if ( streams_send_mon(g_clients[i]->execed) == -1 )
424//    clients_delete(i); // delete client in case we could not write
425  }
426 }
427
428 // TODO: FIXME: should this really be -1?
429 return -1;
430}
431
432int clients_send_filter(struct roar_audio_info * sa, uint32_t pos) {
433 int i;
434 int fh;
435
436 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
437  if ( g_clients[i] == NULL )
438   continue;
439
440  if ( (fh = g_clients[i]->fh) == -1 )
441   continue;
442
443  if ( g_clients[i]->execed == -1 ) {
444   // TODO: add some code to send a message to the client insetd of the raw data.
445  } else {
446//   streams_check(g_clients[i]->execed);
447   streams_send_filter(g_clients[i]->execed);
448//   if ( streams_send_mon(g_clients[i]->execed) == -1 )
449//    clients_delete(i); // delete client in case we could not write
450  }
451 }
452
453 return -1;
454}
455
456int client_stream_exec   (int client, int stream) {
457 int i;
458
459 if ( g_clients[client] == NULL ) {
460  ROAR_WARN("client_stream_exec(client=%i, stream=%i) = -1 // client does not exist", client, stream);
461  return -1;
462 }
463
464 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
465  if ( g_clients[client]->streams[i] == stream ) {
466   g_clients[client]->execed = stream;
467   streams_set_fh(stream, g_clients[client]->fh);
468   streams_set_socktype(stream, ROAR_SOCKET_TYPE_GENSTR);
469   return 0;
470  }
471 }
472
473 ROAR_WARN("client_stream_exec(client=%i, stream=%i) = -1 // client does not own stream", client, stream);
474 return -1;
475}
476
477int client_stream_set_fh (int client, int stream, int fh) {
478 int i;
479
480 if ( g_clients[client] == NULL )
481  return -1;
482
483 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
484  if ( g_clients[client]->streams[i] == stream ) {
485   streams_set_fh(stream, fh);
486   return 0;
487  }
488 }
489
490 return -1;
491}
492
493int client_stream_add    (int client, int stream) {
494 int i;
495
496 if ( g_clients[client] == NULL )
497  return -1;
498
499 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
500  if ( g_clients[client]->streams[i] == -1 ) {
501   g_clients[client]->streams[i] = stream;
502   streams_set_client(stream, client);
503   return 0;
504  }
505 }
506
507 return -1;
508}
509
510int client_stream_delete (int client, int stream) {
511 int i;
512
513 if ( g_clients[client] == NULL )
514  return -1;
515
516 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
517  if ( g_clients[client]->streams[i] == stream ) {
518   g_clients[client]->streams[i] = -1;
519
520   if ( stream == g_clients[client]->execed ) {
521    ROAR_DBG("client_stream_delete(client=%i, stream=%i): stream is execed one, deleting client!", client, stream);
522    clients_delete(client);
523   }
524
525   ROAR_DBG("client_stream_delete(client=%i, stream=%i) = 0", client, stream);
526   return 0;
527  }
528 }
529
530 ROAR_DBG("client_stream_delete(client=%i, stream=%i) = -1", client, stream);
531 return -1;
532}
533
534int client_stream_move   (int client, int stream) {
535 int old_client = streams_get_client(stream);
536
537 ROAR_DBG("client_stream_move(client=%i, stream=%i): old_client = %i", client, stream, old_client);
538
539 if ( old_client != -1 )
540  if ( client_stream_delete(old_client, stream) == -1 )
541   return -1;
542
543 return client_stream_add(client, stream);
544}
545
546//ll
Note: See TracBrowser for help on using the repository browser.