example_server.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * Copyright (C) 2014 Steve Harris et al. (see AUTHORS)
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU Lesser General Public License as
  6. * published by the Free Software Foundation; either version 2.1 of the
  7. * License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU Lesser General Public License for more details.
  13. *
  14. * $Id$
  15. */
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <unistd.h>
  19. #include "lo/lo.h"
  20. int done = 0;
  21. void error(int num, const char *m, const char *path);
  22. int generic_handler(const char *path, const char *types, lo_arg ** argv,
  23. int argc, void *data, void *user_data);
  24. int foo_handler(const char *path, const char *types, lo_arg ** argv,
  25. int argc, void *data, void *user_data);
  26. int quit_handler(const char *path, const char *types, lo_arg ** argv,
  27. int argc, void *data, void *user_data);
  28. int main()
  29. {
  30. /* start a new server on port 7770 */
  31. lo_server_thread st = lo_server_thread_new("7770", error);
  32. /* add method that will match any path and args */
  33. lo_server_thread_add_method(st, NULL, NULL, generic_handler, NULL);
  34. /* add method that will match the path /foo/bar, with two numbers, coerced
  35. * to float and int */
  36. lo_server_thread_add_method(st, "/foo/bar", "fi", foo_handler, NULL);
  37. /* add method that will match the path /quit with no args */
  38. lo_server_thread_add_method(st, "/quit", "", quit_handler, NULL);
  39. lo_server_thread_start(st);
  40. while (!done) {
  41. #ifdef WIN32
  42. Sleep(1);
  43. #else
  44. usleep(1000);
  45. #endif
  46. }
  47. lo_server_thread_free(st);
  48. return 0;
  49. }
  50. void error(int num, const char *msg, const char *path)
  51. {
  52. printf("liblo server error %d in path %s: %s\n", num, path, msg);
  53. fflush(stdout);
  54. }
  55. /* catch any incoming messages and display them. returning 1 means that the
  56. * message has not been fully handled and the server should try other methods */
  57. int generic_handler(const char *path, const char *types, lo_arg ** argv,
  58. int argc, void *data, void *user_data)
  59. {
  60. int i;
  61. printf("path: <%s>\n", path);
  62. for (i = 0; i < argc; i++) {
  63. printf("arg %d '%c' ", i, types[i]);
  64. lo_arg_pp((lo_type)types[i], argv[i]);
  65. printf("\n");
  66. }
  67. printf("\n");
  68. fflush(stdout);
  69. return 1;
  70. }
  71. int foo_handler(const char *path, const char *types, lo_arg ** argv,
  72. int argc, void *data, void *user_data)
  73. {
  74. /* example showing pulling the argument values out of the argv array */
  75. printf("%s <- f:%f, i:%d\n\n", path, argv[0]->f, argv[1]->i);
  76. fflush(stdout);
  77. return 0;
  78. }
  79. int quit_handler(const char *path, const char *types, lo_arg ** argv,
  80. int argc, void *data, void *user_data)
  81. {
  82. done = 1;
  83. printf("quiting\n\n");
  84. fflush(stdout);
  85. return 0;
  86. }
  87. /* vi:set ts=8 sts=4 sw=4: */