annotate src/liblo-0.26/examples/nonblocking_server_example.c @ 83:ae30d91d2ffe

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