/[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 735 - (show annotations) (download)
Tue Aug 16 09:48:42 2005 UTC (14 years, 7 months ago) by capela
File MIME type: text/plain
File size: 28551 byte(s)
* Added support to sampler channel MUTE/SOLO states:
   lscp_set_channel_mute();
   lscp_set_channel_solo();
  with corresponding new lscp_channel_info_t fields.

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

  ViewVC Help
Powered by ViewVC