| 1 | #include"jltest.h"
|
|---|
| 2 |
|
|---|
| 3 | #include<qpushbutton.h>
|
|---|
| 4 | #include<qlayout.h>
|
|---|
| 5 | #include<qcheckbox.h>
|
|---|
| 6 | #include<qmessagebox.h>
|
|---|
| 7 | #include<qgroupbox.h>
|
|---|
| 8 | #include<qprogressbar.h>
|
|---|
| 9 | #include<qradiobutton.h>
|
|---|
| 10 | #include<qbuttongroup.h>
|
|---|
| 11 | #include<qdom.h>
|
|---|
| 12 | #include<qtimer.h>
|
|---|
| 13 | #include<stdlib.h>
|
|---|
| 14 | #include"im.h"
|
|---|
| 15 | #include"xmpp_jidlink.h"
|
|---|
| 16 | #include"xmpp_xmlcommon.h"
|
|---|
| 17 | #include"psiaccount.h"
|
|---|
| 18 | #include"busywidget.h"
|
|---|
| 19 |
|
|---|
| 20 | #define TOTAL_SIZE 32768
|
|---|
| 21 | #define PACKET_SIZE 8192
|
|---|
| 22 | #define PACKET_DELAY 2000
|
|---|
| 23 |
|
|---|
| 24 | class JLTestDlg::Private
|
|---|
| 25 | {
|
|---|
| 26 | public:
|
|---|
| 27 | PsiAccount *pa;
|
|---|
| 28 | Jid jid;
|
|---|
| 29 | BusyWidget *busy;
|
|---|
| 30 | QRadioButton *rb_dtcp, *rb_ibb;
|
|---|
| 31 | QPushButton *pb_test;
|
|---|
| 32 | QProgressBar *pr_status;
|
|---|
| 33 | QButtonGroup *bg_type;
|
|---|
| 34 | JidLink *jl;
|
|---|
| 35 | QLabel *lb_status;
|
|---|
| 36 | int mode;
|
|---|
| 37 |
|
|---|
| 38 | int numBytes;
|
|---|
| 39 | };
|
|---|
| 40 |
|
|---|
| 41 | JLTestDlg::JLTestDlg(const Jid &j, PsiAccount *pa)
|
|---|
| 42 | :QDialog(0, 0, false, WDestructiveClose)
|
|---|
| 43 | {
|
|---|
| 44 | d = new Private;
|
|---|
| 45 | d->pa = pa;
|
|---|
| 46 | d->jid = j;
|
|---|
| 47 | d->mode = ModeConnect;
|
|---|
| 48 | d->jl = 0;
|
|---|
| 49 | setCaption("Link Test");
|
|---|
| 50 | init();
|
|---|
| 51 | d->pb_test->setText(tr("Connect"));
|
|---|
| 52 |
|
|---|
| 53 | d->rb_dtcp->setChecked(true);
|
|---|
| 54 | }
|
|---|
| 55 |
|
|---|
| 56 | JLTestDlg::JLTestDlg(const Jid &j, JidLink *jl, PsiAccount *pa)
|
|---|
| 57 | :QDialog(0, 0, false, WDestructiveClose)
|
|---|
| 58 | {
|
|---|
| 59 | d = new Private;
|
|---|
| 60 | d->pa = pa;
|
|---|
| 61 | d->jid = j;
|
|---|
| 62 | d->mode = ModeAccept;
|
|---|
| 63 | d->jl = jl;
|
|---|
| 64 | setCaption("Link Test");
|
|---|
| 65 | init();
|
|---|
| 66 | d->pb_test->setText(tr("Accept"));
|
|---|
| 67 | if(jl->type() == JidLink::DTCP)
|
|---|
| 68 | d->rb_dtcp->setChecked(true);
|
|---|
| 69 | else
|
|---|
| 70 | d->rb_ibb->setChecked(true);
|
|---|
| 71 | d->bg_type->setEnabled(false);
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | void JLTestDlg::init()
|
|---|
| 75 | {
|
|---|
| 76 | QVBoxLayout *vb = new QVBoxLayout(this, 8);
|
|---|
| 77 |
|
|---|
| 78 | // JID
|
|---|
| 79 | QLabel *l = new QLabel(QString("Link test with %1").arg(d->jid.full()), this);
|
|---|
| 80 | vb->addWidget(l);
|
|---|
| 81 |
|
|---|
| 82 | // type selection
|
|---|
| 83 | d->bg_type = new QButtonGroup(1, Vertical, "Type", this);
|
|---|
| 84 | vb->addWidget(d->bg_type);
|
|---|
| 85 | d->rb_dtcp = new QRadioButton("Direct (S5B)", d->bg_type);
|
|---|
| 86 | d->rb_ibb = new QRadioButton("Thru-server (IBB)", d->bg_type);
|
|---|
| 87 |
|
|---|
| 88 | // status area
|
|---|
| 89 | QGroupBox *gr = new QGroupBox(1, Horizontal, "Status", this);
|
|---|
| 90 | d->pr_status = new QProgressBar(100, gr);
|
|---|
| 91 | d->pr_status->setProgress(0);
|
|---|
| 92 | d->lb_status = new QLabel(gr);
|
|---|
| 93 | d->lb_status->setText("Idle");
|
|---|
| 94 |
|
|---|
| 95 | vb->addWidget(gr);
|
|---|
| 96 |
|
|---|
| 97 | vb->addStretch(1);
|
|---|
| 98 | QHBoxLayout *hb = new QHBoxLayout(vb);
|
|---|
| 99 | d->busy = new BusyWidget(this);
|
|---|
| 100 | hb->addWidget(d->busy);
|
|---|
| 101 | d->pb_test = new QPushButton(this);
|
|---|
| 102 | connect(d->pb_test, SIGNAL(clicked()), SLOT(start()));
|
|---|
| 103 | hb->addWidget(d->pb_test);
|
|---|
| 104 | QPushButton *pb_close = new QPushButton(tr("Close"), this);
|
|---|
| 105 | connect(pb_close, SIGNAL(clicked()), SLOT(close()));
|
|---|
| 106 | hb->addWidget(pb_close);
|
|---|
| 107 |
|
|---|
| 108 | d->pb_test->setFocus();
|
|---|
| 109 | }
|
|---|
| 110 |
|
|---|
| 111 | JLTestDlg::~JLTestDlg()
|
|---|
| 112 | {
|
|---|
| 113 | if(d->jl) {
|
|---|
| 114 | d->jl->close();
|
|---|
| 115 | delete d->jl;
|
|---|
| 116 | }
|
|---|
| 117 | delete d;
|
|---|
| 118 | }
|
|---|
| 119 |
|
|---|
| 120 | void JLTestDlg::reset()
|
|---|
| 121 | {
|
|---|
| 122 | d->mode = ModeConnect;
|
|---|
| 123 | d->pb_test->setEnabled(true);
|
|---|
| 124 | d->pb_test->setText(tr("Connect"));
|
|---|
| 125 | d->lb_status->setText("Idle");
|
|---|
| 126 | d->pr_status->setProgress(0);
|
|---|
| 127 | d->bg_type->setEnabled(true);
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | void JLTestDlg::start()
|
|---|
| 131 | {
|
|---|
| 132 | if(d->mode == ModeConnect) {
|
|---|
| 133 | int type;
|
|---|
| 134 | if(d->rb_dtcp->isChecked())
|
|---|
| 135 | type = JidLink::DTCP;
|
|---|
| 136 | else
|
|---|
| 137 | type = JidLink::IBB;
|
|---|
| 138 |
|
|---|
| 139 | d->jl = new JidLink(d->pa->client());
|
|---|
| 140 | connect(d->jl, SIGNAL(connected()), SLOT(jl_connected()));
|
|---|
| 141 | connect(d->jl, SIGNAL(connectionClosed()), SLOT(jl_connectionClosed()));
|
|---|
| 142 | connect(d->jl, SIGNAL(error(int)), SLOT(jl_error(int)));
|
|---|
| 143 | connect(d->jl, SIGNAL(bytesWritten(int)), SLOT(jl_bytesWritten(int)));
|
|---|
| 144 | connect(d->jl, SIGNAL(readyRead()), SLOT(jl_readyRead()));
|
|---|
| 145 |
|
|---|
| 146 | QDomElement e = textTag(d->pa->client()->doc(), "comment", "this is only a test!");
|
|---|
| 147 | d->jl->connectToJid(d->jid, type, e);
|
|---|
| 148 |
|
|---|
| 149 | d->bg_type->setEnabled(false);
|
|---|
| 150 | d->lb_status->setText("Requesting...");
|
|---|
| 151 | }
|
|---|
| 152 | else {
|
|---|
| 153 | connect(d->jl, SIGNAL(connected()), SLOT(jl_connected()));
|
|---|
| 154 | connect(d->jl, SIGNAL(connectionClosed()), SLOT(jl_connectionClosed()));
|
|---|
| 155 | connect(d->jl, SIGNAL(error(int)), SLOT(jl_error(int)));
|
|---|
| 156 | connect(d->jl, SIGNAL(bytesWritten(int)), SLOT(jl_bytesWritten(int)));
|
|---|
| 157 | connect(d->jl, SIGNAL(readyRead()), SLOT(jl_readyRead()));
|
|---|
| 158 |
|
|---|
| 159 | d->jl->accept();
|
|---|
| 160 |
|
|---|
| 161 | d->lb_status->setText("Connecting...");
|
|---|
| 162 | }
|
|---|
| 163 |
|
|---|
| 164 | d->busy->start();
|
|---|
| 165 | d->pb_test->setEnabled(false);
|
|---|
| 166 | }
|
|---|
| 167 |
|
|---|
| 168 | void JLTestDlg::jl_connected()
|
|---|
| 169 | {
|
|---|
| 170 | d->lb_status->setText("Transferring data...");
|
|---|
| 171 | d->numBytes = 0;
|
|---|
| 172 |
|
|---|
| 173 | if(d->mode == ModeConnect)
|
|---|
| 174 | doNext();
|
|---|
| 175 | }
|
|---|
| 176 |
|
|---|
| 177 | void JLTestDlg::jl_connectionClosed()
|
|---|
| 178 | {
|
|---|
| 179 | d->busy->stop();
|
|---|
| 180 |
|
|---|
| 181 | delete d->jl;
|
|---|
| 182 | d->jl = 0;
|
|---|
| 183 |
|
|---|
| 184 | QMessageBox::information(this, tr("Error"), tr("Connection closed."));
|
|---|
| 185 |
|
|---|
| 186 | reset();
|
|---|
| 187 | }
|
|---|
| 188 |
|
|---|
| 189 | void JLTestDlg::jl_error(int)
|
|---|
| 190 | {
|
|---|
| 191 | d->busy->stop();
|
|---|
| 192 |
|
|---|
| 193 | QString str;
|
|---|
| 194 | delete d->jl;
|
|---|
| 195 | d->jl = 0;
|
|---|
| 196 |
|
|---|
| 197 | /*if(x == DTCPConnection::ErrRequest)
|
|---|
| 198 | str = tr("Error during request!");
|
|---|
| 199 | else if(x == DTCPConnection::ErrConnect)
|
|---|
| 200 | str = tr("Error during connect!");
|
|---|
| 201 | else
|
|---|
| 202 | str = tr("Socket error!");*/
|
|---|
| 203 | str = tr("Stream or connection error.");
|
|---|
| 204 |
|
|---|
| 205 | QMessageBox::information(this, tr("Error"), str);
|
|---|
| 206 |
|
|---|
| 207 | reset();
|
|---|
| 208 | }
|
|---|
| 209 |
|
|---|
| 210 | void JLTestDlg::jl_readyRead()
|
|---|
| 211 | {
|
|---|
| 212 | QByteArray r;
|
|---|
| 213 | r = d->jl->read();
|
|---|
| 214 | printf("got [%d] bytes\n", r.size());
|
|---|
| 215 |
|
|---|
| 216 | d->numBytes += r.size();
|
|---|
| 217 | int percent = 100 * d->numBytes / TOTAL_SIZE;
|
|---|
| 218 | d->pr_status->setProgress(percent);
|
|---|
| 219 |
|
|---|
| 220 | if(d->numBytes >= TOTAL_SIZE) {
|
|---|
| 221 | d->busy->stop();
|
|---|
| 222 |
|
|---|
| 223 | d->jl->close();
|
|---|
| 224 | delete d->jl;
|
|---|
| 225 | d->jl = 0;
|
|---|
| 226 |
|
|---|
| 227 | d->lb_status->setText("Finished");
|
|---|
| 228 | }
|
|---|
| 229 | }
|
|---|
| 230 |
|
|---|
| 231 | void JLTestDlg::jl_bytesWritten(int x)
|
|---|
| 232 | {
|
|---|
| 233 | d->numBytes += x;
|
|---|
| 234 | int percent = 100 * d->numBytes / TOTAL_SIZE;
|
|---|
| 235 | d->pr_status->setProgress(percent);
|
|---|
| 236 |
|
|---|
| 237 | if(d->jl->bytesToWrite() > 0)
|
|---|
| 238 | return;
|
|---|
| 239 |
|
|---|
| 240 | if(d->numBytes >= TOTAL_SIZE) {
|
|---|
| 241 | d->busy->stop();
|
|---|
| 242 |
|
|---|
| 243 | d->jl->close();
|
|---|
| 244 | d->jl->deleteLater();
|
|---|
| 245 | d->jl = 0;
|
|---|
| 246 |
|
|---|
| 247 | d->lb_status->setText("Finished");
|
|---|
| 248 | }
|
|---|
| 249 | else {
|
|---|
| 250 | QTimer::singleShot(PACKET_DELAY, this, SLOT(doNext()));
|
|---|
| 251 | }
|
|---|
| 252 | }
|
|---|
| 253 |
|
|---|
| 254 | void JLTestDlg::doNext()
|
|---|
| 255 | {
|
|---|
| 256 | if(!d->jl)
|
|---|
| 257 | return;
|
|---|
| 258 |
|
|---|
| 259 | writePacket();
|
|---|
| 260 | }
|
|---|
| 261 |
|
|---|
| 262 | void JLTestDlg::writePacket()
|
|---|
| 263 | {
|
|---|
| 264 | QByteArray a(PACKET_SIZE);
|
|---|
| 265 | for(int n = 0; n < (int)a.size(); ++n)
|
|---|
| 266 | a[n] = rand();
|
|---|
| 267 | d->jl->write(a);
|
|---|
| 268 | }
|
|---|