2009-02-08 23:11:14 +01:00
|
|
|
/**
|
|
|
|
* This file is part of the KDE project
|
|
|
|
* Copyright (C) 2009 Kevin Ottens <ervin@kde.org>
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library 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
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public License
|
|
|
|
* along with this library; see the file COPYING.LIB. If not, write to
|
|
|
|
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "sodepclientthread_p.h"
|
|
|
|
|
2009-04-13 14:39:26 +02:00
|
|
|
#include <QtCore/QTimer>
|
|
|
|
#include <QtNetwork/QTcpSocket>
|
|
|
|
|
|
|
|
#include "sodepclient_p.h"
|
2009-02-08 23:11:14 +01:00
|
|
|
#include "sodepmessage.h"
|
|
|
|
#include "sodephelpers_p.h"
|
|
|
|
|
2009-04-13 14:39:26 +02:00
|
|
|
SodepClientThread::SodepClientThread(const QString &hostName, quint16 port, SodepClientPrivate *client)
|
|
|
|
: QThread(), m_hostName(hostName), m_port(port), m_socket(0), m_client(client)
|
2009-02-08 23:11:14 +01:00
|
|
|
{
|
2009-04-13 14:39:26 +02:00
|
|
|
moveToThread(this);
|
2009-02-08 23:11:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
SodepClientThread::~SodepClientThread()
|
|
|
|
{
|
2009-04-13 14:39:26 +02:00
|
|
|
quit();
|
2009-02-08 23:11:14 +01:00
|
|
|
wait();
|
|
|
|
}
|
|
|
|
|
2009-04-13 14:39:26 +02:00
|
|
|
void SodepClientThread::sendMessage(const SodepMessage &message)
|
|
|
|
{
|
2009-04-25 14:26:01 +02:00
|
|
|
QMutexLocker locker(&m_mutex);
|
2009-04-13 14:39:26 +02:00
|
|
|
|
2009-04-25 14:26:01 +02:00
|
|
|
m_messageQueue.enqueue(message);
|
|
|
|
QTimer::singleShot(0, this, SLOT(writeMessageQueue()));
|
2009-04-13 14:39:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void SodepClientThread::writeMessageQueue()
|
|
|
|
{
|
2009-04-25 14:26:01 +02:00
|
|
|
QMutexLocker locker(&m_mutex);
|
2009-04-13 14:39:26 +02:00
|
|
|
|
2009-04-25 14:26:01 +02:00
|
|
|
while (!m_messageQueue.isEmpty()) {
|
|
|
|
sodepWrite(*m_socket, m_messageQueue.dequeue());
|
|
|
|
}
|
2009-04-13 14:39:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void SodepClientThread::readMessage()
|
2009-02-08 23:11:14 +01:00
|
|
|
{
|
2009-04-13 14:39:26 +02:00
|
|
|
if (m_socket->bytesAvailable()==0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
SodepMessage message = sodepReadMessage(*m_socket);
|
|
|
|
emit messageReceived(message);
|
|
|
|
|
|
|
|
if (m_socket->bytesAvailable()>0) {
|
|
|
|
QTimer::singleShot(0, this, SLOT(readMessage()));
|
2009-02-08 23:11:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SodepClientThread::run()
|
|
|
|
{
|
2009-04-13 14:39:26 +02:00
|
|
|
m_socket = new QTcpSocket;
|
2009-02-08 23:11:14 +01:00
|
|
|
|
2009-04-13 14:39:26 +02:00
|
|
|
connect(m_socket, SIGNAL(readyRead()),
|
|
|
|
this, SLOT(readMessage()), Qt::QueuedConnection);
|
|
|
|
connect(this, SIGNAL(messageReceived(SodepMessage)),
|
|
|
|
m_client, SLOT(messageReceived(SodepMessage)));
|
2009-02-08 23:11:14 +01:00
|
|
|
|
2009-04-13 14:39:26 +02:00
|
|
|
m_socket->connectToHost(m_hostName, m_port);
|
2009-04-25 14:26:01 +02:00
|
|
|
m_socket->waitForConnected(30000);
|
2009-04-13 14:39:26 +02:00
|
|
|
|
|
|
|
exec();
|
2009-04-25 14:26:01 +02:00
|
|
|
writeMessageQueue();
|
|
|
|
m_socket->waitForBytesWritten(30000);
|
2009-04-13 14:39:26 +02:00
|
|
|
|
|
|
|
delete m_socket;
|
2009-02-08 23:11:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#include "sodepclientthread_p.moc"
|