2
* This file is part of Soprano Project.
4
* Copyright (C) 2007 Sebastian Trueg <trueg@kde.org>
6
* This library is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU Library General Public
8
* License as published by the Free Software Foundation; either
9
* version 2 of the License, or (at your option) any later version.
11
* This library is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
* Library General Public License for more details.
16
* You should have received a copy of the GNU Library General Public License
17
* along with this library; see the file COPYING.LIB. If not, write to
18
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19
* Boston, MA 02110-1301, USA.
22
#include "clientconnection.h"
23
#include "operators.h"
25
#include "socketdevice.h"
27
#include <soprano/node.h>
28
#include <soprano/statement.h>
29
#include <soprano/model.h>
30
#include <soprano/bindingset.h>
31
#include <soprano/backend.h>
33
#include <QtNetwork/QTcpSocket>
34
#include <QtCore/QMutex>
35
#include <QtCore/QMutexLocker>
36
#include <QtCore/QDir>
38
using namespace Soprano::Server;
41
const int s_defaultTimeout = 30000;
44
class Soprano::Client::ClientConnection::Private
56
Soprano::Client::ClientConnection::ClientConnection( QObject* parent )
63
Soprano::Client::ClientConnection::~ClientConnection()
69
void Soprano::Client::ClientConnection::connect( QIODevice* dev )
75
int Soprano::Client::ClientConnection::createModel( const QString& name, const QList<BackendSetting>& settings )
77
// qDebug() << "(ClientConnection::createModel)";
78
QMutexLocker( &d->mutex );
80
QDataStream stream( d->socket );
82
stream << COMMAND_CREATE_MODEL << name << settings;
84
if ( !d->socket->waitForReadyRead(s_defaultTimeout) ) {
85
setError( "Command timed out." );
92
stream >> id >> error;
99
Soprano::BackendFeatures Soprano::Client::ClientConnection::supportedFeatures()
101
// qDebug() << "(ClientConnection::supportedFeatures)";
102
QMutexLocker( &d->mutex );
104
QDataStream stream( d->socket );
106
stream << COMMAND_SUPPORTED_FEATURES;
108
if ( !d->socket->waitForReadyRead(s_defaultTimeout) ) {
109
setError( "Command timed out." );
116
stream >> features >> error;
119
return ( BackendFeatures )features;
123
Soprano::Error::ErrorCode Soprano::Client::ClientConnection::addStatement( int modelId, const Statement &statement )
125
// qDebug() << "(ClientConnection::addStatement)";
126
QMutexLocker( &d->mutex );
128
QDataStream stream( d->socket );
130
stream << COMMAND_MODEL_ADD_STATEMENT << ( quint32 )modelId << statement;
132
if ( !d->socket->waitForReadyRead(s_defaultTimeout) ) {
133
setError( "Command timed out." );
134
return Error::ErrorUnknown;
139
stream >> ec >> error;
146
int Soprano::Client::ClientConnection::listContexts( int modelId )
148
// qDebug() << "(ClientConnection::listContexts)";
149
QMutexLocker( &d->mutex );
151
QDataStream stream( d->socket );
153
stream << COMMAND_MODEL_LIST_CONTEXTS << ( quint32 )modelId;
155
if ( !d->socket->waitForReadyRead(s_defaultTimeout) ) {
156
setError( "Command timed out." );
157
return Error::ErrorUnknown;
162
stream >> itId >> error;
169
int Soprano::Client::ClientConnection::executeQuery( int modelId, const QString &query, Query::QueryLanguage type, const QString& userQueryLanguage )
171
// qDebug() << "(ClientConnection::executeQuery)";
172
QMutexLocker( &d->mutex );
174
QDataStream stream( d->socket );
176
stream << COMMAND_MODEL_QUERY << ( quint32 )modelId << query << ( quint16 )type << userQueryLanguage;
178
if ( !d->socket->waitForReadyRead(s_defaultTimeout) ) {
179
setError( "Command timed out." );
180
return Error::ErrorUnknown;
185
stream >> itId >> error;
192
int Soprano::Client::ClientConnection::listStatements( int modelId, const Statement &partial )
194
// qDebug() << "(ClientConnection::listStatements)";
195
QMutexLocker( &d->mutex );
197
QDataStream stream( d->socket );
199
stream << COMMAND_MODEL_LIST_STATEMENTS << ( quint32 )modelId << partial;
201
if ( !d->socket->waitForReadyRead(s_defaultTimeout) ) {
202
setError( "Command timed out." );
203
return Error::ErrorUnknown;
208
stream >> itId >> error;
215
Soprano::Error::ErrorCode Soprano::Client::ClientConnection::removeAllStatements( int modelId, const Statement &statement )
217
// qDebug() << "(ClientConnection::removeAllStatements)";
218
QMutexLocker( &d->mutex );
220
QDataStream stream( d->socket );
222
stream << COMMAND_MODEL_REMOVE_ALL_STATEMENTS << ( quint32 )modelId << statement;
224
if ( !d->socket->waitForReadyRead(s_defaultTimeout) ) {
225
setError( "Command timed out." );
226
return Error::ErrorUnknown;
231
stream >> ec >> error;
238
Soprano::Error::ErrorCode Soprano::Client::ClientConnection::removeStatement( int modelId, const Statement &statement )
240
// qDebug() << "(ClientConnection::removeStatement)";
241
QMutexLocker( &d->mutex );
243
QDataStream stream( d->socket );
245
stream << COMMAND_MODEL_REMOVE_STATEMENT << ( quint32 )modelId << statement;
247
if ( !d->socket->waitForReadyRead(s_defaultTimeout) ) {
248
setError( "Command timed out." );
249
return Error::ErrorUnknown;
254
stream >> ec >> error;
261
int Soprano::Client::ClientConnection::statementCount( int modelId )
263
// qDebug() << "(ClientConnection::statementCount)";
264
QMutexLocker( &d->mutex );
266
QDataStream stream( d->socket );
268
stream << COMMAND_MODEL_STATEMENT_COUNT << ( quint32 )modelId;
270
if ( !d->socket->waitForReadyRead(s_defaultTimeout) ) {
271
setError( "Command timed out." );
272
return Error::ErrorUnknown;
277
stream >> count >> error;
284
bool Soprano::Client::ClientConnection::containsStatement( int modelId, const Statement &statement )
286
// qDebug() << "(ClientConnection::containsStatement)";
287
QMutexLocker( &d->mutex );
289
QDataStream stream( d->socket );
291
stream << COMMAND_MODEL_CONTAINS_STATEMENT << ( quint32 )modelId << statement;
293
if ( !d->socket->waitForReadyRead(s_defaultTimeout) ) {
294
setError( "Command timed out." );
300
stream >> r >> error;
307
bool Soprano::Client::ClientConnection::containsAnyStatement( int modelId, const Statement &statement )
309
// qDebug() << "(ClientConnection::containsAnyStatement)";
310
QMutexLocker( &d->mutex );
312
QDataStream stream( d->socket );
314
stream << COMMAND_MODEL_CONTAINS_ANY_STATEMENT << ( quint32 )modelId << statement;
316
if ( !d->socket->waitForReadyRead(s_defaultTimeout) ) {
317
setError( "Command timed out." );
323
stream >> r >> error;
330
bool Soprano::Client::ClientConnection::isEmpty( int modelId )
332
// qDebug() << "(ClientConnection::isEmpty)";
333
QMutexLocker( &d->mutex );
335
QDataStream stream( d->socket );
337
stream << COMMAND_MODEL_IS_EMPTY << ( quint32 )modelId;
339
if ( !d->socket->waitForReadyRead(s_defaultTimeout) ) {
340
setError( "Command timed out." );
346
stream >> r >> error;
353
Soprano::Node Soprano::Client::ClientConnection::createBlankNode( int modelId )
355
// qDebug() << "(ClientConnection::isEmpty)";
356
QMutexLocker( &d->mutex );
358
QDataStream stream( d->socket );
360
stream << COMMAND_MODEL_CREATE_BLANK_NODE << ( quint32 )modelId;
362
if ( !d->socket->waitForReadyRead(s_defaultTimeout) ) {
363
setError( "Command timed out." );
369
stream >> n >> error;
376
bool Soprano::Client::ClientConnection::iteratorNext( int id )
378
// qDebug() << "(ClientConnection::iteratorNext)";
379
QMutexLocker( &d->mutex );
381
QDataStream stream( d->socket );
383
stream << COMMAND_ITERATOR_NEXT << ( quint32 )id;
385
if ( !d->socket->waitForReadyRead(s_defaultTimeout) ) {
386
setError( "Command timed out." );
392
stream >> r >> error;
398
Soprano::Node Soprano::Client::ClientConnection::nodeIteratorCurrent( int id )
400
// qDebug() << "(ClientConnection::nodeIteratorCurrent)";
401
QMutexLocker( &d->mutex );
403
QDataStream stream( d->socket );
405
stream << COMMAND_ITERATOR_CURRENT_NODE << ( quint32 )id;
407
if ( !d->socket->waitForReadyRead(s_defaultTimeout) ) {
408
setError( "Command timed out." );
414
stream >> node >> error;
421
Soprano::Statement Soprano::Client::ClientConnection::statementIteratorCurrent( int id )
423
// qDebug() << "(ClientConnection::statementIteratorCurrent)";
424
QMutexLocker( &d->mutex );
426
QDataStream stream( d->socket );
428
stream << COMMAND_ITERATOR_CURRENT_STATEMENT << ( quint32 )id;
430
if ( !d->socket->waitForReadyRead(s_defaultTimeout) ) {
431
setError( "Command timed out." );
437
stream >> statement >> error;
444
Soprano::BindingSet Soprano::Client::ClientConnection::queryIteratorCurrent( int id )
446
// qDebug() << "(ClientConnection::queryIteratorCurrent)";
447
QMutexLocker( &d->mutex );
449
QDataStream stream( d->socket );
451
stream << COMMAND_ITERATOR_CURRENT_BINDINGSET << ( quint32 )id;
453
if ( !d->socket->waitForReadyRead(s_defaultTimeout) ) {
454
setError( "Command timed out." );
460
stream >> set >> error;
467
Soprano::Statement Soprano::Client::ClientConnection::queryIteratorCurrentStatement( int id )
469
// qDebug() << "(ClientConnection::queryIteratorCurrentStatement)";
470
QMutexLocker( &d->mutex );
472
QDataStream stream( d->socket );
474
stream << COMMAND_ITERATOR_CURRENT_STATEMENT << ( quint32 )id;
476
if ( !d->socket->waitForReadyRead(s_defaultTimeout) ) {
477
setError( "Command timed out." );
483
stream >> statement >> error;
490
int Soprano::Client::ClientConnection::queryIteratorType( int id )
492
// qDebug() << "(ClientConnection::queryIteratorType)";
493
QMutexLocker( &d->mutex );
495
QDataStream stream( d->socket );
497
stream << COMMAND_ITERATOR_QUERY_TYPE << ( quint32 )id;
499
if ( !d->socket->waitForReadyRead(s_defaultTimeout) ) {
500
setError( "Command timed out." );
506
stream >> type >> error;
513
bool Soprano::Client::ClientConnection::queryIteratorBoolValue( int id )
515
// qDebug() << "(ClientConnection::queryIteratorBoolValue)";
516
QMutexLocker( &d->mutex );
518
QDataStream stream( d->socket );
520
stream << COMMAND_ITERATOR_QUERY_BOOL_VALUE << ( quint32 )id;
522
if ( !d->socket->waitForReadyRead(s_defaultTimeout) ) {
523
setError( "Command timed out." );
529
stream >> v >> error;
536
void Soprano::Client::ClientConnection::iteratorClose( int id )
538
// qDebug() << "(ClientConnection::iteratorClose)";
539
QMutexLocker( &d->mutex );
541
QDataStream stream( d->socket );
543
stream << COMMAND_ITERATOR_CLOSE << ( quint32 )id;
545
if ( !d->socket->waitForReadyRead(s_defaultTimeout) ) {
546
setError( "Command timed out." );
557
bool Soprano::Client::ClientConnection::checkProtocolVersion()
559
QMutexLocker( &d->mutex );
561
QDataStream stream( d->socket );
562
stream << COMMAND_SUPPORTS_PROTOCOL_VERSION << ( quint32 )PROTOCOL_VERSION;
564
// wait for a reply, but not forever, in case we are connected to something unknown
565
if ( !d->socket->waitForReadyRead(s_defaultTimeout) ) {
566
setError( "Command timed out." );
577
setError( QString( "Server does not support protocol version %1" ).arg( PROTOCOL_VERSION ) );
582
#include "clientconnection.moc"