Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added configuration server #12

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions configurationServer/client/8_1client.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
HEADERS = client.h
SOURCES = client.cpp \
main.cpp
QT += network
QT += core gui
QT += widgets


# install
target.path = $$[QT_INSTALL_EXAMPLES]/network/fortuneclient
sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS fortuneclient.pro
sources.path = $$[QT_INSTALL_EXAMPLES]/network/fortuneclient
INSTALLS += target sources

symbian {
include($$QT_SOURCE_TREE/examples/symbianpkgrules.pri)
TARGET.CAPABILITY = "NetworkServices ReadUserData WriteUserData"
TARGET.EPOCHEAPSIZE = 0x20000 0x2000000
}
maemo5: include($$QT_SOURCE_TREE/examples/maemo5pkgrules.pri)

symbian: warning(This example might not fully work on Symbian platform)
maemo5: warning(This example might not fully work on Maemo platform)
simulator: warning(This example might not fully work on Simulator platform)

FORMS +=
220 changes: 220 additions & 0 deletions configurationServer/client/client.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
#include "client.h"

Client::Client(QWidget *parent) :
QDialog(parent),
networkSession(0),
blockSize(0)
{

setWindowTitle(tr("Chat client"));

/// creating buttons and text:
hostLabel = new QLabel(tr("Server IP-adress:"));

portLabel = new QLabel(tr("Server port:"));

portLineEdit = new QLineEdit;
portLineEdit->setValidator(new QIntValidator(1, 65535));

messageText = new QLineEdit;

sendButton = new QPushButton(tr("Send"));
sendButton->setDisabled(true);

connectButton = new QPushButton(tr("Connect"));

quitButton = new QPushButton(tr("Quit"));
quitButton->setAutoDefault(false);

chatText = new QTextEdit;
chatText->setReadOnly(true);

/// adding adresses to combo box
hostAdress = new QLineEdit;
/* QList<QHostAddress> adressList = QNetworkInterface::allAddresses();

for (int i = 0; i < adressList.size(); ++i)
{
hostAdress->addItem(adressList.at(i).toString());
}
*/
serverSocket = new QTcpSocket;

///adding operation to buttons:
connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
connect(sendButton, SIGNAL(clicked()), this, SLOT(sendMessage()));
connect(serverSocket, SIGNAL(readyRead()), this, SLOT(receiveMessage()));
connect(connectButton, SIGNAL(clicked()), this, SLOT(connectToServer()));
connect(serverSocket, SIGNAL(disconnected()), this, SLOT(disconnectedFromServer()));
connect(serverSocket, SIGNAL(error(QAbstractSocket::SocketError)),
this, SLOT(displayError(QAbstractSocket::SocketError)));

/// adding widgets into the window:
QGridLayout *connectionLayout = new QGridLayout;
connectionLayout->addWidget(hostLabel, 0, 0);
connectionLayout->addWidget(hostAdress, 0, 1);
connectionLayout->addWidget(portLabel, 1, 0);
connectionLayout->addWidget(portLineEdit, 1, 1);

QHBoxLayout *messageLayout = new QHBoxLayout;
messageLayout->addWidget(messageText);
messageLayout->addWidget(sendButton);

QHBoxLayout *buttonLayout = new QHBoxLayout;
buttonLayout->addStretch(1);
buttonLayout->addWidget(connectButton);
buttonLayout->addStretch(1);
buttonLayout->addWidget(quitButton);
buttonLayout->addStretch(1);

QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addLayout(connectionLayout);
mainLayout->addWidget(chatText);
mainLayout->addLayout(messageLayout);
mainLayout->addLayout(buttonLayout);
setLayout(mainLayout);

portLineEdit->setFocus();

QNetworkConfigurationManager manager;
if (manager.capabilities() & QNetworkConfigurationManager::NetworkSessionRequired)
{
// Get saved network configuration
QSettings settings(QSettings::UserScope, QLatin1String("QtProject"));
settings.beginGroup(QLatin1String("QtNetwork"));
const QString id = settings.value(QLatin1String("DefaultNetworkConfiguration")).toString();
settings.endGroup();

// If the saved network configuration is not currently discovered use the system default
QNetworkConfiguration config = manager.configurationFromIdentifier(id);
if ((config.state() & QNetworkConfiguration::Discovered) !=
QNetworkConfiguration::Discovered)
{
config = manager.defaultConfiguration();
}

networkSession = new QNetworkSession(config, this);
connect(networkSession, SIGNAL(opened()), this, SLOT(sessionOpened()));

networkSession->open();
}
}

void Client::receiveMessage()
{
QDataStream in(serverSocket);
in.setVersion(QDataStream::Qt_5_2);

if (blockSize == 0)
{
if (serverSocket->bytesAvailable() < (int)sizeof(quint16))
return;

in >> blockSize;
}

if (serverSocket->bytesAvailable() < blockSize)
return;

blockSize = 0;
QString newMessage;
in >> newMessage;
chatText->textCursor().insertText("Server: " + newMessage + '\n');
}

void Client::sendMessage()
{
if (messageText->text().isEmpty())
return;
sendButton->setEnabled(false);

QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out << (quint16)messageText->text().length();
out << messageText->text();
serverSocket->write(block);

chatText->textCursor().insertText("You: " + messageText->text() + '\n');

messageText->clear();

sendButton->setEnabled(true);
}

void Client::sessionOpened()
{
// Save the used configuration
QNetworkConfiguration config = networkSession->configuration();
QString id;
if (config.type() == QNetworkConfiguration::UserChoice)
id = networkSession->sessionProperty(QLatin1String("UserChoiceConfiguration")).toString();
else
id = config.identifier();

QSettings settings(QSettings::UserScope, QLatin1String("QtProject"));
settings.beginGroup(QLatin1String("QtNetwork"));
settings.setValue(QLatin1String("DefaultNetworkConfiguration"), id);
settings.endGroup();


sendButton->setEnabled(true);
}

void Client::displayError(QAbstractSocket::SocketError socketError)
{
serverSocket->close();
switch (socketError)
{
case QAbstractSocket::RemoteHostClosedError:
break;
case QAbstractSocket::HostNotFoundError:
QMessageBox::information(this, tr("Chat client"),
tr("The host was not found. Please check the "
"host name and port settings."));
break;
case QAbstractSocket::ConnectionRefusedError:
QMessageBox::information(this, tr("Chat client"),
tr("The connection was refused by the peer. "
"Make sure the fortune server is running, "
"and check that the host name and port "
"settings are correct."));
break;
default:
QMessageBox::information(this, tr("Chat client"),
tr("The following error occurred: %1.")
.arg(serverSocket->errorString()));
}
hostAdress->setEnabled(true);
portLineEdit->setEnabled(true);
}

void Client::connectToServer()
{
serverSocket->connectToHost(hostAdress->text(), portLineEdit->text().toInt());

if (serverSocket->waitForConnected(5000))
{
chatText->clear();
chatText->textCursor().insertText("Server connected!\n");
hostAdress->setDisabled(true);
portLineEdit->setDisabled(true);
connectButton->setDisabled(true);
sendButton->setEnabled(true);
}
else
{
hostAdress->setEnabled(true);
portLineEdit->setEnabled(true);
}
}

void Client::disconnectedFromServer()
{
hostAdress->setDisabled(true);
portLineEdit->setDisabled(true);
connectButton->setEnabled(true);
sendButton->setDisabled(true);

QMessageBox::information(this, tr("Chat Client"),
tr("Server disconnected!"));
}
53 changes: 53 additions & 0 deletions configurationServer/client/client.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include <QDialog>
#include <QtNetwork>
#include <QTextEdit>
#include <QLineEdit>
#include <QComboBox>
#include <QPushButton>
#include <QLabel>
#include <QLayout>
#include <QMessageBox>

class Client : public QDialog
{
Q_OBJECT

public:
explicit Client(QWidget *parent = 0);

private slots:
void sessionOpened();
void displayError(QAbstractSocket::SocketError socketError);

/// sends message
void sendMessage();

/// receives message
void receiveMessage();

void connectToServer();
void disconnectedFromServer();

private:
/// buttons:
QPushButton *quitButton;
QPushButton *sendButton;
QPushButton *connectButton;

/// labels:
QLabel *hostLabel;
QLabel *portLabel;

/// combo box:
QLineEdit *hostAdress;

/// text widgets:
QLineEdit *portLineEdit;
QTextEdit *chatText;
QLineEdit *messageText;


QTcpSocket *serverSocket;
QNetworkSession *networkSession;
quint16 blockSize;
};
24 changes: 24 additions & 0 deletions configurationServer/client/client.ui
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<ui version="4.0">
<class>Client</class>
<widget class="QMainWindow" name="Client" >
<property name="geometry" >
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle" >
<string>Client</string>
</property>
<widget class="QMenuBar" name="menuBar" />
<widget class="QToolBar" name="mainToolBar" />
<widget class="QWidget" name="centralWidget" />
<widget class="QStatusBar" name="statusBar" />
</widget>
<layoutDefault spacing="6" margin="11" />
<pixmapfunction></pixmapfunction>
<resources/>
<connections/>
</ui>
11 changes: 11 additions & 0 deletions configurationServer/client/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "client.h"
#include <QApplication>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Client w;
w.show();

return a.exec();
}
Loading