From f694a32f3e8237acb2b7967c554c2dabc707e000 Mon Sep 17 00:00:00 2001 From: Denis Davydov Date: Fri, 29 Jun 2018 12:37:38 +0300 Subject: [PATCH] Changed some structure of classes Handler and MainWindow. Also added the line "My IP" on the main window. --- src/kernel/handler.cpp | 10 +++++----- src/kernel/handler.hpp | 3 +-- src/kernel/network.cpp | 2 +- src/mainwindow.cpp | 6 +++--- src/mainwindow.ui | 20 +++++++++++++++++--- 5 files changed, 27 insertions(+), 14 deletions(-) diff --git a/src/kernel/handler.cpp b/src/kernel/handler.cpp index 200db95..6550151 100644 --- a/src/kernel/handler.cpp +++ b/src/kernel/handler.cpp @@ -2,6 +2,11 @@ Handler::Handler() { + using namespace std::placeholders; + + handlers = { + {"createSession", std::bind(&Handler::createSession, this, _1)} + }; network = new Network(); connect(network, &Network::json_received, this, &Handler::handle); } @@ -9,11 +14,6 @@ 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); } diff --git a/src/kernel/handler.hpp b/src/kernel/handler.hpp index 18aaad3..a16be97 100644 --- a/src/kernel/handler.hpp +++ b/src/kernel/handler.hpp @@ -16,8 +16,7 @@ class Handler : public QObject private: Network *network; void createSession(QJsonObject jsonReceived); + std::map> handlers; private slots: void handle(QJsonObject jsonReceived); - - }; diff --git a/src/kernel/network.cpp b/src/kernel/network.cpp index 027582f..252f9b3 100644 --- a/src/kernel/network.cpp +++ b/src/kernel/network.cpp @@ -36,7 +36,7 @@ QString Network::local_ipv6() { QHostAddress address; foreach (address, QNetworkInterface::allAddresses()) { - if (address.protocol() == QAbstractSocket::IPv6Protocol && address != QHostAddress(QHostAddress::LocalHostIPv6)) + if (address.protocol() == QAbstractSocket::IPv6Protocol && address != QHostAddress(QHostAddress::LocalHostIPv6) && (address.toString()).contains("fc")) break; } return(address.toString()); diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 4d5d7db..4ca2ca5 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -8,7 +8,10 @@ MainWindow::MainWindow(QWidget *parent) : ui->setupUi(this); network = new Network(false); handler = new Handler(); + timer = new QTimer(); + connect(timer, SIGNAL(timeout()), this, SLOT(slotTimerAlarm())); connect(handler, &Handler::createSessionSuccess, this, &MainWindow::peerReceiverConnected); + ui->myIP->setText(my_ipv6); } MainWindow::~MainWindow() @@ -25,8 +28,6 @@ void MainWindow::on_connectToPeer_clicked() j["action"] = "createSession"; QString s = ui->peerID->text(); network->sendDatagram(j, s); - timer = new QTimer(); - connect(timer, SIGNAL(timeout()), this, SLOT(slotTimerAlarm())); timer->start(10000); } @@ -41,7 +42,6 @@ void MainWindow::slotTimerAlarm() { int ret = QMessageBox::critical(this,QObject::tr("Error"),tr("Timeout Error")); timer->stop(); - delete timer; ui->connectToPeer->setEnabled(true); ui->connectToPeer->setText("Подключиться"); } diff --git a/src/mainwindow.ui b/src/mainwindow.ui index 7e212b7..0eb633a 100644 --- a/src/mainwindow.ui +++ b/src/mainwindow.ui @@ -29,21 +29,35 @@ Influence - - + + - + + + + Мой IP: + + + + Подключиться + + + + true + + +