CMSDK 2.0.1
Cross-platform C++ base library and SDK for the Psyclone AIOS platform
Loading...
Searching...
No Matches
gen_fixtures.cpp
Go to the documentation of this file.
1// Golden DataMessage fixtures from the C++ CMSDK: the reference encoder for non-C++ codecs.
2//
3// The Kotlin codec's tests decode every fixture and check header fields and entries against
4// expected.json; for the 'compact' fixtures (no deleted or shrunk slots) they also re-encode and
5// require byte-for-byte equality with these files. Each fixture is also written in the packed
6// wire form (<name>.packed.bin), which the Kotlin codec must decode and reproduce exactly. Regenerate after any DataMessage change:
7//
8// make -C kotlin/tools (from CMSDK/, after `make` / `make ssl`)
9//
10// Writes kotlin/fixtures/<name>.bin, expected.json and layout.json (via the layout tool).
11#include "DataMessage.h"
12#include "ObjectIDs.h"
13#include "PsyTime.h"
14#include <cstdio>
15#include <string>
16#include <vector>
17
18using namespace cmlabs;
19
20static const uint64 T0 = 63960700000000000ULL; // fixed PsyTime (us since year 0): reproducible files
21
22struct Entry { std::string key, kind, json; };
23struct Fixture { std::string name; DataMessage* msg; bool compact; std::vector<Entry> entries; };
24
25static std::string Esc(const std::string& s) {
26 std::string o;
27 for (unsigned char c : s) {
28 if (c == '"' || c == '\\') { o += '\\'; o += (char)c; }
29 else if (c < 0x20) { char b[8]; snprintf(b, sizeof(b), "\\u%04x", c); o += b; }
30 else o += (char)c;
31 }
32 return o;
33}
34static std::string Hex(const char* d, uint32 n) {
35 static const char* h = "0123456789abcdef";
36 std::string o;
37 for (uint32 i = 0; i < n; i++) { o += h[((uint8)d[i]) >> 4]; o += h[((uint8)d[i]) & 15]; }
38 return o;
39}
40static DataMessage* Base() {
41 DataMessage* m = new DataMessage();
43 m->setSendTime(T0 + 10);
44 m->setRecvTime(T0 + 20);
45 return m;
46}
47static Entry S(const char* k, const char* v) { return Entry{ k, "string", "\"" + Esc(v) + "\"" }; }
48static Entry I(const char* k, int64 v) { return Entry{ k, "int", std::to_string((long long)v) }; }
49
50int main(int argc, char** argv) {
51 std::string out = argc > 1 ? argv[1] : "kotlin/fixtures";
52 std::vector<Fixture> fx;
53
54 fx.push_back({ "empty", Base(), true, {} });
55
56 DataMessage* m = Base();
57 m->setString("Text", "hello");
58 m->setInt("Serial", 4242);
59 m->setInt("Negative", -7);
60 m->setDouble("Gain", 0.75);
61 m->setTime("TCap", T0 + 123456);
62 fx.push_back({ "scalars", m, true, { S("Text", "hello"), I("Serial", 4242), I("Negative", -7),
63 Entry{ "Gain", "double", "0.75" }, Entry{ "TCap", "time", std::to_string((unsigned long long)(T0 + 123456)) } } });
64
65 m = Base();
66 char pcm[320];
67 for (int i = 0; i < 320; i++) pcm[i] = (char)(i < 256 ? i : 0); // includes NULs on purpose
68 m->setString("PsyPost", "AudioFrame");
69 m->setString("PsyDevice", "phone-s25");
70 m->setData("pcm", pcm, 320);
71 m->setString("Unicode", "M\xC3\xADmir \xE2\x80\x93 \xC3\xA6\xC3\xB8\xC3\xA5 \xE2\x9C\x93");
72 fx.push_back({ "audio_frame", m, true, { S("PsyPost", "AudioFrame"), S("PsyDevice", "phone-s25"),
73 Entry{ "pcm", "data", "\"" + Hex(pcm, 320) + "\"" },
74 S("Unicode", "M\xC3\xADmir \xE2\x80\x93 \xC3\xA6\xC3\xB8\xC3\xA5 \xE2\x9C\x93") } });
75
76 m = Base();
77 PsyType t; t.fromString("16.34.35");
78 m->setType(t);
79 PsyContext c; c.fromString("3.7");
80 m->setContextChange(c);
81 m->setTag(77);
82 m->setSerial(9001);
83 m->setReference(123456789012ULL);
84 m->setTTL(3000000);
85 m->setPriority(9);
86 m->setPolicy(1);
87 m->setFrom(12);
88 m->setTo(34);
89 m->setStatus(5);
90 // Every remaining header field gets a distinct non-zero value through the struct itself, so a
91 // codec with a wrong offset reads a wrong value (zeros would hide it).
92 m->data->cenc = 0x0102;
93 m->data->memid = 0x1112131415161718ULL;
94 m->data->origin = 0x2122;
95 m->data->destination = 0x2324;
96 m->data->cyclecputime = 0x31323334;
97 m->data->cyclewalltime = 0x35363738;
98 m->data->chaincputime = 0x41424344;
99 m->data->chainwalltime = 0x45464748;
100 m->data->chaincount = 0x51525354;
101 m->data->systemid = 0x61626364;
102 m->setString("k", "v");
103 fx.push_back({ "header_fields", m, true, { S("k", "v") } });
104
105 DataMessage* inner = Base();
106 inner->setString("Inner", "yes");
107 inner->setInt("N", 3);
108 m = Base();
109 m->setAttachedMessage("Attached", inner);
110 m->setString("After", "tail");
111 fx.push_back({ "attached", m, true, { Entry{ "Attached", "message", "{\"Inner\":\"yes\",\"N\":3}" }, S("After", "tail") } });
112 delete inner;
113
114 // Not compact: a shrinking overwrite (slot keeps its old size) and a deleted slot (UNUSEDID).
115 // Decoders must step by the KEY header's size and skip UNUSED slots.
116 m = Base();
117 m->setString("Long", "a fairly long original value");
118 m->setString("Long", "short");
119 m->setString("Gone", "to be deleted");
120 m->deleteEntry("Gone");
121 m->setInt("Kept", 1);
122 fx.push_back({ "overwrite_delete", m, false, { S("Long", "short"), I("Kept", 1) } });
123
124 std::string js = "{\n \"generator\": \"kotlin/tools/gen_fixtures.cpp (C++ CMSDK)\",\n \"fixtures\": [\n";
125 for (size_t i = 0; i < fx.size(); i++) {
126 DataMessage* d = fx[i].msg;
127 std::string path = out + "/" + fx[i].name + ".bin";
128 FILE* f = fopen(path.c_str(), "wb");
129 if (!f) { fprintf(stderr, "cannot write %s\n", path.c_str()); return 1; }
130 fwrite(d->data, 1, d->data->size, f);
131 fclose(f);
132 // the same message in the packed wire form: <name>.packed.bin
133 uint32 pn = 0;
134 char* pk = d->toPacked(pn);
135 f = pk ? fopen((out + "/" + fx[i].name + ".packed.bin").c_str(), "wb") : NULL;
136 if (!f) { fprintf(stderr, "cannot pack %s\n", fx[i].name.c_str()); return 1; }
137 fwrite(pk, 1, pn, f);
138 fclose(f);
139 free(pk);
140 DataMessageHeader* h = d->data;
141 char hdr[2048];
142 snprintf(hdr, sizeof(hdr),
143 "\"header\": {\"size\":%u,\"time\":%llu,\"sendtime\":%llu,\"recvtime\":%llu,\"ttl\":%llu,"
144 "\"priority\":%u,\"policy\":%u,\"from\":%u,\"to\":%u,\"tag\":%u,\"status\":%u,\"reference\":%llu,"
145 "\"serial\":%llu,\"type\":\"%s\",\"contextchange\":\"%s\",\"userCount\":%u,\"userSize\":%u,"
146 "\"cenc\":%u,\"memid\":%llu,\"origin\":%u,\"destination\":%u,\"cyclecputime\":%u,\"cyclewalltime\":%u,"
147 "\"chaincputime\":%u,\"chainwalltime\":%u,\"chaincount\":%u,\"systemid\":%u}",
148 h->size, (unsigned long long)h->time, (unsigned long long)h->sendtime, (unsigned long long)h->recvtime,
149 (unsigned long long)h->ttl, h->priority, h->policy, h->from, h->to, h->tag, h->status,
150 (unsigned long long)h->reference, (unsigned long long)h->serial, h->type.toString().c_str(),
151 h->contextchange.toString().c_str(), h->userCount, h->userSize,
152 h->cenc, (unsigned long long)h->memid, h->origin, h->destination, h->cyclecputime, h->cyclewalltime,
154 js += " {\"name\":\"" + fx[i].name + "\",\"compact\":" + (fx[i].compact ? "true" : "false") + ",\n " + hdr + ",\n \"entries\": [";
155 for (size_t e = 0; e < fx[i].entries.size(); e++)
156 js += (e ? "," : "") + std::string("\n {\"key\":\"") + fx[i].entries[e].key + "\",\"kind\":\"" +
157 fx[i].entries[e].kind + "\",\"value\":" + fx[i].entries[e].json + "}";
158 js += "]}" + std::string(i + 1 < fx.size() ? ",\n" : "\n");
159 printf("%-18s %5u bytes packed %4u compact=%s\n", fx[i].name.c_str(), h->size, pn, fx[i].compact ? "yes" : "no");
160 delete d;
161 }
162 js += " ]\n}\n";
163 FILE* f = fopen((out + "/expected.json").c_str(), "wb");
164 if (!f) return 1;
165 fwrite(js.data(), 1, js.size(), f);
166 fclose(f);
167 return 0;
168}
The binary DataMessage container — the central data-exchange object of Psyclone/CMSDK.
Object type ids used to tag and verify every binary structure in CMSDK memory.
CMSDK time: µs-resolution 64-bit timestamps and the Time Mapping Constant (TMC).
The central Psyclone data container: a self-contained binary message with typed, named user entries.
bool setTime(const char *key, uint64 value)
setTime(const char* key, uint64 value)
bool setTTL(uint64 ttl)
setTTL(uint64 ttl)
bool deleteEntry(const char *key)
deleteEntry(const char* key)
bool setPriority(uint16 priority)
setPriority(uint16 priority)
bool setString(const char *key, const char *value)
setString(const char* key, const char* value)
bool setInt(const char *key, int64 value)
setInt(const char* key, int64 value)
bool setTag(uint32 tag)
setTag(uint32 tag)
DataMessageHeader * data
Pointer to the message's flat memory block (header + user entries).
bool setStatus(uint16 status)
setStatus(uint16 status)
bool setAttachedMessage(const char *key, DataMessage *msg)
setAttachedMessage(const char* key, DataMessage* msg)
bool setRecvTime(uint64 time)
setRecvTime(uint64 time)
char * toPacked(uint32 &size) const
Encode this message in the packed wire form.
bool setFrom(uint32 from)
setFrom(uint32 from)
bool setTo(uint32 to)
setTo(uint32 to)
bool setCreatedTime(uint64 t)
setCreatedTime(uint64 t)
bool setReference(uint64 ref)
setReference(uint64 ref) Set message reference
bool setType(PsyType &type)
setType(PsyType &type)
bool setDouble(const char *key, double value)
setDouble(const char* key, double value)
bool setPolicy(uint8 policy)
setPolicy(uint8 policy)
bool setData(const char *key, const char *value, uint32 size)
setData(const char* key, const char* value, uint32 size)
bool setSerial(uint64 serial)
setSerial(uint64 serial)
bool setContextChange(PsyContext &context)
setContextChange(PsyContext& context)
bool setSendTime(uint64 time)
setSendTime(uint64 time)
static Entry S(const char *k, const char *v)
static Entry I(const char *k, int64 v)
static const uint64 T0
static DataMessage * Base()
static std::string Esc(const std::string &s)
static std::string Hex(const char *d, uint32 n)
int main()
Definition layout.cpp:23
std::string json
std::string key
std::string kind
std::vector< Entry > entries
std::string name
DataMessage * msg
Hierarchical execution-context identifier.
Definition Types.h:284
bool fromString(const char *text)
Definition Types.h:374
std::string toString()
Definition Types.h:341
Hierarchical message type identifier — the key used for publish/subscribe matching in Psyclone.
Definition Types.h:123
bool fromString(const char *text)
Parse dotted decimal notation (e.g.
Definition Types.h:233
std::string toString()
Render as dotted decimal notation, e.g. "2.201.3"; empty string if invalid.
Definition Types.h:195
The current (version 10) DataMessage wire/shared-memory header.
uint64 sendtime
Time the message was posted/sent, in microseconds.
uint64 reference
Reference id linking this message to another (e.g.
uint16 destination
NodeID of the target node (0 = local/any).
uint32 chaincputime
Accumulated CPU time across the whole causal message chain, in microseconds.
uint64 serial
Serial number, e.g.
uint64 time
Creation time in microseconds since epoch (see PsyTime).
uint64 memid
Shared-memory id assigned when the message is stored in a memory map.
uint32 size
Total size of the whole message block in bytes (header + all entries).
uint32 userCount
Number of user entries currently in use.
uint16 status
User/application status code.
PsyType type
Hierarchical message type (e.g.
uint16 priority
Scheduling priority; higher values are processed first by whiteboards and modules.
uint32 userSize
Total size in bytes of all user entries currently in use.
uint8 policy
Delivery policy flags, e.g.
uint32 cyclewalltime
Wall-clock time spent in this cycle, in microseconds (profiling).
uint32 chainwalltime
Accumulated wall time across the whole causal message chain, in microseconds.
uint64 ttl
Time-to-live in microseconds; 0 means deliver-and-forget (not retained in memory).
PsyContext contextchange
Context switch this message announces (NOCONTEXT if none).
uint32 systemid
Id of the Psyclone system instance that sent the message.
uint32 cyclecputime
CPU time spent processing the triggering message in this cycle, in microseconds (profiling).
uint32 to
Component id of the addressee; 0 for publish/subscribe distribution.
uint16 origin
NodeID of the node the message originated on.
uint32 chaincount
Number of messages in the causal chain leading to (and including) this one.
uint32 from
Component id of the sender.
uint64 recvtime
Time the message was received by the current component, in microseconds.
uint16 cenc
Encoding flags (reserved for compression/encryption of the payload).
uint32 tag
User tag id for temporal/semantic grouping of related messages.