liblo: example_server.c Source File

liblo 0.36

example_server.c
1/*
2 * Copyright (C) 2014 Steve Harris et al. (see AUTHORS)
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as
6 * published by the Free Software Foundation; either version 2.1 of the
7 * License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Lesser General Public License for more details.
13 *
14 * $Id$
15 */
16
17#include <stdio.h>
18#include <stdlib.h>
19#ifndef WIN32
20# include <unistd.h>
21#endif
22#include "lo/lo.h"
23
24int done = 0;
25
26void error(int num, const char *m, const char *path);
27
28int generic_handler(const char *path, const char *types, lo_arg ** argv,
29 int argc, lo_message data, void *user_data);
30
31int foo_handler(const char *path, const char *types, lo_arg ** argv,
32 int argc, lo_message data, void *user_data);
33
34int blobtest_handler(const char *path, const char *types, lo_arg ** argv,
35 int argc, lo_message data, void *user_data);
36
37int pattern_handler(const char *path, const char *types, lo_arg ** argv,
38 int argc, lo_message data, void *user_data);
39
40int quit_handler(const char *path, const char *types, lo_arg ** argv,
41 int argc, lo_message data, void *user_data);
42
43int main()
44{
45 /* start a new server on port 7770 */
46 lo_server_thread st = lo_server_thread_new("7770", error);
47
48 /* add method that will match any path and args */
49 lo_server_thread_add_method(st, NULL, NULL, generic_handler, NULL);
50
51 /* add method that will match the path /foo/bar, with two numbers, coerced
52 * to float and int */
53 lo_server_thread_add_method(st, "/foo/bar", "fi", foo_handler, NULL);
54
55 /* add method that will match the path /blobtest with one blob arg */
56 lo_server_thread_add_method(st, "/blobtest", "b", blobtest_handler, NULL);
57
58 /* catch any message starting with /g using a pattern method */
59 lo_server_thread_add_method(st, "/p*", "", pattern_handler, NULL);
60
61 /* also catch "/q*", but glob_handle returns 1, so quit_handler
62 * gets called after */
63 lo_server_thread_add_method(st, "/q*", "", pattern_handler, NULL);
64
65 /* add method that will match the path /quit with no args */
66 lo_server_thread_add_method(st, "/quit", "", quit_handler, NULL);
67
68 lo_server_thread_start(st);
69
70 while (!done) {
71#ifdef WIN32
72 Sleep(1);
73#else
74 usleep(1000);
75#endif
76 }
77
78 lo_server_thread_free(st);
79
80 return 0;
81}
82
83void error(int num, const char *msg, const char *path)
84{
85 printf("liblo server error %d in path %s: %s\n", num, path, msg);
86 fflush(stdout);
87}
88
89/* catch any incoming messages and display them. returning 1 means that the
90 * message has not been fully handled and the server should try other methods */
91int generic_handler(const char *path, const char *types, lo_arg ** argv,
92 int argc, lo_message data, void *user_data)
93{
94 int i;
95
96 printf("generic handler; path: <%s>\n", path);
97 for (i = 0; i < argc; i++) {
98 printf("arg %d '%c' ", i, types[i]);
99 lo_arg_pp((lo_type)types[i], argv[i]);
100 printf("\n");
101 }
102 printf("\n");
103 fflush(stdout);
104
105 return 1;
106}
107
108int foo_handler(const char *path, const char *types, lo_arg ** argv,
109 int argc, lo_message data, void *user_data)
110{
111 /* example showing pulling the argument values out of the argv array */
112 printf("foo: %s <- f:%f, i:%d\n\n", path, argv[0]->f, argv[1]->i);
113 fflush(stdout);
114
115 return 0;
116}
117
118int blobtest_handler(const char *path, const char *types, lo_arg ** argv,
119 int argc, lo_message data, void *user_data)
120{
121 /* example showing how to get data for a blob */
122 int i, size = argv[0]->blob.size;
123 char mydata[6];
124
125 unsigned char *blobdata = (unsigned char*)lo_blob_dataptr((lo_blob)argv[0]);
126 int blobsize = lo_blob_datasize((lo_blob)argv[0]);
127
128 /* Alternatively:
129 * blobdata = &argv[0]->blob.data;
130 * blobsize = argv[0]->blob.size;
131 */
132
133 /* Don't trust network input! Blob can be anything, so check if
134 each character is a letter A-Z. */
135 for (i=0; i<6 && i<blobsize; i++)
136 if (blobdata[i] >= 'A' && blobdata[i] <= 'Z')
137 mydata[i] = blobdata[i];
138 else
139 mydata[i] = '.';
140 mydata[5] = 0;
141
142 printf("%s <- length:%d '%s'\n", path, size, mydata);
143 fflush(stdout);
144
145 return 0;
146}
147
148int pattern_handler(const char *path, const char *types, lo_arg ** argv,
149 int argc, lo_message data, void *user_data)
150{
151 printf("pattern handler matched: %s\n\n", path);
152 fflush(stdout);
153
154 // Let the dispatcher continue by returning non-zero, so
155 // quit_handler can also catch the message
156 return 1;
157}
158
159int quit_handler(const char *path, const char *types, lo_arg ** argv,
160 int argc, lo_message data, void *user_data)
161{
162 done = 1;
163 printf("quiting\n\n");
164 fflush(stdout);
165
166 return 0;
167}
168
169/* vi:set ts=8 sts=4 sw=4: */
lo_type
lo_type
An enumeration of the OSC types liblo can send and receive.
Definition lo_osc_types.h:61
lo_blob_datasize
uint32_t lo_blob_datasize(lo_blob b)
Return the amount of valid data in a lo_blob object.
lo_blob_dataptr
void * lo_blob_dataptr(lo_blob b)
Return a pointer to the start of the blob data to allow contents to be changed.
lo_arg_pp
void lo_arg_pp(lo_type type, void *data)
Pretty-print a set of typed arguments.
lo.h
lo_server_thread_start
int lo_server_thread_start(lo_server_thread st)
Start the server thread.
lo_server_thread_new
lo_server_thread lo_server_thread_new(const char *port, lo_err_handler err_h)
Create a new server thread to handle incoming OSC messages.
lo_server_thread_free
void lo_server_thread_free(lo_server_thread st)
Free memory taken by a server thread.
lo_server_thread_add_method
lo_method lo_server_thread_add_method(lo_server_thread st, const char *path, const char *typespec, lo_method_handler h, const void *user_data)
Add an OSC method to the specifed server thread.
lo_message
struct lo_message_ * lo_message
A low-level object used to represent messages passed over OSC.
Definition lo_types.h:60
lo_blob
struct lo_blob_ * lo_blob
A object to store an opaque binary data object.
Definition lo_types.h:52
lo_server_thread
struct lo_server_thread_ * lo_server_thread
An object representing a thread containing an OSC server.
Definition lo_types.h:92
lo_arg
Union used to read values from incoming messages.
Definition lo_osc_types.h:104
lo_arg::blob
struct lo_arg::@202165107121317025016233300260200135145372053146 blob