[go: up one dir, main page]

Menu

[205fcb]: / src / cpp_test.cpp  Maximize  Restore  History

Download this file

218 lines (177 with data), 6.5 kB

  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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#include <cstdio>
#include <string>
#include <iostream>
#include <array>
#include <vector>
#include <unistd.h>
#include <lo/lo.h>
#include <lo/lo_cpp.h>
int test1(const char *path, const char *types,
lo_arg **argv, int argc, lo_message m,
void *data)
{
printf("path: %s\n", path);
printf("types: %s\n", types);
printf("i: %d\n", argv[0]->i);
return 0;
}
int test2(lo_arg **argv, int argc)
{
printf("in test2: %d\n", argv[0]->i);
return 0;
}
int test3(lo_arg **argv, int argc, lo_message msg)
{
printf("in test3\n");
return 0;
}
void init(lo::Server &s)
{
int j = 234;
std::cout << "URL: " << s.url() << std::endl;
class test3
{
public:
test3(int j, std::string s) : _s(s), _j(j) {};
int operator () (lo_arg **argv, int argc, lo_message msg)
{
std::cout << _s << ": " << _j << ", " << argv[0]->i << std::endl;
return 0;
}
private:
std::string _s;
int _j;
};
s.add_method("test0", "i", test3(j,"test0"));
s.del_method("test0", "i");
s.add_method("test1", "i", test1, 0);
s.add_method("test2", "i", test2);
s.add_method("test3", "i", test3(j, "test3"));
s.add_method("test4", "i",
[j](lo_arg **argv, int argc)
{
printf("test4: %d, %d\n", j, argv[0]->i);
return 0;
});
j *= 2;
s.add_method("test5", "i",
[j](lo_arg **argv, int argc, lo_message msg)
{
printf("test5: %d, %d -- ", j, argv[0]->i);
lo_message_pp(msg);
return 0;
});
j *= 2;
s.add_method("test6", "i",
[j](lo_message msg)
{
printf("test6: %d -- ", j);
lo_message_pp(msg);
return 0;
});
j *= 2;
s.add_method("test7", "i", [j](){printf("test7: %d\n", j); return 0;});
j *= 2;
s.add_method("test8", "i", [j](){printf("test8a: %d\n", j);});
j *= 2;
s.add_method("test8", "i", [j](){printf("test8b: %d\n", j);});
j*=2;
s.add_method("test9", "i", [j](const char *path, const char *types, lo_arg **argv, int argc)
{printf("test9.1: %d, %s, %s, %d\n", j, path, types, argv[0]->i); return 1;});
j*=2;
s.add_method("test10", "i", [j](const char *types, lo_arg **argv, int argc)
{printf("test10.1: %d, %s, %d\n", j, types, argv[0]->i); return 1;});
j*=2;
s.add_method("test11", "is", [j](const char *types, lo_arg **argv, int argc, lo_message msg)
{printf("test11.1: %d, %s, %d, %s -- ", j, types, argv[0]->i, &argv[1]->s); lo_message_pp(msg); return 1;});
j*=2;
s.add_method("test9", "i", [j](const char *path, const char *types, lo_arg **argv, int argc)
{printf("test9.2: %d, %s, %s, %d\n", j, path, types, argv[0]->i);});
j*=2;
s.add_method("test10", "i", [j](const char *types, lo_arg **argv, int argc)
{printf("test10.2: %d, %s, %d\n", j, types, argv[0]->i);});
j*=2;
s.add_method("test11", "is", [j](const char *types, lo_arg **argv, int argc, lo_message msg)
{printf("test11.2: %d, %s, %d, %s -- ", j, types, argv[0]->i, &argv[1]->s); lo_message_pp(msg);});
j*=2;
s.add_method("test12", "i", [j](const lo::Message m)
{printf("test12 source: %s\n", m.source().url().c_str());});
s.add_method(0, 0, [](const char *path, lo_message m){printf("generic: %s ", path); lo_message_pp(m);});
j*=2;
s.add_bundle_handlers(
[j](lo_timetag time){ printf("Bundle start handler! (j=%d)\n", j); },
[j](){ printf("Bundle end handler! (j=%d)\n", j); } );
}
int main()
{
int context = 999;
lo::ServerThread st(9000,
[=](int num, const char *msg, const char *where)
{printf("error handler: %d\n", context);});
if (!st.is_valid()) {
printf("Nope.\n");
return 1;
}
std::cout << "URL: " << st.url() << std::endl;
init(st);
st.start();
lo::Address a("localhost", "9000");
printf("address host %s, port %s\n", a.hostname().c_str(), a.port().c_str());
printf("iface: %s\n", a.iface().c_str());
a.set_iface(std::string(), std::string("127.0.0.1"));
a.set_iface(0, "127.0.0.1");
printf("iface: %s\n", a.iface().c_str());
a.send_from(st, "test1", "i", 20);
a.send("test2", "i", 40);
a.send("test3", "i", 60);
a.send("test4", "i", 80);
a.send("test5", "i", 100);
a.send("test6", "i", 120);
a.send("test7", "i", 140);
a.send("test8", "i", 160);
a.send("test9", "i", 180);
a.send("test10", "i", 200);
lo::Message m;
m.add("i", 220);
m.add_string(std::string("blah"));
a.send("test11", m);
m.add(lo::Blob(4,"asdf"));
m.add(lo::Blob(std::vector<char>(5, 'a')));
m.add(lo::Blob(std::array<char,5>{"asdf"}));
a.send("blobtest", m);
a.send(
lo::Bundle({
{"test11", lo::Message("is",20,"first in bundle")},
{"test11", lo::Message("is",30,"second in bundle")}
})
);
lo::Bundle b({{"ok1", lo::Message("is",20,"first in bundle")},
lo::Bundle({"ok2", lo::Message("is",30,"second in bundle")})
}, LO_TT_IMMEDIATE);
printf("Bundle:\n"); b.print();
lo::Bundle::Element e(b.get_element(0));
printf("Bundle Message: %s ", e.pm.path.c_str()); e.pm.msg.print();
e = b.get_element(1);
printf("Bundle Bundle:\n"); e.bundle.print();
printf("Bundle timestamp: %u.%u\n",
e.bundle.timestamp().sec,
e.bundle.timestamp().frac);
a.send("test12", "i", 240);
char oscmsg[] = {'/','o','k',0,',','i',0,0,0,0,0,4};
lo::Message::maybe m2 = lo::Message::deserialise(oscmsg, sizeof(oscmsg));
if (m2.first == 0) {
printf("deserialise: %s", oscmsg);
m2.second.print();
}
else {
printf("Unexpected failure in deserialise(): %d\n", m2.first);
exit(1);
}
// Memory for lo_message not copied
lo::Message m3(m2.second);
// Memory for lo_message is copied
lo::Message m4 = m2.second.clone();
sleep(1);
printf("%s: %d\n", a.errstr().c_str(), a.get_errno());
return a.get_errno();
}