1 /**************************************************************************
2 * Copyright (C) 2005-2020 by Oleksandr Shneyder *
3 * <o.shneyder@phoca-gmbh.de> *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program. If not, see <https://www.gnu.org/licenses/>. *
16 ***************************************************************************/
17
18 #include "contest.h"
19 #include "x2gologdebug.h"
20 #include <QTimer>
21 #include "httpbrokerclient.h"
22 #include <QPushButton>
23
ConTest(HttpBrokerClient * broker,QUrl url,QWidget * parent,Qt::WindowFlags f)24 ConTest::ConTest(HttpBrokerClient* broker, QUrl url, QWidget* parent, Qt::WindowFlags f): QDialog(parent, f)
25 {
26 socket=0l;
27 setupUi(this);
28 this->broker=broker;
29 brokerUrl=url;
30 timer=new QTimer(this);
31 connect(timer,SIGNAL(timeout()),this,SLOT(slotTimer()));
32 connect(broker,SIGNAL(connectionTime(int,int)),this, SLOT(slotConSpeed(int,int)));
33 start();
34 }
35
~ConTest()36 ConTest::~ConTest()
37 {
38
39 }
40
resetSocket()41 void ConTest::resetSocket()
42 {
43 if (socket)
44 {
45 socket->disconnectFromHost();
46 socket->close();
47 delete socket;
48 socket=0l;
49 }
50 }
51
52
reset()53 void ConTest::reset()
54 {
55 timer->stop();
56 lhttps->setText("");
57 lssh->setText("");
58 lspeed->setText("");
59 prhttps->setValue(0);
60 prspeed->setValue(0);
61 prssh->setValue(0);
62 httpsOk=false;
63 resetSocket();
64 buttonBox->button(QDialogButtonBox::Retry)->setEnabled(false);
65 }
66
start()67 void ConTest::start()
68 {
69 reset();
70 testConnection(HTTPS);
71 }
72
testConnection(tests test)73 void ConTest::testConnection(tests test)
74 {
75 time=0;
76 timer->start(100);
77 resetSocket();
78 currentTest=test;
79 if (test==SPEED)
80 {
81 if (!httpsOk)
82 {
83 slotConSpeed(1,0);
84 return;
85 }
86 broker->testConnection();
87 return;
88 }
89 socket=new QTcpSocket(this);
90 socket->connectToHost(brokerUrl.host(),test);
91 connect( socket,SIGNAL(connected()),this,SLOT(slotConnected()));
92 connect( socket, SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(slotError(QAbstractSocket::SocketError)));
93 }
94
95
slotConnected()96 void ConTest::slotConnected()
97 {
98 x2goDebug<<"connected\n";
99 timer->stop();
100 QPalette pal=lhttps->palette();
101 pal.setColor(QPalette::WindowText, Qt::green);
102 switch (currentTest)
103 {
104 case HTTPS:
105 prhttps->setValue(100);
106 lhttps->setText(tr("OK"));
107 lhttps->setPalette(pal);
108 httpsOk=true;
109 testConnection(SSH);
110 break;
111 case SSH:
112 prssh->setValue(100);
113 lssh->setText(tr("OK"));
114 lssh->setPalette(pal);
115 testConnection(SPEED);
116 break;
117 default:
118 break;
119 }
120 }
121
slotConSpeed(int msecElapsed,int bytesRecived)122 void ConTest::slotConSpeed(int msecElapsed, int bytesRecived)
123 {
124 timer->stop();
125 prspeed->setValue(100);
126 double sec=msecElapsed/1000.;
127 int KB=bytesRecived/1024;
128 int Kbsec=(int)(KB/sec)*8;
129
130 QPalette pal=lspeed->palette();
131 pal.setColor(QPalette::WindowText, Qt::green);
132
133 if (Kbsec<1000)
134 pal.setColor(QPalette::WindowText, Qt::yellow);
135 if (Kbsec<512)
136 pal.setColor(QPalette::WindowText, Qt::red);
137
138 lspeed->setPalette(pal);
139 lspeed->setText(QString::number(Kbsec)+" Kb/s");
140 buttonBox->button(QDialogButtonBox::Retry)->setEnabled(true);
141 }
142
143
slotError(QAbstractSocket::SocketError socketError)144 void ConTest::slotError(QAbstractSocket::SocketError socketError)
145 {
146 QString error;
147 if (socketError==QAbstractSocket::SocketTimeoutError)
148 error=tr("Socket operation timed out.");
149 else
150 error=socket->errorString();
151
152 x2goDebug<<"Error: "<<error<<endl;
153 timer->stop();
154 QPalette pal=lhttps->palette();
155 pal.setColor(QPalette::WindowText, Qt::red);
156 switch (currentTest)
157 {
158 case HTTPS:
159 prhttps->setValue(100);
160 lhttps->setText(tr("Failed: ")+error);
161 lhttps->setPalette(pal);
162 testConnection(SSH);
163 break;
164 case SSH:
165 prssh->setValue(100);
166 lssh->setText(tr("Failed: ")+error);
167 lssh->setPalette(pal);
168 testConnection(SPEED);
169 break;
170 default:
171 break;
172 }
173 }
174
175
slotTimer()176 void ConTest::slotTimer()
177 {
178 time++;
179 if (time>150)
180 {
181 if (currentTest==SSH || currentTest==HTTPS)
182 {
183 socket->close();
184 slotError(QAbstractSocket::SocketTimeoutError);
185 }
186 }
187 QProgressBar* bar=0l;
188 switch (currentTest)
189 {
190 case SSH:
191 bar=prssh;
192 break;
193 case HTTPS:
194 bar=prhttps;
195 break;
196 case SPEED:
197 bar=prspeed;
198 break;
199 }
200 if (bar->value()==100)
201 bar->setValue(0);
202 else
203 bar->setValue(bar->value()+10);
204
205 }
206