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