source: roaraudio/roard/clients.c @ 668:71ac426690da

Last change on this file since 668:71ac426690da was 668:71ac426690da, checked in by phi, 16 years ago

added license statements

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