author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Fri, 12 Mar 2010 15:46:37 +0200 | |
branch | RCL_3 |
changeset 5 | d3bac044e0f0 |
parent 4 | 3b1da2848fc7 |
child 7 | 3f74d0d4af4c |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the examples of the Qt Toolkit. |
|
8 |
** |
|
9 |
** $QT_BEGIN_LICENSE:LGPL$ |
|
10 |
** No Commercial Usage |
|
11 |
** This file contains pre-release code and may not be distributed. |
|
12 |
** You may use this file in accordance with the terms and conditions |
|
13 |
** contained in the Technology Preview License Agreement accompanying |
|
14 |
** this package. |
|
15 |
** |
|
16 |
** GNU Lesser General Public License Usage |
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 |
** General Public License version 2.1 as published by the Free Software |
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 |
** packaging of this file. Please review the following information to |
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 |
** |
|
24 |
** In addition, as a special exception, Nokia gives you certain additional |
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 |
** |
|
28 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
33 |
** |
|
34 |
** |
|
35 |
** |
|
36 |
** |
|
37 |
** |
|
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) |
|
48 |
static const int TotalBytes = 50 * 1024 * 1024; |
|
49 |
#else |
|
50 |
static const int TotalBytes = 5 * 1024 * 1024; |
|
51 |
#endif |
|
52 |
static const int PayloadSize = 65536; |
|
53 |
||
54 |
Dialog::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 |
||
92 |
void 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 |
||
119 |
void 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 |
||
131 |
void Dialog::startTransfer() |
|
132 |
{ |
|
133 |
bytesToWrite = TotalBytes - (int)tcpClient.write(QByteArray(PayloadSize, '@')); |
|
134 |
clientStatusLabel->setText(tr("Connected")); |
|
135 |
} |
|
136 |
||
137 |
void 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 |
||
156 |
void 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 |
||
168 |
void 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 |
} |