CSL  5.2
example_server.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2004 Steve Harris, Uwe Koloska
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 #include <unistd.h>
20 
21 #include "lo/lo.h"
22 
23 int done = 0;
24 
25 void error(int num, const char *m, const char *path);
26 
27 int generic_handler(const char *path, const char *types, lo_arg **argv,
28  int argc, void *data, void *user_data);
29 
30 int foo_handler(const char *path, const char *types, lo_arg **argv, int argc,
31  void *data, void *user_data);
32 
33 int quit_handler(const char *path, const char *types, lo_arg **argv, int argc,
34  void *data, void *user_data);
35 
36 int main()
37 {
38  /* start a new server on port 7770 */
40 
41  /* add method that will match any path and args */
42  lo_server_thread_add_method(st, NULL, NULL, generic_handler, NULL);
43 
44  /* add method that will match the path /foo/bar, with two numbers, coerced
45  * to float and int */
46  lo_server_thread_add_method(st, "/foo/bar", "fi", foo_handler, NULL);
47 
48  /* add method that will match the path /quit with no args */
49  lo_server_thread_add_method(st, "/quit", "", quit_handler, NULL);
50 
52 
53  while (!done) {
54 #ifdef WIN32
55  Sleep(1);
56 #else
57  usleep(1000);
58 #endif
59  }
60 
62 
63  return 0;
64 }
65 
66 void error(int num, const char *msg, const char *path)
67 {
68  printf("liblo server error %d in path %s: %s\n", num, path, msg);
69  fflush(stdout);
70 }
71 
72 /* catch any incoming messages and display them. returning 1 means that the
73  * message has not been fully handled and the server should try other methods */
74 int generic_handler(const char *path, const char *types, lo_arg **argv,
75  int argc, void *data, void *user_data)
76 {
77  int i;
78 
79  printf("path: <%s>\n", path);
80  for (i=0; i<argc; i++) {
81  printf("arg %d '%c' ", i, types[i]);
82  lo_arg_pp(types[i], argv[i]);
83  printf("\n");
84  }
85  printf("\n");
86  fflush(stdout);
87 
88  return 1;
89 }
90 
91 int foo_handler(const char *path, const char *types, lo_arg **argv, int argc,
92  void *data, void *user_data)
93 {
94  /* example showing pulling the argument values out of the argv array */
95  printf("%s <- f:%f, i:%d\n\n", path, argv[0]->f, argv[1]->i);
96  fflush(stdout);
97 
98  return 0;
99 }
100 
101 int quit_handler(const char *path, const char *types, lo_arg **argv, int argc,
102  void *data, void *user_data)
103 {
104  done = 1;
105  printf("quiting\n\n");
106  fflush(stdout);
107 
108  return 0;
109 }
110 
111 /* vi:set ts=8 sts=4 sw=4: */