#pragma once #include "message.hpp" #include #include #include #include /** * Encoding and decoding control messages. * * Everything here runs the JSON codec in strict mode. That is not the codec's * default, and the default is wrong for a wire: codec_config::strict * is false so that config files can state only their deviations, which means a * worker sending "{}" would decode cleanly into a default constructed struct. * A registration that silently becomes all zeroes is precisely the failure this * protocol must not have, so strict is set explicitly on every wire codec. * * Strict only governs *missing* members. Unknown members are skipped by the * Struct decoder, which is what makes the two stage decode below work and what * lets a newer worker talk to an older orchestrator. */ namespace kel { namespace lbm { namespace swarm { namespace impl { template codec make_wire_codec(){ codec_config cfg; cfg.strict = true; // Control messages nest at most capability -> webgl2 -> feasible -> plan, // so the default guard against pathological input is already generous. return codec{cfg}; } } // namespace impl /** * Serialise a message to the text that goes on the data channel. */ template error_or encode_message(const data &msg){ auto wire_codec = impl::make_wire_codec(); data out; auto eov = wire_codec.encode(msg, out); if(eov.is_error()){ return std::move(eov.get_error()); } return out.stl_string(); } /** * Decode text into a known message type. Fails if any member is absent. */ template error_or> decode_message(std::string_view text){ auto wire_codec = impl::make_wire_codec(); data in{text}; data out; auto eov = wire_codec.decode(in, out); if(eov.is_error()){ return std::move(eov.get_error()); } return out; } /** * Stage one: read just the common members so the caller learns which type to * decode. The payload is skipped rather than parsed into anything, so this * succeeds for every message type and fails only on genuinely malformed input * or a message missing the common members. */ inline error_or> peek_header(std::string_view text){ return decode_message(text); } /** * Fills in the members every message shares. Kept in one place so a message * built by the orchestrator and one built by the worker cannot disagree about * the shape of the envelope. */ template void set_envelope(data &msg, std::string_view type, std::string_view worker_id, uint64_t seq){ msg.template get<"type">().set(std::string{type}); msg.template get<"worker_id">().set(std::string{worker_id}); msg.template get<"seq">().set(seq); } /** * Convenience for the common shape: build a message, stamp the envelope. */ template data make_message(std::string_view type, std::string_view worker_id, uint64_t seq){ data msg; set_envelope(msg, type, worker_id, seq); return msg; } /// Reads the "type" of an already decoded header. inline std::string_view header_type(const data &hdr){ return hdr.template get<"type">().stl_view(); } /// Reads the "worker_id" of an already decoded header. inline std::string_view header_worker_id(const data &hdr){ return hdr.template get<"worker_id">().stl_view(); } } // namespace swarm } // namespace lbm } // namespace kel