Skip to content

Commit eda4edd

Browse files
author
Ferhat Aslan
committed
feat: Add manual setting of network interface
1 parent 579d6ce commit eda4edd

File tree

4 files changed

+111
-25
lines changed

4 files changed

+111
-25
lines changed

bin/EtaNetServerV0_9_4.zip

22.1 MB
Binary file not shown.

dialog.cpp

+95-24
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@
22
#include <QtNetwork>
33

44
#include <stdlib.h>
5-
5+
#include <QDebug>
66
#include "dialog.h"
77
#include "server.h"
88

99
Dialog::Dialog(QWidget *parent)
1010
: QWidget(parent)
1111
{
12+
hostCombo = new QComboBox;
13+
portLineEdit = new QLineEdit();
14+
setIpPortButton = new QPushButton(tr("Set Interface"));
15+
1216
messageLabel = new QLabel;
1317
messageLabel->setWordWrap(true);
1418
statusLabel = new QLabel;
@@ -18,51 +22,62 @@ Dialog::Dialog(QWidget *parent)
1822
dqButton = new QPushButton(tr("Save Data"));
1923
dqButton->setAutoDefault(false);
2024
dqButton->setCheckable(true);
25+
hostLabel = new QLabel(tr("Network Interface:"));
26+
hostLabel->setBuddy(hostCombo);
27+
portLabel = new QLabel(tr("Server port:"));
28+
portLabel->setBuddy(portLineEdit);
2129

22-
QString ipAddress;
30+
hostCombo->setEditable(true);
31+
// find out name of this machine
32+
QString name = QHostInfo::localHostName();
33+
34+
if (name != QLatin1String("localhost"))
35+
hostCombo->addItem(QString("localhost"));
36+
// find out IP addresses of this machine
2337
QList<QHostAddress> ipAddressesList = QNetworkInterface::allAddresses();
24-
// use the first non-localhost IPv4 address
38+
// add non-localhost addresses
2539
for (int i = 0; i < ipAddressesList.size(); ++i) {
2640
if (ipAddressesList.at(i) != QHostAddress::LocalHost &&
2741
ipAddressesList.at(i).toIPv4Address()) {
28-
ipAddress = ipAddressesList.at(i).toString();
29-
break;
42+
hostCombo->addItem(ipAddressesList.at(i).toString());
3043
}
31-
}
32-
33-
QHostAddress ipAddressQ(ipAddress);
3444

35-
36-
if (!server.listen(ipAddressQ, 50005)) {
37-
QMessageBox::critical(this, tr("\u03B7Net Server"),
38-
tr("Unable to start the server: %1.")
39-
.arg(server.errorString()));
40-
this->close();
41-
return;
4245
}
46+
// add localhost addresses
47+
for (int i = 0; i < ipAddressesList.size(); ++i) {
48+
if (ipAddressesList.at(i).isLoopback())
49+
hostCombo->addItem(ipAddressesList.at(i).toString());
50+
}
51+
portLineEdit->setText("50005");
52+
setIpPortButton->setEnabled(!hostCombo->currentText().isEmpty() &&
53+
!portLineEdit->text().isEmpty());
4354

44-
// if we did not find one, use IPv4 localhost
45-
if (ipAddress.isEmpty())
46-
ipAddress = QHostAddress(QHostAddress::LocalHost).toString();
47-
statusLabel->setText(tr("The \u03B7Net server is running on\n\nIP: %1\nport: %2\n\n"
48-
"Run the \u03B7Net Clients now.")
49-
.arg(ipAddress).arg(server.serverPort()));
50-
messageLabel->setText(tr("Wait for data from \u03B7Net clients..."));
55+
statusLabel->setText(tr("The \u03B7Net server ready to start."));
56+
messageLabel->setText(tr("Please select the network interface."));
5157

52-
connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
58+
connect(quitButton, &QAbstractButton::clicked, this, &Dialog::close);
59+
connect(hostCombo, &QComboBox::currentTextChanged, this, &Dialog::networkInterface);
60+
connect(setIpPortButton, &QAbstractButton::clicked, this, &Dialog::setNetworkInterface);
61+
connect(portLineEdit, &QLineEdit::textChanged, this, &Dialog::networkInterface);
5362

5463
QHBoxLayout *buttonLayout = new QHBoxLayout;
5564
buttonLayout->addStretch(1);
65+
buttonLayout->addWidget(setIpPortButton);
5666
buttonLayout->addWidget(dqButton);
5767
buttonLayout->addWidget(quitButton);
5868
buttonLayout->addStretch(1);
5969

6070
QVBoxLayout *mainLayout = new QVBoxLayout;
71+
//QGridLayout *mainLayout = new QGridLayout;
6172
mainLayout->addWidget(statusLabel);
6273
mainLayout->addWidget(messageLabel);
74+
mainLayout->addWidget(hostLabel);
75+
mainLayout->addWidget(hostCombo);
76+
mainLayout->addWidget(portLabel);
77+
mainLayout->addWidget(portLineEdit);
6378
mainLayout->addLayout(buttonLayout);
6479
setLayout(mainLayout);
65-
setWindowTitle(tr("\u03B7Net Server"));
80+
setWindowTitle(tr("\u03B7Net Server v0.9.4"));
6681
}
6782

6883
Dialog& Dialog::getInstance()
@@ -81,3 +96,59 @@ bool Dialog::getDQButtonState(void)
8196
qDebug() << "ButtonState: " << dqButton->isChecked();
8297
return dqButton->isChecked();
8398
}
99+
100+
void Dialog::networkInterface()
101+
{
102+
if (!this->server.isListening())
103+
{
104+
setIpPortButton->setEnabled(!hostCombo->currentText().isEmpty() &&
105+
!portLineEdit->text().isEmpty());
106+
107+
}
108+
109+
}
110+
111+
void Dialog::setNetworkInterface()
112+
{
113+
setIpPortButton->setEnabled(false);
114+
hostCombo->setVisible(false);
115+
portLineEdit->setVisible(false);
116+
portLabel->setVisible(false);
117+
hostLabel->setVisible(false);
118+
119+
120+
121+
QString ipAddress;
122+
123+
ipAddress = hostCombo->currentText();
124+
qDebug() << ipAddress;
125+
QHostAddress ipAddressQ(ipAddress);
126+
127+
128+
if (!this->server.listen(ipAddressQ, portLineEdit->text().toInt())) {
129+
QMessageBox::critical(this, tr("\u03B7Net Server"),
130+
tr("Unable to start the server: %1.")
131+
.arg(this->server.errorString()));
132+
this->close();
133+
return;
134+
}
135+
136+
// // if we did not find one, use IPv4 localhost
137+
// if (ipAddress.isEmpty())
138+
// ipAddress = QHostAddress(QHostAddress::LocalHost).toString();
139+
statusLabel->setText(tr("The \u03B7Net server is running on\n\nIP: %1\nport: %2\n\n"
140+
"Run the \u03B7Net Clients now.")
141+
.arg(ipAddress).arg(server.serverPort()));
142+
messageLabel->setText(tr("Wait for data from \u03B7Net clients..."));
143+
}
144+
145+
void Dialog::close()
146+
{
147+
hostCombo->deleteLater();
148+
portLineEdit->deleteLater();
149+
hostLabel->deleteLater();
150+
portLabel->deleteLater();
151+
QWidget::close();
152+
}
153+
154+

dialog.h

+15
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@
44
#include <QWidget>
55
#include "server.h"
66

7+
78
QT_BEGIN_NAMESPACE
9+
class QComboBox;
10+
class QLineEdit;
811
class QLabel;
912
class QPushButton;
13+
1014
QT_END_NAMESPACE
1115

1216
/**
@@ -56,8 +60,18 @@ class Dialog : public QWidget
5660
*/
5761
Dialog(const Dialog&);
5862

63+
//slots:
64+
void networkInterface(void);
65+
void setNetworkInterface(void);
66+
void close(void);
67+
5968

6069
private:
70+
QComboBox *hostCombo;
71+
QLineEdit *portLineEdit;
72+
QPushButton *setIpPortButton;
73+
QLabel *hostLabel;
74+
QLabel *portLabel;
6175
/**
6276
* @brief label - is used for displaying text
6377
*/
@@ -78,6 +92,7 @@ class Dialog : public QWidget
7892
* @brief server - creates the server instance
7993
*/
8094
EtaNetServer server;
95+
8196
};
8297

8398
#endif

threadedetanetserver.pro

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ QT += network widgets
22

33
QT += core sql
44

5-
TARGET = EtaNetServerV0_9_3
5+
TARGET = EtaNetServerV0_9_4
66

77
HEADERS = dialog.h \
88
dbhandler.h \

0 commit comments

Comments
 (0)