source: roaraudio/roard/clients.c @ 255:ddde3ecf5754

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

make execed streams work again

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