source: roaraudio/roard/clients.c @ 2598:bd895c2f1b4f

Last change on this file since 2598:bd895c2f1b4f was 2598:bd895c2f1b4f, checked in by phi, 15 years ago

add debug lion

File size: 11.6 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    ROAR_DBG("clients_check_all(*): g_clients[i=%i]->streams[j=%i] = %i, fh = %i", i, j, g_clients[i]->streams[j], fh);
220    if ( fh > -1 ) {
221     FD_SET(fh, &r);
222
223     if ( fh > max_fh )
224      max_fh = fh;
225    }
226
227    have_stream = 1;
228   }
229   //printf("D: client=%i, stream=%i, fh=%i\n", i, j, fh);
230  }
231
232  if ( !have_stream && have_streamless < MAX_STREAMLESS ) {
233   streamless[have_streamless  ].id = i;
234   if ( (streamless[have_streamless++].fh = g_clients[i]->fh) == -1 )
235    have_streamless--;
236  }
237 }
238
239 if ( max_fh == -1 )
240  return 0;
241
242 if ( (ret = select(max_fh + 1, &r, NULL, &e, &tv)) < 1 ) {
243  return ret < 0 ? ret : have;
244 }
245
246 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
247  if ( g_clients[i] == NULL )
248   continue;
249
250  if ( (fh = g_clients[i]->fh) != -1 ) {
251   if ( FD_ISSET(fh, &r) ) {
252    if ( g_clients[i]->execed == -1 ) {
253     clients_check(i);
254     if ( g_clients[i] != NULL && g_clients[i]->execed != -1 ) {
255      FD_CLR(fh, &r);
256     }
257/*
258    } else {
259     streams_check(g_clients[i]->execed);
260*/
261    }
262   }
263
264   if ( FD_ISSET(fh, &e) ) {
265    clients_delete(i);
266    continue;
267   }
268  }
269
270  if ( g_clients[i] == NULL )
271   continue;
272
273  for (j = 0; j < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; j++) {
274   ROAR_DBG("clients_check_all(*): D: client=%i, stream=%i, g_clients[i=%i] = %p", i, j, i, g_clients[i]);
275   if ( g_clients[i] == NULL ) // streams_check() bellow can delete our client (why?)
276    break;
277
278   //ROAR_WARN("clients_check_all(*): client=%i: client exists", i);
279   ROAR_DBG("clients_check_all(*): client=%i, stream=%i: id=%i", i, j, g_clients[i]->streams[j]);
280
281   if ( (fh = streams_get_fh(g_clients[i]->streams[j])) != -1 ) {
282    ROAR_DBG("clients_check_all(*): client=%i, stream=%i: fh=%i", i, j, fh);
283    if ( fh == -2 ) {
284     streams_check(g_clients[i]->streams[j]);
285    } else if ( FD_ISSET(fh, &r) ) {
286     streams_check(g_clients[i]->streams[j]);
287    }
288   }
289  }
290 }
291
292 if ( have_streamless ) {
293   FD_ZERO(&r);
294
295   tv.tv_sec  = 0;
296   tv.tv_usec = 1;
297
298   max_fh = -1;
299
300   for (i = 0; i < have_streamless; i++) {
301    if ( ! g_clients[j = streamless[i].id] )
302     continue;
303
304    if ( g_clients[j]->execed != -1 )
305     continue;
306
307    fh = streamless[i].fh;
308
309    ROAR_DBG("clients_check_all(void): fh=%i", fh);
310    FD_SET(fh, &r);
311
312    if ( fh > max_fh )
313     max_fh = fh;
314   }
315
316   if ( (ret = select(max_fh + 1, &r, NULL, NULL, &tv)) < 0 ) {
317    return ret;
318   }
319
320   for (i = 0; i < have_streamless; i++) {
321    if ( FD_ISSET(streamless[i].fh, &r) ) {
322     clients_check(streamless[i].id);
323    }
324   }
325 }
326
327 ROAR_DBG("clients_check_all(void) = %i // have value", have);
328 return have;
329#else
330 return -1;
331#endif
332}
333
334int clients_check     (int id) {
335 struct roar_message    m;
336 struct roar_connection con;
337 char * data = NULL;
338 int oldcmd;
339 int r;
340 int rv = 0;
341
342 if ( g_clients[id] == NULL )
343  return -1;
344 if ( g_clients[id]->fh == -1 )
345  return -1;
346
347 roar_connect_fh(&con, g_clients[id]->fh);
348
349 switch (g_clients[id]->proto) {
350  case ROAR_PROTO_ROARAUDIO:
351    r = roar_recv_message(&con, &m, &data);
352
353    if ( r == -1 ) { // should we drop the client?
354     clients_delete(id);
355     return -1;
356    }
357
358    roar_debug_message_print(&m);
359
360    oldcmd = m.cmd;
361
362    if ( (r = command_exec(id, &m, data)) == -1 ) {
363     m.cmd     = ROAR_CMD_ERROR;
364     m.datalen = 0;
365     ROAR_DBG("clients_check(*): Exec of command faild!");
366    } else {
367     if ( m.cmd == oldcmd ) {
368      m.cmd     = ROAR_CMD_OK;
369      m.datalen = 0;
370     } else if ( m.cmd == ROAR_CMD_OK_STOP ) {
371      m.cmd     = ROAR_CMD_OK;
372      rv        = 1;
373     }
374    }
375
376    roar_send_message(&con, &m, NULL);
377   break;
378#ifndef ROAR_WITHOUT_DCOMP_EMUL_ESD
379#ifdef ROAR_HAVE_ESD
380  case ROAR_PROTO_ESOUND:
381    rv = emul_esd_check_client(id, NULL);
382   break;
383#endif
384#endif
385  default:
386    rv = -1;
387 }
388
389 if ( data )
390  free(data);
391
392 ROAR_DBG("clients_check(id=%i) = %i", id, rv);
393 return rv;
394}
395
396int clients_send_mon  (struct roar_audio_info * sa, uint32_t pos) {
397 int i;
398// int fh;
399 int j;
400
401 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
402  if ( g_clients[i] == NULL )
403   continue;
404
405/*
406  if ( (fh = g_clients[i]->fh) == -1 )
407   continue;
408*/
409
410  ROAR_DBG("clients_send_mon(*): client=%i, execed=%i", i, g_clients[i]->execed);
411
412  if ( g_clients[i]->execed == -1 ) {
413   // TODO: add some code to send a message to the client insetd of the raw data.
414   for (j = 0; j < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; j++) {
415    //if ( (fh = streams_get_fh(g_clients[i]->streams[j])) != -1 ) {
416    if ( g_clients[i]->streams[j] != -1 ) {
417     ROAR_DBG("clients_send_mon(*): client=%i, stream=%i -> %i", i, j, g_clients[i]->streams[j]);
418     streams_send_mon(g_clients[i]->streams[j]);
419    }
420   }
421  } else {
422//   streams_check(g_clients[i]->execed);
423   streams_send_mon(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 // TODO: FIXME: should this really be -1?
430 return -1;
431}
432
433int clients_send_filter(struct roar_audio_info * sa, uint32_t pos) {
434 int i;
435 int fh;
436
437 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
438  if ( g_clients[i] == NULL )
439   continue;
440
441  if ( (fh = g_clients[i]->fh) == -1 )
442   continue;
443
444  if ( g_clients[i]->execed == -1 ) {
445   // TODO: add some code to send a message to the client insetd of the raw data.
446  } else {
447//   streams_check(g_clients[i]->execed);
448   streams_send_filter(g_clients[i]->execed);
449//   if ( streams_send_mon(g_clients[i]->execed) == -1 )
450//    clients_delete(i); // delete client in case we could not write
451  }
452 }
453
454 return -1;
455}
456
457int client_stream_exec   (int client, int stream) {
458 int i;
459
460 if ( g_clients[client] == NULL ) {
461  ROAR_WARN("client_stream_exec(client=%i, stream=%i) = -1 // client does not exist", client, stream);
462  return -1;
463 }
464
465 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
466  if ( g_clients[client]->streams[i] == stream ) {
467   g_clients[client]->execed = stream;
468   streams_set_fh(stream, g_clients[client]->fh);
469   streams_set_socktype(stream, ROAR_SOCKET_TYPE_GENSTR);
470   return 0;
471  }
472 }
473
474 ROAR_WARN("client_stream_exec(client=%i, stream=%i) = -1 // client does not own stream", client, stream);
475 return -1;
476}
477
478int client_stream_set_fh (int client, int stream, int fh) {
479 int i;
480
481 if ( g_clients[client] == NULL )
482  return -1;
483
484 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
485  if ( g_clients[client]->streams[i] == stream ) {
486   streams_set_fh(stream, fh);
487   return 0;
488  }
489 }
490
491 return -1;
492}
493
494int client_stream_add    (int client, int stream) {
495 int i;
496
497 if ( g_clients[client] == NULL )
498  return -1;
499
500 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
501  if ( g_clients[client]->streams[i] == -1 ) {
502   g_clients[client]->streams[i] = stream;
503   streams_set_client(stream, client);
504   return 0;
505  }
506 }
507
508 return -1;
509}
510
511int client_stream_delete (int client, int stream) {
512 int i;
513
514 if ( g_clients[client] == NULL )
515  return -1;
516
517 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
518  if ( g_clients[client]->streams[i] == stream ) {
519   g_clients[client]->streams[i] = -1;
520
521   if ( stream == g_clients[client]->execed ) {
522    ROAR_DBG("client_stream_delete(client=%i, stream=%i): stream is execed one, deleting client!", client, stream);
523    clients_delete(client);
524   }
525
526   ROAR_DBG("client_stream_delete(client=%i, stream=%i) = 0", client, stream);
527   return 0;
528  }
529 }
530
531 ROAR_DBG("client_stream_delete(client=%i, stream=%i) = -1", client, stream);
532 return -1;
533}
534
535int client_stream_move   (int client, int stream) {
536 int old_client = streams_get_client(stream);
537
538 ROAR_DBG("client_stream_move(client=%i, stream=%i): old_client = %i", client, stream, old_client);
539
540 if ( old_client != -1 )
541  if ( client_stream_delete(old_client, stream) == -1 )
542   return -1;
543
544 return client_stream_add(client, stream);
545}
546
547//ll
Note: See TracBrowser for help on using the repository browser.