AntHocNet 2.6.1
Paper-faithful ant-colony ad hoc routing: the shared core and its adapters
Loading...
Searching...
No Matches
xray.h
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-only
2// Copyright (C) 2026 Daniel Henrique Joppi
3
4#ifndef AHN_WEB_XRAY_H
5#define AHN_WEB_XRAY_H
6
7/**
8 * @file
9 * The ant X-ray (#637): an ant's wire bytes decoded by the core's own codec
10 * into a small JSON object. The game shows these fields beside the bytes; the
11 * native build prints the same JSON for the same bytes (ahn-web-trace
12 * --decode), and web/test/xray.test.mjs requires the two to agree.
13 */
14#include <cstdint>
15#include <cstdio>
16#include <string>
17#include <vector>
18
21
22namespace ahn_web {
23
24/// `bytes` decoded by core::codec::deserialize, as JSON. Doubles print with 17
25/// significant digits, so they parse back to the identical value.
26inline std::string antJson(const std::vector<std::uint8_t>& bytes) {
28 if (bytes.empty() || !anthocnet::core::codec::deserialize(bytes, m)) return "{\"ok\":false}";
29 char buf[96];
30 std::string s = "{\"ok\":true";
31 auto num = [&](const char* key, double v) {
32 std::snprintf(buf, sizeof buf, ",\"%s\":%.17g", key, v);
33 s += buf;
34 };
35 auto pairs = [&](const char* key, const auto& list, auto second) {
36 s += ",\"";
37 s += key;
38 s += "\":[";
39 bool first = true;
40 for (const auto& e : list) {
41 std::snprintf(buf, sizeof buf, "%s[%d,%.17g]", first ? "" : ",",
42 static_cast<int>(e.node), second(e));
43 s += buf;
44 first = false;
45 }
46 s += "]";
47 };
48 num("version", bytes[0]);
49 num("type", static_cast<int>(m.type));
50 num("direction", static_cast<int>(m.direction));
51 num("src", m.src);
52 num("dst", m.dst);
53 num("seqNum", m.seqNum);
54 num("timeStart", m.timeStart);
55 num("lifeAnt", m.lifeAnt);
56 num("broadcastBudget", m.broadcastBudget);
57 num("length", static_cast<double>(bytes.size()));
58 pairs("visited", m.visited, [](const anthocnet::core::AntHop& h) { return h.time; });
59 pairs("history", m.history, [](const anthocnet::core::AntHop& h) { return h.time; });
60 pairs("helloDests", m.helloDests,
61 [](const anthocnet::core::HelloDest& d) { return d.pheromone; });
62 // ADR-0025: only when present, so default traces are unchanged.
63 if (m.hasKinematics) {
65 std::snprintf(buf, sizeof buf, ",\"kinematics\":[%.9g,%.9g,%.9g", k.x, k.y, k.z);
66 s += buf;
67 std::snprintf(buf, sizeof buf, ",%.9g,%.9g,%.9g]", k.vx, k.vy, k.vz);
68 s += buf;
69 }
70 return s + "}";
71}
72
73} // namespace ahn_web
74
75#endif // AHN_WEB_XRAY_H
Built-in teaching scenarios: one per network family, shaped like the repo's presets but scaled down t...
Definition scenarios.h:18
std::string antJson(const std::vector< std::uint8_t > &bytes)
bytes decoded by core::codec::deserialize, as JSON.
Definition xray.h:26
bool deserialize(const std::uint8_t *bytes, std::size_t len, AntMessage &msg)
Decode bytes into msg.
One entry on an ant's visited-node stack: a node and the trip time the ant took to reach it from the ...
Complete, copyable description of an ant packet.
Definition ant_message.h:48
NodeAddress src
Originating node.
Definition ant_message.h:52
double timeStart
Generation time, for trip-time accounting.
Definition ant_message.h:59
bool hasKinematics
Hello only (ADR-0025): the sender's position and velocity, sent as an optional trailing section when ...
Definition ant_message.h:75
NodeAddress dst
Final destination.
Definition ant_message.h:53
std::uint32_t seqNum
Widened from the original u_int8_t so it does not wrap after 256 ants (which silently broke (src,...
Definition ant_message.h:57
int broadcastBudget
Remaining number of times this ant may be (re)broadcast.
Definition ant_message.h:65
VisitedPath history
Back-ant stack: path being reinforced.
Definition ant_message.h:68
double lifeAnt
Repair-ant lifetime budget (seconds).
Definition ant_message.h:60
VisitedPath visited
Forward stack: nodes seen on the way out.
Definition ant_message.h:67
std::vector< HelloDest > helloDests
Hello-ant adverts.
Definition ant_message.h:70
A (destination, pheromone) advert carried by a hello ant for building the virtual pheromone table.
Definition ant_message.h:42
A node's position (m) and velocity (m/s) in the adapter's Cartesian frame (ADR-0025).
Definition types.h:32