nonblocking_server_example.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * nonblocking_server_example.c
  3. *
  4. * This code demonstrates two methods of monitoring both an lo_server
  5. * and other I/O from a single thread.
  6. *
  7. * Copyright (C) 2014 Steve Harris et al. (see AUTHORS)
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU Lesser General Public License as
  11. * published by the Free Software Foundation; either version 2.1 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Lesser General Public License for more details.
  18. *
  19. * $Id$
  20. */
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <sys/time.h>
  24. #include <sys/types.h>
  25. #include <strings.h>
  26. #include <unistd.h>
  27. #include "lo/lo.h"
  28. int done = 0;
  29. void error(int num, const char *m, const char *path);
  30. int generic_handler(const char *path, const char *types, lo_arg ** argv,
  31. int argc, void *data, void *user_data);
  32. int foo_handler(const char *path, const char *types, lo_arg ** argv,
  33. int argc, void *data, void *user_data);
  34. int quit_handler(const char *path, const char *types, lo_arg ** argv,
  35. int argc, void *data, void *user_data);
  36. void read_stdin(void);
  37. int main()
  38. {
  39. int lo_fd;
  40. fd_set rfds;
  41. #ifndef WIN32
  42. struct timeval tv;
  43. #endif
  44. int retval;
  45. /* start a new server on port 7770 */
  46. lo_server s = lo_server_new("7770", error);
  47. /* add method that will match any path and args */
  48. lo_server_add_method(s, NULL, NULL, generic_handler, NULL);
  49. /* add method that will match the path /foo/bar, with two numbers, coerced
  50. * to float and int */
  51. lo_server_add_method(s, "/foo/bar", "fi", foo_handler, NULL);
  52. /* add method that will match the path /quit with no args */
  53. lo_server_add_method(s, "/quit", "", quit_handler, NULL);
  54. /* get the file descriptor of the server socket, if supported */
  55. lo_fd = lo_server_get_socket_fd(s);
  56. if (lo_fd > 0) {
  57. /* select() on lo_server fd is supported, so we'll use select()
  58. * to watch both stdin and the lo_server fd. */
  59. do {
  60. FD_ZERO(&rfds);
  61. #ifndef WIN32
  62. FD_SET(0, &rfds); /* stdin */
  63. #endif
  64. FD_SET(lo_fd, &rfds);
  65. retval = select(lo_fd + 1, &rfds, NULL, NULL, NULL); /* no timeout */
  66. if (retval == -1) {
  67. printf("select() error\n");
  68. exit(1);
  69. } else if (retval > 0) {
  70. if (FD_ISSET(0, &rfds)) {
  71. read_stdin();
  72. }
  73. if (FD_ISSET(lo_fd, &rfds)) {
  74. lo_server_recv_noblock(s, 0);
  75. }
  76. }
  77. } while (!done);
  78. } else {
  79. /* lo_server protocol does not support select(), so we'll watch
  80. * stdin while polling the lo_server. */
  81. #ifdef WIN32
  82. printf
  83. ("non-blocking input from stdin not supported under Windows\n");
  84. exit(1);
  85. #else
  86. do {
  87. FD_ZERO(&rfds);
  88. FD_SET(0, &rfds);
  89. tv.tv_sec = 0;
  90. tv.tv_usec = 10000;
  91. retval = select(1, &rfds, NULL, NULL, &tv); /* timeout every 10ms */
  92. if (retval == -1) {
  93. printf("select() error\n");
  94. exit(1);
  95. } else if (retval > 0 && FD_ISSET(0, &rfds)) {
  96. read_stdin();
  97. }
  98. lo_server_recv_noblock(s, 0);
  99. } while (!done);
  100. #endif
  101. }
  102. return 0;
  103. }
  104. void error(int num, const char *msg, const char *path)
  105. {
  106. printf("liblo server error %d in path %s: %s\n", num, path, msg);
  107. }
  108. /* catch any incoming messages and display them. returning 1 means that the
  109. * message has not been fully handled and the server should try other methods */
  110. int generic_handler(const char *path, const char *types, lo_arg ** argv,
  111. int argc, void *data, void *user_data)
  112. {
  113. int i;
  114. printf("path: <%s>\n", path);
  115. for (i = 0; i < argc; i++) {
  116. printf("arg %d '%c' ", i, types[i]);
  117. lo_arg_pp((lo_type)types[i], argv[i]);
  118. printf("\n");
  119. }
  120. printf("\n");
  121. fflush(stdout);
  122. return 1;
  123. }
  124. int foo_handler(const char *path, const char *types, lo_arg ** argv,
  125. int argc, void *data, void *user_data)
  126. {
  127. /* example showing pulling the argument values out of the argv array */
  128. printf("%s <- f:%f, i:%d\n\n", path, argv[0]->f, argv[1]->i);
  129. fflush(stdout);
  130. return 0;
  131. }
  132. int quit_handler(const char *path, const char *types, lo_arg ** argv,
  133. int argc, void *data, void *user_data)
  134. {
  135. done = 1;
  136. printf("quiting\n\n");
  137. return 0;
  138. }
  139. void read_stdin(void)
  140. {
  141. char buf[256];
  142. int len = read(0, buf, 256);
  143. if (len > 0) {
  144. printf("stdin: ");
  145. fwrite(buf, len, 1, stdout);
  146. printf("\n");
  147. fflush(stdout);
  148. }
  149. }
  150. /* vi:set ts=8 sts=4 sw=4: */