forked from yaxu/0mqosctest
/
client.c
65 lines (55 loc) · 1.48 KB
/
client.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <lo/lo.h>
#include <stdlib.h>
#include <zmq.h>
#include <assert.h>
#define MAXSZ 1024
lo_server s;
void error(int num, const char *msg, const char *path) {
printf("liblo server error %d in path %s: %s\n", num, path, msg);
}
/**/
int generic_handler(const char *path, const char *types, lo_arg **argv,
int argc, lo_message msg, void *user_data) {
int i;
int sz = lo_message_length(msg, path);
printf("path: <%s> size: %d\n", path, sz);
for (i=0; i<argc; i++) {
printf("arg %d '%c' ", i, types[i]);
lo_arg_pp(types[i], argv[i]);
printf("\n");
}
printf("\n");
return 1;
}
void osc_init() {
s = lo_server_new("12321", error);
lo_server_add_method(s, NULL, NULL, generic_handler, NULL);
}
int main (int argc, char *argv [])
{
void *context = zmq_ctx_new ();
void *subscriber = zmq_socket (context, ZMQ_SUB);
void *buffer = (void *) malloc(MAXSZ);
lo_message msg;
int rc = zmq_connect (subscriber, "tcp://localhost:5556");
assert (rc == 0);
// Subscribe to all
rc = zmq_setsockopt(subscriber, ZMQ_SUBSCRIBE,
NULL, 0);
assert (rc == 0);
osc_init();
while(1) {
int size = zmq_recv(subscriber, buffer, MAXSZ, 0);
msg = (lo_message) buffer;
if (size > 0) {
printf("received a message that's %d big\n", size);
lo_server_dispatch_data(s, buffer, size);
}
else {
printf("oops.\n");
}
}
zmq_close (subscriber);
zmq_ctx_destroy (context);
return 0;
}