Changed the structure of the Handler class, for convenient addition of new methods.

This commit is contained in:
Denis Davydov 2018-06-28 16:08:23 +03:00
parent c82cf387f1
commit 1d68c85c0e
2 changed files with 17 additions and 5 deletions

View File

@ -8,18 +8,29 @@ Handler::Handler()
void Handler::handle(QJsonObject jsonReceived) void Handler::handle(QJsonObject jsonReceived)
{
using namespace std::placeholders;
std::map<QString, std::function<void(QJsonObject)>> handlers = {
{"createSession", std::bind(&Handler::createSession, this, _1)}
};
QString action = jsonReceived["action"].toString();
handlers[action](jsonReceived);
}
void Handler::createSession(QJsonObject jsonReceived)
{ {
QJsonObject jsonSend; QJsonObject jsonSend;
if(jsonReceived["action"] == "createSession" && !jsonReceived.contains("status")) if(jsonReceived.contains("status") != true)
{ {
jsonSend["peerID"] = my_ipv6; jsonSend["peerID"] = my_ipv6;
jsonSend["action"] = "createSession"; jsonSend["action"] = "createSession";
jsonSend["status"] = true; jsonSend["status"] = true;
QString peerReceiver = jsonReceived["peerID"].toString(); QString peerReceiver = jsonReceived["peerID"].toString();
network->sendDatagram(jsonSend, peerReceiver); network->sendDatagram(jsonSend, peerReceiver);
} }
else else
{ {
emit createSessionSuccess(); emit createSessionSuccess();
} }
} }

View File

@ -15,6 +15,7 @@ class Handler : public QObject
void createSessionSuccess(); void createSessionSuccess();
private: private:
Network *network; Network *network;
void createSession(QJsonObject jsonReceived);
private slots: private slots:
void handle(QJsonObject jsonReceived); void handle(QJsonObject jsonReceived);