source: roaraudio/roard/clients.c @ 767:7205e1dce16c

Last change on this file since 767:7205e1dce16c was 767:7205e1dce16c, checked in by phi, 16 years ago

added client_stream_move()

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