IRSOL
C++ code implementing socket server for interacting with Baumer camera.
serializer.cpp
Go to the documentation of this file.
2
4#include "irsol/logging.hpp"
6#include "irsol/utils.hpp"
7
8#include <variant>
9
10namespace irsol {
11namespace protocol {
12
13internal::SerializedMessage
15{
16
17 return std::visit(
18 [](auto&& msg) -> internal::SerializedMessage {
19 using T = std::decay_t<decltype(msg)>;
20 return serialize<T>(std::move(msg));
21 },
22 msg);
23}
24
25std::string
27{
28 return std::visit(
29 [](auto&& val) -> std::string {
30 using T = std::decay_t<decltype(val)>;
31 return serializeValue<T>(std::move(val));
32 },
33 value);
34}
35
38{
39 IRSOL_LOG_TRACE("Serializing Success message: {}", msg.toString());
40 std::string result = msg.identifier;
41 if(msg.source == InMessageKind::ASSIGNMENT) {
43 msg.hasBody(),
44 "Body is not present in 'Success' message, created from 'Assignment'. This should "
45 "never happen, as a successful assignment should always provide a body for the "
46 "associated Success message.");
47 auto body = *msg.body;
48 result += "=" + serializeValue(std::move(body)) + Serializer::message_termination;
49 } else if(msg.source == InMessageKind::INQUIRY) {
50 if(msg.hasBody()) {
51 auto body = *msg.body;
52 result += "=" + serializeValue(std::move(body)) + Serializer::message_termination;
53 } else {
55 }
56 } else if(msg.source == InMessageKind::COMMAND) {
57 result += ";\n";
58 }
59 return {result, {}};
60}
61
64{
65 IRSOL_LOG_TRACE("Serializing binary buffer: {}", msg.toString());
66 // TODO: implement serialization
67 throw std::runtime_error("Binary data serialization not supported");
68}
69
72{
73 IRSOL_LOG_TRACE("Serializing image binary data: {}", msg.toString());
74
75 std::vector<irsol::types::byte_t> payload;
76 payload.reserve(msg.data.size() + 128); // Reserve extra for header/meta
77
78 {
79 std::string imagePrefix = "img=";
80 auto imagePrefixAsBytes = irsol::utils::stringToBytes(imagePrefix);
81 std::move(imagePrefixAsBytes.begin(), imagePrefixAsBytes.end(), std::back_inserter(payload));
82 }
83 payload.emplace_back(Serializer::SpecialBytes::SOH);
84 {
85 std::stringstream ss;
86 ss << "u" << msg.BYTES_PER_ELEMENT * 8 << "[" << msg.shape[0] << "," << msg.shape[1] << "]";
87 for(auto& att : msg.attributes) {
88 ss << " " << serializeBinaryDataAttribute(std::move(att));
89 }
90 auto attributesString = ss.str();
92 "Attributes for message '{}' serialized to {}", msg.toString(), attributesString);
93 auto attributesStringAsBytes = irsol::utils::stringToBytes(attributesString);
94
95 payload.insert(payload.end(), attributesStringAsBytes.begin(), attributesStringAsBytes.end());
96 }
97 payload.emplace_back(Serializer::SpecialBytes::STX);
98
99 // Copy image data to the end of the payload buffer
100 size_t dataOffset = payload.size();
101 payload.resize(payload.size() + msg.data.size());
102 std::memcpy(&payload[dataOffset], msg.data.data(), msg.data.size());
103
104 // Swap bytes in-place for 16-bit data (assume always 16-bit)
105 // This swaps each pair of bytes in the image data region of the payload.
107 payload.begin() + static_cast<std::ptrdiff_t>(dataOffset), payload.end());
108
109 payload.emplace_back(Serializer::SpecialBytes::ETX);
110
111 auto message = internal::SerializedMessage("", std::move(payload));
112 return std::move(message);
113}
114
117{
118 IRSOL_LOG_TRACE("Serializing color image binary data: {}", msg.toString());
119 // TODO: implement serialization
120 throw std::runtime_error("Binary data serialization not supported");
121}
122
125{
126 IRSOL_LOG_TRACE("Serializing error message: {}", msg.toString());
127 return {msg.identifier + ": Error: " + msg.description + Serializer::message_termination, {}};
128}
129
130std::string
132{
133 std::stringstream ss;
134 ss << att.identifier << "=" << serializeValue(std::move(att.value));
135 return ss.str();
136}
137
138} // namespace protocol
139} // namespace irsol
static std::string serializeBinaryDataAttribute(irsol::protocol::BinaryDataAttribute &&att)
Serialize a irsol::protocol::BinaryDataAttribute to a string.
static internal::SerializedMessage serializeBinaryDataBuffer(BinaryDataBuffer &&msg)
Serializes a irsol::protocol::BinaryDataBuffer message to a binary protocol message.
static internal::SerializedMessage serializeImageBinaryData(ImageBinaryData &&msg)
Serializes a irsol::protocol::ImageBinaryData message to a binary protocol message.
static constexpr const char * message_termination
Message line termination sequence (newline character) used to "close" a serialized message.
static std::string serializeValue(irsol::types::protocol_value_t &&value)
Serialize a protocol primitive value to a string.
static internal::SerializedMessage serializeColorImageBinaryData(ColorImageBinaryData &&msg)
Serializes a irsol::protocol::ColorImageBinaryData message to a binary protocol message.
static internal::SerializedMessage serialize(OutMessage &&msg)
Serialize an irsol::protocol::OutMessage variant into a serialized protocol message.
static internal::SerializedMessage serializeError(Error &&msg)
Serializes a irsol::protocol::Error message to a binary protocol message.
static internal::SerializedMessage serializeSuccess(Success &&msg)
Serializes a irsol::protocol::Success message to a binary protocol message.
#define IRSOL_ASSERT_ERROR
Error-level assertion macro.
Definition assert.hpp:134
#define IRSOL_LOG_DEBUG(...)
Logs a debug-level message using the default logger.
Definition logging.hpp:91
#define IRSOL_LOG_TRACE(...)
Logs a trace-level message using the default logger.
Definition logging.hpp:90
std::variant< Success, BinaryDataBuffer, ImageBinaryData, ColorImageBinaryData, Error > OutMessage
Variant type representing any outgoing message.
Definition variants.hpp:149
Logging utilities and configuration for the irsol library.
std::variant< int, double, std::string > protocol_value_t
Variant type representing protocol values that can be one of several types.
Definition types.hpp:150
std::vector< irsol::types::byte_t > stringToBytes(const std::string &s)
Converts a std::string to a std::vector of irsol::types::byte_t.
Definition utils.cpp:182
Utilities for managing and transforming pixel formats between each other.
Utility functions for protocol string handling and validation in the irsol library.
Serialization utilities for protocol messages and primitive values.
Utility for swapping pixel bytes in a buffer.
Represents a single binary data attribute within the protocol.
Definition binary.hpp:35
Represents an error response message from the server.
Definition error.hpp:36
static constexpr irsol::types::byte_t SOH
Start of Header (SOH) byte: 0x01.
static constexpr irsol::types::byte_t STX
Start of Text (STX) byte: 0x02.
static constexpr irsol::types::byte_t ETX
End of Text (ETX) byte: 0x03.
Represents a success response message from the server.
Definition success.hpp:39
Represents a binary data object within the protocol.
Definition binary.hpp:107
Represents a serialized protocol message with header and payload.
auto value
auto result
auto msg
General utility functions used throughout the irsol library.