From 1efd8ab1b4d4a2b1c8236aee33f51d960964c20b Mon Sep 17 00:00:00 2001 From: Niklas Halle Date: Wed, 26 Aug 2020 12:00:21 +0200 Subject: fixes (i hope) --- include/Handler.hpp | 13 +++++++++++ src/SimpleHandlers.cpp | 12 +++++----- src/main.cpp | 59 +++++++++++++++++++++++++++++++++++--------------- 3 files changed, 60 insertions(+), 24 deletions(-) diff --git a/include/Handler.hpp b/include/Handler.hpp index 3aed302..3328d91 100644 --- a/include/Handler.hpp +++ b/include/Handler.hpp @@ -3,6 +3,7 @@ #include #include #include +#include #include "crow.h" @@ -12,14 +13,26 @@ namespace Handler { void *payload)> handler_function; struct CommandHandler { + CommandHandler(std::string command, handler_function func, std::queue *description, void *payload = nullptr) + : command{std::move(command)}, func{std::move(func)}, description{description}, payload{payload} {} + ~CommandHandler() { delete description; + description = nullptr; + } + + CommandHandler(CommandHandler &&o) noexcept { + func = o.func; + description = o.description; + o.description = nullptr; + payload = o.payload; } [[nodiscard]] json exec(std::string const &arguments, std::string const &session) const { return func(arguments, session, payload); } + std::string command; handler_function func{nullptr}; // handler takes ownership of the description, using it after passing is unsafe std::queue *description{nullptr}; diff --git a/src/SimpleHandlers.cpp b/src/SimpleHandlers.cpp index c9d0fcf..40cfa68 100644 --- a/src/SimpleHandlers.cpp +++ b/src/SimpleHandlers.cpp @@ -24,7 +24,7 @@ Handler::json Handler::wikiHandler(std::string const &arguments, std::string con } Handler::json Handler::helpHandler(std::string const &arguments, std::string const &session, void *payload) { - auto commands = *(std::unordered_map *) payload; + auto commands = (std::vector *) payload; std::vector reply_vec; reply_vec.emplace_back(create_text( @@ -34,15 +34,15 @@ Handler::json Handler::helpHandler(std::string const &arguments, std::string con "Dear my fellow users, these are my official commands, which are offered to my believers.\n" "\n")); - for (auto const &itor : commands) { + for (auto const &itor : *commands) { std::vector commandAnnotations; commandAnnotations.emplace_back(create_annotation(Reply::AnnotationType::command)); reply_vec.emplace_back(create_text("- ")); - reply_vec.emplace_back(create_text(itor.first, std::move(commandAnnotations))); - while (!itor.second.description->empty()) { - reply_vec.emplace_back(std::move(itor.second.description->back())); - itor.second.description->pop(); + reply_vec.emplace_back(create_text(itor.command, std::move(commandAnnotations))); + while (!itor.description->empty()) { + reply_vec.emplace_back(std::move(itor.description->back())); + itor.description->pop(); } } diff --git a/src/main.cpp b/src/main.cpp index 59fd6a1..8e0b442 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -19,27 +19,37 @@ int main() { crow::SimpleApp app; - auto createPlainDescriptionFromText = [] (std::string const &text) -> std::queue* { + auto createPlainDescriptionFromText = [](std::string const &text) -> std::queue * { auto reply_queue = new std::queue; reply_queue->emplace(Response::create_text(text)); return reply_queue; }; // command --> handler - std::unordered_map commands{ - {"wiki", {.func = Handler::wikiHandler, .description = createPlainDescriptionFromText("Sends you to Wikipedia!")}}, - {"stop", {.func = Handler::stopHandler, .description = createPlainDescriptionFromText("Stops the bot"), .payload = &app}}, - {"mensa", {.func = Handler::mensaHandler, .description = createPlainDescriptionFromText("{adlershof, nord, sued} Shows the daily menu of the mensa at various places.")}}, - {"klinger", {.func = Handler::klingerHandler, .description = createPlainDescriptionFromText("Greats in french. Bonjour!")}}, - {"relation", {.func = Handler::relationShipHandler, .description = createPlainDescriptionFromText("[name1] [name2] Shows the result of an odd astrological religious pseudo-algorithm, based on the fact how much blessing a relationship receives by the glorious N.")}}, - }; - - commands.insert({"help", - {.func = Handler::helpHandler, .description = createPlainDescriptionFromText("This is my holy manual."), .payload = &commands}}); + std::vector commands; + commands.emplace_back( + Handler::CommandHandler{"wiki", Handler::wikiHandler, createPlainDescriptionFromText( + "Sends you to Wikipedia!")}); + commands.emplace_back( + Handler::CommandHandler{"stop", Handler::stopHandler, createPlainDescriptionFromText( + "Stops the bot"), &app}); + commands.emplace_back( + Handler::CommandHandler{"mensa", Handler::mensaHandler, createPlainDescriptionFromText( + "{adlershof, nord, sued} Shows the daily menu of the mensa at various places.")}); + commands.emplace_back( + Handler::CommandHandler{"klinger", Handler::klingerHandler, createPlainDescriptionFromText( + "Greats in french. Bonjour!")}); + commands.emplace_back( + Handler::CommandHandler{"relation", Handler::relationShipHandler, createPlainDescriptionFromText( + "[name1] [name2] Shows the result of an odd astrological religious pseudo-algorithm, based on the fact how much blessing a relationship receives by the glorious N.")}); + commands.emplace_back(Handler::CommandHandler{"help", Handler::helpHandler, createPlainDescriptionFromText( + "This is my holy manual."), &commands}); CROW_ROUTE(app, "/") .methods("POST"_method) - ([&commands](crow::request const &request) { + ([&commands]( + crow::request const &request + ) { auto data = crow::json::load(request.body); if (!data) @@ -53,15 +63,28 @@ int main() { std::string arguments = (data.count("arguments") ? data["arguments"].s() : std::string("")); std::string session = (data.count("session") ? data["session"].s() : std::string("null")); - if (auto itor = commands.find(command); itor != commands.end()) { - auto handler = itor->second; - return crow::response{handler.exec(arguments, session)}; + auto itor = std::find_if( + commands.begin(), + commands.end(), + [command](Handler::CommandHandler const &handler) { + return handler.command == command; + }); + if (itor != commands.end()) { + return crow::response{itor->exec(arguments, session)}; } - return crow::response{Response::simple_response("No such command!", "null", false)}; + return crow::response{ + Response::simple_response("No such command!", "null", false)}; }); - app.port(18080).multithreaded().run(); + app.port(18080). + + multithreaded() + + . + + run(); - std::cout << "Stopped successfully" << std::endl; + std::cout << "Stopped successfully" << + std::endl; } -- cgit v1.2.3-54-g00ecf