source: roaraudio/roard/clients.c @ 2815:8cf175ac7bf9

Last change on this file since 2815:8cf175ac7bf9 was 2815:8cf175ac7bf9, checked in by phi, 15 years ago

added nnode support

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