2009-09-02 04:21:41 +02:00
|
|
|
/*
|
|
|
|
* Copyright © 2009 Rob Scheepmaker <r.scheepmaker@student.utwente.nl>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Library General Public License version 2 as
|
|
|
|
* published by the Free Software Foundation
|
|
|
|
*
|
|
|
|
* 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 Library General Public
|
|
|
|
* License along with this program; if not, write to the
|
|
|
|
* Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
2009-09-02 04:27:16 +02:00
|
|
|
#include "remoteservicejob_p.h"
|
|
|
|
|
2009-10-20 21:15:32 +02:00
|
|
|
#include <kconfiggroup.h>
|
2009-09-02 08:28:32 +02:00
|
|
|
#include <kurl.h>
|
2009-09-02 04:21:41 +02:00
|
|
|
|
2009-09-02 04:27:16 +02:00
|
|
|
#include <QtCore/QBuffer>
|
2009-09-02 04:21:41 +02:00
|
|
|
#include <QtJolie/Client>
|
2009-09-02 04:27:16 +02:00
|
|
|
#include <QtJolie/Message>
|
|
|
|
#include <QtJolie/PendingCallWatcher>
|
|
|
|
#include <QtJolie/PendingReply>
|
2009-09-02 04:21:41 +02:00
|
|
|
|
|
|
|
#include "../servicejob.h"
|
2009-09-03 23:10:33 +02:00
|
|
|
#include "../remote/accessmanager.h"
|
|
|
|
#include "../remote/authorizationmanager.h"
|
2009-09-02 04:27:16 +02:00
|
|
|
#include "authorizationmanager_p.h"
|
|
|
|
#include "remoteservice_p.h"
|
2009-10-20 21:15:32 +02:00
|
|
|
#include "servicejob_p.h"
|
2009-09-02 04:27:16 +02:00
|
|
|
#include "joliemessagehelper_p.h"
|
|
|
|
#include <qtimer.h>
|
2009-09-02 04:21:41 +02:00
|
|
|
|
|
|
|
namespace Plasma
|
|
|
|
{
|
|
|
|
|
|
|
|
RemoteServiceJob::RemoteServiceJob(KUrl location,
|
2009-09-02 04:27:16 +02:00
|
|
|
const QString& destination,
|
2009-09-02 04:21:41 +02:00
|
|
|
const QString& operation,
|
|
|
|
QMap<QString,QVariant>& parameters,
|
2009-09-02 04:27:16 +02:00
|
|
|
QByteArray initialToken,
|
|
|
|
RemoteService* parent)
|
|
|
|
: ServiceJob(destination, operation, parameters, parent),
|
|
|
|
m_token(initialToken),
|
|
|
|
m_location(location),
|
2009-10-20 21:15:32 +02:00
|
|
|
m_service(parent),
|
|
|
|
m_delayedDesc(0)
|
2009-09-02 04:21:41 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2009-10-20 21:15:32 +02:00
|
|
|
RemoteServiceJob::~RemoteServiceJob()
|
|
|
|
{
|
|
|
|
delete m_delayedDesc;
|
|
|
|
m_delayedDesc = 0;
|
|
|
|
}
|
|
|
|
|
2009-09-02 04:21:41 +02:00
|
|
|
void RemoteServiceJob::start()
|
|
|
|
{
|
2009-09-02 04:27:16 +02:00
|
|
|
QTimer::singleShot(30000, this, SLOT(timeout()));
|
|
|
|
|
2009-10-20 20:23:32 +02:00
|
|
|
if (m_service->m_busy || !m_service->m_ready) {
|
2009-09-02 04:27:16 +02:00
|
|
|
//enqueue and wait
|
|
|
|
m_service->m_queue.enqueue(this);
|
|
|
|
kDebug() << "already busy... enqueue, queue contains " << m_service->m_queue.count();
|
|
|
|
return;
|
2009-10-20 20:23:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// the service is now busy ... with us!
|
|
|
|
m_service->m_busy = true;
|
|
|
|
|
|
|
|
// while waiting in the queue, our validity may have changed; it's all async
|
|
|
|
// so don't assume anything
|
|
|
|
checkValidity();
|
|
|
|
if (error()) {
|
|
|
|
emitResult();
|
|
|
|
return;
|
2009-09-02 04:27:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//serialize the parameters
|
|
|
|
QByteArray params;
|
|
|
|
QBuffer buffer(¶ms);
|
|
|
|
buffer.open(QIODevice::WriteOnly);
|
|
|
|
QDataStream out(&buffer);
|
|
|
|
out << parameters();
|
|
|
|
|
|
|
|
Jolie::Message message(m_location.path(KUrl::RemoveTrailingSlash).remove(0, 1).toUtf8(),
|
|
|
|
"startOperationCall");
|
|
|
|
Jolie::Value data;
|
|
|
|
data.children(Message::Field::OPERATION) << (Jolie::Value(operationName().toAscii()));
|
|
|
|
data.children(Message::Field::PARAMETERS) << Jolie::Value(params);
|
|
|
|
data.children(Message::Field::DESTINATION) << Jolie::Value(destination().toAscii());
|
|
|
|
message.setData(data);
|
|
|
|
|
2009-10-20 20:23:32 +02:00
|
|
|
Jolie::Client *client = m_service->m_client;
|
2009-09-02 04:27:16 +02:00
|
|
|
Jolie::PendingCall pendingReply = client->asyncCall(m_service->signMessage(message));
|
|
|
|
Jolie::PendingCallWatcher *watcher = new Jolie::PendingCallWatcher(pendingReply, this);
|
|
|
|
connect(watcher, SIGNAL(finished(Jolie::PendingCallWatcher*)),
|
|
|
|
this, SLOT(callCompleted(Jolie::PendingCallWatcher*)));
|
|
|
|
}
|
|
|
|
|
2009-10-20 21:15:32 +02:00
|
|
|
void RemoteServiceJob::setDelayedDescription(const KConfigGroup &desc)
|
|
|
|
{
|
|
|
|
if (!m_delayedDesc) {
|
|
|
|
m_delayedDesc = new KConfigGroup(desc);
|
|
|
|
} else {
|
|
|
|
*m_delayedDesc = desc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-20 20:23:32 +02:00
|
|
|
void RemoteServiceJob::checkValidity()
|
|
|
|
{
|
|
|
|
if (!m_service->isOperationEnabled(operationName())) {
|
|
|
|
setError(-1);
|
|
|
|
setErrorText(i18n("Job no longer valid, operation is not enabled!"));
|
2009-10-20 21:15:32 +02:00
|
|
|
} else if (m_delayedDesc) {
|
|
|
|
d->parameters = m_service->parametersFromDescription(*m_delayedDesc);
|
2009-10-20 20:23:32 +02:00
|
|
|
} else {
|
|
|
|
KConfigGroup description = m_service->operationDescription(operationName());
|
|
|
|
QMapIterator<QString, QVariant> param(parameters());
|
|
|
|
while (param.hasNext()) {
|
|
|
|
param.next();
|
|
|
|
if (!description.hasKey(param.key())) {
|
|
|
|
setError(-1);
|
|
|
|
setErrorText(i18n("Job no longer valid, invalid parameters."));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-10-20 21:15:32 +02:00
|
|
|
|
|
|
|
delete m_delayedDesc;
|
|
|
|
m_delayedDesc = 0;
|
2009-10-20 20:23:32 +02:00
|
|
|
}
|
|
|
|
|
2009-09-02 04:27:16 +02:00
|
|
|
void RemoteServiceJob::callCompleted(Jolie::PendingCallWatcher *watcher)
|
|
|
|
{
|
|
|
|
m_service->m_busy = false;
|
|
|
|
|
|
|
|
Jolie::PendingReply reply = *watcher;
|
|
|
|
Jolie::Message response = reply.reply();
|
|
|
|
|
2009-09-02 04:21:41 +02:00
|
|
|
//TODO:async
|
|
|
|
if (response.fault().isValid()) {
|
2009-09-02 04:27:16 +02:00
|
|
|
kDebug() << "fault: " << response.fault().name();
|
|
|
|
setError(-1);
|
|
|
|
setErrorText(Message::errorMessage(response.fault().name()));
|
|
|
|
emitResult();
|
2009-09-02 04:21:41 +02:00
|
|
|
return;
|
|
|
|
}
|
2009-09-02 04:27:16 +02:00
|
|
|
|
|
|
|
m_service->m_token = Message::field(Message::Field::TOKEN, response);
|
|
|
|
|
|
|
|
QVariant variantResult;
|
|
|
|
QByteArray byteArrayResult = Message::field(Message::Field::RESULT, response);
|
|
|
|
QBuffer buffer(&byteArrayResult);
|
|
|
|
buffer.open(QIODevice::ReadOnly);
|
|
|
|
QDataStream in(&buffer);
|
|
|
|
in >> variantResult;
|
|
|
|
|
|
|
|
setResult(variantResult);
|
2009-09-02 04:21:41 +02:00
|
|
|
}
|
|
|
|
|
2009-09-02 04:27:16 +02:00
|
|
|
void RemoteServiceJob::timeout()
|
|
|
|
{
|
|
|
|
m_service->m_busy = false;
|
|
|
|
kDebug() << "Service job timed out.";
|
|
|
|
setError(-1);
|
|
|
|
setErrorText(i18n("Timeout."));
|
2009-09-04 01:04:10 +02:00
|
|
|
m_service->m_queue.removeAll(this);
|
2009-09-02 04:27:16 +02:00
|
|
|
emitResult();
|
|
|
|
}
|
2009-09-02 04:21:41 +02:00
|
|
|
|
2009-09-02 04:27:16 +02:00
|
|
|
} // namespace Plasma
|