1 /*
2  *  This program is in the public domain
3  *
4  *  $Id: dssi_osc_update.c,v 1.7 2009/01/09 22:41:41 smbolton Exp $
5  */
6 
7 #include <unistd.h>
8 #include <stdlib.h>
9 #include <stdio.h>
10 #include <string.h>
11 #include <lo/lo.h>
12 
13 static volatile int done = 0;
14 
update_handler(const char * path,const char * types,lo_arg ** argv,int argc,lo_message msg,void * user_data)15 int update_handler(const char *path, const char *types, lo_arg **argv,
16                    int argc, lo_message msg, void *user_data)
17 {
18     int i;
19 
20     if (argc) {
21         printf("%s:", path);
22         for (i = 0; i < argc; ++i) {
23             printf(" ");
24             lo_arg_pp((lo_type)types[i], argv[i]);
25         }
26         printf("\n");
27     } else {
28         printf("%s\n", path);
29     }
30     done = 0;
31 
32     return 0;
33 }
34 
osc_error(int num,const char * msg,const char * path)35 void osc_error(int num, const char *msg, const char *path)
36 {
37     printf("liblo server error %d in path %s: %s\n", num, path, msg);
38     exit(1);
39 }
40 
main(int argc,char * argv[])41 int main(int argc, char *argv[])
42 {
43     lo_server_thread st;
44     lo_address a;
45     char *host, *port, *path;
46     char update_path[256], *tmp_url, my_url[256];
47 
48     if (argc != 2) {
49 	fprintf(stderr, "usage: %s <osc url>\n", argv[0]);
50 	return 1;
51     }
52 
53     host = lo_url_get_hostname(argv[1]);
54     port = lo_url_get_port(argv[1]);
55     path = lo_url_get_path(argv[1]);
56     a = lo_address_new(host, port);
57 
58     snprintf(update_path, 255, "%s/update", path);
59 
60     st = lo_server_thread_new(NULL, osc_error);
61     tmp_url = lo_server_thread_get_url(st);
62     snprintf(my_url, 255, "%s%s", tmp_url, (strlen(path) > 1 ? path + 1 : path));
63     free(tmp_url);
64     lo_server_thread_add_method(st, NULL, NULL, update_handler, NULL);
65     lo_server_thread_start(st);
66 
67     printf("sending osc.udp://%s:%s%s \"%s\"\n", host, port, update_path, my_url);
68     lo_send(a, update_path, "s", my_url);
69     free(host);
70     free(port);
71     free(path);
72 
73     /* quit if we go 5 seconds without receiving any OSC messages */
74     while (done < 5) {
75 	sleep(1);
76 	done++;
77     }
78 
79     return 0;
80 }
81 
82 /* vi:set ts=8 sts=4 sw=4: */
83