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
|
#include <assert.h>
#include "../ipc.c"
struct str {
char msg[126];
int len;
};
void set_output(struct str* output, char* msg) {
int i = 0;
while (msg[i] != 0) {
assert(i < 126);
output->msg[i] = msg[i];
i++;
}
output->len = i;
}
struct strs {
struct str* data;
int capacity;
int count;
};
struct strs init_strs() {
return (struct strs) {
.data = malloc(sizeof(struct str) * 10),
.count = 0,
.capacity = 10,
};
}
void add_str(struct strs* strs, char* str) {
if (strs->count + 1 > strs->capacity) {
strs->capacity += 30;
strs->data = realloc(strs->data, sizeof(struct str) * strs->capacity);
}
struct str item;
set_output(&item, str);
strs->data[strs->count] = item;
strs->count++;
}
struct strs handle_input(mqd_t tx, mqd_t rx, char* payload, int len) {
printf("Message: %.*s", len, payload);
fflush(stdout);
send_queue(tx, payload, len);
char* buf = read_queue(rx);
assert(strcmp(buf, "Sending") == 0);
struct strs output = init_strs();
int n = get_num_messages(rx);
while (n > 0) {
char* buf = read_queue(rx);
add_str(&output, buf);
free(buf);
n--;
if (n == 0) n = get_num_messages(rx);
}
printf(" -> done\n");
// if (buf) free(buf);
//
// buf = read_queue(rx);
// printf("%p\n", buf);
// add_str(&output, buf);
// if (buf) free(buf);
return output;
}
|