From 1d68c85c0e8a73489666a026059f8ae9b8769d9c Mon Sep 17 00:00:00 2001 From: Denis Davydov Date: Thu, 28 Jun 2018 16:08:23 +0300 Subject: [PATCH] Changed the structure of the Handler class, for convenient addition of new methods. --- src/kernel/handler.cpp | 21 ++++++++++++++++----- src/kernel/handler.hpp | 1 + 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/kernel/handler.cpp b/src/kernel/handler.cpp index 50f75b9..200db95 100644 --- a/src/kernel/handler.cpp +++ b/src/kernel/handler.cpp @@ -8,18 +8,29 @@ Handler::Handler() void Handler::handle(QJsonObject jsonReceived) +{ + using namespace std::placeholders; + std::map> handlers = { + {"createSession", std::bind(&Handler::createSession, this, _1)} + }; + + QString action = jsonReceived["action"].toString(); + handlers[action](jsonReceived); +} + +void Handler::createSession(QJsonObject jsonReceived) { QJsonObject jsonSend; - if(jsonReceived["action"] == "createSession" && !jsonReceived.contains("status")) + if(jsonReceived.contains("status") != true) { jsonSend["peerID"] = my_ipv6; jsonSend["action"] = "createSession"; jsonSend["status"] = true; QString peerReceiver = jsonReceived["peerID"].toString(); network->sendDatagram(jsonSend, peerReceiver); - } - else - { + } + else + { emit createSessionSuccess(); - } + } } diff --git a/src/kernel/handler.hpp b/src/kernel/handler.hpp index e2ace3a..18aaad3 100644 --- a/src/kernel/handler.hpp +++ b/src/kernel/handler.hpp @@ -15,6 +15,7 @@ class Handler : public QObject void createSessionSuccess(); private: Network *network; + void createSession(QJsonObject jsonReceived); private slots: void handle(QJsonObject jsonReceived);