source: roaraudio/roard/clients.c @ 610:c422c1ec35fe

Last change on this file since 610:c422c1ec35fe was 610:c422c1ec35fe, checked in by phi, 16 years ago

added debuging line

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