author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Tue, 06 Jul 2010 15:10:48 +0300 | |
changeset 30 | 5dc02b23752f |
parent 18 | 2f34d5167611 |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
18
2f34d5167611
Revision: 201011
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 |
** |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
9 |
** $QT_BEGIN_LICENSE:BSD$ |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
10 |
** You may use this file under the terms of the BSD license as follows: |
0 | 11 |
** |
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
12 |
** "Redistribution and use in source and binary forms, with or without |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
13 |
** modification, are permitted provided that the following conditions are |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
14 |
** met: |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
15 |
** * Redistributions of source code must retain the above copyright |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
16 |
** notice, this list of conditions and the following disclaimer. |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
17 |
** * Redistributions in binary form must reproduce the above copyright |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
18 |
** notice, this list of conditions and the following disclaimer in |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
19 |
** the documentation and/or other materials provided with the |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
20 |
** distribution. |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
21 |
** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
22 |
** the names of its contributors may be used to endorse or promote |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
23 |
** products derived from this software without specific prior written |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
24 |
** permission. |
0 | 25 |
** |
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
26 |
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
27 |
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
28 |
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
29 |
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
30 |
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
31 |
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
32 |
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
33 |
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
34 |
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
35 |
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
36 |
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
0 | 37 |
** $QT_END_LICENSE$ |
38 |
** |
|
39 |
****************************************************************************/ |
|
40 |
||
41 |
#include <QtGui> |
|
42 |
#include <QtNetwork> |
|
43 |
||
44 |
#include "dialog.h" |
|
45 |
||
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
46 |
#if !defined(Q_OS_WINCE) && !defined(Q_OS_SYMBIAN) |
0 | 47 |
static const int TotalBytes = 50 * 1024 * 1024; |
48 |
#else |
|
49 |
static const int TotalBytes = 5 * 1024 * 1024; |
|
50 |
#endif |
|
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
51 |
static const int PayloadSize = 64 * 1024; // 64 KB |
0 | 52 |
|
53 |
Dialog::Dialog(QWidget *parent) |
|
54 |
: QDialog(parent) |
|
55 |
{ |
|
56 |
clientProgressBar = new QProgressBar; |
|
57 |
clientStatusLabel = new QLabel(tr("Client ready")); |
|
58 |
serverProgressBar = new QProgressBar; |
|
59 |
serverStatusLabel = new QLabel(tr("Server ready")); |
|
60 |
||
61 |
startButton = new QPushButton(tr("&Start")); |
|
62 |
quitButton = new QPushButton(tr("&Quit")); |
|
63 |
||
64 |
buttonBox = new QDialogButtonBox; |
|
65 |
buttonBox->addButton(startButton, QDialogButtonBox::ActionRole); |
|
66 |
buttonBox->addButton(quitButton, QDialogButtonBox::RejectRole); |
|
67 |
||
68 |
connect(startButton, SIGNAL(clicked()), this, SLOT(start())); |
|
69 |
connect(quitButton, SIGNAL(clicked()), this, SLOT(close())); |
|
70 |
connect(&tcpServer, SIGNAL(newConnection()), |
|
71 |
this, SLOT(acceptConnection())); |
|
72 |
connect(&tcpClient, SIGNAL(connected()), this, SLOT(startTransfer())); |
|
73 |
connect(&tcpClient, SIGNAL(bytesWritten(qint64)), |
|
74 |
this, SLOT(updateClientProgress(qint64))); |
|
75 |
connect(&tcpClient, SIGNAL(error(QAbstractSocket::SocketError)), |
|
76 |
this, SLOT(displayError(QAbstractSocket::SocketError))); |
|
77 |
||
78 |
QVBoxLayout *mainLayout = new QVBoxLayout; |
|
79 |
mainLayout->addWidget(clientProgressBar); |
|
80 |
mainLayout->addWidget(clientStatusLabel); |
|
81 |
mainLayout->addWidget(serverProgressBar); |
|
82 |
mainLayout->addWidget(serverStatusLabel); |
|
83 |
mainLayout->addStretch(1); |
|
84 |
mainLayout->addSpacing(10); |
|
85 |
mainLayout->addWidget(buttonBox); |
|
86 |
setLayout(mainLayout); |
|
87 |
||
88 |
setWindowTitle(tr("Loopback")); |
|
89 |
} |
|
90 |
||
91 |
void Dialog::start() |
|
92 |
{ |
|
93 |
startButton->setEnabled(false); |
|
94 |
||
95 |
#ifndef QT_NO_CURSOR |
|
96 |
QApplication::setOverrideCursor(Qt::WaitCursor); |
|
97 |
#endif |
|
98 |
||
99 |
bytesWritten = 0; |
|
100 |
bytesReceived = 0; |
|
101 |
||
102 |
while (!tcpServer.isListening() && !tcpServer.listen()) { |
|
103 |
QMessageBox::StandardButton ret = QMessageBox::critical(this, |
|
104 |
tr("Loopback"), |
|
105 |
tr("Unable to start the test: %1.") |
|
106 |
.arg(tcpServer.errorString()), |
|
107 |
QMessageBox::Retry |
|
108 |
| QMessageBox::Cancel); |
|
109 |
if (ret == QMessageBox::Cancel) |
|
110 |
return; |
|
111 |
} |
|
112 |
||
113 |
serverStatusLabel->setText(tr("Listening")); |
|
114 |
clientStatusLabel->setText(tr("Connecting")); |
|
115 |
tcpClient.connectToHost(QHostAddress::LocalHost, tcpServer.serverPort()); |
|
116 |
} |
|
117 |
||
118 |
void Dialog::acceptConnection() |
|
119 |
{ |
|
120 |
tcpServerConnection = tcpServer.nextPendingConnection(); |
|
121 |
connect(tcpServerConnection, SIGNAL(readyRead()), |
|
122 |
this, SLOT(updateServerProgress())); |
|
123 |
connect(tcpServerConnection, SIGNAL(error(QAbstractSocket::SocketError)), |
|
124 |
this, SLOT(displayError(QAbstractSocket::SocketError))); |
|
125 |
||
126 |
serverStatusLabel->setText(tr("Accepted connection")); |
|
127 |
tcpServer.close(); |
|
128 |
} |
|
129 |
||
130 |
void Dialog::startTransfer() |
|
131 |
{ |
|
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
132 |
// called when the TCP client connected to the loopback server |
0 | 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 |
{ |
|
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
158 |
// callen when the TCP client has written some bytes |
0 | 159 |
bytesWritten += (int)numBytes; |
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
160 |
|
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
161 |
// only write more if not finished and when the Qt write buffer is below a certain size. |
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
162 |
if (bytesToWrite > 0 && tcpClient.bytesToWrite() <= 4*PayloadSize) |
0 | 163 |
bytesToWrite -= (int)tcpClient.write(QByteArray(qMin(bytesToWrite, PayloadSize), '@')); |
164 |
||
165 |
clientProgressBar->setMaximum(TotalBytes); |
|
166 |
clientProgressBar->setValue(bytesWritten); |
|
167 |
clientStatusLabel->setText(tr("Sent %1MB") |
|
168 |
.arg(bytesWritten / (1024 * 1024))); |
|
169 |
} |
|
170 |
||
171 |
void Dialog::displayError(QAbstractSocket::SocketError socketError) |
|
172 |
{ |
|
173 |
if (socketError == QTcpSocket::RemoteHostClosedError) |
|
174 |
return; |
|
175 |
||
176 |
QMessageBox::information(this, tr("Network error"), |
|
177 |
tr("The following error occurred: %1.") |
|
178 |
.arg(tcpClient.errorString())); |
|
179 |
||
180 |
tcpClient.close(); |
|
181 |
tcpServer.close(); |
|
182 |
clientProgressBar->reset(); |
|
183 |
serverProgressBar->reset(); |
|
184 |
clientStatusLabel->setText(tr("Client ready")); |
|
185 |
serverStatusLabel->setText(tr("Server ready")); |
|
186 |
startButton->setEnabled(true); |
|
187 |
#ifndef QT_NO_CURSOR |
|
188 |
QApplication::restoreOverrideCursor(); |
|
189 |
#endif |
|
190 |
} |