source: roaraudio/roard/clients.c @ 2706:280ad02bb452

Last change on this file since 2706:280ad02bb452 was 2706:280ad02bb452, checked in by phi, 15 years ago

not only send data to execed stream but all streams of client

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