source: roaraudio/roard/clients.c @ 66:a75c2005faeb

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

finaly got sources working!

File size: 6.3 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
118 FD_ZERO(&r);
119 FD_ZERO(&e);
120
121 tv.tv_sec  = 0;
122 tv.tv_usec = 1;
123
124 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
125  if ( g_clients[i] == NULL )
126   continue;
127
128  if ( (fh = g_clients[i]->fh) != -1 ) {
129   FD_SET(fh, &r);
130   FD_SET(fh, &e);
131
132   if ( fh > max_fh )
133    max_fh = fh;
134  } else {
135
136   for (j = 0; j < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; j++) {
137    if ( (fh = streams_get_fh(g_clients[i]->streams[j])) != -1 ) {
138     FD_SET(fh, &r);
139
140     if ( fh > max_fh )
141      max_fh = fh;
142    }
143   }
144  }
145
146 }
147
148 if ( (ret = select(max_fh + 1, &r, NULL, &e, &tv)) < 1 ) {
149  return ret;
150 }
151
152 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
153  if ( g_clients[i] == NULL )
154   continue;
155
156  if ( (fh = g_clients[i]->fh) != -1 ) {
157
158   if ( FD_ISSET(fh, &r) ) {
159    if ( g_clients[i]->execed == -1 ) {
160     clients_check(i);
161    } else {
162     streams_check(g_clients[i]->execed);
163    }
164   }
165
166   if ( FD_ISSET(fh, &e) )
167    clients_delete(i);
168  } else {
169   for (j = 0; j < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; j++) {
170    if ( (fh = streams_get_fh(g_clients[i]->streams[j])) != -1 ) {
171     if ( FD_ISSET(fh, &r) ) {
172      streams_check(g_clients[i]->streams[j]);
173     }
174    }
175   }
176  }
177 }
178
179 return 0;
180}
181
182int clients_check     (int id) {
183 struct roar_message    m;
184 struct roar_connection con;
185 char * data = NULL;
186 int oldcmd;
187 int r;
188
189 if ( g_clients[id] == NULL )
190  return -1;
191 if ( g_clients[id]->fh == -1 )
192  return -1;
193
194 con.fh = g_clients[id]->fh;
195
196 r = roar_recv_message(&con, &m, &data);
197
198 if ( r == -1 ) { // should we drop the client?
199  clients_delete(id);
200  return -1;
201 }
202
203 roar_debug_message_print(&m);
204
205 oldcmd = m.cmd;
206
207 if ( (r = command_exec(id, &m, data)) == -1 ) {
208  m.cmd     = ROAR_CMD_ERROR;
209  m.datalen = 0;
210  ROAR_DBG("clients_check(*): Exec of command faild!");
211 } else {
212  if ( m.cmd == oldcmd ) {
213   m.cmd     = ROAR_CMD_OK;
214   m.datalen = 0;
215  }
216 }
217
218 roar_send_message(&con, &m, NULL);
219
220 if ( data )
221  free(data);
222
223 ROAR_DBG("clients_check(id=%i) = 0", id);
224 return 0;
225}
226
227int clients_send_mon  (struct roar_audio_info * sa, uint32_t pos) {
228 int i;
229 int fh;
230
231 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
232  if ( g_clients[i] == NULL )
233   continue;
234
235  if ( (fh = g_clients[i]->fh) == -1 )
236   continue;
237
238  if ( g_clients[i]->execed == -1 ) {
239   // TODO: add some code to send a message to the client insetd of the raw data.
240  } else {
241//   streams_check(g_clients[i]->execed);
242   streams_send_mon(g_clients[i]->execed);
243//   if ( streams_send_mon(g_clients[i]->execed) == -1 )
244//    clients_delete(i); // delete client in case we could not write
245  }
246 }
247
248 return -1;
249}
250
251int clients_send_filter(struct roar_audio_info * sa, uint32_t pos) {
252 int i;
253 int fh;
254
255 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
256  if ( g_clients[i] == NULL )
257   continue;
258
259  if ( (fh = g_clients[i]->fh) == -1 )
260   continue;
261
262  if ( g_clients[i]->execed == -1 ) {
263   // TODO: add some code to send a message to the client insetd of the raw data.
264  } else {
265//   streams_check(g_clients[i]->execed);
266   streams_send_filter(g_clients[i]->execed);
267//   if ( streams_send_mon(g_clients[i]->execed) == -1 )
268//    clients_delete(i); // delete client in case we could not write
269  }
270 }
271
272 return -1;
273}
274
275int client_stream_exec   (int client, int stream) {
276 int i;
277
278 if ( g_clients[client] == NULL )
279  return -1;
280
281 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
282  if ( g_clients[client]->streams[i] == stream ) {
283   g_clients[client]->execed = stream;
284   streams_set_fh(stream, g_clients[client]->fh);
285   return 0;
286  }
287 }
288
289 return -1;
290}
291
292int client_stream_add    (int client, int stream) {
293 int i;
294
295 if ( g_clients[client] == NULL )
296  return -1;
297
298 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
299  if ( g_clients[client]->streams[i] == -1 ) {
300   g_clients[client]->streams[i] = stream;
301   streams_set_client(stream, client);
302   return 0;
303  }
304 }
305
306 return -1;
307}
308
309int client_stream_delete (int client, int stream) {
310 int i;
311
312 if ( g_clients[client] == NULL )
313  return -1;
314
315 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
316  if ( g_clients[client]->streams[i] == stream ) {
317   g_clients[client]->streams[i] = -1;
318
319   if ( stream == g_clients[client]->execed ) {
320    ROAR_DBG("client_stream_delete(client=%i, stream=%i): stream is execed one, deleting client!", client, stream);
321    clients_delete(client);
322   }
323
324   ROAR_DBG("client_stream_delete(client=%i, stream=%i) = 0", client, stream);
325   return 0;
326  }
327 }
328
329 ROAR_DBG("client_stream_delete(client=%i, stream=%i) = -1", client, stream);
330 return -1;
331}
332
333//ll
Note: See TracBrowser for help on using the repository browser.