IRSOL
C++ code implementing socket server for interacting with Baumer camera.
main.cpp
Go to the documentation of this file.
1
4
5#include "irsol/irsol.hpp"
6
8const std::string
10{
11#ifndef PROGRAM_NAME
12#define PROGRAM_NAME "message-protocols-demo"
13#endif
14 return PROGRAM_NAME;
15}
16
18void
19tryParse(const std::string& raw)
20{
22 if(result) {
23 IRSOL_LOG_INFO("Parsed '{}': {}", raw, irsol::protocol::toString(*result));
24 } else {
25 IRSOL_LOG_ERROR("Failed to parse '{}'", raw);
26 }
27}
28
30void
32{
33 IRSOL_LOG_INFO("=== Starting Parsing Demo ===");
34
35 // Valid Assignments with different value types
36 tryParse("intVal=42");
37 tryParse("floatVal=3.1415");
38 tryParse("floatValNegative=-3.1415");
39 tryParse("strVal1=\"hello world\"");
40 tryParse("strVal2={hello world}");
41
42 // Valid Commands and Inquiries
43 tryParse("capture"); // Command
44 tryParse("temperature?"); // Inquiry
45
46 // Invalid cases (malformed syntax)
47 tryParse("=42"); // Missing identifier
48 tryParse("foo==42"); // Extra equals
49 tryParse("foo="); // No value
50 tryParse("incomplete[1,2"); // Unterminated array
51 tryParse("badcmd!"); // Invalid command
52 tryParse("_badcmd"); // Invalid command
53
54 IRSOL_LOG_INFO("=== Parsing Demo Complete ===");
55}
56
58void
60{
61 // Demo 1: Serialize Success from a Command
62 irsol::protocol::Command command("demoCommand");
64 auto serializedCommand = irsol::protocol::Serializer::serialize(std::move(successCommand));
65 IRSOL_LOG_INFO("Success(Command): {}", serializedCommand.toString());
66
67 // Demo 2: Serialize Success from an Assignment
70 auto serializedAssignment = irsol::protocol::Serializer::serialize(std::move(successAssignment));
71 IRSOL_LOG_INFO("Success(Assignment): {}", serializedAssignment.toString());
72
73 // Demo 3: Serialize Success from an Inquiry
74 irsol::protocol::Inquiry inquiry("demoInquiry");
75 irsol::protocol::Success successInquiry =
77 auto serializedInquiry = irsol::protocol::Serializer::serialize(std::move(successInquiry));
78 IRSOL_LOG_INFO("Success(Inquiry): {}", serializedInquiry.toString());
79
80 // Demo 4: Serialize Error from Command
81 irsol::protocol::Error errorCommand = irsol::protocol::Error::from(command, "Command failed");
82 auto serializedErrorCommand = irsol::protocol::Serializer::serialize(std::move(errorCommand));
83 IRSOL_LOG_INFO("Error(Command): {}", serializedErrorCommand.toString());
84
85 // Demo 5: Serialize Error from Assignment
86 irsol::protocol::Error errorAssignment =
88 auto serializedErrorAssignment =
89 irsol::protocol::Serializer::serialize(std::move(errorAssignment));
90 IRSOL_LOG_INFO("Error(Assignment): {}", serializedErrorAssignment.toString());
91
92 // Demo 6: Serialize Error from Inquiry
93 irsol::protocol::Error errorInquiry = irsol::protocol::Error::from(inquiry, "Not found");
94 auto serializedErrorInquiry = irsol::protocol::Serializer::serialize(std::move(errorInquiry));
95 IRSOL_LOG_INFO("Error(Inquiry): {}", serializedErrorInquiry.toString());
96}
97
99void
101{
102 demoParsing();
104}
105
107int
109{
110 // Initialize logging
111 std::string logPath = "logs/" + getProgramName() + ".log";
112 irsol::initLogging(logPath.c_str(), spdlog::level::debug);
114
115 // Run demo
116 demoProtocol();
117
118 return 0;
119}
static std::optional< InMessage > parse(const std::string &line)
Attempts to parse a single protocol input line into a structured InMessage.
Definition parser.cpp:11
static internal::SerializedMessage serialize(OutMessage &&msg)
Serialize an irsol::protocol::OutMessage variant into a serialized protocol message.
const std::string getProgramName()
Returns the program name, typically used for logging. If PROGRAM_NAME is not defined at compile time,...
Definition main.cpp:12
#define PROGRAM_NAME
int main()
Definition main.cpp:90
void tryParse(const std::string &raw)
Tries to parse a message string and logs the result or error.
Definition main.cpp:19
void demoParsing()
Demonstrates parsing raw protocol strings into structured InMessages.
Definition main.cpp:31
void demoSerializing()
Demonstrates serializing protocol OutMessages into SerializedMessages.
Definition main.cpp:59
void demoProtocol()
Demonstrates protocol message creation and serialization.
Definition main.cpp:100
void initAssertHandler()
Initializes the assertion handler system.
Definition assert.cpp:14
#define IRSOL_LOG_INFO(...)
Logs an info-level message using the default logger.
Definition logging.hpp:92
#define IRSOL_LOG_ERROR(...)
Logs an error-level message using the default logger.
Definition logging.hpp:94
void initLogging(const char *fileSinkFilename="logs/irsol.log", std::optional< spdlog::level::level_enum > minLogLevel=std::nullopt)
Initializes the irsol logging system.
Definition logging.cpp:108
std::string toString(const InMessage &msg)
Converts an incoming message variant to a human-readable string.
Definition variants.cpp:19
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
Represents an assignment operation in the protocol.
Represents a command invocation in the protocol.
Definition command.hpp:33
Represents an error response message from the server.
Definition error.hpp:36
static Error from(const T &msg, const std::string &description)
Creates an error from a specific incoming message type.
Definition error.hpp:63
Represents a value inquiry in the protocol.
Definition inquiry.hpp:32
Represents a success response message from the server.
Definition success.hpp:39
static Success from(const Assignment &msg, std::optional< irsol::types::protocol_value_t > overrideValue=std::nullopt)
Creates a success message from an Assignment.
Definition success.hpp:75
irsol::protocol::Assignment assignment
auto result