source: roaraudio/roard/req.c @ 77:284968f15643

Last change on this file since 77:284968f15643 was 77:284968f15643, checked in by phi, 16 years ago

prepered a lot of code for multi-stream clientss

File size: 8.4 KB
Line 
1//req.c:
2
3#include "roard.h"
4
5int req_on_noop        (int client, struct roar_message * mes, char * data) {
6 mes->cmd     = ROAR_CMD_OK;
7 mes->datalen = 0;
8 return 0;
9}
10
11int req_on_identify    (int client, struct roar_message * mes, char * data) {
12 struct roar_client * c;
13 int max_len;
14
15 if ( mes->datalen < 1 )
16  return -1;
17
18 clients_get(client, &c);
19
20 if ( mes->data[0] == 1 ) {
21  c->pid       = ROAR_NET2HOST32(*(uint32_t*)((mes->data)+1));
22
23  max_len = (mes->datalen - 5) < (ROAR_BUFFER_NAME-1) ? (mes->datalen - 5) : (ROAR_BUFFER_NAME-1);
24
25  strncpy(c->name, mes->data + 5, max_len);
26  c->name[max_len] = 0;
27
28  mes->cmd     = ROAR_CMD_OK;
29  mes->datalen = 0;
30
31  ROAR_DBG("req_on_identify(*): client=%i, pid=%i", client, c->pid);
32  ROAR_DBG("req_on_identify(*) = 0");
33  return 0;
34 }
35
36 return -1;
37}
38
39int req_on_auth        (int client, struct roar_message * mes, char * data) {
40 // TODO: add code to support some auth.
41 mes->cmd     = ROAR_CMD_OK;
42 mes->datalen = 0;
43 return 0;
44}
45
46
47int req_on_new_stream  (int client, struct roar_message * mes, char * data) {
48 int stream;
49 struct roar_stream * s;
50
51 if ((stream = streams_new()) == -1 )
52  return -1;
53
54 if ( streams_get(stream, (struct roar_stream_server **)&s) == -1 ) {
55  streams_delete(stream);
56  return -1;
57 }
58
59 if ( client_stream_add(client, stream) == -1 ) {
60  streams_delete(stream);
61  return -1;
62 }
63
64 if ( roar_stream_m2s(s, mes) == -1 ) {
65  streams_delete(stream);
66  return -1;
67 }
68
69 mes->cmd     = ROAR_CMD_OK;
70 mes->stream  = stream;
71 mes->datalen = 0;
72
73 return 0;
74}
75
76int req_on_exec_stream (int client, struct roar_message * mes, char * data) {
77 int r;
78
79 if ( (r = client_stream_exec(client, mes->stream)) == -1 )
80  return -1;
81
82 mes->cmd     = ROAR_CMD_OK;
83 mes->datalen = 0;
84
85 return 0;
86}
87
88int req_on_con_stream  (int client, struct roar_message * mes, char * data) {
89 char   host[80] = {0};
90 int    port = 0;
91 int    type;
92 int    fh;
93 int    len;
94
95 if ( mes->datalen < 4 )
96  return -1;
97
98 if ( *(mes->data) != 0 )
99  return -1;
100
101 type = (unsigned)mes->data[1];
102 port = ((uint16_t*)mes->data)[1];
103
104 len = mes->datalen > 83 ? 79 : mes->datalen - 4;
105
106 strncmp(host, &(mes->data[4]), len);
107 host[len] = 0;
108
109 if ( type > ROAR_SOCKET_TYPE_MAX )
110  return -1;
111
112 if ( type == ROAR_SOCKET_TYPE_FILE ) // disabled because of security resons
113  return -1;
114
115 if ( type == ROAR_SOCKET_TYPE_FORK ) // why should we connect to ourself?
116  return -1;
117
118 if ( (fh = roar_socket_open(ROAR_SOCKET_MODE_CONNECT, type, host, port)) == -1 )
119  return -1;
120
121 close(fh);
122}
123
124
125int req_on_set_meta    (int client, struct roar_message * mes, char * data) {
126 return -1;
127}
128
129
130int req_on_server_oinfo    (int client, struct roar_message * mes, char * data) {
131 struct roar_stream s;
132//ROAR_DIR_OUTPUT
133
134 s.dir           = ROAR_DIR_OUTPUT;
135 s.pos_rel_id    = -1;
136 s.info.rate     = g_sa->rate;
137 s.info.bits     = g_sa->bits;
138 s.info.channels = g_sa->channels;
139 s.info.codec    = g_sa->codec;
140
141 if ( roar_stream_s2m(&s, mes) == -1 )
142  return -1;
143
144 mes->cmd = ROAR_CMD_OK;
145
146 return 0;
147}
148
149
150int req_on_get_standby (int client, struct roar_message * mes, char * data) {
151 mes->cmd = ROAR_CMD_OK;
152 mes->datalen = 2;
153
154 *((uint16_t*)mes->data) = ROAR_HOST2NET16((unsigned) g_standby);
155
156 return 0;
157}
158
159int req_on_set_standby (int client, struct roar_message * mes, char * data) {
160 if ( mes->datalen != 2 )
161  return -1;
162
163 g_standby = ROAR_NET2HOST16(*((uint16_t*)mes->data));
164
165 mes->cmd     = ROAR_CMD_OK;
166 mes->datalen = 0;
167
168 return 0;
169}
170
171int req_on_exit      (int client, struct roar_message * mes, char * data) {
172 mes->cmd     = ROAR_CMD_OK;
173 mes->datalen = 0;
174
175 alive = 0;
176
177 return 0;
178}
179
180int req_on_list_clients(int client, struct roar_message * mes, char * data) {
181 unsigned char filter, cmp;
182 uint32_t id;
183 int clients[ROAR_CLIENTS_MAX];
184 int i, c = 0;
185
186 if ( roar_ctl_m2f(mes, &filter, &cmp, &id) == -1 )
187  return -1;
188
189 // TODO: add code to support filter
190 if ( filter != ROAR_CTL_FILTER_ANY )
191  return -1;
192
193 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
194  if ( g_clients[i] != NULL ) {
195   clients[c++] = i;
196  }
197 }
198
199 roar_ctl_ia2m(mes, clients, c);
200
201 mes->cmd = ROAR_CMD_OK;
202
203 return 0;
204}
205int req_on_list_streams(int client, struct roar_message * mes, char * data) {
206 unsigned char filter, cmp;
207 uint32_t id;
208 int streams[ROAR_STREAMS_MAX];
209 int i, c = 0;
210
211 if ( roar_ctl_m2f(mes, &filter, &cmp, &id) == -1 )
212  return -1;
213
214 // TODO: add code to support filter
215 if ( filter != ROAR_CTL_FILTER_ANY )
216  return -1;
217
218 for (i = 0; i < ROAR_STREAMS_MAX; i++) {
219  if ( g_streams[i] != NULL ) {
220   streams[c++] = i;
221  }
222 }
223
224 roar_ctl_ia2m(mes, streams, c);
225
226 mes->cmd = ROAR_CMD_OK;
227
228 return 0;
229}
230
231int req_on_get_client  (int client, struct roar_message * mes, char * data) {
232 struct roar_client * c;
233
234 if ( mes->datalen != 1 )
235  return -1;
236
237 if ( clients_get(mes->data[0], &c) == -1 )
238  return -1;
239
240 mes->cmd = ROAR_CMD_OK;
241
242 return roar_ctl_c2m(mes, c);
243}
244
245int req_on_get_stream  (int client, struct roar_message * mes, char * data) {
246 struct roar_stream_server * s;
247
248 if ( mes->datalen != 1 )
249  return -1;
250
251 if ( streams_get(mes->data[0], &s) == -1 )
252  return -1;
253
254 mes->cmd = ROAR_CMD_OK;
255
256 return roar_stream_s2m(ROAR_STREAM(s), mes);
257}
258
259int req_on_kick (int client, struct roar_message * mes, char * data) {
260 uint16_t * info = (uint16_t *) mes->data;
261
262 if ( mes->datalen != 4 )
263  return -1;
264
265 if ( info[0] == ROAR_OT_CLIENT ) {
266  clients_delete(info[1]);
267 } else if ( info[0] == ROAR_OT_STREAM ) {
268  streams_delete(info[1]);
269 } else {
270  return -1;
271 }
272
273 mes->cmd     = ROAR_CMD_OK;
274 mes->datalen = 0;
275
276 return 0;
277}
278
279int req_on_set_vol (int client, struct roar_message * mes, char * data) {
280 uint16_t * info = (uint16_t *) mes->data;
281 int stream;
282 struct roar_stream_server * s;
283 int i;
284 int chans;
285
286 ROAR_DBG("req_on_set_vol(*) = ?");
287 ROAR_DBG("req_on_set_vol(*): mes->datalen=%i", mes->datalen);
288
289 if ( mes->datalen < (4*2) )
290  return -1;
291
292 if ( info[0] != 0 ) // version
293  return -1;
294
295 stream = info[1];
296 ROAR_DBG("req_on_set_vol(*): stream=%i", stream);
297
298 // TODO: change this code.
299 //       we should not directly change the stream object but use some stream_*()-func
300 //       for that job.
301
302 if ( stream < 0 || stream >= ROAR_STREAMS_MAX )
303  return -1;
304
305 s = g_streams[stream];
306
307 if ( s == NULL )
308  return -1;
309
310 ROAR_DBG("req_on_set_vol(*): s=%p", s);
311
312 if ( info[2] == ROAR_SET_VOL_ALL ) {
313  chans = (mes->datalen/2) - 3;
314  ROAR_DBG("req_on_set_vol(*): mode is ROAR_SET_VOL_ALL, channes=%i", chans);
315
316  if ( chans >= ROAR_MAX_CHANNELS )
317   return -1;
318
319  ROAR_DBG("req_on_set_vol(*): mixer at %p", s->mixer.mixer);
320
321  for (i = 0; i < chans; i++) {
322   s->mixer.mixer[i] = info[i+3];
323   ROAR_DBG("req_on_set_vol(*): channel %i: %i", i, info[i+3]);
324  }
325
326  ROAR_DBG("req_on_set_vol(*): mixer changed!");
327
328 } else if ( info[2] == ROAR_SET_VOL_ONE ) {
329  ROAR_DBG("req_on_set_vol(*): mode is ROAR_SET_VOL_ONE");
330  if ( info[3] >= ROAR_MAX_CHANNELS )
331   return -1;
332
333  s->mixer.mixer[info[3]] = info[4];
334 } else {
335  return -1;
336 }
337
338 mes->cmd     = ROAR_CMD_OK;
339 mes->datalen = 0;
340
341 return 0;
342}
343
344int req_on_get_vol (int client, struct roar_message * mes, char * data) {
345 uint16_t * info = (uint16_t *) mes->data;
346 int stream;
347 struct roar_stream_server * s;
348 int i;
349 int chans;
350
351 ROAR_DBG("req_on_get_vol(*) = ?");
352 ROAR_DBG("req_on_get_vol(*): mes->datalen=%i", mes->datalen);
353
354 if ( mes->datalen < (2*2) )
355  return -1;
356
357 if ( info[0] != 0 ) // version
358  return -1;
359
360 stream = info[1];
361 ROAR_DBG("req_on_get_vol(*): stream=%i", stream);
362
363 // TODO: change this code.
364 //       we should not directly change the stream object but use some stream_*()-func
365 //       for that job.
366
367 if ( stream < 0 || stream >= ROAR_STREAMS_MAX )
368  return -1;
369
370 s = g_streams[stream];
371
372 if ( s == NULL )
373  return -1;
374
375 ROAR_DBG("req_on_get_vol(*): s=%p", s);
376
377 // ok, we have everything
378
379 info[0] = 0;
380 info[1] = chans = ROAR_STREAM(s)->info.channels;
381
382 for (i = 0; i < chans; i++)
383  info[2+i] = s->mixer.mixer[i];
384
385 mes->datalen = (2 + chans)*2;
386 mes->cmd = ROAR_CMD_OK;
387
388 return 0;
389}
390
391int req_on_add_data (int client, struct roar_message * mes, char * data) {
392 struct roar_buffer * b;
393 char               * buf;
394
395 if ( roar_buffer_new(&b, mes->datalen) == -1 ) {
396  ROAR_ERR("req_on_add_data(*): Can not alloc buffer space!");
397  ROAR_DBG("req_on_add_data(*) = -1");
398  return -1;
399 }
400
401 roar_buffer_get_data(b, (void **)&buf);
402
403 if ( data == NULL ) {
404  memcpy(buf, mes->data, mes->datalen);
405 } else {
406  memcpy(buf, data, mes->datalen);
407 }
408
409 if ( stream_add_buffer(mes->stream, b) == -1 ) {
410  roar_buffer_free(b);
411  return -1;
412 }
413
414 mes->cmd     = ROAR_CMD_OK;
415 mes->datalen = 0;
416
417 return 0;
418}
419
420//ll
Note: See TracBrowser for help on using the repository browser.