liblo  0.31
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 
24 int done = 0;
25 
26 void error(int num, const char *m, const char *path);
27 
28 int generic_handler(const char *path, const char *types, lo_arg ** argv,
29  int argc, void *data, void *user_data);
30 
31 int foo_handler(const char *path, const char *types, lo_arg ** argv,
32  int argc, void *data, void *user_data);
33 
34 int blobtest_handler(const char *path, const char *types, lo_arg ** argv,
35  int argc, void *data, void *user_data);
36 
37 int quit_handler(const char *path, const char *types, lo_arg ** argv,
38  int argc, void *data, void *user_data);
39 
40 int main()
41 {
42  /* start a new server on port 7770 */
43  lo_server_thread st = lo_server_thread_new("7770", error);
44 
45  /* add method that will match any path and args */
46  lo_server_thread_add_method(st, NULL, NULL, generic_handler, NULL);
47 
48  /* add method that will match the path /foo/bar, with two numbers, coerced
49  * to float and int */
50  lo_server_thread_add_method(st, "/foo/bar", "fi", foo_handler, NULL);
51 
52  /* add method that will match the path /blobtest with one blob arg */
53  lo_server_thread_add_method(st, "/blobtest", "b", blobtest_handler, NULL);
54 
55  /* add method that will match the path /quit with no args */
56  lo_server_thread_add_method(st, "/quit", "", quit_handler, NULL);
57 
59 
60  while (!done) {
61 #ifdef WIN32
62  Sleep(1);
63 #else
64  usleep(1000);
65 #endif
66  }
67 
69 
70  return 0;
71 }
72 
73 void error(int num, const char *msg, const char *path)
74 {
75  printf("liblo server error %d in path %s: %s\n", num, path, msg);
76  fflush(stdout);
77 }
78 
79 /* catch any incoming messages and display them. returning 1 means that the
80  * message has not been fully handled and the server should try other methods */
81 int generic_handler(const char *path, const char *types, lo_arg ** argv,
82  int argc, void *data, void *user_data)
83 {
84  int i;
85 
86  printf("path: <%s>\n", path);
87  for (i = 0; i < argc; i++) {
88  printf("arg %d '%c' ", i, types[i]);
89  lo_arg_pp((lo_type)types[i], argv[i]);
90  printf("\n");
91  }
92  printf("\n");
93  fflush(stdout);
94 
95  return 1;
96 }
97 
98 int foo_handler(const char *path, const char *types, lo_arg ** argv,
99  int argc, void *data, void *user_data)
100 {
101  /* example showing pulling the argument values out of the argv array */
102  printf("%s <- f:%f, i:%d\n\n", path, argv[0]->f, argv[1]->i);
103  fflush(stdout);
104 
105  return 0;
106 }
107 
108 int blobtest_handler(const char *path, const char *types, lo_arg ** argv,
109  int argc, void *data, void *user_data)
110 {
111  /* example showing how to get data for a blob */
112  int i, size = argv[0]->blob.size;
113  char mydata[6];
114 
115  unsigned char *blobdata = (unsigned char*)lo_blob_dataptr((lo_blob)argv[0]);
116  int blobsize = lo_blob_datasize((lo_blob)argv[0]);
117 
118  /* Alternatively:
119  * blobdata = &argv[0]->blob.data;
120  * blobsize = argv[0]->blob.size;
121  */
122 
123  /* Don't trust network input! Blob can be anything, so check if
124  each character is a letter A-Z. */
125  for (i=0; i<6 && i<blobsize; i++)
126  if (blobdata[i] >= 'A' && blobdata[i] <= 'Z')
127  mydata[i] = blobdata[i];
128  else
129  mydata[i] = '.';
130  mydata[5] = 0;
131 
132  printf("%s <- length:%d '%s'\n", path, size, mydata);
133  fflush(stdout);
134 
135  return 0;
136 }
137 
138 int quit_handler(const char *path, const char *types, lo_arg ** argv,
139  int argc, void *data, void *user_data)
140 {
141  done = 1;
142  printf("quiting\n\n");
143  fflush(stdout);
144 
145  return 0;
146 }
147 
148 /* vi:set ts=8 sts=4 sw=4: */
lo_blob_datasize
uint32_t lo_blob_datasize(lo_blob b)
Return the amount of valid data in a lo_blob object.
lo_server_thread_free
void lo_server_thread_free(lo_server_thread st)
Free memory taken by a server thread.
lo_arg_pp
void lo_arg_pp(lo_type type, void *data)
Pretty-print a set of typed arguments.
lo_blob
void * lo_blob
A object to store an opaque binary data object.
Definition: lo_types.h:52
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_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_server_thread
void * lo_server_thread
An object representing a thread containing an OSC server.
Definition: lo_types.h:92
lo_server_thread_start
int lo_server_thread_start(lo_server_thread st)
Start the server thread.
lo.h
lo_arg::blob
struct lo_arg::@0 blob
lo_arg
Union used to read values from incoming messages.
Definition: lo_osc_types.h:104
lo_type
lo_type
An enumeration of the OSC types liblo can send and receive.
Definition: lo_osc_types.h:61
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.