From: Andrew Karpow Date: Fri, 8 Nov 2013 14:13:10 +0000 (+0100) Subject: CMake: requires DATADIR, renamed main.c X-Git-Url: http://gitweb.hugovil.com/?a=commitdiff_plain;h=1b4424910532dca45155bb5eb917f4da0a44f80c;p=ympd.git CMake: requires DATADIR, renamed main.c --- diff --git a/src/http_server.c b/src/http_server.c index bb6a1d8..8a25271 100644 --- a/src/http_server.c +++ b/src/http_server.c @@ -3,8 +3,7 @@ #include #include "http_server.h" -#define INSTALL_DATADIR "/home/andy/workspace/ympd" -#define LOCAL_RESOURCE_PATH INSTALL_DATADIR"/htdocs" +#define LOCAL_RESOURCE_PATH DATADIR"/htdocs" char *resource_path = LOCAL_RESOURCE_PATH; struct serveable { @@ -38,7 +37,7 @@ int callback_http(struct libwebsocket_context *context, void *in, size_t len) { char buf[256]; - int n; + size_t n; switch (reason) { case LWS_CALLBACK_HTTP: diff --git a/src/main.c b/src/main.c deleted file mode 100644 index a6b7f52..0000000 --- a/src/main.c +++ /dev/null @@ -1,110 +0,0 @@ -#include -#include -#include -#include -#include - -#include "http_server.h" -#include "mpd_client.h" - -struct libwebsocket_protocols protocols[] = { - /* first protocol must always be HTTP handler */ - { - "http-only", /* name */ - callback_http, /* callback */ - sizeof (struct per_session_data__http), /* per_session_data_size */ - 0, /* max frame size / rx buffer */ - }, - { - "ympd-client", - callback_ympd, - sizeof(struct per_session_data__ympd), - 255, - }, - - { NULL, NULL, 0, 0 } /* terminator */ -}; - -int force_exit = 0; - -void bye() -{ - force_exit = 1; -} - -int main(int argc, char **argv) -{ - int n = 0; - struct libwebsocket_context *context; - int opts = 0; - char interface_name[128] = ""; - const char *iface = NULL; - struct lws_context_creation_info info; - unsigned int oldus = 0; - protocol_array = protocols; - - atexit(bye); - memset(&info, 0, sizeof info); - info.port = 7681; - - while (n >= 0) { - n = getopt(argc, argv, "i:p:"); - if (n < 0) - continue; - switch (n) { - case 'p': - info.port = atoi(optarg); - break; - case 'i': - strncpy(interface_name, optarg, sizeof interface_name); - interface_name[(sizeof interface_name) - 1] = '\0'; - iface = interface_name; - break; - case 'h': - fprintf(stderr, "Usage: %s [OPTION]...\n" - "[-=

] [--mpd-port=

] " - "[-i ] " - "[--resource_path ]\n", argv[0]); - exit(1); - } - } - - info.iface = iface; - info.protocols = protocols; - info.extensions = libwebsocket_get_internal_extensions(); - info.gid = -1; - info.uid = -1; - info.ssl_cert_filepath = NULL; - info.ssl_private_key_filepath = NULL; - info.options = opts; - - context = libwebsocket_create_context(&info); - if (context == NULL) { - lwsl_err("libwebsocket init failed\n"); - return EXIT_FAILURE; - } - - n = 0; - while (n >= 0 && !force_exit) { - struct timeval tv; - - gettimeofday(&tv, NULL); - - /* - * This provokes the LWS_CALLBACK_SERVER_WRITEABLE for every - * live websocket connection using the DUMB_INCREMENT protocol, - * as soon as it can take more packets (usually immediately) - */ - - if (((unsigned int)tv.tv_usec - oldus) > 1000 * 500) { - mpd_loop(); - libwebsocket_callback_on_writable_all_protocol(&protocols[1]); - oldus = tv.tv_usec; - } - - n = libwebsocket_service(context, 50); - } - - libwebsocket_context_destroy(context); - return 0; -} diff --git a/src/ympd.c b/src/ympd.c new file mode 100644 index 0000000..a6b7f52 --- /dev/null +++ b/src/ympd.c @@ -0,0 +1,110 @@ +#include +#include +#include +#include +#include + +#include "http_server.h" +#include "mpd_client.h" + +struct libwebsocket_protocols protocols[] = { + /* first protocol must always be HTTP handler */ + { + "http-only", /* name */ + callback_http, /* callback */ + sizeof (struct per_session_data__http), /* per_session_data_size */ + 0, /* max frame size / rx buffer */ + }, + { + "ympd-client", + callback_ympd, + sizeof(struct per_session_data__ympd), + 255, + }, + + { NULL, NULL, 0, 0 } /* terminator */ +}; + +int force_exit = 0; + +void bye() +{ + force_exit = 1; +} + +int main(int argc, char **argv) +{ + int n = 0; + struct libwebsocket_context *context; + int opts = 0; + char interface_name[128] = ""; + const char *iface = NULL; + struct lws_context_creation_info info; + unsigned int oldus = 0; + protocol_array = protocols; + + atexit(bye); + memset(&info, 0, sizeof info); + info.port = 7681; + + while (n >= 0) { + n = getopt(argc, argv, "i:p:"); + if (n < 0) + continue; + switch (n) { + case 'p': + info.port = atoi(optarg); + break; + case 'i': + strncpy(interface_name, optarg, sizeof interface_name); + interface_name[(sizeof interface_name) - 1] = '\0'; + iface = interface_name; + break; + case 'h': + fprintf(stderr, "Usage: %s [OPTION]...\n" + "[-=

] [--mpd-port=

] " + "[-i ] " + "[--resource_path ]\n", argv[0]); + exit(1); + } + } + + info.iface = iface; + info.protocols = protocols; + info.extensions = libwebsocket_get_internal_extensions(); + info.gid = -1; + info.uid = -1; + info.ssl_cert_filepath = NULL; + info.ssl_private_key_filepath = NULL; + info.options = opts; + + context = libwebsocket_create_context(&info); + if (context == NULL) { + lwsl_err("libwebsocket init failed\n"); + return EXIT_FAILURE; + } + + n = 0; + while (n >= 0 && !force_exit) { + struct timeval tv; + + gettimeofday(&tv, NULL); + + /* + * This provokes the LWS_CALLBACK_SERVER_WRITEABLE for every + * live websocket connection using the DUMB_INCREMENT protocol, + * as soon as it can take more packets (usually immediately) + */ + + if (((unsigned int)tv.tv_usec - oldus) > 1000 * 500) { + mpd_loop(); + libwebsocket_callback_on_writable_all_protocol(&protocols[1]); + oldus = tv.tv_usec; + } + + n = libwebsocket_service(context, 50); + } + + libwebsocket_context_destroy(context); + return 0; +}