cannam@89: /* cannam@89: * nonblocking_server_example.c cannam@89: * cannam@89: * This code demonstrates two methods of monitoring both an lo_server cannam@89: * and other I/O from a single thread. cannam@89: * cannam@89: * Copyright (C) 2004 Steve Harris, Uwe Koloska cannam@89: * cannam@89: * This program is free software; you can redistribute it and/or modify cannam@89: * it under the terms of the GNU Lesser General Public License as cannam@89: * published by the Free Software Foundation; either version 2.1 of the cannam@89: * License, or (at your option) any later version. cannam@89: * cannam@89: * This program is distributed in the hope that it will be useful, cannam@89: * but WITHOUT ANY WARRANTY; without even the implied warranty of cannam@89: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the cannam@89: * GNU Lesser General Public License for more details. cannam@89: * cannam@89: * $Id$ cannam@89: */ cannam@89: cannam@89: #include cannam@89: #include cannam@89: #include cannam@89: #include cannam@89: #include cannam@89: #include cannam@89: cannam@89: #include "lo/lo.h" cannam@89: cannam@89: int done = 0; cannam@89: cannam@89: void error(int num, const char *m, const char *path); cannam@89: cannam@89: int generic_handler(const char *path, const char *types, lo_arg **argv, cannam@89: int argc, void *data, void *user_data); cannam@89: cannam@89: int foo_handler(const char *path, const char *types, lo_arg **argv, int argc, cannam@89: void *data, void *user_data); cannam@89: cannam@89: int quit_handler(const char *path, const char *types, lo_arg **argv, int argc, cannam@89: void *data, void *user_data); cannam@89: cannam@89: void read_stdin(void); cannam@89: cannam@89: int main() cannam@89: { cannam@89: int lo_fd; cannam@89: fd_set rfds; cannam@89: struct timeval tv; cannam@89: int retval; cannam@89: cannam@89: /* start a new server on port 7770 */ cannam@89: lo_server s = lo_server_new("7770", error); cannam@89: cannam@89: /* add method that will match any path and args */ cannam@89: lo_server_add_method(s, NULL, NULL, generic_handler, NULL); cannam@89: cannam@89: /* add method that will match the path /foo/bar, with two numbers, coerced cannam@89: * to float and int */ cannam@89: lo_server_add_method(s, "/foo/bar", "fi", foo_handler, NULL); cannam@89: cannam@89: /* add method that will match the path /quit with no args */ cannam@89: lo_server_add_method(s, "/quit", "", quit_handler, NULL); cannam@89: cannam@89: /* get the file descriptor of the server socket, if supported */ cannam@89: lo_fd = lo_server_get_socket_fd(s); cannam@89: cannam@89: if (lo_fd > 0) { cannam@89: cannam@89: /* select() on lo_server fd is supported, so we'll use select() cannam@89: * to watch both stdin and the lo_server fd. */ cannam@89: cannam@89: do { cannam@89: cannam@89: FD_ZERO(&rfds); cannam@89: #ifndef WIN32 cannam@89: FD_SET(0, &rfds); /* stdin */ cannam@89: #endif cannam@89: FD_SET(lo_fd, &rfds); cannam@89: cannam@89: retval = select(lo_fd + 1, &rfds, NULL, NULL, NULL); /* no timeout */ cannam@89: cannam@89: if (retval == -1) { cannam@89: cannam@89: printf("select() error\n"); cannam@89: exit(1); cannam@89: cannam@89: } else if (retval > 0) { cannam@89: cannam@89: if (FD_ISSET(0, &rfds)) { cannam@89: cannam@89: read_stdin(); cannam@89: cannam@89: } cannam@89: if (FD_ISSET(lo_fd, &rfds)) { cannam@89: cannam@89: lo_server_recv_noblock(s, 0); cannam@89: cannam@89: } cannam@89: } cannam@89: cannam@89: } while (!done); cannam@89: cannam@89: } else { cannam@89: cannam@89: /* lo_server protocol does not support select(), so we'll watch cannam@89: * stdin while polling the lo_server. */ cannam@89: #ifdef WIN32 cannam@89: printf("non-blocking input from stdin not supported under Windows\n"); cannam@89: exit(1); cannam@89: #else cannam@89: do { cannam@89: cannam@89: FD_ZERO(&rfds); cannam@89: FD_SET(0, &rfds); cannam@89: tv.tv_sec = 0; cannam@89: tv.tv_usec = 10000; cannam@89: cannam@89: retval = select(1, &rfds, NULL, NULL, &tv); /* timeout every 10ms */ cannam@89: cannam@89: if (retval == -1) { cannam@89: cannam@89: printf("select() error\n"); cannam@89: exit(1); cannam@89: cannam@89: } else if (retval > 0 && FD_ISSET(0, &rfds)) { cannam@89: cannam@89: read_stdin(); cannam@89: cannam@89: } cannam@89: cannam@89: lo_server_recv_noblock(s, 0); cannam@89: cannam@89: } while (!done); cannam@89: #endif cannam@89: } cannam@89: cannam@89: return 0; cannam@89: } cannam@89: cannam@89: void error(int num, const char *msg, const char *path) cannam@89: { cannam@89: printf("liblo server error %d in path %s: %s\n", num, path, msg); cannam@89: } cannam@89: cannam@89: /* catch any incoming messages and display them. returning 1 means that the cannam@89: * message has not been fully handled and the server should try other methods */ cannam@89: int generic_handler(const char *path, const char *types, lo_arg **argv, cannam@89: int argc, void *data, void *user_data) cannam@89: { cannam@89: int i; cannam@89: cannam@89: printf("path: <%s>\n", path); cannam@89: for (i=0; if, argv[1]->i); cannam@89: fflush(stdout); cannam@89: cannam@89: return 0; cannam@89: } cannam@89: cannam@89: int quit_handler(const char *path, const char *types, lo_arg **argv, int argc, cannam@89: void *data, void *user_data) cannam@89: { cannam@89: done = 1; cannam@89: printf("quiting\n\n"); cannam@89: cannam@89: return 0; cannam@89: } cannam@89: cannam@89: void read_stdin(void) cannam@89: { cannam@89: char buf[256]; cannam@89: int len = read(0, buf, 256); cannam@89: if (len > 0) { cannam@89: printf("stdin: "); cannam@89: fwrite(buf, len, 1, stdout); cannam@89: printf("\n"); cannam@89: fflush(stdout); cannam@89: } cannam@89: } cannam@89: cannam@89: /* vi:set ts=8 sts=4 sw=4: */