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.
|
|
|
|
*/
|
|
|
|
|
2009-04-25 15:50:35 +02:00
|
|
|
#include "clientthread_p.h"
|
2009-02-08 23:11:14 +01:00
|
|
|
|
2009-04-13 14:39:26 +02:00
|
|
|
#include <QtNetwork/QTcpSocket>
|
|
|
|
|
2009-04-25 15:50:35 +02:00
|
|
|
#include "client_p.h"
|
|
|
|
#include "message.h"
|
2009-02-08 23:11:14 +01:00
|
|
|
#include "sodephelpers_p.h"
|
|
|
|
|
2009-09-02 04:27:16 +02:00
|
|
|
#define QT_NO_CAST_FROM_ASCII
|
|
|
|
#define QT_NO_CAST_TO_ASCII
|
|
|
|
|
2009-04-25 15:50:35 +02:00
|
|
|
using namespace Jolie;
|
|
|
|
|
|
|
|
ClientThread::ClientThread(const QString &hostName, quint16 port, ClientPrivate *client)
|
2009-04-13 14:39:26 +02:00
|
|
|
: 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
|
|
|
}
|
|
|
|
|
2009-04-25 15:50:35 +02:00
|
|
|
ClientThread::~ClientThread()
|
2009-02-08 23:11:14 +01:00
|
|
|
{
|
2009-04-13 14:39:26 +02:00
|
|
|
quit();
|
2009-02-08 23:11:14 +01:00
|
|
|
wait();
|
|
|
|
}
|
|
|
|
|
2009-04-25 15:50:35 +02:00
|
|
|
void ClientThread::sendMessage(const Message &message)
|
2009-04-13 14:39:26 +02:00
|
|
|
{
|
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);
|
2009-05-08 10:22:45 +02:00
|
|
|
QMetaObject::invokeMethod(this, "writeMessageQueue", Qt::QueuedConnection);
|
2009-04-13 14:39:26 +02:00
|
|
|
}
|
|
|
|
|
2009-04-25 15:50:35 +02:00
|
|
|
void ClientThread::writeMessageQueue()
|
2009-04-13 14:39:26 +02:00
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
2009-04-25 15:50:35 +02:00
|
|
|
void ClientThread::readMessage()
|
2009-02-08 23:11:14 +01:00
|
|
|
{
|
2009-04-13 14:39:26 +02:00
|
|
|
if (m_socket->bytesAvailable()==0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-04-25 15:50:35 +02:00
|
|
|
Message message = sodepReadMessage(*m_socket);
|
2009-04-13 14:39:26 +02:00
|
|
|
emit messageReceived(message);
|
|
|
|
|
|
|
|
if (m_socket->bytesAvailable()>0) {
|
2009-05-08 10:22:45 +02:00
|
|
|
QMetaObject::invokeMethod(this, "readMessage", Qt::QueuedConnection);
|
2009-02-08 23:11:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-25 15:50:35 +02:00
|
|
|
void ClientThread::run()
|
2009-02-08 23:11:14 +01:00
|
|
|
{
|
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);
|
2009-04-25 15:50:35 +02:00
|
|
|
connect(this, SIGNAL(messageReceived(Jolie::Message)),
|
|
|
|
m_client, SLOT(messageReceived(Jolie::Message)));
|
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();
|
|
|
|
|
|
|
|
delete m_socket;
|
2009-02-08 23:11:14 +01:00
|
|
|
}
|
|
|
|
|
2009-04-25 15:50:35 +02:00
|
|
|
#include "clientthread_p.moc"
|