/
backend_oscstreamdb.c
157 lines (130 loc) · 3.84 KB
/
backend_oscstreamdb.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include <stdlib.h>
#include <stdio.h>
#include <sys/param.h>
#include <string.h>
#include <sys/time.h>
#include <lo/lo.h>
#include "backend_oscstreamdb.h"
#include "command.h"
backend_oscstreamdb_options_t backend_oscstreamdb_options;
FILE* oscstreamdb_process = 0;
lo_address a_write = 0;
void oscstreamdb_defaults()
{
memset(&backend_oscstreamdb_options, 0,
sizeof(backend_oscstreamdb_options));
backend_oscstreamdb_options.executable_path = "oscstreamdb";
backend_oscstreamdb_options.stream = 0;
backend_oscstreamdb_options.database = "oscstreamdb";
}
int oscstreamdb_start()
{
/* Note: We are using "script" here to force oscstreamdb to flush
* its output more quickly. */
char cmd[MAXPATHLEN];
if (backend_oscstreamdb_options.database) {
snprintf(cmd, MAXPATHLEN, "script -q -c "
"\'%s -d \"%s\" -s \"%s\"\' /dev/null",
backend_oscstreamdb_options.executable_path,
backend_oscstreamdb_options.database,
backend_oscstreamdb_options.stream);
}
else {
snprintf(cmd, MAXPATHLEN, "script -q -c "
"\'%s -s \"%s\"\' /dev/null",
backend_oscstreamdb_options.executable_path,
backend_oscstreamdb_options.stream);
}
oscstreamdb_process = popen(cmd, "r");
if (!oscstreamdb_process) {
printf("Error running oscstreamdb.\n");
return 1;
}
return 0;
}
void oscstreamdb_stop()
{
if (oscstreamdb_process)
{
pclose(oscstreamdb_process);
oscstreamdb_process = 0;
}
}
int oscstreamdb_poll()
{
struct timeval t = {0,0};
int fd = fileno(oscstreamdb_process);
fd_set set;
FD_ZERO(&set);
FD_SET(fd, &set);
while (!feof(oscstreamdb_process)
&& select(fd+1, &set, NULL, NULL, &t)>0)
{
char s[1024];
if (fgets(s, 1024, oscstreamdb_process)) {
fputs(s, stdout);
fflush(stdout);
}
if (!a_write && strncmp(s, "Ports:", 6)==0) {
char *w = strstr(s, "Write RX=");
if (w) {
char rx[10];
snprintf(rx, 10, "%d", atoi(w + 9));
printf("Creating address for write port %d\n", atoi(rx));
a_write = lo_address_new("localhost", rx);
}
}
need_prompt = 1;
FD_ZERO(&set);
FD_SET(fd, &set);
t.tv_sec = 0;
t.tv_usec = 0;
}
return feof(oscstreamdb_process);
}
/* TODO: Bundle messages together that happen in the same call to poll(). */
void oscstreamdb_write_value(mapper_signal msig, void *v,
mapper_timetag_t *tt)
{
int i;
char str[1024], *path = str;
msig_full_name(msig, path, 1024);
if (path[0]=='/')
path ++;
while (path[0] && path[0]!='/')
path ++;
mapper_db_signal mprop = msig_properties(msig);
if (!a_write)
printf("No write port yet. :(\n");
else {
lo_bundle b;
if (!tt || !tt->sec) {
lo_timetag now;
lo_timetag_now(&now);
b = lo_bundle_new(now);
}
else
b = lo_bundle_new(*tt);
if (!b) {
printf("Could not create lo_bundle.\n");
return;
}
lo_message m = lo_message_new();
if (!m) {
printf("Could not create lo_message.\n");
lo_bundle_free(b);
return;
}
if (mprop->type == 'i') {
for (i=0; i<mprop->length; i++)
lo_message_add_int32(m, ((int*)v)[i]);
}
else if (mprop->type == 'f') {
for (i=0; i<mprop->length; i++)
lo_message_add_float(m, ((float*)v)[i]);
}
lo_bundle_add_message(b, path, m);
lo_send_bundle(a_write, b);
lo_bundle_free_messages(b);
}
}