ympd.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /* ympd
  2. (c) 2013-2014 Andrew Karpow <andy@ympd.org>
  3. This project's homepage is: http://www.ympd.org
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions
  6. are met:
  7. - Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. - Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in the
  11. documentation and/or other materials provided with the distribution.
  12. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  13. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  14. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  15. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
  16. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  17. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  18. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  19. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  20. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  21. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  22. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23. */
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <stdio.h>
  27. #include <unistd.h>
  28. #include <getopt.h>
  29. #include <sys/time.h>
  30. #include <pthread.h>
  31. #include "mongoose.h"
  32. #include "http_server.h"
  33. #include "mpd_client.h"
  34. #include "config.h"
  35. extern char *optarg;
  36. int force_exit = 0;
  37. void bye()
  38. {
  39. force_exit = 1;
  40. }
  41. static int server_callback(struct mg_connection *c) {
  42. //printf("Got REQ: (%lu) WS:%d\n", c->content_len, c->is_websocket);
  43. //fwrite(c->content, c->content_len, 1, stdout);
  44. //printf("\n");
  45. if (c->is_websocket)
  46. {
  47. c->content[c->content_len] = '\0';
  48. if(c->content_len)
  49. return callback_mpd(c);
  50. else
  51. return MG_CLIENT_CONTINUE;
  52. }
  53. else
  54. return callback_http(c);
  55. }
  56. int main(int argc, char **argv)
  57. {
  58. int n, option_index = 0;
  59. struct mg_server *server = mg_create_server(NULL);
  60. unsigned int current_timer = 0, last_timer = 0;
  61. atexit(bye);
  62. mg_set_option(server, "listening_port", "8080");
  63. mpd.port = 6600;
  64. strcpy(mpd.host, "127.0.0.1");
  65. static struct option long_options[] = {
  66. {"host", required_argument, 0, 'h'},
  67. {"port", required_argument, 0, 'p'},
  68. {"webport", required_argument, 0, 'w'},
  69. {"user", required_argument, 0, 'u'},
  70. {"version", no_argument, 0, 'V'},
  71. {"help", no_argument, 0, 0 },
  72. {0, 0, 0, 0 }
  73. };
  74. while((n = getopt_long(argc, argv, "h:p:w:u::V",
  75. long_options, &option_index)) != -1) {
  76. switch (n) {
  77. case 'h':
  78. strncpy(mpd.host, optarg, sizeof(mpd.host));
  79. break;
  80. case 'p':
  81. mpd.port = atoi(optarg);
  82. case 'w':
  83. mg_set_option(server, "listening_port", optarg);
  84. break;
  85. case 'u':
  86. mg_set_option(server, "run_as_user", optarg);
  87. break;
  88. case 'V':
  89. fprintf(stdout, "ympd %d.%d.%d\n"
  90. "Copyright (C) 2014 Andrew Karpow <andy@ndyk.de>\n"
  91. "built " __DATE__ " "__TIME__ " ("__VERSION__")\n",
  92. YMPD_VERSION_MAJOR, YMPD_VERSION_MINOR, YMPD_VERSION_PATCH);
  93. return EXIT_SUCCESS;
  94. break;
  95. default:
  96. fprintf(stderr, "Usage: %s [OPTION]...\n\n"
  97. "\t-h, --host <host>\t\tconnect to mpd at host [localhost]\n"
  98. "\t-p, --port <port>\t\tconnect to mpd at port [6600]\n"
  99. "\t-w, --webport [ip:]<port>\t\tlisten interface/port for webserver [8080]\n"
  100. "\t-u, --user <username>\t\t\tdrop priviliges to user after socket bind\n"
  101. "\t-V, --version\t\t\tget version\n"
  102. "\t--help\t\t\t\tthis help\n"
  103. , argv[0]);
  104. return EXIT_FAILURE;
  105. }
  106. }
  107. mg_set_http_close_handler(server, mpd_close_handler);
  108. mg_set_request_handler(server, server_callback);
  109. while (!force_exit) {
  110. current_timer = mg_poll_server(server, 200);
  111. if(current_timer - last_timer)
  112. {
  113. last_timer = current_timer;
  114. mpd_poll(server);
  115. }
  116. }
  117. mpd_disconnect();
  118. mg_destroy_server(&server);
  119. return EXIT_SUCCESS;
  120. }