1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation ([email protected])
|
---|
6 | **
|
---|
7 | ** This file is part of the examples of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:BSD$
|
---|
10 | ** You may use this file under the terms of the BSD license as follows:
|
---|
11 | **
|
---|
12 | ** "Redistribution and use in source and binary forms, with or without
|
---|
13 | ** modification, are permitted provided that the following conditions are
|
---|
14 | ** met:
|
---|
15 | ** * Redistributions of source code must retain the above copyright
|
---|
16 | ** notice, this list of conditions and the following disclaimer.
|
---|
17 | ** * Redistributions in binary form must reproduce the above copyright
|
---|
18 | ** notice, this list of conditions and the following disclaimer in
|
---|
19 | ** the documentation and/or other materials provided with the
|
---|
20 | ** distribution.
|
---|
21 | ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
|
---|
22 | ** the names of its contributors may be used to endorse or promote
|
---|
23 | ** products derived from this software without specific prior written
|
---|
24 | ** permission.
|
---|
25 | **
|
---|
26 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
---|
27 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
---|
28 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
---|
29 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
---|
30 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
---|
31 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
---|
32 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
33 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
34 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
35 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
---|
36 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
---|
37 | ** $QT_END_LICENSE$
|
---|
38 | **
|
---|
39 | ****************************************************************************/
|
---|
40 |
|
---|
41 | #include <QtGui>
|
---|
42 | #include <QtNetwork>
|
---|
43 |
|
---|
44 | #include "dialog.h"
|
---|
45 |
|
---|
46 | #if !defined(Q_OS_WINCE) && !defined(Q_OS_SYMBIAN)
|
---|
47 | static const int TotalBytes = 50 * 1024 * 1024;
|
---|
48 | #else
|
---|
49 | static const int TotalBytes = 5 * 1024 * 1024;
|
---|
50 | #endif
|
---|
51 | static const int PayloadSize = 64 * 1024; // 64 KB
|
---|
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 | {
|
---|
132 | // called when the TCP client connected to the loopback server
|
---|
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 | // callen when the TCP client has written some bytes
|
---|
159 | bytesWritten += (int)numBytes;
|
---|
160 |
|
---|
161 | // only write more if not finished and when the Qt write buffer is below a certain size.
|
---|
162 | if (bytesToWrite > 0 && tcpClient.bytesToWrite() <= 4*PayloadSize)
|
---|
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 | }
|
---|