source: trunk/examples/network/loopback/dialog.cpp@ 92

Last change on this file since 92 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 6.5 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the examples of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include <QtGui>
43#include <QtNetwork>
44
45#include "dialog.h"
46
47#if !defined(Q_OS_WINCE)
48static const int TotalBytes = 50 * 1024 * 1024;
49#else
50static const int TotalBytes = 5 * 1024 * 1024;
51#endif
52static const int PayloadSize = 65536;
53
54Dialog::Dialog(QWidget *parent)
55 : QDialog(parent)
56{
57 clientProgressBar = new QProgressBar;
58 clientStatusLabel = new QLabel(tr("Client ready"));
59 serverProgressBar = new QProgressBar;
60 serverStatusLabel = new QLabel(tr("Server ready"));
61
62 startButton = new QPushButton(tr("&Start"));
63 quitButton = new QPushButton(tr("&Quit"));
64
65 buttonBox = new QDialogButtonBox;
66 buttonBox->addButton(startButton, QDialogButtonBox::ActionRole);
67 buttonBox->addButton(quitButton, QDialogButtonBox::RejectRole);
68
69 connect(startButton, SIGNAL(clicked()), this, SLOT(start()));
70 connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
71 connect(&tcpServer, SIGNAL(newConnection()),
72 this, SLOT(acceptConnection()));
73 connect(&tcpClient, SIGNAL(connected()), this, SLOT(startTransfer()));
74 connect(&tcpClient, SIGNAL(bytesWritten(qint64)),
75 this, SLOT(updateClientProgress(qint64)));
76 connect(&tcpClient, SIGNAL(error(QAbstractSocket::SocketError)),
77 this, SLOT(displayError(QAbstractSocket::SocketError)));
78
79 QVBoxLayout *mainLayout = new QVBoxLayout;
80 mainLayout->addWidget(clientProgressBar);
81 mainLayout->addWidget(clientStatusLabel);
82 mainLayout->addWidget(serverProgressBar);
83 mainLayout->addWidget(serverStatusLabel);
84 mainLayout->addStretch(1);
85 mainLayout->addSpacing(10);
86 mainLayout->addWidget(buttonBox);
87 setLayout(mainLayout);
88
89 setWindowTitle(tr("Loopback"));
90}
91
92void Dialog::start()
93{
94 startButton->setEnabled(false);
95
96#ifndef QT_NO_CURSOR
97 QApplication::setOverrideCursor(Qt::WaitCursor);
98#endif
99
100 bytesWritten = 0;
101 bytesReceived = 0;
102
103 while (!tcpServer.isListening() && !tcpServer.listen()) {
104 QMessageBox::StandardButton ret = QMessageBox::critical(this,
105 tr("Loopback"),
106 tr("Unable to start the test: %1.")
107 .arg(tcpServer.errorString()),
108 QMessageBox::Retry
109 | QMessageBox::Cancel);
110 if (ret == QMessageBox::Cancel)
111 return;
112 }
113
114 serverStatusLabel->setText(tr("Listening"));
115 clientStatusLabel->setText(tr("Connecting"));
116 tcpClient.connectToHost(QHostAddress::LocalHost, tcpServer.serverPort());
117}
118
119void Dialog::acceptConnection()
120{
121 tcpServerConnection = tcpServer.nextPendingConnection();
122 connect(tcpServerConnection, SIGNAL(readyRead()),
123 this, SLOT(updateServerProgress()));
124 connect(tcpServerConnection, SIGNAL(error(QAbstractSocket::SocketError)),
125 this, SLOT(displayError(QAbstractSocket::SocketError)));
126
127 serverStatusLabel->setText(tr("Accepted connection"));
128 tcpServer.close();
129}
130
131void Dialog::startTransfer()
132{
133 bytesToWrite = TotalBytes - (int)tcpClient.write(QByteArray(PayloadSize, '@'));
134 clientStatusLabel->setText(tr("Connected"));
135}
136
137void Dialog::updateServerProgress()
138{
139 bytesReceived += (int)tcpServerConnection->bytesAvailable();
140 tcpServerConnection->readAll();
141
142 serverProgressBar->setMaximum(TotalBytes);
143 serverProgressBar->setValue(bytesReceived);
144 serverStatusLabel->setText(tr("Received %1MB")
145 .arg(bytesReceived / (1024 * 1024)));
146
147 if (bytesReceived == TotalBytes) {
148 tcpServerConnection->close();
149 startButton->setEnabled(true);
150#ifndef QT_NO_CURSOR
151 QApplication::restoreOverrideCursor();
152#endif
153 }
154}
155
156void Dialog::updateClientProgress(qint64 numBytes)
157{
158 bytesWritten += (int)numBytes;
159 if (bytesToWrite > 0)
160 bytesToWrite -= (int)tcpClient.write(QByteArray(qMin(bytesToWrite, PayloadSize), '@'));
161
162 clientProgressBar->setMaximum(TotalBytes);
163 clientProgressBar->setValue(bytesWritten);
164 clientStatusLabel->setText(tr("Sent %1MB")
165 .arg(bytesWritten / (1024 * 1024)));
166}
167
168void Dialog::displayError(QAbstractSocket::SocketError socketError)
169{
170 if (socketError == QTcpSocket::RemoteHostClosedError)
171 return;
172
173 QMessageBox::information(this, tr("Network error"),
174 tr("The following error occurred: %1.")
175 .arg(tcpClient.errorString()));
176
177 tcpClient.close();
178 tcpServer.close();
179 clientProgressBar->reset();
180 serverProgressBar->reset();
181 clientStatusLabel->setText(tr("Client ready"));
182 serverStatusLabel->setText(tr("Server ready"));
183 startButton->setEnabled(true);
184#ifndef QT_NO_CURSOR
185 QApplication::restoreOverrideCursor();
186#endif
187}
Note: See TracBrowser for help on using the repository browser.