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
7#include <memory>
8#include <string>
9#include <vector>
10
13const std::string
15{
16#ifndef PROGRAM_NAME
17#define PROGRAM_NAME "message-handlers-demo"
18#endif
19 return PROGRAM_NAME;
20}
21
24{
25public:
26 MyAssignmentHandler(std::shared_ptr<irsol::server::handlers::Context> ctx)
27 : irsol::server::handlers::AssignmentHandler(ctx)
28 {}
29
32 std::vector<irsol::server::handlers::out_message_t> operator()(
34 irsol::protocol::Assignment&& message) override
35 {
36 return process(nullptr, std::move(message));
37 }
38
39protected:
40 std::vector<irsol::server::handlers::out_message_t> process(
41 IRSOL_MAYBE_UNUSED std::shared_ptr<irsol::server::ClientSession> client,
42 irsol::protocol::Assignment&& message) override
43 {
44 IRSOL_LOG_INFO("[MyAssignmentHandler] Called with {}.", message.toString());
45 // Respond with a Success message
46 // Due to move-only semantics of `Success`, we must create the response vector
47 // in this way. Using brace-initialization like `return
48 // {irsol::protocol::Success::from(message)};` would not work here.
49 std::vector<irsol::server::handlers::out_message_t> result;
50 result.emplace_back(irsol::protocol::Success::from(message));
51 return result;
52 }
53};
54
56void
58{
59 // Don't pass a Context, as we don't need it in this demo
60 std::shared_ptr<irsol::server::handlers::Context> ctx = nullptr;
61
62 // Create a MessageHandler instance
64
65 // Create and register a custom Assignment handler for "foo" identifier.
68
69 // Simulate a session and client id
71
72 // Dispatch messages to the MessageHandler
73
74 // Create test Assignment messages
75 irsol::protocol::Assignment fooMsg("foo", 42);
76 IRSOL_LOG_INFO("Dispatching {}...", fooMsg.toString());
77 auto fooResult = msgHandler.handle(clientId, std::move(fooMsg));
78 IRSOL_LOG_INFO("fooResult size: {}", fooResult.size());
79 for(const auto& msg : fooResult) {
81 }
82
83 // Try dispatching an unregistered identifier
84 irsol::protocol::Assignment unknownMsg("baz", 0);
85 IRSOL_LOG_INFO("Dispatching {} (should not find handler)...", unknownMsg.toString());
86 auto unknownResult = msgHandler.handle(clientId, std::move(unknownMsg));
87 IRSOL_LOG_INFO("unknownResult size: {}", unknownResult.size());
88 for(const auto& msg : unknownResult) {
90 }
91}
92
94int
96{
97 // Construct log file path based on program name
98 std::string logPath = "logs/" + getProgramName() + ".log";
99 irsol::initLogging(logPath.c_str());
100
101 // Enable custom assertion handler
103
104 demoHandlers();
105 return 0;
106}
Example custom handler for Assignment messages.
Definition main.cpp:24
std::vector< irsol::server::handlers::out_message_t > process(IRSOL_MAYBE_UNUSED std::shared_ptr< irsol::server::ClientSession > client, irsol::protocol::Assignment &&message) override
Definition main.cpp:40
std::vector< irsol::server::handlers::out_message_t > operator()(IRSOL_MAYBE_UNUSED const irsol::types::client_id_t &clientId, irsol::protocol::Assignment &&message) override
Definition main.cpp:32
MyAssignmentHandler(std::shared_ptr< irsol::server::handlers::Context > ctx)
Definition main.cpp:26
Binds incoming protocol messages to the appropriate per-client logic.
handling_function_response_t handle(const irsol::types::client_id_t &clientId, protocol::InMessage &&message) const
Dispatches an incoming message to the correct user-defined handler.
bool registerHandler(const std::string &identifier, handler_function_t< T > handler)
Registers a user-defined handler for a specific message type and identifier.
Generic handler base class for protocol messages.
Definition base.hpp:39
std::shared_ptr< Context > ctx
Handler context (provides access to app and utilities).
Definition base.hpp:81
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 demoHandlers()
Demonstrates handler creation, registration, and dispatch.
Definition main.cpp:57
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
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
#define IRSOL_MAYBE_UNUSED
Suppresses compiler warnings about unused variables or parameters.
Definition macros.hpp:39
constexpr auto makeHandler(std::shared_ptr< Context > ctx, Args &&... args)
Constructs a handler instance of the given type.
Definition factory.hpp:28
std::string client_id_t
Represents a unique client identifier. Typically used to identify connected clients by string IDs.
Definition types.hpp:55
std::string uuid()
Generates a new UUID string.
Definition utils.cpp:16
Represents an assignment operation in the protocol.
std::string toString() const
Converts the assignment to a human-readable string.
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
auto result
auto msg