File: Dialog.pm

package info (click to toggle)
qt4-perl 4.8.4-1.2
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 8,636 kB
  • ctags: 8,100
  • sloc: perl: 42,963; cpp: 28,039; makefile: 160; xml: 98; sh: 4
file content (221 lines) | stat: -rw-r--r-- 6,314 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package Dialog;

use strict;
use warnings;
use QtCore4;
use QtGui4;
use QtNetwork4;
use QtCore4::isa qw( Qt::Dialog );
use QtCore4::slots
    start => [],
    acceptConnection => [],
    startTransfer => [],
    updateServerProgress => [],
    updateClientProgress => ['qint64'],
    displayError => ['QAbstractSocket::SocketError'];
use List::Util qw( min );

sub clientProgressBar() {
    return this->{clientProgressBar};
}

sub serverProgressBar() {
    return this->{serverProgressBar};
}

sub clientStatusLabel() {
    return this->{clientStatusLabel};
}

sub serverStatusLabel() {
    return this->{serverStatusLabel};
}

sub startButton() {
    return this->{startButton};
}

sub quitButton() {
    return this->{quitButton};
}

sub buttonBox() {
    return this->{buttonBox};
}

sub tcpServer() {
    return this->{tcpServer};
}

sub tcpClient() {
    return this->{tcpClient};
}

sub tcpServerConnection() {
    return this->{tcpServerConnection};
}

sub bytesToWrite() {
    return this->{bytesToWrite};
}

sub bytesWritten() {
    return this->{bytesWritten};
}

sub bytesReceived() {
    return this->{bytesReceived};
}
    #Qt::TcpServer tcpServer;
    #Qt::TcpSocket tcpClient;
    #int bytesToWrite;
    #int bytesWritten;
    #int bytesReceived;

my $TotalBytes = 50 * 1024 * 1024;
my $PayloadSize = 65536;

sub NEW
{
    my ($class, $parent) = @_;
    $class->SUPER::NEW($parent);
    this->{tcpServer} = Qt::TcpServer(undef);
    this->{tcpClient} = Qt::TcpSocket(undef);
    this->{clientProgressBar} = Qt::ProgressBar();
    this->{clientStatusLabel} = Qt::Label(this->tr('Client ready'));
    this->{serverProgressBar} = Qt::ProgressBar();
    this->{serverStatusLabel} = Qt::Label(this->tr('Server ready'));

    this->{startButton} = Qt::PushButton(this->tr('&Start'));
    this->{quitButton} = Qt::PushButton(this->tr('&Quit'));

    this->{buttonBox} = Qt::DialogButtonBox();
    buttonBox->addButton(startButton, Qt::DialogButtonBox::ActionRole());
    buttonBox->addButton(quitButton, Qt::DialogButtonBox::RejectRole());

    this->connect(startButton, SIGNAL 'clicked()', this, SLOT 'start()');
    this->connect(quitButton, SIGNAL 'clicked()', this, SLOT 'close()');
    this->connect(tcpServer, SIGNAL 'newConnection()',
            this, SLOT 'acceptConnection()');
    this->connect(tcpClient, SIGNAL 'connected()', this, SLOT 'startTransfer()');
    this->connect(tcpClient, SIGNAL 'bytesWritten(qint64)',
            this, SLOT 'updateClientProgress(qint64)');
    this->connect(tcpClient, SIGNAL 'error(QAbstractSocket::SocketError)',
            this, SLOT 'displayError(QAbstractSocket::SocketError)');

    my $mainLayout = Qt::VBoxLayout();
    $mainLayout->addWidget(clientProgressBar);
    $mainLayout->addWidget(clientStatusLabel);
    $mainLayout->addWidget(serverProgressBar);
    $mainLayout->addWidget(serverStatusLabel);
    $mainLayout->addStretch(1);
    $mainLayout->addSpacing(10);
    $mainLayout->addWidget(buttonBox);
    this->setLayout($mainLayout);

    setWindowTitle(this->tr('Loopback'));
}

sub start
{
    startButton->setEnabled(0);

#ifndef QT_NO_CURSOR
    Qt::Application::setOverrideCursor(Qt::Cursor(Qt::WaitCursor()));
#endif

    this->{bytesWritten} = 0;
    this->{bytesReceived} = 0;

    while (!tcpServer->isListening() && !tcpServer->listen()) {
        my $ret = Qt::MessageBox::critical(this,
                                        this->tr('Loopback'),
                               sprintf( this->tr('Unable to start the test: %s.'),
					tcpServer->errorString() ),
                                        Qt::MessageBox::Retry()
					| Qt::MessageBox::Cancel());
        if ($ret == Qt::MessageBox::Cancel()) {
            return;
        }
    }

    serverStatusLabel->setText(this->tr('Listening'));
    clientStatusLabel->setText(this->tr('Connecting'));
    tcpClient->connectToHost(Qt::HostAddress(Qt::HostAddress::LocalHost()), tcpServer->serverPort());
}

sub acceptConnection
{
    this->{tcpServerConnection} = tcpServer->nextPendingConnection();
    this->connect(tcpServerConnection, SIGNAL 'readyRead()',
            this, SLOT 'updateServerProgress()');
    this->connect(tcpServerConnection, SIGNAL 'error(QAbstractSocket::SocketError)',
            this, SLOT 'displayError(QAbstractSocket::SocketError)');

    serverStatusLabel->setText(this->tr('Accepted connection'));
    tcpServer->close();
}

sub startTransfer
{
    this->{bytesToWrite} = $TotalBytes - sprintf '%d', tcpClient->write(Qt::ByteArray($PayloadSize, '@'));
    clientStatusLabel->setText(this->tr('Connected'));
}

sub updateServerProgress
{
    this->{bytesReceived} += sprintf '%d', tcpServerConnection->bytesAvailable();
    tcpServerConnection->readAll();

    serverProgressBar->setMaximum($TotalBytes);
    serverProgressBar->setValue(bytesReceived);
    serverStatusLabel->setText(sprintf this->tr('Received %dMB'),
                               (bytesReceived / (1024 * 1024)));

    if (bytesReceived == $TotalBytes) {
        tcpServerConnection->close();
        startButton->setEnabled(1);
#ifndef QT_NO_CURSOR
        Qt::Application::restoreOverrideCursor();
#endif
    }
}

sub updateClientProgress
{
    my ($numBytes) = @_;
    this->{bytesWritten} += sprintf '%d', $numBytes;
    if (bytesToWrite > 0) {
        this->{bytesToWrite} -= sprintf '%d', tcpClient->write(Qt::ByteArray(min(bytesToWrite, $PayloadSize), '@'));
    }

    clientProgressBar->setMaximum($TotalBytes);
    clientProgressBar->setValue(bytesWritten);
    clientStatusLabel->setText(sprintf this->tr('Sent %dMB'),
                               bytesWritten / (1024 * 1024));
}

sub displayError
{
    my ($socketError) = @_;
    if ($socketError == Qt::TcpSocket::RemoteHostClosedError()) {
        return;
    }

    Qt::MessageBox::information(this, this->tr('Network error'),
                     sprintf this->tr('The following error occurred: %s.'),
                             tcpClient->errorString());

    tcpClient->close();
    tcpServer->close();
    clientProgressBar->reset();
    serverProgressBar->reset();
    clientStatusLabel->setText(this->tr('Client ready'));
    serverStatusLabel->setText(this->tr('Server ready'));
    startButton->setEnabled(1);
#ifndef QT_NO_CURSOR
    Qt::Application::restoreOverrideCursor();
#endif
}

1;