source: roaraudio/roard/req.c @ 78:997d49ae275a

Last change on this file since 78:997d49ae275a was 78:997d49ae275a, checked in by phi, 16 years ago

added basic support to connect to streams of existing clients

File size: 8.5 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 if ( client_stream_set_fh(client, mes->stream, fh) == -1 ) {
122  close(fh);
123  return 1;
124 }
125
126 return 0;
127
128 mes->datalen = 0;
129 mes->cmd     = ROAR_CMD_OK;
130}
131
132
133int req_on_set_meta    (int client, struct roar_message * mes, char * data) {
134 return -1;
135}
136
137
138int req_on_server_oinfo    (int client, struct roar_message * mes, char * data) {
139 struct roar_stream s;
140//ROAR_DIR_OUTPUT
141
142 s.dir           = ROAR_DIR_OUTPUT;
143 s.pos_rel_id    = -1;
144 s.info.rate     = g_sa->rate;
145 s.info.bits     = g_sa->bits;
146 s.info.channels = g_sa->channels;
147 s.info.codec    = g_sa->codec;
148
149 if ( roar_stream_s2m(&s, mes) == -1 )
150  return -1;
151
152 mes->cmd = ROAR_CMD_OK;
153
154 return 0;
155}
156
157
158int req_on_get_standby (int client, struct roar_message * mes, char * data) {
159 mes->cmd = ROAR_CMD_OK;
160 mes->datalen = 2;
161
162 *((uint16_t*)mes->data) = ROAR_HOST2NET16((unsigned) g_standby);
163
164 return 0;
165}
166
167int req_on_set_standby (int client, struct roar_message * mes, char * data) {
168 if ( mes->datalen != 2 )
169  return -1;
170
171 g_standby = ROAR_NET2HOST16(*((uint16_t*)mes->data));
172
173 mes->cmd     = ROAR_CMD_OK;
174 mes->datalen = 0;
175
176 return 0;
177}
178
179int req_on_exit      (int client, struct roar_message * mes, char * data) {
180 mes->cmd     = ROAR_CMD_OK;
181 mes->datalen = 0;
182
183 alive = 0;
184
185 return 0;
186}
187
188int req_on_list_clients(int client, struct roar_message * mes, char * data) {
189 unsigned char filter, cmp;
190 uint32_t id;
191 int clients[ROAR_CLIENTS_MAX];
192 int i, c = 0;
193
194 if ( roar_ctl_m2f(mes, &filter, &cmp, &id) == -1 )
195  return -1;
196
197 // TODO: add code to support filter
198 if ( filter != ROAR_CTL_FILTER_ANY )
199  return -1;
200
201 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
202  if ( g_clients[i] != NULL ) {
203   clients[c++] = i;
204  }
205 }
206
207 roar_ctl_ia2m(mes, clients, c);
208
209 mes->cmd = ROAR_CMD_OK;
210
211 return 0;
212}
213int req_on_list_streams(int client, struct roar_message * mes, char * data) {
214 unsigned char filter, cmp;
215 uint32_t id;
216 int streams[ROAR_STREAMS_MAX];
217 int i, c = 0;
218
219 if ( roar_ctl_m2f(mes, &filter, &cmp, &id) == -1 )
220  return -1;
221
222 // TODO: add code to support filter
223 if ( filter != ROAR_CTL_FILTER_ANY )
224  return -1;
225
226 for (i = 0; i < ROAR_STREAMS_MAX; i++) {
227  if ( g_streams[i] != NULL ) {
228   streams[c++] = i;
229  }
230 }
231
232 roar_ctl_ia2m(mes, streams, c);
233
234 mes->cmd = ROAR_CMD_OK;
235
236 return 0;
237}
238
239int req_on_get_client  (int client, struct roar_message * mes, char * data) {
240 struct roar_client * c;
241
242 if ( mes->datalen != 1 )
243  return -1;
244
245 if ( clients_get(mes->data[0], &c) == -1 )
246  return -1;
247
248 mes->cmd = ROAR_CMD_OK;
249
250 return roar_ctl_c2m(mes, c);
251}
252
253int req_on_get_stream  (int client, struct roar_message * mes, char * data) {
254 struct roar_stream_server * s;
255
256 if ( mes->datalen != 1 )
257  return -1;
258
259 if ( streams_get(mes->data[0], &s) == -1 )
260  return -1;
261
262 mes->cmd = ROAR_CMD_OK;
263
264 return roar_stream_s2m(ROAR_STREAM(s), mes);
265}
266
267int req_on_kick (int client, struct roar_message * mes, char * data) {
268 uint16_t * info = (uint16_t *) mes->data;
269
270 if ( mes->datalen != 4 )
271  return -1;
272
273 if ( info[0] == ROAR_OT_CLIENT ) {
274  clients_delete(info[1]);
275 } else if ( info[0] == ROAR_OT_STREAM ) {
276  streams_delete(info[1]);
277 } else {
278  return -1;
279 }
280
281 mes->cmd     = ROAR_CMD_OK;
282 mes->datalen = 0;
283
284 return 0;
285}
286
287int req_on_set_vol (int client, struct roar_message * mes, char * data) {
288 uint16_t * info = (uint16_t *) mes->data;
289 int stream;
290 struct roar_stream_server * s;
291 int i;
292 int chans;
293
294 ROAR_DBG("req_on_set_vol(*) = ?");
295 ROAR_DBG("req_on_set_vol(*): mes->datalen=%i", mes->datalen);
296
297 if ( mes->datalen < (4*2) )
298  return -1;
299
300 if ( info[0] != 0 ) // version
301  return -1;
302
303 stream = info[1];
304 ROAR_DBG("req_on_set_vol(*): stream=%i", stream);
305
306 // TODO: change this code.
307 //       we should not directly change the stream object but use some stream_*()-func
308 //       for that job.
309
310 if ( stream < 0 || stream >= ROAR_STREAMS_MAX )
311  return -1;
312
313 s = g_streams[stream];
314
315 if ( s == NULL )
316  return -1;
317
318 ROAR_DBG("req_on_set_vol(*): s=%p", s);
319
320 if ( info[2] == ROAR_SET_VOL_ALL ) {
321  chans = (mes->datalen/2) - 3;
322  ROAR_DBG("req_on_set_vol(*): mode is ROAR_SET_VOL_ALL, channes=%i", chans);
323
324  if ( chans >= ROAR_MAX_CHANNELS )
325   return -1;
326
327  ROAR_DBG("req_on_set_vol(*): mixer at %p", s->mixer.mixer);
328
329  for (i = 0; i < chans; i++) {
330   s->mixer.mixer[i] = info[i+3];
331   ROAR_DBG("req_on_set_vol(*): channel %i: %i", i, info[i+3]);
332  }
333
334  ROAR_DBG("req_on_set_vol(*): mixer changed!");
335
336 } else if ( info[2] == ROAR_SET_VOL_ONE ) {
337  ROAR_DBG("req_on_set_vol(*): mode is ROAR_SET_VOL_ONE");
338  if ( info[3] >= ROAR_MAX_CHANNELS )
339   return -1;
340
341  s->mixer.mixer[info[3]] = info[4];
342 } else {
343  return -1;
344 }
345
346 mes->cmd     = ROAR_CMD_OK;
347 mes->datalen = 0;
348
349 return 0;
350}
351
352int req_on_get_vol (int client, struct roar_message * mes, char * data) {
353 uint16_t * info = (uint16_t *) mes->data;
354 int stream;
355 struct roar_stream_server * s;
356 int i;
357 int chans;
358
359 ROAR_DBG("req_on_get_vol(*) = ?");
360 ROAR_DBG("req_on_get_vol(*): mes->datalen=%i", mes->datalen);
361
362 if ( mes->datalen < (2*2) )
363  return -1;
364
365 if ( info[0] != 0 ) // version
366  return -1;
367
368 stream = info[1];
369 ROAR_DBG("req_on_get_vol(*): stream=%i", stream);
370
371 // TODO: change this code.
372 //       we should not directly change the stream object but use some stream_*()-func
373 //       for that job.
374
375 if ( stream < 0 || stream >= ROAR_STREAMS_MAX )
376  return -1;
377
378 s = g_streams[stream];
379
380 if ( s == NULL )
381  return -1;
382
383 ROAR_DBG("req_on_get_vol(*): s=%p", s);
384
385 // ok, we have everything
386
387 info[0] = 0;
388 info[1] = chans = ROAR_STREAM(s)->info.channels;
389
390 for (i = 0; i < chans; i++)
391  info[2+i] = s->mixer.mixer[i];
392
393 mes->datalen = (2 + chans)*2;
394 mes->cmd = ROAR_CMD_OK;
395
396 return 0;
397}
398
399int req_on_add_data (int client, struct roar_message * mes, char * data) {
400 struct roar_buffer * b;
401 char               * buf;
402
403 if ( roar_buffer_new(&b, mes->datalen) == -1 ) {
404  ROAR_ERR("req_on_add_data(*): Can not alloc buffer space!");
405  ROAR_DBG("req_on_add_data(*) = -1");
406  return -1;
407 }
408
409 roar_buffer_get_data(b, (void **)&buf);
410
411 if ( data == NULL ) {
412  memcpy(buf, mes->data, mes->datalen);
413 } else {
414  memcpy(buf, data, mes->datalen);
415 }
416
417 if ( stream_add_buffer(mes->stream, b) == -1 ) {
418  roar_buffer_free(b);
419  return -1;
420 }
421
422 mes->cmd     = ROAR_CMD_OK;
423 mes->datalen = 0;
424
425 return 0;
426}
427
428//ll
Note: See TracBrowser for help on using the repository browser.