|
From: Rui N. C. <rn...@rn...> - 2004-01-15 14:38:16
|
Hi all,
On the path for a GUI for linuxsampler, I've been taking some of my spare
time by writing an early implementation for the LSCP (the LinuxSampler
Control Protocol), as defined from the current available draft document.
My implementation, while still rather crude, is taking the form of a
programming library for plain conventional C, codenamed liblscp.
One of my objectives is that liblscp evolves as the implementation for a
linuxsampler API, while being a fair abstraction for the network and/or
ipc aspects of LSCP.
For the facts, liblscp is actually a wrapper for the LSCP specification,
taking all the TCP/UDP socket communication into it's main control, hiding
all or some of the protocol bureoucracy from the user and exposing a
simple and opaque C programming language interface, mainly by mapping
events to user function callback handlers.
The design of liblscp assumed that the programming interface provided is
useable and applicable either for server (linuxsampler itself) and/or
client (gui's) development.
Some design features (no rocket-sci here :)
* Multi-threaded server; but clients blocks for synchronous request calls.
* Multi-client; one server instance serves many clients, local and/or
remote.
* Server events broadcasted and delivered to client callbacks.
* Client requests processed on server supplied callback.
Please note that (as usual :) documentation is none at this stage but I'll
challenge you to look at the source code provided on the tarball below. A
barebones server and client test programs are included (lscp_server_test
and lscp_client_test).
As a quick reference for the server programming, one links to liblscp to
create a server instance, just like this:
#include <lscp_server.h>
lscp_server_t *server;
server = lscp_server_create (server_port, server_callback,
server_data);
where server_port is the port number where the server will be
listening for connections; server_callback is the server supplied
callback function that will handle every client request; server_data is
any reference to data that will be fed into server_callback without
modification.
The server callback function must have the following prototype:
lscp_status_t server_callback ( lscp_connect_t *conn, const char
*request, int reqlen, void *server_data );
where conn is just a client connection handle, that shall be used for the
server responses; the request text which has a length of reqlen bytes;
server_data is the same value given on lscp_server_create.
While handling each request the server must cook it's response and
eventually issue the following:
lscp_server_result (conn, result, reslen);
where conn is the client handle, and result is a pointer to the server
response literal text of reslen bytes. Of course the response shall obey
to the protocol specification.
The server issues a broadcast to its subscribers by simply issuing:
lscp_server_broadcast (server, buf, buflen);
which will trigger the client callback function, which will be fed with an
exact copy of buf/len; this is the intended way to deliver all
notifications to each subscribed client.
When its time to shutdown the server instance, just don't forget to call
the server destructor:
lscp_server_destroy (server);
and we're done with the server.
Likewise, as for client programming, you create a client instance just
like that:
#include <lscp_client.h>
lscp_client_t *client;
client = lscp_client_create (server_host, server_port,
client_callback, client_data);
where server_host is the hostname of the server we wish to connect, and
server_port is the respective port number; client_callback is the client
supplied callback function that will handle every server notification
event; client_data is intended for client context and will be fed to
client_callback without intervention.
The client may issue a request to server by use of:
lscp_client_call (client, request, reqlen, &result, &reslen);
and the server response will be returned on result.
The client callback function prototype is very similar to the server one:
lscp_status_t client_callback ( lscp_client_t *client, const char
*buf, int buflen, void *client_data );
where buf will be a pointer to the event text which is buflen bytes in
length; client_data is exactly the same value given on lscp_client_create
call.
This callback function is the place to handle all server notifications and
will be only called if the client is currently subscribed. No response
from the client is expected while processing an event within
client_callback.
A client subscribes to receive notifications by calling:
lscp_client_subscribe (client);
after which it will start receiving events by means of the supplied
client_callback function. To unsubscribe and stop this deliverance:
lscp_client_unsubscribe (client);
Finally, when a client is about to quit, the proper terminator is in order:
lscp_client_destroy (client);
Nuff said. If you care or dare, you may check out the tarball attached to
this message (liblscp-0.0.4.tar.gz) or better yet, track the revolving
under:
http://www.rncbc.org/ls/
Please note that the code is known to compile and run on linux AND on
win32 (!). On linux the main target is a shared library (liblscp.so) so
remember to set your LS_LIBRARY_PATH accordingly before running the test
programs.
A final disclaimer goes to the fact that I AM NOT a socket nor thread
programming guru, whatsoever. So fundamental mistakes may be lying around,
somewhere. Besides that ItJustWorks(tm:).
I'm eager to hear your feedback and comments. As usual, destructive
criticism will be sent to /dev/null ;)
Hope to be on the right track,
and towards linuxsampler integration.
Otherwise sorry for the bandwidth waste.
Cheers.
--
rncbc aka Rui Nuno Capela
rn...@rn...
|