Skip to content

Commit

Permalink
Add new output targets: File and stdout
Browse files Browse the repository at this point in the history
oscsend --help
...
url     : specifies the destination parameters using a liblo URL.
          e.g. UDP        "osc.udp://localhost:9000"
               Multicast  "osc.udp://224.0.1.9:9000"
               TCP        "osc.tcp://localhost:9000"
               File       "file:///tmp/dump.osc"        <<< new
               stdout     -                             <<< new

Example:
oscsend - /hello sf "world?" .3 | hexdump -C
00000000  2f 68 65 6c 6c 6f 00 00  2c 73 66 00 77 6f 72 6c  |/hello..,sf.worl|
00000010  64 3f 00 00 3e 99 99 9a                           |d?..>...|
00000018
  • Loading branch information
7890 committed Mar 12, 2019
1 parent 8b2b2cc commit 9ff929f
Showing 1 changed file with 57 additions and 13 deletions.
70 changes: 57 additions & 13 deletions src/tools/oscsend.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ void usage(void)
"url : specifies the destination parameters using a liblo URL.\n"
" e.g. UDP \"osc.udp://localhost:9000\"\n"
" Multicast \"osc.udp://224.0.1.9:9000\"\n"
" TCP \"osc.tcp://localhost:9000\"\n\n"
" TCP \"osc.tcp://localhost:9000\"\n"
" File \"file:///tmp/dump.osc\"\n"
" stdout -\n\n"

"address : the OSC address where the message to be sent.\n"
"types : specifies the types of the following values.\n",
VERSION);
Expand Down Expand Up @@ -254,7 +257,8 @@ int main(int argc, char **argv)
{
lo_address target;
lo_message message;
int ret, i=1;
int ret, i=1, dump_to_file=0, dump_to_stdout=0;
char file_uri[256];

if (argc < 3) {
usage();
Expand All @@ -267,13 +271,24 @@ int main(int argc, char **argv)
}

if (strstr(argv[i], "://")!=0) {
target = lo_address_new_from_url(argv[i]);
if (target == NULL) {
fprintf(stderr, "Failed to open %s\n", argv[i]);
exit(1);
if(strncmp(argv[i], "file://", 7)==0 && strlen(argv[i])>7) {
dump_to_file=1;
memset(file_uri, 0, sizeof(file_uri));
strncpy(file_uri, argv[i]+7, sizeof(file_uri)-1);
}
else {
target = lo_address_new_from_url(argv[i]);
if (target == NULL) {
fprintf(stderr, "Failed to open %s\n", argv[i]);
exit(1);
}
}
i++;
}
else if(strncmp(argv[i], "-", 1)==0 && strlen(argv[i])==1) {
dump_to_stdout=1;
i++;
}
else if (argv[i+1] == NULL) {
fprintf(stderr, "No port number is given.\n");
exit(1);
Expand All @@ -292,20 +307,49 @@ int main(int argc, char **argv)
exit(1);
}

lo_address_set_ttl(target, 1);

message = create_message(&argv[i+1]);
if (message == NULL) {
fprintf(stderr, "Failed to create OSC message.\n");
exit(1);
}

ret = lo_send_message(target, argv[i], message);
if (ret == -1) {
fprintf(stderr, "An error occured: %s\n",
lo_address_errstr(target));
exit(1);
if(!dump_to_file && !dump_to_stdout) {
lo_address_set_ttl(target, 1);

ret = lo_send_message(target, argv[i], message);
if (ret == -1) {
fprintf(stderr, "An error occured: %s\n",
lo_address_errstr(target));
lo_message_free(message);
exit(1);
}
}
else {
FILE *fout = NULL;

if(dump_to_file) {
FILE *f=fopen(file_uri, "w+");
if(f == NULL) {
fprintf(stderr, "An error occured: Could not open file for writing OSC message: '%s'\n",
file_uri);
lo_message_free(message);
exit(1);
}
fout=f;
}
else if(dump_to_stdout) {
fout=stdout;
}

size_t size;
void *msg_ptr=lo_message_serialise(message, argv[i], NULL, &size);
fwrite(msg_ptr, 1, size, fout);
fflush(fout);
fclose(fout);
free(msg_ptr);
}

lo_message_free(message);

return 0;
}

0 comments on commit 9ff929f

Please sign in to comment.