source: roaraudio/roard/clients.c @ 346:ab17e2447601

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

set acl to NULL

File size: 7.0 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->fh     = -1;
35
36    *n->name = 0;
37    *n->host = 0;
38
39    n->acl   = NULL;
40
41    n->execed = -1;
42    for (s = 0; s < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; s++)
43     n->streams[s] = -1;
44
45    g_clients[i] = n;
46
47    ROAR_DBG("clients_new(void) = %i", i);
48    return i;
49   } else {
50    ROAR_ERR("clients_new(void): Can not alloc memory for new client: %s", strerror(errno));
51    ROAR_ERR("clients_new(void) = -1");
52    return -1;
53   }
54  }
55 }
56
57 return -1;
58}
59
60int clients_delete (int id) {
61 int i;
62
63 if ( g_clients[id] == NULL )
64  return -1;
65
66 if (g_clients[id]->execed != -1) {
67//  return streams_delete(g_clients[id]->execed);
68  g_clients[id]->execed = -1;
69 }
70
71 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
72  streams_delete(g_clients[id]->streams[i]);
73 }
74
75 if ( g_clients[id]->fh != -1 )
76  close(g_clients[id]->fh);
77
78 free(g_clients[id]);
79 g_clients[id] = NULL;
80
81 ROAR_DBG("clients_delete(id=%i) = 0", id);
82 return 0;
83}
84
85int clients_get       (int id, struct roar_client ** client) {
86 *client = g_clients[id];
87
88 if ( *client == NULL )
89  return -1;
90
91 return 0;
92}
93
94int clients_set_fh    (int id, int    fh) {
95 if ( g_clients[id] == NULL )
96  return -1;
97
98 g_clients[id]->fh = fh;
99
100 return 0;
101}
102
103int clients_set_pid   (int id, int    pid) {
104 if ( g_clients[id] == NULL )
105  return -1;
106
107 g_clients[id]->pid = pid;
108
109 return 0;
110}
111
112int clients_check_all (void) {
113 struct timeval tv;
114 fd_set r, e;
115 int i, j;
116 int ret;
117 int fh;
118 int max_fh = -1;
119 int have = 0;
120
121 FD_ZERO(&r);
122 FD_ZERO(&e);
123
124 tv.tv_sec  = 0;
125 tv.tv_usec = 1;
126
127 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
128  if ( g_clients[i] == NULL )
129   continue;
130
131  if ( (fh = g_clients[i]->fh) != -1 ) {
132   have++;
133
134   FD_SET(fh, &r);
135   FD_SET(fh, &e);
136
137   if ( fh > max_fh )
138    max_fh = fh;
139  }
140
141  for (j = 0; j < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; j++) {
142   if ( (fh = streams_get_fh(g_clients[i]->streams[j])) != -1 ) {
143    FD_SET(fh, &r);
144
145    if ( fh > max_fh )
146     max_fh = fh;
147   }
148   //printf("D: client=%i, stream=%i, fh=%i\n", i, j, fh);
149  }
150
151 }
152
153 if ( max_fh == -1 )
154  return 0;
155
156 if ( (ret = select(max_fh + 1, &r, NULL, &e, &tv)) < 1 ) {
157  return ret < 0 ? ret : have;
158 }
159
160 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
161  if ( g_clients[i] == NULL )
162   continue;
163
164  if ( (fh = g_clients[i]->fh) != -1 ) {
165   if ( FD_ISSET(fh, &r) ) {
166    if ( g_clients[i]->execed == -1 ) {
167     clients_check(i);
168/*
169    } else {
170     streams_check(g_clients[i]->execed);
171*/
172    }
173   }
174
175   if ( FD_ISSET(fh, &e) ) {
176    clients_delete(i);
177    continue;
178   }
179  }
180
181  if ( g_clients[i] == NULL )
182   continue;
183
184  for (j = 0; j < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; j++) {
185   //printf("D: client=%i, stream=%i, g_clients[i=%i] = %p\n", i, j, i, g_clients[i]);
186   if ( g_clients[i] == NULL ) // streams_check() bellow can delete our client (why?)
187    break;
188   if ( (fh = streams_get_fh(g_clients[i]->streams[j])) != -1 ) {
189    if ( FD_ISSET(fh, &r) ) {
190     streams_check(g_clients[i]->streams[j]);
191    }
192   }
193  }
194 }
195
196 ROAR_DBG("clients_check_all(void) = %i // have value", have);
197 return have;
198}
199
200int clients_check     (int id) {
201 struct roar_message    m;
202 struct roar_connection con;
203 char * data = NULL;
204 int oldcmd;
205 int r;
206
207 if ( g_clients[id] == NULL )
208  return -1;
209 if ( g_clients[id]->fh == -1 )
210  return -1;
211
212 con.fh = g_clients[id]->fh;
213
214 r = roar_recv_message(&con, &m, &data);
215
216 if ( r == -1 ) { // should we drop the client?
217  clients_delete(id);
218  return -1;
219 }
220
221 roar_debug_message_print(&m);
222
223 oldcmd = m.cmd;
224
225 if ( (r = command_exec(id, &m, data)) == -1 ) {
226  m.cmd     = ROAR_CMD_ERROR;
227  m.datalen = 0;
228  ROAR_DBG("clients_check(*): Exec of command faild!");
229 } else {
230  if ( m.cmd == oldcmd ) {
231   m.cmd     = ROAR_CMD_OK;
232   m.datalen = 0;
233  }
234 }
235
236 roar_send_message(&con, &m, NULL);
237
238 if ( data )
239  free(data);
240
241 ROAR_DBG("clients_check(id=%i) = 0", id);
242 return 0;
243}
244
245int clients_send_mon  (struct roar_audio_info * sa, uint32_t pos) {
246 int i;
247 int fh;
248
249 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
250  if ( g_clients[i] == NULL )
251   continue;
252
253  if ( (fh = g_clients[i]->fh) == -1 )
254   continue;
255
256  if ( g_clients[i]->execed == -1 ) {
257   // TODO: add some code to send a message to the client insetd of the raw data.
258  } else {
259//   streams_check(g_clients[i]->execed);
260   streams_send_mon(g_clients[i]->execed);
261//   if ( streams_send_mon(g_clients[i]->execed) == -1 )
262//    clients_delete(i); // delete client in case we could not write
263  }
264 }
265
266 return -1;
267}
268
269int clients_send_filter(struct roar_audio_info * sa, uint32_t pos) {
270 int i;
271 int fh;
272
273 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
274  if ( g_clients[i] == NULL )
275   continue;
276
277  if ( (fh = g_clients[i]->fh) == -1 )
278   continue;
279
280  if ( g_clients[i]->execed == -1 ) {
281   // TODO: add some code to send a message to the client insetd of the raw data.
282  } else {
283//   streams_check(g_clients[i]->execed);
284   streams_send_filter(g_clients[i]->execed);
285//   if ( streams_send_mon(g_clients[i]->execed) == -1 )
286//    clients_delete(i); // delete client in case we could not write
287  }
288 }
289
290 return -1;
291}
292
293int client_stream_exec   (int client, int stream) {
294 int i;
295
296 if ( g_clients[client] == NULL )
297  return -1;
298
299 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
300  if ( g_clients[client]->streams[i] == stream ) {
301   g_clients[client]->execed = stream;
302   streams_set_fh(stream, g_clients[client]->fh);
303   return 0;
304  }
305 }
306
307 return -1;
308}
309
310int client_stream_set_fh (int client, int stream, int fh) {
311 int i;
312
313 if ( g_clients[client] == NULL )
314  return -1;
315
316 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
317  if ( g_clients[client]->streams[i] == stream ) {
318   streams_set_fh(stream, fh);
319   return 0;
320  }
321 }
322
323 return -1;
324}
325
326int client_stream_add    (int client, int stream) {
327 int i;
328
329 if ( g_clients[client] == NULL )
330  return -1;
331
332 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
333  if ( g_clients[client]->streams[i] == -1 ) {
334   g_clients[client]->streams[i] = stream;
335   streams_set_client(stream, client);
336   return 0;
337  }
338 }
339
340 return -1;
341}
342
343int client_stream_delete (int client, int stream) {
344 int i;
345
346 if ( g_clients[client] == NULL )
347  return -1;
348
349 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
350  if ( g_clients[client]->streams[i] == stream ) {
351   g_clients[client]->streams[i] = -1;
352
353   if ( stream == g_clients[client]->execed ) {
354    ROAR_DBG("client_stream_delete(client=%i, stream=%i): stream is execed one, deleting client!", client, stream);
355    clients_delete(client);
356   }
357
358   ROAR_DBG("client_stream_delete(client=%i, stream=%i) = 0", client, stream);
359   return 0;
360  }
361 }
362
363 ROAR_DBG("client_stream_delete(client=%i, stream=%i) = -1", client, stream);
364 return -1;
365}
366
367//ll
Note: See TracBrowser for help on using the repository browser.