annotate src/liblo-0.26/examples/nonblocking_server_example.c @ 89:8a15ff55d9af

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