File: contest.cpp

package info (click to toggle)
x2goclient 3.99.2.1-5
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 6,596 kB
  • sloc: cpp: 21,625; perl: 512; makefile: 129; sh: 17
file content (207 lines) | stat: -rw-r--r-- 4,922 bytes parent folder | download
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
/*
    <one line to give the program's name and a brief idea of what it does.>
    Copyright (C) 2005-2012 Oleksandr Shneyder

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/

#include "contest.h"
#include "x2gologdebug.h"
#include <QTimer>
#include "httpbrokerclient.h"
#include <QPushButton>

ConTest::ConTest(HttpBrokerClient* broker, QUrl url, QWidget* parent, Qt::WindowFlags f): QDialog(parent, f)
{
    socket=0l;
    setupUi(this);
    this->broker=broker;
    brokerUrl=url;
    timer=new QTimer(this);
    connect(timer,SIGNAL(timeout()),this,SLOT(slotTimer()));
    connect(broker,SIGNAL(connectionTime(int,int)),this, SLOT(slotConSpeed(int,int)));
    start();
}

ConTest::~ConTest()
{

}

void ConTest::resetSocket()
{
    if (socket)
    {
        socket->disconnectFromHost();
        socket->close();
        delete socket;
        socket=0l;
    }
}


void ConTest::reset()
{
    timer->stop();
    lhttps->setText("");
    lssh->setText("");
    lspeed->setText("");
    prhttps->setValue(0);
    prspeed->setValue(0);
    prssh->setValue(0);
    httpsOk=false;
    resetSocket();
    buttonBox->button(QDialogButtonBox::Retry)->setEnabled(false);
}

void ConTest::start()
{
    reset();
    testConnection(HTTPS);
}

void ConTest::testConnection(tests test)
{
    time=0;
    timer->start(100);
    resetSocket();
    currentTest=test;
    if (test==SPEED)
    {
        if (!httpsOk)
        {
            slotConSpeed(1,0);
            return;
        }
        broker->testConnection();
        return;
    }
    socket=new QTcpSocket(this);
    socket->connectToHost(brokerUrl.host(),test);
    connect( socket,SIGNAL(connected()),this,SLOT(slotConnected()));
    connect( socket, SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(slotError(QAbstractSocket::SocketError)));
}


void ConTest::slotConnected()
{
    x2goDebug<<"connected\n";
    timer->stop();
    QPalette pal=lhttps->palette();
    pal.setColor(QPalette::WindowText, Qt::green);
    switch (currentTest)
    {
    case HTTPS:
        prhttps->setValue(100);
        lhttps->setText(tr("OK"));
        lhttps->setPalette(pal);
        httpsOk=true;
        testConnection(SSH);
        break;
    case SSH:
        prssh->setValue(100);
        lssh->setText(tr("OK"));
        lssh->setPalette(pal);
        testConnection(SPEED);
        break;
    default:
        break;
    }
}

void ConTest::slotConSpeed(int msecElapsed, int bytesRecived)
{
    timer->stop();
    prspeed->setValue(100);
    double sec=msecElapsed/1000.;
    int KB=bytesRecived/1024;
    int Kbsec=(int)(KB/sec)*8;

    QPalette pal=lspeed->palette();
    pal.setColor(QPalette::WindowText, Qt::green);

    if (Kbsec<1000)
        pal.setColor(QPalette::WindowText, Qt::yellow);
    if (Kbsec<512)
        pal.setColor(QPalette::WindowText, Qt::red);

    lspeed->setPalette(pal);
    lspeed->setText(QString::number(Kbsec)+" Kb/s");
    buttonBox->button(QDialogButtonBox::Retry)->setEnabled(true);
}


void ConTest::slotError(QAbstractSocket::SocketError socketError)
{
    QString error;
    if (socketError==QAbstractSocket::SocketTimeoutError)
        error=tr("Socket operation timed out");
    else
        error=socket->errorString();

    x2goDebug<<"Error: "<<error<<endl;
    timer->stop();
    QPalette pal=lhttps->palette();
    pal.setColor(QPalette::WindowText, Qt::red);
    switch (currentTest)
    {
    case HTTPS:
        prhttps->setValue(100);
        lhttps->setText(tr("Failed: ")+error);
        lhttps->setPalette(pal);
        testConnection(SSH);
        break;
    case SSH:
        prssh->setValue(100);
        lssh->setText(tr("Failed: ")+error);
        lssh->setPalette(pal);
        testConnection(SPEED);
        break;
    default:
        break;
    }
}


void ConTest::slotTimer()
{
    time++;
    if (time>150)
    {
        if (currentTest==SSH || currentTest==HTTPS)
        {
            socket->close();
            slotError(QAbstractSocket::SocketTimeoutError);
        }
    }
    QProgressBar* bar=0l;
    switch (currentTest)
    {
    case SSH:
        bar=prssh;
        break;
    case HTTPS:
        bar=prhttps;
        break;
    case SPEED:
        bar=prspeed;
        break;
    }
    if (bar->value()==100)
        bar->setValue(0);
    else
        bar->setValue(bar->value()+10);

}