/[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 177 - (show annotations) (download)
Tue Jul 6 14:06:17 2004 UTC (17 years, 11 months ago) by capela
File MIME type: text/plain
File size: 19777 byte(s)
Simple use of condition variable on event subscription synchronization.

1 // example_server.c
2 //
3 /****************************************************************************
4 Copyright (C) 2004, 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_STATUS: 100\r\n"
74 "AUDIO_OUTPUT_DEVICE: 0\r\n"
75 "AUDIO_OUTPUT_CHANNELS: 2\r\n"
76 "AUDIO_OUTPUT_ROUTING: 0,1\r\n"
77 "MIDI_INPUT_DEVICE: 0\r\n"
78 "MIDI_INPUT_PORT: 0\r\n"
79 "MIDI_INPUT_CHANNEL: ALL\r\n"
80 "VOLUME: 0.5\r\n";
81 }
82 else if (lscp_parser_test(&tok, "VOICE_COUNT")) {
83 // Current number of active voices:
84 // GET CHANNEL VOICE_COUNT <sampler-channel>
85 sprintf(szTemp, "%d", rand() % 100);
86 pszResult = szTemp;
87 }
88 else if (lscp_parser_test(&tok, "STREAM_COUNT")) {
89 // Current number of active disk streams:
90 // GET CHANNEL STREAM_COUNT <sampler-channel>
91 pszResult = "3\r\n";
92 }
93 else if (lscp_parser_test(&tok, "BUFFER_FILL")) {
94 if (lscp_parser_test(&tok, "BYTES")) {
95 // Current fill state of disk stream buffers:
96 // GET CHANNEL BUFFER_FILL BYTES <sampler-channel>
97 sprintf(szTemp, "[1]%d,[2]%d,[3]%d\r\n", rand(), rand(), rand());
98 pszResult = szTemp;
99 }
100 else if (lscp_parser_test(&tok, "PERCENTAGE")) {
101 // Current fill state of disk stream buffers:
102 // GET CHANNEL BUFFER_FILL PERCENTAGE <sampler-channel>
103 sprintf(szTemp, "[1]%d%%,[2]%d%%,[3]%d%%\r\n", rand() % 100, rand() % 100, rand() % 100);
104 pszResult = szTemp;
105 }
106 else ret = LSCP_FAILED;
107 }
108 else ret = LSCP_FAILED;
109 }
110 else if (lscp_parser_test(&tok, "CHANNELS")) {
111 // Current number of sampler channels:
112 // GET CHANNELS
113 sprintf(szTemp, "%d", iSamplerChannel);
114 pszResult = szTemp;
115 }
116 else if (lscp_parser_test(&tok, "AVAILABLE_AUDIO_OUTPUT_DRIVERS")) {
117 // Getting all available audio output drivers.
118 // GET AVAILABLE_AUDIO_OUTPUT_DRIVERS
119 pszResult = "Alsa,Jack,CoreAudio\r\n";
120 }
121 else if (lscp_parser_test(&tok, "AVAILABLE_MIDI_INPUT_DRIVERS")) {
122 // Getting all available MIDI input drivers.
123 // GET AVAILABLE_MIDI_INPUT_DRIVERS
124 pszResult = "Alsa,MidiShare,CoreMidi\r\n";
125 }
126 else if (lscp_parser_test2(&tok, "AUDIO_OUTPUT_DRIVER", "INFO")) {
127 // Getting informations about a specific audio output driver.
128 // GET AUDIO_OUTPUT_DRIVER INFO <audio-output-type>
129 if (lscp_parser_test(&tok, "Alsa")) {
130 pszResult = "DESCRIPTION: 'ALSA PCM'\r\n"
131 "VERSION: '1.0'\r\n"
132 "PARAMETERS: channels,samplerate,active,card\r\n";
133 }
134 else if (lscp_parser_test(&tok, "Jack")) {
135 pszResult = "DESCRIPTION: JACK Audio Connection Kit\r\n"
136 "VERSION: 0.98.1\r\n"
137 "PARAMETERS: channels,samplerate,active\r\n";
138 }
139 else ret = LSCP_FAILED;
140 }
141 else if (lscp_parser_test2(&tok, "MIDI_INPUT_DRIVER", "INFO")) {
142 // Getting informations about a specific MIDI input driver.
143 // GET MIDI_INPUT_DRIVER INFO <midi-input-type>
144 if (lscp_parser_test(&tok, "Alsa")) {
145 pszResult = "DESCRIPTION: ALSA Sequencer\r\n"
146 "VERSION: 1.0\r\n"
147 "PARAMETERS: ports,active\r\n";
148 }
149 else ret = LSCP_FAILED;
150 }
151 else if (lscp_parser_test2(&tok, "AUDIO_OUTPUT_DRIVER_PARAMETER", "INFO")) {
152 // Getting informations about a specific audio output driver parameter.
153 // GET AUDIO_OUTPUT_DRIVER_PARAMETER INFO <audio-output-type> <param>
154 if (lscp_parser_test2(&tok, "Alsa", "active")) {
155 pszResult = "DESCRIPTION: 'ALSA PCM device active state'\r\n"
156 "TYPE: BOOL\r\n"
157 "MANDATORY: TRUE\r\n"
158 "FIX: TRUE\r\n"
159 "MULTIPLICITY: FALSE\r\n"
160 "DEPENDS: channels,samplerate,card\r\n"
161 "DEFAULT: TRUE\r\n"
162 "RANGE_MIN: FALSE\r\n"
163 "RANGE_MIN: TRUE\r\n"
164 "POSSIBILITIES: FALSE,TRUE\r\n";
165 }
166 else if (lscp_parser_test2(&tok, "Jack", "active")) {
167 pszResult = "DESCRIPTION: 'JACK device active state'\r\n"
168 "TYPE: BOOL\r\n"
169 "MANDATORY: TRUE\r\n"
170 "FIX: TRUE\r\n"
171 "MULTIPLICITY: FALSE\r\n"
172 "DEPENDS: channels,samplerate\r\n"
173 "DEFAULT: TRUE\r\n"
174 "RANGE_MIN: FALSE\r\n"
175 "RANGE_MIN: TRUE\r\n"
176 "POSSIBILITIES: FALSE,TRUE\r\n";
177 }
178 else ret = LSCP_FAILED;
179 }
180 else if (lscp_parser_test2(&tok, "MIDI_INPUT_DRIVER_PARAMETER", "INFO")) {
181 // Getting informations about a specific MIDI input driver parameter.
182 // GET MIDI_INPUT_DRIVER_PARAMETER INFO <midi-input-type> <param>
183 if (lscp_parser_test2(&tok, "Alsa", "active")) {
184 pszResult = "DESCRIPTION: 'ALSA Sequencer device active state'\r\n"
185 "TYPE: BOOL\r\n"
186 "MANDATORY: TRUE\r\n"
187 "FIX: TRUE\r\n"
188 "MULTIPLICITY: FALSE\r\n"
189 "DEPENDS: channels,ports\r\n"
190 "DEFAULT: TRUE\r\n"
191 "RANGE_MIN: FALSE\r\n"
192 "RANGE_MIN: TRUE\r\n"
193 "POSSIBILITIES: FALSE,TRUE\r\n";
194 }
195 else ret = LSCP_FAILED;
196 }
197 else if (lscp_parser_test2(&tok, "AUDIO_OUTPUT_DEVICE", "INFO")) {
198 // Getting informations about a specific audio output device.
199 // GET AUDIO_OUTPUT_DEVICE INFO <audio-device-id>
200 if (lscp_parser_nextint(&tok) < iAudioDevice) {
201 pszResult = "driver: Alsa\r\n"
202 "active: TRUE\r\n"
203 "channels: 2\r\n"
204 "samplerate: 44100\r\n";
205 }
206 else ret = LSCP_FAILED;
207 }
208 else if (lscp_parser_test2(&tok, "MIDI_INPUT_DEVICE", "INFO")) {
209 // Getting informations about a specific MIDI input device.
210 // GET MIDI_INPUT_DEVICE INFO <midi-device-id>
211 if (lscp_parser_nextint(&tok) < iMidiDevice) {
212 pszResult = "driver: Alsa\r\n"
213 "active: TRUE\r\n"
214 "channels: 16\r\n"
215 "ports: 1\r\n";
216 }
217 else ret = LSCP_FAILED;
218 }
219 else if (lscp_parser_test(&tok, "AVAILABLE_ENGINES")) {
220 // Getting all available engines:
221 // GET AVAILABLE_ENGINES
222 pszResult = "GigEngine,DLSEngine,AkaiEngine\r\n";
223 }
224 else if (lscp_parser_test2(&tok, "ENGINE", "INFO")) {
225 // Getting information about an engine.
226 // GET ENGINE INFO <engine-name>
227 if (lscp_parser_test(&tok, "GigEngine")) {
228 pszResult = "DESCRIPTION: GigaSampler Engine\r\n"
229 "VERSION: 0.3\r\n";
230 }
231 else if (lscp_parser_test(&tok, "DLSEngine")) {
232 pszResult = "DESCRIPTION: 'DLS Generic Engine'\r\n"
233 "VERSION: 0.2\r\n";
234 }
235 else if (lscp_parser_test(&tok, "AkaiEngine")) {
236 pszResult = "DESCRIPTION: Akai Sampler Engine\r\n"
237 "VERSION: 0.1\r\n";
238 }
239 else ret = LSCP_FAILED;
240 }
241 else ret = LSCP_FAILED;
242 }
243 else if (lscp_parser_test(&tok, "LIST")) {
244 if (lscp_parser_test(&tok, "CHANNELS")) {
245 // Getting all created sampler channel list.
246 // GET CHANNELS
247 if (iSamplerChannel > 0) {
248 strcpy(szTemp, "0");
249 for (i = 1; i < iSamplerChannel && strlen(szTemp) < sizeof(szTemp) - 8; i++)
250 sprintf(szTemp + strlen(szTemp), ",%d", i);
251 strcat(szTemp, "\r\n");
252 pszResult = szTemp;
253 }
254 else ret = LSCP_FAILED;
255 }
256 else if (lscp_parser_test(&tok, "AUDIO_OUTPUT_DEVICES")) {
257 // Getting all created audio output device list.
258 // GET AUDIO_OUTPUT_DEVICES
259 if (iAudioDevice > 0) {
260 strcpy(szTemp, "0");
261 for (i = 1; i < iAudioDevice && strlen(szTemp) < sizeof(szTemp) - 8; i++)
262 sprintf(szTemp + strlen(szTemp), ",%d", i);
263 strcat(szTemp, "\r\n");
264 pszResult = szTemp;
265 }
266 else ret = LSCP_FAILED;
267 }
268 else if (lscp_parser_test(&tok, "MIDI_INPUT_DEVICES")) {
269 // Getting all created MID input device list.
270 // GET MIDI_INPUT_DEVICES
271 if (iMidiDevice > 0) {
272 strcpy(szTemp, "0");
273 for (i = 1; i < iMidiDevice && strlen(szTemp) < sizeof(szTemp) - 8; i++)
274 sprintf(szTemp + strlen(szTemp), ",%d", i);
275 strcat(szTemp, "\r\n");
276 pszResult = szTemp;
277 }
278 else ret = LSCP_FAILED;
279 }
280 else ret = LSCP_FAILED;
281 }
282 else if (lscp_parser_test(&tok, "SET")) {
283 if (lscp_parser_test(&tok, "CHANNEL")) {
284 if (lscp_parser_test(&tok, "VOLUME")) {
285 // Setting channel volume:
286 // SET CHANNEL VOLUME <sampler-channel> <volume>
287 }
288 else if (lscp_parser_test(&tok, "AUDIO_OUTPUT_TYPE")) {
289 // Setting audio output type:
290 // SET CHANNEL AUDIO_OUTPUT_TYPE <sampler-channel> <audio-output-type>
291 }
292 else if (lscp_parser_test(&tok, "AUDIO_OUTPUT_DEVICE")) {
293 // Setting audio output device:
294 // SET CHANNEL AUDIO_OUTPUT_DEVICE <sampler-channel> <device-id>
295 }
296 else if (lscp_parser_test(&tok, "AUDIO_OUTPUT_CHANNEL")) {
297 // Setting audio output channel:
298 // SET CHANNEL AUDIO_OUTPUT_CHANNEL <sampler-channel> <audio-in> <audio-out>
299 }
300 else if (lscp_parser_test(&tok, "MIDI_INPUT_TYPE")) {
301 // Setting MIDI input type:
302 // SET CHANNEL MIDI_INPUT_TYPE <sampler-channel> <midi-input-type>
303 }
304 else if (lscp_parser_test(&tok, "MIDI_INPUT_DEVICE")) {
305 // Setting MIDI input device:
306 // SET CHANNEL MIDI_INPUT_DEVICE <sampler-channel> <device-id>
307 }
308 else if (lscp_parser_test(&tok, "MIDI_INPUT_PORT")) {
309 // Setting MIDI input port:
310 // SET CHANNEL MIDI_INPUT_PORT <sampler-channel> <midi-input-port>
311 }
312 else if (lscp_parser_test(&tok, "MIDI_INPUT_CHANNEL")) {
313 // Setting MIDI input channel:
314 // SET CHANNEL MIDI_INPUT_CHANNEL <sampler-channel> <midi-input-chan>
315 }
316 else ret = LSCP_FAILED;
317 }
318 else ret = LSCP_FAILED;
319 }
320 else if (lscp_parser_test(&tok, "LOAD")) {
321 if (lscp_parser_test(&tok, "ENGINE")) {
322 // Loading a sampler engine:
323 // LOAD ENGINE <engine-name> <sampler-channel>
324 }
325 else if (lscp_parser_test(&tok, "INSTRUMENT")) {
326 // Loading an instrument:
327 // LOAD INSTRUMENT [NON_MODAL] <filename> <instr-index> <sampler-channel>
328 }
329 else ret = LSCP_FAILED;
330 }
331 else if (lscp_parser_test2(&tok, "ADD", "CHANNEL")) {
332 // Adding a new sampler channel:
333 // ADD CHANNEL
334 sprintf(szTemp, "OK[%d]", iSamplerChannel++);
335 pszResult = szTemp;
336 }
337 else if (lscp_parser_test2(&tok, "REMOVE", "CHANNEL")) {
338 // Removing a sampler channel:
339 // REMOVE CHANNEL <sampler-channel>
340 if (lscp_parser_nextint(&tok) > iSamplerChannel)
341 ret = LSCP_FAILED;
342 }
343 else if (lscp_parser_test2(&tok, "RESET", "CHANNEL")) {
344 // Resetting a sampler channel:
345 // RESET CHANNEL <sampler-channel>
346 if (lscp_parser_nextint(&tok) > iSamplerChannel)
347 ret = LSCP_FAILED;
348 }
349 else if (lscp_parser_test(&tok, "CREATE")) {
350 if (lscp_parser_test(&tok, "AUDIO_OUTPUT_DEVICE")) {
351 // Creating an audio output device.
352 // CREATE AUDIO_OUTPUT_DEVICE <audio-output-driver> [<params>]
353 sprintf(szTemp, "OK[%d]", iAudioDevice++);
354 pszResult = szTemp;
355 }
356 else if (lscp_parser_test(&tok, "MIDI_INPUT_DEVICE")) {
357 // Creating an MIDI input device.
358 // CREATE MIDI_INPUT_DEVICE <midi-input-driver> [<params>]
359 sprintf(szTemp, "OK[%d]", iMidiDevice++);
360 pszResult = szTemp;
361 }
362 else ret = LSCP_FAILED;
363 }
364 else if (lscp_parser_test(&tok, "DESTROY")) {
365 if (lscp_parser_test(&tok, "AUDIO_OUTPUT_DEVICE")) {
366 // Destroying an audio output device.
367 // DESTROY AUDIO_OUTPUT_DEVICE <audio-device-id>
368 if (lscp_parser_nextint(&tok) > iAudioDevice)
369 ret = LSCP_FAILED;
370 }
371 else if (lscp_parser_test(&tok, "MIDI_INPUT_DEVICE")) {
372 // Destroying an MIDI intput device.
373 // DESTROY MIDI_INPUT_DEVICE <midi-device-id>
374 if (lscp_parser_nextint(&tok) > iMidiDevice)
375 ret = LSCP_FAILED;
376 }
377 else ret = LSCP_FAILED;
378 }
379 else if (lscp_parser_test(&tok, "SUBSCRIBE")) {
380 // Register frontend for receiving event notification messages:
381 // SUBSCRIBE <event>
382 ret = lscp_server_subscribe(pConnect, lscp_event_from_text(lscp_parser_next(&tok)));
383 }
384 else if (lscp_parser_test(&tok, "UNSUBSCRIBE")) {
385 // Deregister frontend for not receiving event notification messages anymore:
386 // UNSUBSCRIBE <event>
387 ret = lscp_server_unsubscribe(pConnect, lscp_event_from_text(lscp_parser_next(&tok)));
388 }
389 else if (lscp_parser_test(&tok, "QUIT")) {
390 // Close client connection:
391 // QUIT
392 lscp_parser_free(&tok);
393 return LSCP_FAILED; // Disconnect.
394 }
395 else ret = LSCP_FAILED;
396
397 lscp_parser_free(&tok);
398
399 if (pszResult == NULL)
400 pszResult = (ret == LSCP_OK ? "OK\r\n" : "ERR:1:Failed\r\n");
401
402 fprintf(stderr, "> %s", pszResult);
403
404 return lscp_server_result(pConnect, pszResult, strlen(pszResult));
405 }
406
407 ////////////////////////////////////////////////////////////////////////
408
409 void server_usage (void)
410 {
411 printf("\n %s %s (Build: %s)\n", lscp_server_package(), lscp_server_version(), lscp_server_build());
412
413 fputs("\n Available server commands: help, exit, quit, list", stdout);
414 fputs("\n (all else are broadcast verbatim to subscribers)\n\n", stdout);
415 }
416
417 void server_prompt (void)
418 {
419 fputs("lscp_server> ", stdout);
420 }
421
422 int main (int argc, char *argv[] )
423 {
424 lscp_server_t *pServer;
425 char szLine[200];
426 int cchLine;
427 lscp_connect_t *p;
428
429 #if defined(WIN32)
430 if (WSAStartup(MAKEWORD(1, 1), &_wsaData) != 0) {
431 fprintf(stderr, "lscp_server: WSAStartup failed.\n");
432 return -1;
433 }
434 #endif
435
436 srand(time(NULL));
437
438 pServer = lscp_server_create(SERVER_PORT, server_callback, NULL);
439 if (pServer == NULL)
440 return -1;
441
442 server_usage();
443 server_prompt();
444
445 while (fgets(szLine, sizeof(szLine), stdin)) {
446
447 cchLine = strlen(szLine);
448 while (cchLine > 0 && (szLine[cchLine - 1] == '\n' || szLine[cchLine - 1] == '\r'))
449 cchLine--;
450 szLine[cchLine] = '\0';
451
452 if (strcmp(szLine, "exit") == 0 || strcmp(szLine, "quit") == 0)
453 break;
454 else
455 if (strcmp(szLine, "list") == 0) {
456 for (p = pServer->connects.first; p; p = p->next) {
457 printf("client: sock=%d addr=%s port=%d events=0x%04x.\n",
458 p->client.sock,
459 inet_ntoa(p->client.addr.sin_addr),
460 ntohs(p->client.addr.sin_port),
461 (int) p->events
462 );
463 }
464 }
465 else
466 if (cchLine > 0 && strcmp(szLine, "help") != 0)
467 lscp_server_broadcast(pServer, LSCP_EVENT_MISCELLANEOUS, szLine, strlen(szLine));
468 else
469 server_usage();
470
471 server_prompt();
472 }
473
474 lscp_server_destroy(pServer);
475
476 #if defined(WIN32)
477 WSACleanup();
478 #endif
479
480 return 0;
481 }
482
483 // end of example_server.c

  ViewVC Help
Powered by ViewVC