source: roaraudio/roard/clients.c @ 2608:9cf47c24c544

Last change on this file since 2608:9cf47c24c544 was 2608:9cf47c24c544, checked in by phi, 15 years ago

new debug lion

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