/[svn]/liblscp/trunk/examples/example_server.c
ViewVC logotype

Contents of /liblscp/trunk/examples/example_server.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 523 - (show annotations) (download)
Mon May 9 10:17:12 2005 UTC (14 years, 11 months ago) by capela
File MIME type: text/plain
File size: 27732 byte(s)
* [bug #9] Fixed for a LSCP command syntax convention
consistency, regarding the enumeration of available
sampler engines, Audio and MIDI drivers; this has
affected the signature of the following functions:
  lscp_get_available_engines();
  lscp_get_available_audio_drivers();
  lscp_get_available_midi_drivers();
which are now returning an integer count of engines
and drivers, respectively, while the following
functions are now being introduced:
  lscp_list_available_engines();
  lscp_list_available_audio_drivers();
  lscp_list_available_midi_drivers();
taking on the previous functionality, returning
a comma separated list of names.

1 // example_server.c
2 //
3 /****************************************************************************
4 Copyright (C) 2004-2005, rncbc aka Rui Nuno Capela. All rights reserved.
5
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation; either version 2
9 of the License, or (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19
20 *****************************************************************************/
21
22 #include "server.h"
23 #include "parser.h"
24
25 #include <time.h>
26
27 #define SERVER_PORT 8888
28
29 #if defined(WIN32)
30 static WSADATA _wsaData;
31 #endif
32
33 ////////////////////////////////////////////////////////////////////////
34
35 lscp_status_t server_callback ( lscp_connect_t *pConnect, const char *pchBuffer, int cchBuffer, void *pvData )
36 {
37 lscp_status_t ret = LSCP_OK;
38 lscp_parser_t tok;
39 const char *pszResult = NULL;
40 char szTemp[256];
41 int i;
42 static int iSamplerChannel = 0;
43 static int iAudioDevice = 0;
44 static int iMidiDevice = 0;
45
46 if (pchBuffer == NULL) {
47 fprintf(stderr, "server_callback: addr=%s port=%d: ",
48 inet_ntoa(pConnect->client.addr.sin_addr),
49 htons(pConnect->client.addr.sin_port));
50 switch (cchBuffer) {
51 case LSCP_CONNECT_OPEN:
52 fprintf(stderr, "New client connection.\n");
53 break;
54 case LSCP_CONNECT_CLOSE:
55 fprintf(stderr, "Connection closed.\n");
56 break;
57 }
58 return ret;
59 }
60
61 lscp_socket_trace("server_callback", &(pConnect->client.addr), pchBuffer, cchBuffer);
62
63 lscp_parser_init(&tok, pchBuffer, cchBuffer);
64
65 if (lscp_parser_test(&tok, "GET")) {
66 if (lscp_parser_test(&tok, "CHANNEL")) {
67 if (lscp_parser_test(&tok, "INFO")) {
68 // Getting sampler channel informations:
69 // GET CHANNEL INFO <sampler-channel>
70 pszResult = "ENGINE_NAME: DummyEngine\r\n"
71 "INSTRUMENT_FILE: DummyInstrument.gig\r\n"
72 "INSTRUMENT_NR: 0\r\n"
73 "INSTRUMENT_NAME: Dummy Instrument\r\n"
74 "INSTRUMENT_STATUS: 100\r\n"
75 "AUDIO_OUTPUT_DEVICE: 0\r\n"
76 "AUDIO_OUTPUT_CHANNELS: 2\r\n"
77 "AUDIO_OUTPUT_ROUTING: 0,1\r\n"
78 "MIDI_INPUT_DEVICE: 0\r\n"
79 "MIDI_INPUT_PORT: 0\r\n"
80 "MIDI_INPUT_CHANNEL: ALL\r\n"
81 "VOLUME: 0.5\r\n";
82 }
83 else if (lscp_parser_test(&tok, "VOICE_COUNT")) {
84 // Current number of active voices:
85 // GET CHANNEL VOICE_COUNT <sampler-channel>
86 sprintf(szTemp, "%d", rand() % 100);
87 pszResult = szTemp;
88 }
89 else if (lscp_parser_test(&tok, "STREAM_COUNT")) {
90 // Current number of active disk streams:
91 // GET CHANNEL STREAM_COUNT <sampler-channel>
92 pszResult = "3\r\n";
93 }
94 else if (lscp_parser_test(&tok, "BUFFER_FILL")) {
95 if (lscp_parser_test(&tok, "BYTES")) {
96 // Current fill state of disk stream buffers:
97 // GET CHANNEL BUFFER_FILL BYTES <sampler-channel>
98 sprintf(szTemp, "[1]%d,[2]%d,[3]%d\r\n", rand(), rand(), rand());
99 pszResult = szTemp;
100 }
101 else if (lscp_parser_test(&tok, "PERCENTAGE")) {
102 // Current fill state of disk stream buffers:
103 // GET CHANNEL BUFFER_FILL PERCENTAGE <sampler-channel>
104 sprintf(szTemp, "[1]%d%%,[2]%d%%,[3]%d%%\r\n", rand() % 100, rand() % 100, rand() % 100);
105 pszResult = szTemp;
106 }
107 else ret = LSCP_FAILED;
108 }
109 else ret = LSCP_FAILED;
110 }
111 else if (lscp_parser_test(&tok, "CHANNELS")) {
112 // Current number of sampler channels:
113 // GET CHANNELS
114 sprintf(szTemp, "%d", iSamplerChannel);
115 pszResult = szTemp;
116 }
117 else if (lscp_parser_test(&tok, "AVAILABLE_AUDIO_OUTPUT_DRIVERS")) {
118 // Getting all available audio output driver count.
119 // GET AVAILABLE_AUDIO_OUTPUT_DRIVERS
120 pszResult = "2\r\n";
121 }
122 else if (lscp_parser_test(&tok, "AVAILABLE_MIDI_INPUT_DRIVERS")) {
123 // Getting all available MIDI input driver count.
124 // GET AVAILABLE_MIDI_INPUT_DRIVERS
125 pszResult = "1\r\n";
126 }
127 else if (lscp_parser_test2(&tok, "AUDIO_OUTPUT_DRIVER", "INFO")) {
128 // Getting informations about a specific audio output driver.
129 // GET AUDIO_OUTPUT_DRIVER INFO <audio-output-type>
130 if (lscp_parser_test(&tok, "Alsa")) {
131 pszResult = "DESCRIPTION: 'ALSA PCM'\r\n"
132 "VERSION: '1.0'\r\n"
133 "PARAMETERS: channels,samplerate,active\r\n";
134 }
135 else if (lscp_parser_test(&tok, "Jack")) {
136 pszResult = "DESCRIPTION: JACK Audio Connection Kit\r\n"
137 "VERSION: 0.98.1\r\n"
138 "PARAMETERS: channels,samplerate,active\r\n";
139 }
140 else ret = LSCP_FAILED;
141 }
142 else if (lscp_parser_test2(&tok, "MIDI_INPUT_DRIVER", "INFO")) {
143 // Getting informations about a specific MIDI input driver.
144 // GET MIDI_INPUT_DRIVER INFO <midi-input-type>
145 if (lscp_parser_test(&tok, "Alsa")) {
146 pszResult = "DESCRIPTION: ALSA Sequencer\r\n"
147 "VERSION: 1.0\r\n"
148 "PARAMETERS: ports,active\r\n";
149 }
150 else ret = LSCP_FAILED;
151 }
152 else if (lscp_parser_test2(&tok, "AUDIO_OUTPUT_DRIVER_PARAMETER", "INFO")) {
153 // Getting informations about a specific audio output driver parameter.
154 // GET AUDIO_OUTPUT_DRIVER_PARAMETER INFO <audio-output-type> <param>
155 if (lscp_parser_test(&tok, "Alsa")) {
156 if (lscp_parser_test(&tok, "active")) {
157 pszResult = "DESCRIPTION: 'ALSA PCM active state'\r\n"
158 "TYPE: BOOL\r\n"
159 "MANDATORY: TRUE\r\n"
160 "FIX: TRUE\r\n"
161 "MULTIPLICITY: FALSE\r\n"
162 "DEPENDS: channels,samplerate,card\r\n"
163 "DEFAULT: TRUE\r\n"
164 "RANGE_MIN: FALSE\r\n"
165 "RANGE_MAX: TRUE\r\n"
166 "POSSIBILITIES: FALSE,TRUE\r\n";
167 }
168 else if (lscp_parser_test(&tok, "channels")) {
169 pszResult = "DESCRIPTION: 'Number of ALSA PCM channels'\r\n"
170 "TYPE: INT\r\n"
171 "MANDATORY: TRUE\r\n"
172 "FIX: TRUE\r\n"
173 "MULTIPLICITY: FALSE\r\n"
174 "DEFAULT: 2\r\n";
175 }
176 else if (lscp_parser_test(&tok, "samplerate")) {
177 pszResult = "DESCRIPTION: 'ALSA PCM sample rate'\r\n"
178 "TYPE: INT\r\n"
179 "MANDATORY: TRUE\r\n"
180 "FIX: TRUE\r\n"
181 "MULTIPLICITY: TRUE\r\n"
182 "DEFAULT: 44100\r\n"
183 "POSSIBILITIES: 44100,48000,96000\r\n";
184 }
185 else ret = LSCP_FAILED;
186 }
187 else if (lscp_parser_test(&tok, "Jack")) {
188 if (lscp_parser_test(&tok, "active")) {
189 pszResult = "DESCRIPTION: 'JACK active state'\r\n"
190 "TYPE: BOOL\r\n"
191 "MANDATORY: TRUE\r\n"
192 "FIX: TRUE\r\n"
193 "MULTIPLICITY: FALSE\r\n"
194 "DEPENDS: channels,samplerate\r\n"
195 "DEFAULT: TRUE\r\n"
196 "RANGE_MIN: FALSE\r\n"
197 "RANGE_MAX: TRUE\r\n"
198 "POSSIBILITIES: FALSE,TRUE\r\n";
199 }
200 else if (lscp_parser_test(&tok, "channels")) {
201 pszResult = "DESCRIPTION: 'Number of JACK audio channels'\r\n"
202 "TYPE: INT\r\n"
203 "MANDATORY: TRUE\r\n"
204 "FIX: TRUE\r\n"
205 "MULTIPLICITY: FALSE\r\n"
206 "DEFAULT: 2\r\n";
207 }
208 else if (lscp_parser_test(&tok, "samplerate")) {
209 pszResult = "DESCRIPTION: 'JACK sample rate'\r\n"
210 "TYPE: INT\r\n"
211 "MANDATORY: TRUE\r\n"
212 "FIX: TRUE\r\n"
213 "MULTIPLICITY: TRUE\r\n"
214 "DEFAULT: 44100\r\n"
215 "POSSIBILITIES: 44100,48000,96000\r\n";
216 }
217 else ret = LSCP_FAILED;
218 }
219 else ret = LSCP_FAILED;
220 }
221 else if (lscp_parser_test2(&tok, "MIDI_INPUT_DRIVER_PARAMETER", "INFO")) {
222 // Getting informations about a specific MIDI input driver parameter.
223 // GET MIDI_INPUT_DRIVER_PARAMETER INFO <midi-input-type> <param>
224 if (lscp_parser_test(&tok, "Alsa")) {
225 if (lscp_parser_test(&tok, "active")) {
226 pszResult = "DESCRIPTION: 'ALSA Sequencer device active state'\r\n"
227 "TYPE: BOOL\r\n"
228 "MANDATORY: TRUE\r\n"
229 "FIX: TRUE\r\n"
230 "MULTIPLICITY: FALSE\r\n"
231 "DEPENDS: channels,ports\r\n"
232 "DEFAULT: TRUE\r\n"
233 "RANGE_MIN: FALSE\r\n"
234 "RANGE_MAX: TRUE\r\n"
235 "POSSIBILITIES: FALSE,TRUE\r\n";
236 }
237 else if (lscp_parser_test(&tok, "ports")) {
238 pszResult = "DESCRIPTION: 'Number of ALSA Sequencer ports'\r\n"
239 "TYPE: INT\r\n"
240 "MANDATORY: FALSE\r\n"
241 "FIX: FALSE\r\n"
242 "MULTIPLICITY: FALSE\r\n"
243 "DEFAULT: 1\r\n"
244 "RANGE_MIN: 1\r\n"
245 "RANGE_MAX: 4\r\n";
246 }
247 else ret = LSCP_FAILED;
248 }
249 else ret = LSCP_FAILED;
250 }
251 else if (lscp_parser_test2(&tok, "AUDIO_OUTPUT_DEVICE", "INFO")) {
252 // Getting informations about a specific audio output device.
253 // GET AUDIO_OUTPUT_DEVICE INFO <audio-device-id>
254 if (lscp_parser_nextint(&tok) < iAudioDevice) {
255 pszResult = "driver: Alsa\r\n"
256 "active: TRUE\r\n"
257 "channels: 2\r\n"
258 "samplerate: 44100\r\n";
259 }
260 else ret = LSCP_FAILED;
261 }
262 else if (lscp_parser_test2(&tok, "MIDI_INPUT_DEVICE", "INFO")) {
263 // Getting informations about a specific MIDI input device.
264 // GET MIDI_INPUT_DEVICE INFO <midi-device-id>
265 if (lscp_parser_nextint(&tok) < iMidiDevice) {
266 pszResult = "driver: Alsa\r\n"
267 "active: TRUE\r\n"
268 "channels: 16\r\n"
269 "ports: 1\r\n";
270 }
271 else ret = LSCP_FAILED;
272 }
273 else if (lscp_parser_test2(&tok, "AUDIO_OUTPUT_CHANNEL", "INFO")) {
274 // Getting informations about an audio channel.
275 // GET AUDIO_OUTPUT_CHANNEL INFO <audio-device-id> <audio-channel>
276 if (lscp_parser_nextint(&tok) < iAudioDevice) {
277 pszResult = "name: DummyMonitor\r\n"
278 "is_mix_channel: FALSE\r\n"
279 "mix_channel_destination: 0\r\n";
280 }
281 else ret = LSCP_FAILED;
282 }
283 else if (lscp_parser_test2(&tok, "MIDI_INPUT_PORT", "INFO")) {
284 // Getting informations about a MIDI port.
285 // GET MIDI_INPUT_PORT INFO <midi-device-id> <midi-port>
286 if (lscp_parser_nextint(&tok) < iMidiDevice) {
287 pszResult = "name: DummyKeyboard\r\n"
288 "alsa_seq_bindings: '64:0'\r\n";
289 }
290 else ret = LSCP_FAILED;
291 }
292 else if (lscp_parser_test2(&tok, "AUDIO_OUTPUT_CHANNEL_PARAMETER", "INFO")) {
293 // Getting informations about specific audio channel parameter.
294 // GET AUDIO_OUTPUT_CHANNEL_PARAMETER INFO <audio-device-id> <audio-channel> <param>
295 if (lscp_parser_nextint(&tok) < iAudioDevice) {
296 lscp_parser_nextint(&tok);
297 if (lscp_parser_test(&tok, "is_mix_channel")) {
298 pszResult = "DESCRIPTION: 'Whether this is an audio mix channel'\r\n"
299 "TYPE: BOOL\r\n"
300 "MANDATORY: TRUE\r\n"
301 "FIX: FALSE\r\n"
302 "MULTIPLICITY: FALSE\r\n"
303 "POSSIBILITIES: FALSE,TRUE\r\n";
304 }
305 else if (lscp_parser_test(&tok, "mix_channel_destination")) {
306 pszResult = "DESCRIPTION: 'Audio mix channel destination'\r\n"
307 "TYPE: INT\r\n"
308 "MANDATORY: TRUE\r\n"
309 "FIX: FALSE\r\n"
310 "MULTIPLICITY: TRUE\r\n"
311 "POSSIBILITIES: 0,1\r\n";
312 }
313 else ret = LSCP_FAILED;
314 }
315 else ret = LSCP_FAILED;
316 }
317 else if (lscp_parser_test2(&tok, "MIDI_INPUT_PORT_PARAMETER", "INFO")) {
318 // Getting informations about specific MIDI port parameter.
319 // GET MIDI_INPUT_PORT_PARAMETER INFO <midi-device-id> <midi-port> <param>
320 if (lscp_parser_nextint(&tok) < iMidiDevice) {
321 lscp_parser_nextint(&tok);
322 if (lscp_parser_test(&tok, "alsa_seq_bindings")) {
323 pszResult = "DESCRIPTION: 'Alsa sequencer port bindings'\r\n"
324 "TYPE: STRING\r\n"
325 "MANDATORY: TRUE\r\n"
326 "FIX: FALSE\r\n"
327 "MULTIPLICITY: TRUE\r\n"
328 "POSSIBILITIES: '64:0','68:0','68:1'\r\n";
329 }
330 else ret = LSCP_FAILED;
331 }
332 else ret = LSCP_FAILED;
333 }
334 else if (lscp_parser_test(&tok, "AUDIO_OUTPUT_DEVICES")) {
335 // Getting all created audio output device count.
336 // GET AUDIO_OUTPUT_DEVICES
337 sprintf(szTemp, "%d\r\n", iAudioDevice);
338 pszResult = szTemp;
339 }
340 else if (lscp_parser_test(&tok, "MIDI_INPUT_DEVICES")) {
341 // Getting all created MID input device count.
342 // GET MIDI_INPUT_DEVICES
343 sprintf(szTemp, "%d\r\n", iMidiDevice);
344 pszResult = szTemp;
345 }
346 else if (lscp_parser_test(&tok, "AVAILABLE_ENGINES")) {
347 // Getting all available engine count:
348 // GET AVAILABLE_ENGINES
349 pszResult = "3\r\n";
350 }
351 else if (lscp_parser_test2(&tok, "ENGINE", "INFO")) {
352 // Getting information about an engine.
353 // GET ENGINE INFO <engine-name>
354 if (lscp_parser_test(&tok, "GigEngine")) {
355 pszResult = "DESCRIPTION: GigaSampler Engine\r\n"
356 "VERSION: 0.3\r\n";
357 }
358 else if (lscp_parser_test(&tok, "DLSEngine")) {
359 pszResult = "DESCRIPTION: 'DLS Generic Engine'\r\n"
360 "VERSION: 0.2\r\n";
361 }
362 else if (lscp_parser_test(&tok, "AkaiEngine")) {
363 pszResult = "DESCRIPTION: Akai Sampler Engine\r\n"
364 "VERSION: 0.1\r\n";
365 }
366 else ret = LSCP_FAILED;
367 }
368 else ret = LSCP_FAILED;
369 }
370 else if (lscp_parser_test(&tok, "LIST")) {
371 if (lscp_parser_test(&tok, "CHANNELS")) {
372 // Getting all created sampler channel list.
373 // LIST CHANNELS
374 if (iSamplerChannel > 0) {
375 strcpy(szTemp, "0");
376 for (i = 1; i < iSamplerChannel && strlen(szTemp) < sizeof(szTemp) - 8; i++)
377 sprintf(szTemp + strlen(szTemp), ",%d", i);
378 strcat(szTemp, "\r\n");
379 pszResult = szTemp;
380 }
381 else ret = LSCP_FAILED;
382 }
383 else if (lscp_parser_test(&tok, "AVAILABLE_ENGINES")) {
384 // Getting all available engines:
385 // LIST AVAILABLE_ENGINES
386 pszResult = "GigEngine,DLSEngine,AkaiEngine\r\n";
387 }
388 else if (lscp_parser_test(&tok, "AVAILABLE_AUDIO_OUTPUT_DRIVERS")) {
389 // Getting all available audio output drivers.
390 // LIST AVAILABLE_AUDIO_OUTPUT_DRIVERS
391 pszResult = "ALSA,JACK\r\n";
392 }
393 else if (lscp_parser_test(&tok, "AVAILABLE_MIDI_INPUT_DRIVERS")) {
394 // Getting all available MIDI input drivers.
395 // LIST AVAILABLE_MIDI_INPUT_DRIVERS
396 pszResult = "ALSA\r\n";
397 }
398 else if (lscp_parser_test(&tok, "AUDIO_OUTPUT_DEVICES")) {
399 // Getting all created audio output device list.
400 // LIST AUDIO_OUTPUT_DEVICES
401 if (iAudioDevice > 0) {
402 strcpy(szTemp, "0");
403 for (i = 1; i < iAudioDevice && strlen(szTemp) < sizeof(szTemp) - 8; i++)
404 sprintf(szTemp + strlen(szTemp), ",%d", i);
405 strcat(szTemp, "\r\n");
406 pszResult = szTemp;
407 }
408 else ret = LSCP_FAILED;
409 }
410 else if (lscp_parser_test(&tok, "MIDI_INPUT_DEVICES")) {
411 // Getting all created MID input device list.
412 // LIST MIDI_INPUT_DEVICES
413 if (iMidiDevice > 0) {
414 strcpy(szTemp, "0");
415 for (i = 1; i < iMidiDevice && strlen(szTemp) < sizeof(szTemp) - 8; i++)
416 sprintf(szTemp + strlen(szTemp), ",%d", i);
417 strcat(szTemp, "\r\n");
418 pszResult = szTemp;
419 }
420 else ret = LSCP_FAILED;
421 }
422 else ret = LSCP_FAILED;
423 }
424 else if (lscp_parser_test(&tok, "SET")) {
425 if (lscp_parser_test(&tok, "CHANNEL")) {
426 if (lscp_parser_test(&tok, "VOLUME")) {
427 // Setting channel volume:
428 // SET CHANNEL VOLUME <sampler-channel> <volume>
429 }
430 else if (lscp_parser_test(&tok, "AUDIO_OUTPUT_TYPE")) {
431 // Setting audio output type:
432 // SET CHANNEL AUDIO_OUTPUT_TYPE <sampler-channel> <audio-output-type>
433 }
434 else if (lscp_parser_test(&tok, "AUDIO_OUTPUT_DEVICE")) {
435 // Setting audio output device:
436 // SET CHANNEL AUDIO_OUTPUT_DEVICE <sampler-channel> <device-id>
437 }
438 else if (lscp_parser_test(&tok, "AUDIO_OUTPUT_CHANNEL")) {
439 // Setting audio output channel:
440 // SET CHANNEL AUDIO_OUTPUT_CHANNEL <sampler-channel> <audio-in> <audio-out>
441 }
442 else if (lscp_parser_test(&tok, "MIDI_INPUT_TYPE")) {
443 // Setting MIDI input type:
444 // SET CHANNEL MIDI_INPUT_TYPE <sampler-channel> <midi-input-type>
445 }
446 else if (lscp_parser_test(&tok, "MIDI_INPUT_DEVICE")) {
447 // Setting MIDI input device:
448 // SET CHANNEL MIDI_INPUT_DEVICE <sampler-channel> <device-id>
449 }
450 else if (lscp_parser_test(&tok, "MIDI_INPUT_PORT")) {
451 // Setting MIDI input port:
452 // SET CHANNEL MIDI_INPUT_PORT <sampler-channel> <midi-input-port>
453 }
454 else if (lscp_parser_test(&tok, "MIDI_INPUT_CHANNEL")) {
455 // Setting MIDI input channel:
456 // SET CHANNEL MIDI_INPUT_CHANNEL <sampler-channel> <midi-input-chan>
457 }
458 else ret = LSCP_FAILED;
459 }
460 else ret = LSCP_FAILED;
461 }
462 else if (lscp_parser_test(&tok, "LOAD")) {
463 if (lscp_parser_test(&tok, "ENGINE")) {
464 // Loading a sampler engine:
465 // LOAD ENGINE <engine-name> <sampler-channel>
466 }
467 else if (lscp_parser_test(&tok, "INSTRUMENT")) {
468 // Loading an instrument:
469 // LOAD INSTRUMENT [NON_MODAL] <filename> <instr-index> <sampler-channel>
470 }
471 else ret = LSCP_FAILED;
472 }
473 else if (lscp_parser_test2(&tok, "ADD", "CHANNEL")) {
474 // Adding a new sampler channel:
475 // ADD CHANNEL
476 if (iSamplerChannel < 16) {
477 sprintf(szTemp, "OK[%d]", iSamplerChannel++);
478 pszResult = szTemp;
479 } else {
480 iSamplerChannel = 0;
481 ret = LSCP_FAILED;
482 }
483 }
484 else if (lscp_parser_test2(&tok, "REMOVE", "CHANNEL")) {
485 // Removing a sampler channel:
486 // REMOVE CHANNEL <sampler-channel>
487 if (lscp_parser_nextint(&tok) < iSamplerChannel)
488 iSamplerChannel--;
489 else
490 ret = LSCP_FAILED;
491 }
492 else if (lscp_parser_test(&tok, "RESET")) {
493 if (lscp_parser_test(&tok, "CHANNEL")) {
494 // Resetting a sampler channel:
495 // RESET CHANNEL <sampler-channel>
496 if (lscp_parser_nextint(&tok) > iSamplerChannel)
497 ret = LSCP_FAILED;
498 } else {
499 // Reset sampler:
500 // RESET
501 iSamplerChannel = 0;
502 iAudioDevice = 0;
503 iMidiDevice = 0;
504 }
505 }
506 else if (lscp_parser_test(&tok, "CREATE")) {
507 if (lscp_parser_test(&tok, "AUDIO_OUTPUT_DEVICE")) {
508 // Creating an audio output device.
509 // CREATE AUDIO_OUTPUT_DEVICE <audio-output-driver> [<params>]
510 if (iAudioDevice < 8) {
511 sprintf(szTemp, "OK[%d]", iAudioDevice++);
512 pszResult = szTemp;
513 } else {
514 iAudioDevice = 0;
515 ret = LSCP_FAILED;
516 }
517 }
518 else if (lscp_parser_test(&tok, "MIDI_INPUT_DEVICE")) {
519 // Creating an MIDI input device.
520 // CREATE MIDI_INPUT_DEVICE <midi-input-driver> [<params>]
521 if (iMidiDevice < 8) {
522 sprintf(szTemp, "OK[%d]", iMidiDevice++);
523 pszResult = szTemp;
524 } else {
525 iMidiDevice = 0;
526 ret = LSCP_FAILED;
527 }
528 }
529 else ret = LSCP_FAILED;
530 }
531 else if (lscp_parser_test(&tok, "DESTROY")) {
532 if (lscp_parser_test(&tok, "AUDIO_OUTPUT_DEVICE")) {
533 // Destroying an audio output device.
534 // DESTROY AUDIO_OUTPUT_DEVICE <audio-device-id>
535 if (lscp_parser_nextint(&tok) < iAudioDevice)
536 iAudioDevice--;
537 else
538 ret = LSCP_FAILED;
539 }
540 else if (lscp_parser_test(&tok, "MIDI_INPUT_DEVICE")) {
541 // Destroying an MIDI intput device.
542 // DESTROY MIDI_INPUT_DEVICE <midi-device-id>
543 if (lscp_parser_nextint(&tok) < iMidiDevice)
544 iMidiDevice--;
545 else
546 ret = LSCP_FAILED;
547 }
548 else ret = LSCP_FAILED;
549 }
550 else if (lscp_parser_test(&tok, "SUBSCRIBE")) {
551 // Register frontend for receiving event notification messages:
552 // SUBSCRIBE <event>
553 ret = lscp_server_subscribe(pConnect, lscp_event_from_text(lscp_parser_next(&tok)));
554 }
555 else if (lscp_parser_test(&tok, "UNSUBSCRIBE")) {
556 // Deregister frontend for not receiving event notification messages anymore:
557 // UNSUBSCRIBE <event>
558 ret = lscp_server_unsubscribe(pConnect, lscp_event_from_text(lscp_parser_next(&tok)));
559 }
560 else if (lscp_parser_test(&tok, "QUIT")) {
561 // Close client connection:
562 // QUIT
563 lscp_parser_free(&tok);
564 return LSCP_FAILED; // Disconnect.
565 }
566 else ret = LSCP_FAILED;
567
568 lscp_parser_free(&tok);
569
570 if (pszResult == NULL)
571 pszResult = (ret == LSCP_OK ? "OK\r\n" : "ERR:1:Failed\r\n");
572
573 fprintf(stderr, "> %s", pszResult);
574
575 return lscp_server_result(pConnect, pszResult, strlen(pszResult));
576 }
577
578 ////////////////////////////////////////////////////////////////////////
579
580
581 void server_usage (void)
582 {
583 printf("\n %s %s (Build: %s)\n", lscp_server_package(), lscp_server_version(), lscp_server_build());
584
585 fputs("\n Available server commands: help, exit, quit, list", stdout);
586 fputs("\n (all else are broadcast verbatim to subscribers)\n\n", stdout);
587 }
588
589 void server_prompt (void)
590 {
591 fputs("lscp_server> ", stdout);
592 }
593
594 int main (int argc, char *argv[] )
595 {
596 lscp_server_t *pServer;
597 char szLine[200];
598 int cchLine;
599 lscp_connect_t *p;
600
601 #if defined(WIN32)
602 if (WSAStartup(MAKEWORD(1, 1), &_wsaData) != 0) {
603 fprintf(stderr, "lscp_server: WSAStartup failed.\n");
604 return -1;
605 }
606 #endif
607
608 srand(time(NULL));
609
610 pServer = lscp_server_create(SERVER_PORT, server_callback, NULL);
611 if (pServer == NULL)
612 return -1;
613
614 server_usage();
615 server_prompt();
616
617 while (fgets(szLine, sizeof(szLine), stdin)) {
618
619 cchLine = strlen(szLine);
620 while (cchLine > 0 && (szLine[cchLine - 1] == '\n' || szLine[cchLine - 1] == '\r'))
621 cchLine--;
622 szLine[cchLine] = '\0';
623
624 if (strcmp(szLine, "exit") == 0 || strcmp(szLine, "quit") == 0)
625 break;
626 else
627 if (strcmp(szLine, "list") == 0) {
628 for (p = pServer->connects.first; p; p = p->next) {
629 printf("client: sock=%d addr=%s port=%d events=0x%04x.\n",
630 p->client.sock,
631 inet_ntoa(p->client.addr.sin_addr),
632 ntohs(p->client.addr.sin_port),
633 (int) p->events
634 );
635 }
636 }
637 else
638 if (cchLine > 0 && strcmp(szLine, "help") != 0)
639 lscp_server_broadcast(pServer, LSCP_EVENT_MISCELLANEOUS, szLine, strlen(szLine));
640 else
641 server_usage();
642
643 server_prompt();
644 }
645
646 lscp_server_destroy(pServer);
647
648 #if defined(WIN32)
649 WSACleanup();
650 #endif
651
652 return 0;
653 }
654
655 // end of example_server.c

  ViewVC Help
Powered by ViewVC