Changed some structure of classes Handler and MainWindow. Also added the line "My IP" on the main window.

This commit is contained in:
Denis Davydov 2018-06-29 12:37:38 +03:00
parent 8d8649e5f9
commit f694a32f3e
5 changed files with 27 additions and 14 deletions

View File

@ -2,6 +2,11 @@
Handler::Handler() Handler::Handler()
{ {
using namespace std::placeholders;
handlers = {
{"createSession", std::bind(&Handler::createSession, this, _1)}
};
network = new Network(); network = new Network();
connect(network, &Network::json_received, this, &Handler::handle); connect(network, &Network::json_received, this, &Handler::handle);
} }
@ -9,11 +14,6 @@ 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(); QString action = jsonReceived["action"].toString();
handlers[action](jsonReceived); handlers[action](jsonReceived);
} }

View File

@ -16,8 +16,7 @@ class Handler : public QObject
private: private:
Network *network; Network *network;
void createSession(QJsonObject jsonReceived); void createSession(QJsonObject jsonReceived);
std::map<QString, std::function<void(QJsonObject)>> handlers;
private slots: private slots:
void handle(QJsonObject jsonReceived); void handle(QJsonObject jsonReceived);
}; };

View File

@ -36,7 +36,7 @@ QString Network::local_ipv6()
{ {
QHostAddress address; QHostAddress address;
foreach (address, QNetworkInterface::allAddresses()) { 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; break;
} }
return(address.toString()); return(address.toString());

View File

@ -8,7 +8,10 @@ MainWindow::MainWindow(QWidget *parent) :
ui->setupUi(this); ui->setupUi(this);
network = new Network(false); network = new Network(false);
handler = new Handler(); handler = new Handler();
timer = new QTimer();
connect(timer, SIGNAL(timeout()), this, SLOT(slotTimerAlarm()));
connect(handler, &Handler::createSessionSuccess, this, &MainWindow::peerReceiverConnected); connect(handler, &Handler::createSessionSuccess, this, &MainWindow::peerReceiverConnected);
ui->myIP->setText(my_ipv6);
} }
MainWindow::~MainWindow() MainWindow::~MainWindow()
@ -25,8 +28,6 @@ void MainWindow::on_connectToPeer_clicked()
j["action"] = "createSession"; j["action"] = "createSession";
QString s = ui->peerID->text(); QString s = ui->peerID->text();
network->sendDatagram(j, s); network->sendDatagram(j, s);
timer = new QTimer();
connect(timer, SIGNAL(timeout()), this, SLOT(slotTimerAlarm()));
timer->start(10000); timer->start(10000);
} }
@ -41,7 +42,6 @@ void MainWindow::slotTimerAlarm()
{ {
int ret = QMessageBox::critical(this,QObject::tr("Error"),tr("Timeout Error")); int ret = QMessageBox::critical(this,QObject::tr("Error"),tr("Timeout Error"));
timer->stop(); timer->stop();
delete timer;
ui->connectToPeer->setEnabled(true); ui->connectToPeer->setEnabled(true);
ui->connectToPeer->setText("Подключиться"); ui->connectToPeer->setText("Подключиться");
} }

View File

@ -29,21 +29,35 @@
<string>Influence</string> <string>Influence</string>
</property> </property>
<widget class="QWidget" name="centralWidget"> <widget class="QWidget" name="centralWidget">
<layout class="QHBoxLayout" name="horizontalLayout_2"> <layout class="QGridLayout" name="gridLayout">
<item> <item row="1" column="0" colspan="4">
<widget class="QLineEdit" name="peerID"> <widget class="QLineEdit" name="peerID">
<property name="text"> <property name="text">
<string/> <string/>
</property> </property>
</widget> </widget>
</item> </item>
<item> <item row="0" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>Мой IP:</string>
</property>
</widget>
</item>
<item row="1" column="4">
<widget class="QPushButton" name="connectToPeer"> <widget class="QPushButton" name="connectToPeer">
<property name="text"> <property name="text">
<string>Подключиться</string> <string>Подключиться</string>
</property> </property>
</widget> </widget>
</item> </item>
<item row="0" column="1">
<widget class="QLineEdit" name="myIP">
<property name="readOnly">
<bool>true</bool>
</property>
</widget>
</item>
</layout> </layout>
</widget> </widget>
</widget> </widget>