source: roaraudio/roard/clients.c @ 3255:970b81dc7c18

Last change on this file since 3255:970b81dc7c18 was 3255:970b81dc7c18, checked in by phi, 14 years ago

got PA simple protocol basicly to work

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