source: roaraudio/roard/clients.c @ 256:d4ff43612f7a

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

got codec filter vorbis basicly working

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->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 ( max_fh == -1 )
152  return 0;
153
154 if ( (ret = select(max_fh + 1, &r, NULL, &e, &tv)) < 1 ) {
155  return ret < 0 ? ret : have;
156 }
157
158 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
159  if ( g_clients[i] == NULL )
160   continue;
161
162  if ( (fh = g_clients[i]->fh) != -1 ) {
163   if ( FD_ISSET(fh, &r) ) {
164    if ( g_clients[i]->execed == -1 ) {
165     clients_check(i);
166/*
167    } else {
168     streams_check(g_clients[i]->execed);
169*/
170    }
171   }
172
173   if ( FD_ISSET(fh, &e) ) {
174    clients_delete(i);
175    continue;
176   }
177  }
178
179  if ( g_clients[i] == NULL )
180   continue;
181
182  for (j = 0; j < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; j++) {
183   //printf("D: client=%i, stream=%i, g_clients[i=%i] = %p\n", i, j, i, g_clients[i]);
184   if ( g_clients[i] == NULL ) // streams_check() bellow can delete our client (why?)
185    break;
186   if ( (fh = streams_get_fh(g_clients[i]->streams[j])) != -1 ) {
187    if ( FD_ISSET(fh, &r) ) {
188     streams_check(g_clients[i]->streams[j]);
189    }
190   }
191  }
192 }
193
194 ROAR_DBG("clients_check_all(void) = %i // have value", have);
195 return have;
196}
197
198int clients_check     (int id) {
199 struct roar_message    m;
200 struct roar_connection con;
201 char * data = NULL;
202 int oldcmd;
203 int r;
204
205 if ( g_clients[id] == NULL )
206  return -1;
207 if ( g_clients[id]->fh == -1 )
208  return -1;
209
210 con.fh = g_clients[id]->fh;
211
212 r = roar_recv_message(&con, &m, &data);
213
214 if ( r == -1 ) { // should we drop the client?
215  clients_delete(id);
216  return -1;
217 }
218
219 roar_debug_message_print(&m);
220
221 oldcmd = m.cmd;
222
223 if ( (r = command_exec(id, &m, data)) == -1 ) {
224  m.cmd     = ROAR_CMD_ERROR;
225  m.datalen = 0;
226  ROAR_DBG("clients_check(*): Exec of command faild!");
227 } else {
228  if ( m.cmd == oldcmd ) {
229   m.cmd     = ROAR_CMD_OK;
230   m.datalen = 0;
231  }
232 }
233
234 roar_send_message(&con, &m, NULL);
235
236 if ( data )
237  free(data);
238
239 ROAR_DBG("clients_check(id=%i) = 0", id);
240 return 0;
241}
242
243int clients_send_mon  (struct roar_audio_info * sa, uint32_t pos) {
244 int i;
245 int fh;
246
247 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
248  if ( g_clients[i] == NULL )
249   continue;
250
251  if ( (fh = g_clients[i]->fh) == -1 )
252   continue;
253
254  if ( g_clients[i]->execed == -1 ) {
255   // TODO: add some code to send a message to the client insetd of the raw data.
256  } else {
257//   streams_check(g_clients[i]->execed);
258   streams_send_mon(g_clients[i]->execed);
259//   if ( streams_send_mon(g_clients[i]->execed) == -1 )
260//    clients_delete(i); // delete client in case we could not write
261  }
262 }
263
264 return -1;
265}
266
267int clients_send_filter(struct roar_audio_info * sa, uint32_t pos) {
268 int i;
269 int fh;
270
271 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
272  if ( g_clients[i] == NULL )
273   continue;
274
275  if ( (fh = g_clients[i]->fh) == -1 )
276   continue;
277
278  if ( g_clients[i]->execed == -1 ) {
279   // TODO: add some code to send a message to the client insetd of the raw data.
280  } else {
281//   streams_check(g_clients[i]->execed);
282   streams_send_filter(g_clients[i]->execed);
283//   if ( streams_send_mon(g_clients[i]->execed) == -1 )
284//    clients_delete(i); // delete client in case we could not write
285  }
286 }
287
288 return -1;
289}
290
291int client_stream_exec   (int client, int stream) {
292 int i;
293
294 if ( g_clients[client] == NULL )
295  return -1;
296
297 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
298  if ( g_clients[client]->streams[i] == stream ) {
299   g_clients[client]->execed = stream;
300   streams_set_fh(stream, g_clients[client]->fh);
301   return 0;
302  }
303 }
304
305 return -1;
306}
307
308int client_stream_set_fh (int client, int stream, int fh) {
309 int i;
310
311 if ( g_clients[client] == NULL )
312  return -1;
313
314 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
315  if ( g_clients[client]->streams[i] == stream ) {
316   streams_set_fh(stream, fh);
317   return 0;
318  }
319 }
320
321 return -1;
322}
323
324int client_stream_add    (int client, int stream) {
325 int i;
326
327 if ( g_clients[client] == NULL )
328  return -1;
329
330 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
331  if ( g_clients[client]->streams[i] == -1 ) {
332   g_clients[client]->streams[i] = stream;
333   streams_set_client(stream, client);
334   return 0;
335  }
336 }
337
338 return -1;
339}
340
341int client_stream_delete (int client, int stream) {
342 int i;
343
344 if ( g_clients[client] == NULL )
345  return -1;
346
347 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
348  if ( g_clients[client]->streams[i] == stream ) {
349   g_clients[client]->streams[i] = -1;
350
351   if ( stream == g_clients[client]->execed ) {
352    ROAR_DBG("client_stream_delete(client=%i, stream=%i): stream is execed one, deleting client!", client, stream);
353    clients_delete(client);
354   }
355
356   ROAR_DBG("client_stream_delete(client=%i, stream=%i) = 0", client, stream);
357   return 0;
358  }
359 }
360
361 ROAR_DBG("client_stream_delete(client=%i, stream=%i) = -1", client, stream);
362 return -1;
363}
364
365//ll
Note: See TracBrowser for help on using the repository browser.