source: roaraudio/roard/clients.c @ 1480:0f037c1b4e7f

Last change on this file since 1480:0f037c1b4e7f was 1480:0f037c1b4e7f, checked in by phi, 15 years ago

we can only check for new data from clients if we have ROAR_HAVE_SELECT

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