Skip to content

Commit ce83a82

Browse files
committed
added zoscdump utility with initial implementation
1 parent 4690d82 commit ce83a82

File tree

2 files changed

+201
-1
lines changed

2 files changed

+201
-1
lines changed

project.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@
8888

8989
<!-- Command-line utilities -->
9090
<main name = "zmakecert" />
91-
91+
<main name = "zoscdump" />
92+
9293
<!-- Private command-line utilities -->
9394
<main name = "zsp" private = "1" />
9495
<main name = "test_randof" private = "1" />

src/zoscdump.c

Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
/* =========================================================================
2+
zoscdump [options] <url>...
3+
4+
A command line utility for receiving OSC messages similar to liblo's
5+
oscdump.
6+
7+
Copyright (c) the Contributors as noted in the AUTHORS file.
8+
This file is part of CZMQ, the high-level C binding for 0MQ:
9+
http://czmq.zeromq.org.
10+
11+
This Source Code Form is subject to the terms of the Mozilla Public
12+
License, v. 2.0. If a copy of the MPL was not distributed with this
13+
file, You can obtain one at http://mozilla.org/MPL/2.0/.
14+
=========================================================================
15+
*/
16+
17+
/*
18+
@header
19+
zoscdump -
20+
@discuss
21+
@end
22+
*/
23+
24+
#include "czmq_classes.h"
25+
26+
static bool verbose = false;
27+
28+
static int
29+
print_help()
30+
{
31+
puts ("zoscdump [options] <url>...");
32+
puts (" --verbose / -v verbose output");
33+
puts (" --help / -h this information");
34+
puts (" <url> url to listen on, i.e udp://127.0.0.1:1234");
35+
return 0;
36+
}
37+
38+
zsock_t *
39+
determine_socket( const char *bind)
40+
{
41+
zsock_t *retsock = NULL;
42+
zrex_t *rex = zrex_new (NULL);
43+
// determine transport
44+
if (zrex_eq(rex, bind, "^(.+)://.*") )
45+
{
46+
const char *transport = zrex_hit(rex, 1);
47+
if ( streq(transport, "udp") )
48+
retsock = zsock_new_dgram(bind);
49+
50+
else if ( streq(transport, "tcp") )
51+
{
52+
retsock = zsock_new(ZMQ_STREAM);
53+
zsock_bind(retsock, bind);
54+
}
55+
56+
else if ( streq(transport, "ipc") )
57+
retsock = zsock_new_pull( bind );
58+
59+
else
60+
zsys_error("Not a valid transport in %s", transport);
61+
62+
if (retsock == NULL)
63+
zsys_error("can't bind socket to %s", bind);
64+
65+
return retsock;
66+
}
67+
else
68+
zsys_error("can't determine transport from %s", bind);
69+
70+
return NULL;
71+
}
72+
73+
void
74+
recv_dgram(zmsg_t *msg)
75+
{
76+
char *sender = zmsg_popstr(msg);
77+
printf("%s ", sender);
78+
zframe_t *frame = zmsg_pop(msg);
79+
assert(frame);
80+
if (zframe_size(frame) > 0 )
81+
{
82+
zosc_t *oscmsg = zosc_fromframe(frame);
83+
assert(oscmsg);
84+
zosc_print(oscmsg);
85+
}
86+
else
87+
{
88+
zframe_destroy(&frame);
89+
}
90+
91+
zstr_free(&sender);
92+
}
93+
94+
void
95+
recv_stream(zmsg_t *msg)
96+
{
97+
zframe_t *senderid = zmsg_pop(msg);
98+
assert( zframe_size(senderid) == 5);
99+
zframe_t *frame = zmsg_pop(msg);
100+
assert(frame);
101+
if (zframe_size(frame) > 0 ) // on connection we receive a zero payload
102+
{
103+
zosc_t *oscmsg = zosc_fromframe(frame);
104+
assert(oscmsg);
105+
printf("%s ", zframe_strhex (senderid));
106+
zosc_print(oscmsg);
107+
}
108+
else
109+
{
110+
zsys_info("host id %s connected", zframe_strhex (senderid));
111+
zframe_destroy(&frame);
112+
}
113+
zframe_destroy(&senderid);
114+
}
115+
116+
int
117+
main (int argc, char *argv [])
118+
{
119+
zargs_t *args = zargs_new(argc, argv);
120+
assert(args);
121+
122+
if ( zargs_arguments(args) == 0 )
123+
return print_help();
124+
125+
if ( zargs_hasx (args, "--help", "-h", NULL) )
126+
return print_help();
127+
128+
if (zargs_hasx(args, "--verbose", "-v", NULL) )
129+
verbose = true;
130+
131+
zpoller_t *poller = zpoller_new(NULL);
132+
assert(poller);
133+
zlist_t *sockets = zlist_new();
134+
assert(sockets);
135+
136+
int ret = 0;
137+
138+
const char *bind = zargs_first(args);
139+
while (bind)
140+
{
141+
zsock_t *sock = determine_socket(bind);
142+
if (sock == NULL)
143+
{
144+
ret = 1;
145+
break;
146+
}
147+
148+
zlist_append(sockets, sock);
149+
if (verbose)
150+
zsys_info("Listening dgram socket on %s", bind);
151+
152+
ret = zpoller_add(poller, sock);
153+
if (ret != 0 )
154+
{
155+
zsys_error("can't add bound socket %s to poller", bind);
156+
ret = 1;
157+
break;
158+
}
159+
160+
bind = zargs_next(args);
161+
}
162+
zargs_destroy(&args);
163+
164+
while ( ret == 0)
165+
{
166+
void *which = (void *) zpoller_wait(poller, -1);
167+
168+
if (zpoller_terminated( poller ) || zpoller_expired( poller ) )
169+
break;
170+
171+
zmsg_t *msg = zmsg_recv(which);
172+
assert(msg);
173+
174+
switch ( zsock_type(which) )
175+
{
176+
case ZMQ_DGRAM:
177+
recv_dgram(msg);
178+
break;
179+
case ZMQ_STREAM:
180+
recv_stream(msg);
181+
break;
182+
default:
183+
zsys_error("Unsupported socket type");
184+
break;
185+
}
186+
zmsg_destroy(&msg);
187+
fflush(stdout);
188+
}
189+
190+
zpoller_destroy(&poller);
191+
zsock_t *sock = zlist_first(sockets);
192+
while (sock)
193+
{
194+
zsock_destroy(&sock);
195+
sock = zlist_next(sockets);
196+
}
197+
198+
return ret;
199+
}

0 commit comments

Comments
 (0)