|
| 1 | +/// @file examples/04-message-handlers/main.cpp |
| 2 | +/// @brief Demonstrates creation, registration, and dispatch of handlers using |
| 3 | +/// @ref irsol::server::handlers::MessageHandler. |
| 4 | + |
| 5 | +#include "irsol/irsol.hpp" |
| 6 | + |
| 7 | +#include <memory> |
| 8 | +#include <string> |
| 9 | +#include <vector> |
| 10 | + |
| 11 | +/// @brief Returns the program name, typically used for logging. |
| 12 | +/// If `PROGRAM_NAME` is not defined at compile time, returns `"message-handlers-demo"`. |
| 13 | +const std::string |
| 14 | +getProgramName() |
| 15 | +{ |
| 16 | +#ifndef PROGRAM_NAME |
| 17 | +#define PROGRAM_NAME "message-handlers-demo" |
| 18 | +#endif |
| 19 | + return PROGRAM_NAME; |
| 20 | +} |
| 21 | + |
| 22 | +/// @brief Example custom handler for Assignment messages. |
| 23 | +class MyAssignmentHandler : public irsol::server::handlers::AssignmentHandler |
| 24 | +{ |
| 25 | +public: |
| 26 | + MyAssignmentHandler(std::shared_ptr<irsol::server::handlers::Context> ctx) |
| 27 | + : irsol::server::handlers::AssignmentHandler(ctx) |
| 28 | + {} |
| 29 | + |
| 30 | + /// Overrides the base operator() as we skip the collection of the client's Session |
| 31 | + /// in this demo, which is the behavior of the base class. |
| 32 | + std::vector<irsol::server::handlers::out_message_t> operator()( |
| 33 | + IRSOL_MAYBE_UNUSED const irsol::types::client_id_t& clientId, |
| 34 | + irsol::protocol::Assignment&& message) override |
| 35 | + { |
| 36 | + return process(nullptr, std::move(message)); |
| 37 | + } |
| 38 | + |
| 39 | +protected: |
| 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 | + |
| 55 | +/// @brief Demonstrates handler creation, registration, and dispatch. |
| 56 | +void |
| 57 | +demoHandlers() |
| 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 |
| 63 | + irsol::server::handlers::MessageHandler msgHandler; |
| 64 | + |
| 65 | + // Create and register a custom Assignment handler for "foo" identifier. |
| 66 | + msgHandler.registerHandler<irsol::protocol::Assignment>( |
| 67 | + "foo", irsol::server::handlers::makeHandler<MyAssignmentHandler>(ctx)); |
| 68 | + |
| 69 | + // Simulate a session and client id |
| 70 | + irsol::types::client_id_t clientId = irsol::utils::uuid(); |
| 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) { |
| 80 | + IRSOL_LOG_INFO("Response: {}", irsol::protocol::toString(msg)); |
| 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) { |
| 89 | + IRSOL_LOG_INFO("Response: {}", irsol::protocol::toString(msg)); |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +/// @brief Main entry point for the handler demo application. |
| 94 | +int |
| 95 | +main() |
| 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 |
| 102 | + irsol::initAssertHandler(); |
| 103 | + |
| 104 | + demoHandlers(); |
| 105 | + return 0; |
| 106 | +} |
0 commit comments