source: roaraudio/roard/clients.c @ 2523:7f552c31ded6

Last change on this file since 2523:7f552c31ded6 was 2523:7f552c31ded6, checked in by phi, 15 years ago

use emul_esd_check_client() in case of esd client

File size: 11.4 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  case ROAR_PROTO_ESOUND:
378    rv = emul_esd_check_client(id, NULL);
379   break;
380  default:
381    rv = -1;
382 }
383
384 if ( data )
385  free(data);
386
387 ROAR_DBG("clients_check(id=%i) = %i", id, rv);
388 return rv;
389}
390
391int clients_send_mon  (struct roar_audio_info * sa, uint32_t pos) {
392 int i;
393// int fh;
394 int j;
395
396 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
397  if ( g_clients[i] == NULL )
398   continue;
399
400/*
401  if ( (fh = g_clients[i]->fh) == -1 )
402   continue;
403*/
404
405  ROAR_DBG("clients_send_mon(*): client=%i, execed=%i", i, g_clients[i]->execed);
406
407  if ( g_clients[i]->execed == -1 ) {
408   // TODO: add some code to send a message to the client insetd of the raw data.
409   for (j = 0; j < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; j++) {
410    //if ( (fh = streams_get_fh(g_clients[i]->streams[j])) != -1 ) {
411    if ( g_clients[i]->streams[j] != -1 ) {
412     ROAR_DBG("clients_send_mon(*): client=%i, stream=%i -> %i", i, j, g_clients[i]->streams[j]);
413     streams_send_mon(g_clients[i]->streams[j]);
414    }
415   }
416  } else {
417//   streams_check(g_clients[i]->execed);
418   streams_send_mon(g_clients[i]->execed);
419//   if ( streams_send_mon(g_clients[i]->execed) == -1 )
420//    clients_delete(i); // delete client in case we could not write
421  }
422 }
423
424 // TODO: FIXME: should this really be -1?
425 return -1;
426}
427
428int clients_send_filter(struct roar_audio_info * sa, uint32_t pos) {
429 int i;
430 int fh;
431
432 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
433  if ( g_clients[i] == NULL )
434   continue;
435
436  if ( (fh = g_clients[i]->fh) == -1 )
437   continue;
438
439  if ( g_clients[i]->execed == -1 ) {
440   // TODO: add some code to send a message to the client insetd of the raw data.
441  } else {
442//   streams_check(g_clients[i]->execed);
443   streams_send_filter(g_clients[i]->execed);
444//   if ( streams_send_mon(g_clients[i]->execed) == -1 )
445//    clients_delete(i); // delete client in case we could not write
446  }
447 }
448
449 return -1;
450}
451
452int client_stream_exec   (int client, int stream) {
453 int i;
454
455 if ( g_clients[client] == NULL ) {
456  ROAR_WARN("client_stream_exec(client=%i, stream=%i) = -1 // client does not exist", client, stream);
457  return -1;
458 }
459
460 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
461  if ( g_clients[client]->streams[i] == stream ) {
462   g_clients[client]->execed = stream;
463   streams_set_fh(stream, g_clients[client]->fh);
464   streams_set_socktype(stream, ROAR_SOCKET_TYPE_GENSTR);
465   return 0;
466  }
467 }
468
469 ROAR_WARN("client_stream_exec(client=%i, stream=%i) = -1 // client does not own stream", client, stream);
470 return -1;
471}
472
473int client_stream_set_fh (int client, int stream, int fh) {
474 int i;
475
476 if ( g_clients[client] == NULL )
477  return -1;
478
479 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
480  if ( g_clients[client]->streams[i] == stream ) {
481   streams_set_fh(stream, fh);
482   return 0;
483  }
484 }
485
486 return -1;
487}
488
489int client_stream_add    (int client, int stream) {
490 int i;
491
492 if ( g_clients[client] == NULL )
493  return -1;
494
495 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
496  if ( g_clients[client]->streams[i] == -1 ) {
497   g_clients[client]->streams[i] = stream;
498   streams_set_client(stream, client);
499   return 0;
500  }
501 }
502
503 return -1;
504}
505
506int client_stream_delete (int client, int stream) {
507 int i;
508
509 if ( g_clients[client] == NULL )
510  return -1;
511
512 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
513  if ( g_clients[client]->streams[i] == stream ) {
514   g_clients[client]->streams[i] = -1;
515
516   if ( stream == g_clients[client]->execed ) {
517    ROAR_DBG("client_stream_delete(client=%i, stream=%i): stream is execed one, deleting client!", client, stream);
518    clients_delete(client);
519   }
520
521   ROAR_DBG("client_stream_delete(client=%i, stream=%i) = 0", client, stream);
522   return 0;
523  }
524 }
525
526 ROAR_DBG("client_stream_delete(client=%i, stream=%i) = -1", client, stream);
527 return -1;
528}
529
530int client_stream_move   (int client, int stream) {
531 int old_client = streams_get_client(stream);
532
533 ROAR_DBG("client_stream_move(client=%i, stream=%i): old_client = %i", client, stream, old_client);
534
535 if ( old_client != -1 )
536  if ( client_stream_delete(old_client, stream) == -1 )
537   return -1;
538
539 return client_stream_add(client, stream);
540}
541
542//ll
Note: See TracBrowser for help on using the repository browser.