IRSOL
C++ code implementing socket server for interacting with Baumer camera.
main.cpp File Reference

Demonstrates creation, registration, and dispatch of handlers using irsol::server::handlers::MessageHandler. More...

#include "irsol/irsol.hpp"
#include <memory>
#include <string>
#include <vector>

Go to the source code of this file.

Classes

class  MyAssignmentHandler
 Example custom handler for Assignment messages. More...
 

Macros

#define PROGRAM_NAME   "message-handlers-demo"
 

Functions

const std::string getProgramName ()
 Returns the program name, typically used for logging. If PROGRAM_NAME is not defined at compile time, returns "message-handlers-demo".
 
void demoHandlers ()
 Demonstrates handler creation, registration, and dispatch.
 
int main ()
 Main entry point for the handler demo application.
 

Detailed Description

Demonstrates creation, registration, and dispatch of handlers using irsol::server::handlers::MessageHandler.

Definition in file main.cpp.

Macro Definition Documentation

◆ PROGRAM_NAME

#define PROGRAM_NAME   "message-handlers-demo"

Function Documentation

◆ demoHandlers()

void demoHandlers ( )

Demonstrates handler creation, registration, and dispatch.

Definition at line 57 of file main.cpp.

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}
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.
#define IRSOL_LOG_INFO(...)
Logs an info-level message using the default logger.
Definition logging.hpp:92
std::string toString(const InMessage &msg)
Converts an incoming message variant to a human-readable string.
Definition variants.cpp:19
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.
auto msg

◆ getProgramName()

const std::string getProgramName ( )

Returns the program name, typically used for logging. If PROGRAM_NAME is not defined at compile time, returns "message-handlers-demo".

Definition at line 14 of file main.cpp.

15{
16#ifndef PROGRAM_NAME
17#define PROGRAM_NAME "message-handlers-demo"
18#endif
19 return PROGRAM_NAME;
20}
#define PROGRAM_NAME

◆ main()

int main ( )

Main entry point for the handler demo application.

Definition at line 95 of file main.cpp.

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}
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
void demoHandlers()
Demonstrates handler creation, registration, and dispatch.
Definition main.cpp:57
void initAssertHandler()
Initializes the assertion handler system.
Definition assert.cpp:14
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