pars 0.2.1
Loading...
Searching...
No Matches
serializer.h
Go to the documentation of this file.
1/*
2Copyright (c) 2025 Giuseppe Roberti.
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without modification,
6are permitted provided that the following conditions are met:
7
81. Redistributions of source code must retain the above copyright notice, this
9list of conditions and the following disclaimer.
10
112. Redistributions in binary form must reproduce the above copyright notice,
12this list of conditions and the following disclaimer in the documentation and/or
13other materials provided with the distribution.
14
153. Neither the name of the copyright holder nor the names of its contributors
16may be used to endorse or promote products derived from this software without
17specific prior written permission.
18
19THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29*/
30#pragma once
31
32#include "nngxx/msg.h"
33
34#include "pars/concept/event.h"
35#include "pars/ev/klass.h"
36#include "pars/log.h"
37
38#include <cereal/archives/binary.hpp>
39
40#include <cstring>
41#include <istream>
42#include <ostream>
43#include <spanstream>
44#include <sstream>
45#include <string_view>
46
47namespace pars::ev
48{
49
51{
52 template<event_c event_t>
53 static nngxx::msg to_network(event_t& ev)
54 {
55 // 1. serialize the event to a stringstream
56 auto ostring = std::ostringstream();
57 auto ostream = std::ostream(ostring.rdbuf());
58 auto ar = cereal::BinaryOutputArchive(ostream);
60
61 // 2. compute the event_hash
62 auto event_hash = uuid<klass<event_t>>::hash;
63
64 // 3. create the nngxx::msg to hold the hash+event
65 auto serialization = ostring.rdbuf()->view();
66 auto m = nngxx::make_msg(sizeof(event_hash) + serialization.size())
67 .value_or_abort();
68 auto b = m.body();
69
70 // 4. append the event hash
71 std::memcpy(b.data<char>(), &event_hash, sizeof(event_hash));
72
73 // 5. append the serialized event
74 std::memcpy(b.data<char>() + sizeof(event_hash), serialization.data(),
75 serialization.size());
76
77 pars::debug(SL, lf::event, "Serialized Event [{}] to Message [{}]", ev, m);
78
79 return m;
80 }
81
82 template<event_c event_t>
83 static event_t to_event(const nngxx::msg& m)
84 {
85 // 1. compute received and requested event hash
86 auto recv_event_hash = hash_from_msg(m);
87 auto req_event_hash = uuid<klass<event_t>>::hash;
88
89 // 2. check they correspond
90 if (recv_event_hash - req_event_hash != 0)
91 throw std::runtime_error("Requested event mismatch!");
92
93 // 3. deserialize event
94 auto body = m.body();
95 auto view =
96 std::string_view(body.data<char>() + sizeof(uint64_t), body.size());
97 auto istring = std::ispanstream(view);
98 auto istream = std::istream(istring.rdbuf());
99 auto ar = cereal::BinaryInputArchive(istream);
100
101 auto ev = event_t{};
103
104 pars::debug(SL, lf::event, "Serialized Message [{}] to Event [{}]", m, ev);
105
106 return ev;
107 }
108};
109
110} // namespace pars::ev
#define SL
Definition log.h:58
clev::own< nng_msg * > msg
Definition msg.h:39
@ event
Definition flags.h:43
void debug(spdlog::source_loc loc, pars::lf lf, spdlog::format_string_t< args_t... > fmt, args_t &&... args)
Definition log.h:129
static event_t to_event(const nngxx::msg &m)
Definition serializer.h:83
static nngxx::msg to_network(event_t &ev)
Definition serializer.h:53