~aleteoryx/muditaos

e2d211641942f6f551fd68fa0f5d4dacf70be859 — Adam Dobrowolski 4 years ago 96b5ee6
[EGD-6617] Added option to disconnect handler in service

Till now we were only able to connect handlers to services, but
disconnect was not possible. With nullptr handler system would crash
2 files changed, 15 insertions(+), 1 deletions(-)

M module-sys/Service/Service.cpp
M module-sys/Service/Service.hpp
M module-sys/Service/Service.cpp => module-sys/Service/Service.cpp +14 -1
@@ 158,6 158,9 @@ namespace sys
        const auto idx = type_index(typeid(*message));
        if (const auto handler = message_handlers.find(idx); handler != message_handlers.end()) {
            const auto &handlerFunction = handler->second;
            if (handlerFunction == nullptr) {
                return {true, nullptr};
            }
            return {true, handlerFunction(message)};
        }
        return {false, nullptr};


@@ 168,7 171,7 @@ namespace sys
        auto idx = type_index(type);
        if (message_handlers.find(idx) == message_handlers.end()) {
            log_debug("Registering new message handler on %s", type.name());
            message_handlers[idx] = handler;
            message_handlers[idx] = std::move(handler);
            return true;
        }
        LOG_ERROR("Handler for: %s already registered!", type.name());


@@ 186,6 189,16 @@ namespace sys
        return connect(&msg, handler);
    }

    bool Service::disconnect(const std::type_info &type)
    {
        auto iter = message_handlers.find(type);
        if (iter == std::end(message_handlers)) {
            return false;
        }
        message_handlers.erase(iter);
        return true;
    }

    void Service::CloseHandler()
    {
        timers.stop();

M module-sys/Service/Service.hpp => module-sys/Service/Service.hpp +1 -0
@@ 95,6 95,7 @@ namespace sys
        bool connect(const std::type_info &type, MessageHandler handler);
        bool connect(Message *msg, MessageHandler handler);
        bool connect(Message &&msg, MessageHandler handler);
        bool disconnect(const std::type_info &type);

        void sendCloseReadyMessage(Service *service);