source: roaraudio/roard/clients.c @ 1504:d00454bcfd80

Last change on this file since 1504:d00454bcfd80 was 1504:d00454bcfd80, checked in by phi, 15 years ago

use fh=-2 as specal *read me* info

File size: 10.3 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    if ( fh > -1 ) {
209     FD_SET(fh, &r);
210
211     if ( fh > max_fh )
212      max_fh = fh;
213    }
214
215    have_stream = 1;
216   }
217   //printf("D: client=%i, stream=%i, fh=%i\n", i, j, fh);
218  }
219
220  if ( !have_stream && have_streamless < MAX_STREAMLESS ) {
221   streamless[have_streamless  ].id = i;
222   if ( (streamless[have_streamless++].fh = g_clients[i]->fh) == -1 )
223    have_streamless--;
224  }
225 }
226
227 if ( max_fh == -1 )
228  return 0;
229
230 if ( (ret = select(max_fh + 1, &r, NULL, &e, &tv)) < 1 ) {
231  return ret < 0 ? ret : have;
232 }
233
234 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
235  if ( g_clients[i] == NULL )
236   continue;
237
238  if ( (fh = g_clients[i]->fh) != -1 ) {
239   if ( FD_ISSET(fh, &r) ) {
240    if ( g_clients[i]->execed == -1 ) {
241     clients_check(i);
242/*
243    } else {
244     streams_check(g_clients[i]->execed);
245*/
246    }
247   }
248
249   if ( FD_ISSET(fh, &e) ) {
250    clients_delete(i);
251    continue;
252   }
253  }
254
255  if ( g_clients[i] == NULL )
256   continue;
257
258  for (j = 0; j < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; j++) {
259   //printf("D: client=%i, stream=%i, g_clients[i=%i] = %p\n", i, j, i, g_clients[i]);
260   if ( g_clients[i] == NULL ) // streams_check() bellow can delete our client (why?)
261    break;
262   if ( (fh = streams_get_fh(g_clients[i]->streams[j])) != -1 ) {
263    if ( fh == -2 ) {
264     streams_check(g_clients[i]->streams[j]);
265    } else if ( FD_ISSET(fh, &r) ) {
266     streams_check(g_clients[i]->streams[j]);
267    }
268   }
269  }
270 }
271
272 if ( have_streamless ) {
273   FD_ZERO(&r);
274
275   tv.tv_sec  = 0;
276   tv.tv_usec = 1;
277
278   max_fh = -1;
279
280   for (i = 0; i < have_streamless; i++) {
281    if ( ! g_clients[j = streamless[i].id] )
282     continue;
283
284    if ( g_clients[j]->execed != -1 )
285     continue;
286
287    fh = streamless[i].fh;
288
289    ROAR_DBG("clients_check_all(void): fh=%i", fh);
290    FD_SET(fh, &r);
291
292    if ( fh > max_fh )
293     max_fh = fh;
294   }
295
296   if ( (ret = select(max_fh + 1, &r, NULL, NULL, &tv)) < 0 ) {
297    return ret;
298   }
299
300   for (i = 0; i < have_streamless; i++) {
301    if ( FD_ISSET(streamless[i].fh, &r) ) {
302     clients_check(streamless[i].id);
303    }
304   }
305 }
306
307 ROAR_DBG("clients_check_all(void) = %i // have value", have);
308 return have;
309#else
310 return -1;
311#endif
312}
313
314int clients_check     (int id) {
315 struct roar_message    m;
316 struct roar_connection con;
317 char * data = NULL;
318 int oldcmd;
319 int r;
320 int rv = 0;
321
322 if ( g_clients[id] == NULL )
323  return -1;
324 if ( g_clients[id]->fh == -1 )
325  return -1;
326
327 con.fh = g_clients[id]->fh;
328
329 r = roar_recv_message(&con, &m, &data);
330
331 if ( r == -1 ) { // should we drop the client?
332  clients_delete(id);
333  return -1;
334 }
335
336 roar_debug_message_print(&m);
337
338 oldcmd = m.cmd;
339
340 if ( (r = command_exec(id, &m, data)) == -1 ) {
341  m.cmd     = ROAR_CMD_ERROR;
342  m.datalen = 0;
343  ROAR_DBG("clients_check(*): Exec of command faild!");
344 } else {
345  if ( m.cmd == oldcmd ) {
346   m.cmd     = ROAR_CMD_OK;
347   m.datalen = 0;
348  } else if ( m.cmd == ROAR_CMD_OK_STOP ) {
349   m.cmd     = ROAR_CMD_OK;
350   rv        = 1;
351  }
352 }
353
354 roar_send_message(&con, &m, NULL);
355
356 if ( data )
357  free(data);
358
359 ROAR_DBG("clients_check(id=%i) = 0", id);
360 return rv;
361}
362
363int clients_send_mon  (struct roar_audio_info * sa, uint32_t pos) {
364 int i;
365// int fh;
366 int j;
367
368 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
369  if ( g_clients[i] == NULL )
370   continue;
371
372/*
373  if ( (fh = g_clients[i]->fh) == -1 )
374   continue;
375*/
376
377  if ( g_clients[i]->execed == -1 ) {
378   // TODO: add some code to send a message to the client insetd of the raw data.
379   for (j = 0; j < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; j++) {
380    //if ( (fh = streams_get_fh(g_clients[i]->streams[j])) != -1 ) {
381    ROAR_DBG("clients_send_mon(*): client=%i, stream=%i -> %i", i, j, g_clients[i]->streams[j]);
382    if ( g_clients[i]->streams[j] != -1 )
383     streams_send_mon(g_clients[i]->streams[j]);
384   }
385  } else {
386//   streams_check(g_clients[i]->execed);
387   streams_send_mon(g_clients[i]->execed);
388//   if ( streams_send_mon(g_clients[i]->execed) == -1 )
389//    clients_delete(i); // delete client in case we could not write
390  }
391 }
392
393 return -1;
394}
395
396int clients_send_filter(struct roar_audio_info * sa, uint32_t pos) {
397 int i;
398 int fh;
399
400 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
401  if ( g_clients[i] == NULL )
402   continue;
403
404  if ( (fh = g_clients[i]->fh) == -1 )
405   continue;
406
407  if ( g_clients[i]->execed == -1 ) {
408   // TODO: add some code to send a message to the client insetd of the raw data.
409  } else {
410//   streams_check(g_clients[i]->execed);
411   streams_send_filter(g_clients[i]->execed);
412//   if ( streams_send_mon(g_clients[i]->execed) == -1 )
413//    clients_delete(i); // delete client in case we could not write
414  }
415 }
416
417 return -1;
418}
419
420int client_stream_exec   (int client, int stream) {
421 int i;
422
423 if ( g_clients[client] == NULL )
424  return -1;
425
426 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
427  if ( g_clients[client]->streams[i] == stream ) {
428   g_clients[client]->execed = stream;
429   streams_set_fh(stream, g_clients[client]->fh);
430   streams_set_socktype(stream, ROAR_SOCKET_TYPE_GENSTR);
431   return 0;
432  }
433 }
434
435 return -1;
436}
437
438int client_stream_set_fh (int client, int stream, int fh) {
439 int i;
440
441 if ( g_clients[client] == NULL )
442  return -1;
443
444 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
445  if ( g_clients[client]->streams[i] == stream ) {
446   streams_set_fh(stream, fh);
447   return 0;
448  }
449 }
450
451 return -1;
452}
453
454int client_stream_add    (int client, int stream) {
455 int i;
456
457 if ( g_clients[client] == NULL )
458  return -1;
459
460 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
461  if ( g_clients[client]->streams[i] == -1 ) {
462   g_clients[client]->streams[i] = stream;
463   streams_set_client(stream, client);
464   return 0;
465  }
466 }
467
468 return -1;
469}
470
471int client_stream_delete (int client, int stream) {
472 int i;
473
474 if ( g_clients[client] == NULL )
475  return -1;
476
477 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
478  if ( g_clients[client]->streams[i] == stream ) {
479   g_clients[client]->streams[i] = -1;
480
481   if ( stream == g_clients[client]->execed ) {
482    ROAR_DBG("client_stream_delete(client=%i, stream=%i): stream is execed one, deleting client!", client, stream);
483    clients_delete(client);
484   }
485
486   ROAR_DBG("client_stream_delete(client=%i, stream=%i) = 0", client, stream);
487   return 0;
488  }
489 }
490
491 ROAR_DBG("client_stream_delete(client=%i, stream=%i) = -1", client, stream);
492 return -1;
493}
494
495int client_stream_move   (int client, int stream) {
496 int old_client = streams_get_client(stream);
497
498 ROAR_DBG("client_stream_move(client=%i, stream=%i): old_client = %i", client, stream, old_client);
499
500 if ( old_client != -1 )
501  if ( client_stream_delete(old_client, stream) == -1 )
502   return -1;
503
504 return client_stream_add(client, stream);
505}
506
507//ll
Note: See TracBrowser for help on using the repository browser.