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
|
// longtest.c -- test long messages that require allocation
//
#include <stdio.h>
#include "o2.h"
#include "assert.h"
#include "string.h"
#include "o2_message.h"
int got_the_message = FALSE;
o2_blob_ptr a_blob;
char a_midi_msg[4];
int arg_count = 0;
// receive arg_count floats
void service_f(o2_msg_data_ptr data, const char *types,
o2_arg_ptr *argv, int argc, void *user_data)
{
o2_extract_start(data);
for (int i = 0; i < arg_count; i++) {
assert(*types == 'f');
#ifndef NDEBUG
o2_arg_ptr arg = // only needed for assert()
#endif
o2_get_next('f');
assert(arg);
assert(arg->f == i + 123);
types++;
}
assert(*types == 0); // end of string, got arg_count floats
got_the_message = TRUE;
}
// receive arg_count doubles
void service_d(o2_msg_data_ptr data, const char *types,
o2_arg_ptr *argv, int argc, void *user_data)
{
o2_extract_start(data);
for (int i = 0; i < arg_count; i++) {
assert(*types == 'd');
#ifndef NDEBUG
o2_arg_ptr arg = // only needed for assert()
#endif
o2_get_next('d');
assert(arg);
assert(arg->d == i + 1234);
types++;
}
assert(*types == 0); // end of string, got arg_count floats
got_the_message = TRUE;
}
// receive arg_count floats, coerced to ints, with parsing
void service_fc(o2_msg_data_ptr data, const char *types,
o2_arg_ptr *argv, int argc, void *user_data)
{
assert(argc == arg_count);
o2_extract_start(data);
for (int i = 0; i < arg_count; i++) {
assert(*types == 'i');
assert(argv[i]);
#ifndef NDEBUG
int actual = // only needed for assert
#endif
argv[i]->i;
assert(actual == i + 123);
types++;
}
assert(*types == 0); // end of string, got arg_count floats
got_the_message = TRUE;
}
// receive arg_count doubles, coerced to ints, with parsing
void service_dc(o2_msg_data_ptr data, const char *types,
o2_arg_ptr *argv, int argc, void *user_data)
{
assert(argc == arg_count);
o2_extract_start(data);
for (int i = 0; i < arg_count; i++) {
assert(*types == 'h');
assert(argv[i]);
#ifndef NDEBUG
int64_t actual = argv[i]->h; // only needed for assert()
#endif
assert(actual == i + 1234);
types++;
}
assert(*types == 0); // end of string, got arg_count floats
got_the_message = TRUE;
}
void send_the_message()
{
while (!got_the_message) {
o2_poll();
}
got_the_message = FALSE;
}
int main(int argc, const char * argv[])
{
const int N = 100;
char address[32];
char types[200];
o2_initialize("test");
o2_service_new("one");
// send from 0 to N-1 floats, without coercion
for (int i = 0; i < N; i++) {
sprintf(address, "/one/f%d", i);
for (int j = 0; j < i; j++) {
types[j] = 'f';
}
types[i] = 0;
o2_method_new(address, types, &service_f, NULL, FALSE, FALSE);
o2_send_start();
for (int j = 0; j < i; j++) {
o2_add_float(j + 123.0F);
}
arg_count = i;
o2_send_finish(0, address, TRUE);
send_the_message();
}
printf("DONE sending 0 to %d floats\n", N - 1);
// send from 0 to N-1 doubles, without coercion
for (int i = 0; i < N; i++) {
sprintf(address, "/one/d%d", i);
for (int j = 0; j < i; j++) {
types[j] = 'd';
}
types[i] = 0;
o2_method_new(address, types, &service_d, NULL, FALSE, FALSE);
o2_send_start();
for (int j = 0; j < i; j++) {
o2_add_double(j + 1234);
}
arg_count = i;
o2_send_finish(0, address, TRUE);
send_the_message();
}
printf("DONE sending 0 to %d doubles\n", N - 1);
// send from 0 to N-1 floats, with coercion to int and parsing
for (int i = 0; i < N; i++) {
sprintf(address, "/one/fc%d", i);
for (int j = 0; j < i; j++) {
types[j] = 'i';
}
types[i] = 0;
o2_method_new(address, types, &service_fc, NULL, TRUE, TRUE);
o2_send_start();
for (int j = 0; j < i; j++) {
o2_add_float(j + 123.0F);
}
arg_count = i;
o2_send_finish(0, address, TRUE);
send_the_message();
}
printf("DONE sending 0 to %d floats coerced to ints with parsing\n",
N - 1);
// send from 0 to N-1 doubles, with coercion to int64_t and parsing
for (int i = 0; i < N; i++) {
sprintf(address, "/one/dc%d", i);
for (int j = 0; j < i; j++) {
types[j] = 'h';
}
types[i] = 0;
o2_method_new(address, types, &service_dc, NULL, TRUE, TRUE);
o2_send_start();
for (int j = 0; j < i; j++) {
o2_add_double(j + 1234);
}
arg_count = i;
o2_send_finish(0, address, TRUE);
send_the_message();
}
printf("DONE sending 0 to %d doubles coerced to int64_t with parsing\n",
N - 1);
printf("DONE\n");
o2_finish();
return 0;
}
|