~ubuntu-branches/ubuntu/gutsy/soprano/gutsy

« back to all changes in this revision

Viewing changes to server/clientconnection.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2007-10-12 14:43:48 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20071012144348-yajzi51v4k23ahxf
Tags: 1.95.0~beta2-1ubuntu1
* Sync with Debian
* Add versioned build-dep on raptor

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * This file is part of Soprano Project.
 
3
 *
 
4
 * Copyright (C) 2007 Sebastian Trueg <trueg@kde.org>
 
5
 *
 
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.
 
10
 *
 
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.
 
15
 *
 
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.
 
20
 */
 
21
 
 
22
#include "clientconnection.h"
 
23
#include "operators.h"
 
24
#include "commands.h"
 
25
#include "socketdevice.h"
 
26
 
 
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>
 
32
 
 
33
#include <QtNetwork/QTcpSocket>
 
34
#include <QtCore/QMutex>
 
35
#include <QtCore/QMutexLocker>
 
36
#include <QtCore/QDir>
 
37
 
 
38
using namespace Soprano::Server;
 
39
 
 
40
namespace {
 
41
    const int s_defaultTimeout = 30000;
 
42
}
 
43
 
 
44
class Soprano::Client::ClientConnection::Private
 
45
{
 
46
public:
 
47
    Private()
 
48
        : socket( 0 ) {
 
49
    }
 
50
 
 
51
    QIODevice* socket;
 
52
    QMutex mutex;
 
53
};
 
54
 
 
55
 
 
56
Soprano::Client::ClientConnection::ClientConnection( QObject* parent )
 
57
    : QObject( parent ),
 
58
      d( new Private() )
 
59
{
 
60
}
 
61
 
 
62
 
 
63
Soprano::Client::ClientConnection::~ClientConnection()
 
64
{
 
65
    delete d;
 
66
}
 
67
 
 
68
 
 
69
void Soprano::Client::ClientConnection::connect( QIODevice* dev )
 
70
{
 
71
    d->socket = dev;
 
72
}
 
73
 
 
74
 
 
75
int Soprano::Client::ClientConnection::createModel( const QString& name, const QList<BackendSetting>& settings )
 
76
{
 
77
//    qDebug() << "(ClientConnection::createModel)";
 
78
    QMutexLocker( &d->mutex );
 
79
 
 
80
    QDataStream stream( d->socket );
 
81
 
 
82
    stream << COMMAND_CREATE_MODEL << name << settings;
 
83
 
 
84
    if ( !d->socket->waitForReadyRead(s_defaultTimeout) ) {
 
85
        setError( "Command timed out." );
 
86
        return 0;
 
87
    }
 
88
 
 
89
    quint32 id;
 
90
    Error::Error error;
 
91
 
 
92
    stream >> id >> error;
 
93
 
 
94
    setError( error );
 
95
    return id;
 
96
}
 
97
 
 
98
 
 
99
Soprano::BackendFeatures Soprano::Client::ClientConnection::supportedFeatures()
 
100
{
 
101
//    qDebug() << "(ClientConnection::supportedFeatures)";
 
102
    QMutexLocker( &d->mutex );
 
103
 
 
104
    QDataStream stream( d->socket );
 
105
 
 
106
    stream << COMMAND_SUPPORTED_FEATURES;
 
107
 
 
108
    if ( !d->socket->waitForReadyRead(s_defaultTimeout) ) {
 
109
        setError( "Command timed out." );
 
110
        return 0;
 
111
    }
 
112
 
 
113
    quint32 features;
 
114
    Error::Error error;
 
115
 
 
116
    stream >> features >> error;
 
117
 
 
118
    setError( error );
 
119
    return ( BackendFeatures )features;
 
120
}
 
121
 
 
122
 
 
123
Soprano::Error::ErrorCode Soprano::Client::ClientConnection::addStatement( int modelId, const Statement &statement )
 
124
{
 
125
//    qDebug() << "(ClientConnection::addStatement)";
 
126
    QMutexLocker( &d->mutex );
 
127
 
 
128
    QDataStream stream( d->socket );
 
129
 
 
130
    stream << COMMAND_MODEL_ADD_STATEMENT << ( quint32 )modelId << statement;
 
131
 
 
132
    if ( !d->socket->waitForReadyRead(s_defaultTimeout) ) {
 
133
        setError( "Command timed out." );
 
134
        return Error::ErrorUnknown;
 
135
    }
 
136
 
 
137
    Error::ErrorCode ec;
 
138
    Error::Error error;
 
139
    stream >> ec >> error;
 
140
 
 
141
    setError( error );
 
142
    return ec;
 
143
}
 
144
 
 
145
 
 
146
int Soprano::Client::ClientConnection::listContexts( int modelId )
 
147
{
 
148
//    qDebug() << "(ClientConnection::listContexts)";
 
149
    QMutexLocker( &d->mutex );
 
150
 
 
151
    QDataStream stream( d->socket );
 
152
 
 
153
    stream << COMMAND_MODEL_LIST_CONTEXTS << ( quint32 )modelId;
 
154
 
 
155
    if ( !d->socket->waitForReadyRead(s_defaultTimeout) ) {
 
156
        setError( "Command timed out." );
 
157
        return Error::ErrorUnknown;
 
158
    }
 
159
 
 
160
    quint32 itId;
 
161
    Error::Error error;
 
162
    stream >> itId >> error;
 
163
 
 
164
    setError( error );
 
165
    return itId;
 
166
}
 
167
 
 
168
 
 
169
int Soprano::Client::ClientConnection::executeQuery( int modelId, const QString &query, Query::QueryLanguage type, const QString& userQueryLanguage )
 
170
{
 
171
//    qDebug() << "(ClientConnection::executeQuery)";
 
172
    QMutexLocker( &d->mutex );
 
173
 
 
174
    QDataStream stream( d->socket );
 
175
 
 
176
    stream << COMMAND_MODEL_QUERY << ( quint32 )modelId << query << ( quint16 )type << userQueryLanguage;
 
177
 
 
178
    if ( !d->socket->waitForReadyRead(s_defaultTimeout) ) {
 
179
        setError( "Command timed out." );
 
180
        return Error::ErrorUnknown;
 
181
    }
 
182
 
 
183
    quint32 itId;
 
184
    Error::Error error;
 
185
    stream >> itId >> error;
 
186
 
 
187
    setError( error );
 
188
    return itId;
 
189
}
 
190
 
 
191
 
 
192
int Soprano::Client::ClientConnection::listStatements( int modelId, const Statement &partial )
 
193
{
 
194
//    qDebug() << "(ClientConnection::listStatements)";
 
195
    QMutexLocker( &d->mutex );
 
196
 
 
197
    QDataStream stream( d->socket );
 
198
 
 
199
    stream << COMMAND_MODEL_LIST_STATEMENTS << ( quint32 )modelId << partial;
 
200
 
 
201
    if ( !d->socket->waitForReadyRead(s_defaultTimeout) ) {
 
202
        setError( "Command timed out." );
 
203
        return Error::ErrorUnknown;
 
204
    }
 
205
 
 
206
    quint32 itId;
 
207
    Error::Error error;
 
208
    stream >> itId >> error;
 
209
 
 
210
    setError( error );
 
211
    return itId;
 
212
}
 
213
 
 
214
 
 
215
Soprano::Error::ErrorCode Soprano::Client::ClientConnection::removeAllStatements( int modelId, const Statement &statement )
 
216
{
 
217
//    qDebug() << "(ClientConnection::removeAllStatements)";
 
218
    QMutexLocker( &d->mutex );
 
219
 
 
220
    QDataStream stream( d->socket );
 
221
 
 
222
    stream << COMMAND_MODEL_REMOVE_ALL_STATEMENTS << ( quint32 )modelId << statement;
 
223
 
 
224
    if ( !d->socket->waitForReadyRead(s_defaultTimeout) ) {
 
225
        setError( "Command timed out." );
 
226
        return Error::ErrorUnknown;
 
227
    }
 
228
 
 
229
    Error::ErrorCode ec;
 
230
    Error::Error error;
 
231
    stream >> ec >> error;
 
232
 
 
233
    setError( error );
 
234
    return ec;
 
235
}
 
236
 
 
237
 
 
238
Soprano::Error::ErrorCode Soprano::Client::ClientConnection::removeStatement( int modelId, const Statement &statement )
 
239
{
 
240
//    qDebug() << "(ClientConnection::removeStatement)";
 
241
    QMutexLocker( &d->mutex );
 
242
 
 
243
    QDataStream stream( d->socket );
 
244
 
 
245
    stream << COMMAND_MODEL_REMOVE_STATEMENT << ( quint32 )modelId << statement;
 
246
 
 
247
    if ( !d->socket->waitForReadyRead(s_defaultTimeout) ) {
 
248
        setError( "Command timed out." );
 
249
        return Error::ErrorUnknown;
 
250
    }
 
251
 
 
252
    Error::ErrorCode ec;
 
253
    Error::Error error;
 
254
    stream >> ec >> error;
 
255
 
 
256
    setError( error );
 
257
    return ec;
 
258
}
 
259
 
 
260
 
 
261
int Soprano::Client::ClientConnection::statementCount( int modelId )
 
262
{
 
263
//    qDebug() << "(ClientConnection::statementCount)";
 
264
    QMutexLocker( &d->mutex );
 
265
 
 
266
    QDataStream stream( d->socket );
 
267
 
 
268
    stream << COMMAND_MODEL_STATEMENT_COUNT << ( quint32 )modelId;
 
269
 
 
270
    if ( !d->socket->waitForReadyRead(s_defaultTimeout) ) {
 
271
        setError( "Command timed out." );
 
272
        return Error::ErrorUnknown;
 
273
    }
 
274
 
 
275
    qint32 count;
 
276
    Error::Error error;
 
277
    stream >> count >> error;
 
278
 
 
279
    setError( error );
 
280
    return count;
 
281
}
 
282
 
 
283
 
 
284
bool Soprano::Client::ClientConnection::containsStatement( int modelId, const Statement &statement )
 
285
{
 
286
//    qDebug() << "(ClientConnection::containsStatement)";
 
287
    QMutexLocker( &d->mutex );
 
288
 
 
289
    QDataStream stream( d->socket );
 
290
 
 
291
    stream << COMMAND_MODEL_CONTAINS_STATEMENT << ( quint32 )modelId << statement;
 
292
 
 
293
    if ( !d->socket->waitForReadyRead(s_defaultTimeout) ) {
 
294
        setError( "Command timed out." );
 
295
        return false;
 
296
    }
 
297
 
 
298
    bool r;
 
299
    Error::Error error;
 
300
    stream >> r >> error;
 
301
 
 
302
    setError( error );
 
303
    return r;
 
304
}
 
305
 
 
306
 
 
307
bool Soprano::Client::ClientConnection::containsAnyStatement( int modelId, const Statement &statement )
 
308
{
 
309
//    qDebug() << "(ClientConnection::containsAnyStatement)";
 
310
    QMutexLocker( &d->mutex );
 
311
 
 
312
    QDataStream stream( d->socket );
 
313
 
 
314
    stream << COMMAND_MODEL_CONTAINS_ANY_STATEMENT << ( quint32 )modelId << statement;
 
315
 
 
316
    if ( !d->socket->waitForReadyRead(s_defaultTimeout) ) {
 
317
        setError( "Command timed out." );
 
318
        return false;
 
319
    }
 
320
 
 
321
    bool r;
 
322
    Error::Error error;
 
323
    stream >> r >> error;
 
324
 
 
325
    setError( error );
 
326
    return r;
 
327
}
 
328
 
 
329
 
 
330
bool Soprano::Client::ClientConnection::isEmpty( int modelId )
 
331
{
 
332
//    qDebug() << "(ClientConnection::isEmpty)";
 
333
    QMutexLocker( &d->mutex );
 
334
 
 
335
    QDataStream stream( d->socket );
 
336
 
 
337
    stream << COMMAND_MODEL_IS_EMPTY << ( quint32 )modelId;
 
338
 
 
339
    if ( !d->socket->waitForReadyRead(s_defaultTimeout) ) {
 
340
        setError( "Command timed out." );
 
341
        return false;
 
342
    }
 
343
 
 
344
    bool r;
 
345
    Error::Error error;
 
346
    stream >> r >> error;
 
347
 
 
348
    setError( error );
 
349
    return r;
 
350
}
 
351
 
 
352
 
 
353
Soprano::Node Soprano::Client::ClientConnection::createBlankNode( int modelId )
 
354
{
 
355
//    qDebug() << "(ClientConnection::isEmpty)";
 
356
    QMutexLocker( &d->mutex );
 
357
 
 
358
    QDataStream stream( d->socket );
 
359
 
 
360
    stream << COMMAND_MODEL_CREATE_BLANK_NODE << ( quint32 )modelId;
 
361
 
 
362
    if ( !d->socket->waitForReadyRead(s_defaultTimeout) ) {
 
363
        setError( "Command timed out." );
 
364
        return Node();
 
365
    }
 
366
 
 
367
    Node n;
 
368
    Error::Error error;
 
369
    stream >> n >> error;
 
370
 
 
371
    setError( error );
 
372
    return n;
 
373
}
 
374
 
 
375
 
 
376
bool Soprano::Client::ClientConnection::iteratorNext( int id )
 
377
{
 
378
//    qDebug() << "(ClientConnection::iteratorNext)";
 
379
    QMutexLocker( &d->mutex );
 
380
 
 
381
    QDataStream stream( d->socket );
 
382
 
 
383
    stream << COMMAND_ITERATOR_NEXT << ( quint32 )id;
 
384
 
 
385
    if ( !d->socket->waitForReadyRead(s_defaultTimeout) ) {
 
386
        setError( "Command timed out." );
 
387
        return false;
 
388
    }
 
389
 
 
390
    bool r;
 
391
    Error::Error error;
 
392
    stream >> r >> error;
 
393
    setError( error );
 
394
    return r;
 
395
}
 
396
 
 
397
 
 
398
Soprano::Node Soprano::Client::ClientConnection::nodeIteratorCurrent( int id )
 
399
{
 
400
//    qDebug() << "(ClientConnection::nodeIteratorCurrent)";
 
401
    QMutexLocker( &d->mutex );
 
402
 
 
403
    QDataStream stream( d->socket );
 
404
 
 
405
    stream << COMMAND_ITERATOR_CURRENT_NODE << ( quint32 )id;
 
406
 
 
407
    if ( !d->socket->waitForReadyRead(s_defaultTimeout) ) {
 
408
        setError( "Command timed out." );
 
409
        return Node();
 
410
    }
 
411
 
 
412
    Node node;
 
413
    Error::Error error;
 
414
    stream >> node >> error;
 
415
 
 
416
    setError( error );
 
417
    return node;
 
418
}
 
419
 
 
420
 
 
421
Soprano::Statement Soprano::Client::ClientConnection::statementIteratorCurrent( int id )
 
422
{
 
423
//    qDebug() << "(ClientConnection::statementIteratorCurrent)";
 
424
    QMutexLocker( &d->mutex );
 
425
 
 
426
    QDataStream stream( d->socket );
 
427
 
 
428
    stream << COMMAND_ITERATOR_CURRENT_STATEMENT << ( quint32 )id;
 
429
 
 
430
    if ( !d->socket->waitForReadyRead(s_defaultTimeout) ) {
 
431
        setError( "Command timed out." );
 
432
        return Statement();
 
433
    }
 
434
 
 
435
    Statement statement;
 
436
    Error::Error error;
 
437
    stream >> statement >> error;
 
438
 
 
439
    setError( error );
 
440
    return statement;
 
441
}
 
442
 
 
443
 
 
444
Soprano::BindingSet Soprano::Client::ClientConnection::queryIteratorCurrent( int id )
 
445
{
 
446
//    qDebug() << "(ClientConnection::queryIteratorCurrent)";
 
447
    QMutexLocker( &d->mutex );
 
448
 
 
449
    QDataStream stream( d->socket );
 
450
 
 
451
    stream << COMMAND_ITERATOR_CURRENT_BINDINGSET << ( quint32 )id;
 
452
 
 
453
    if ( !d->socket->waitForReadyRead(s_defaultTimeout) ) {
 
454
        setError( "Command timed out." );
 
455
        return BindingSet();
 
456
    }
 
457
 
 
458
    BindingSet set;
 
459
    Error::Error error;
 
460
    stream >> set >> error;
 
461
 
 
462
    setError( error );
 
463
    return set;
 
464
}
 
465
 
 
466
 
 
467
Soprano::Statement Soprano::Client::ClientConnection::queryIteratorCurrentStatement( int id )
 
468
{
 
469
//    qDebug() << "(ClientConnection::queryIteratorCurrentStatement)";
 
470
    QMutexLocker( &d->mutex );
 
471
 
 
472
    QDataStream stream( d->socket );
 
473
 
 
474
    stream << COMMAND_ITERATOR_CURRENT_STATEMENT << ( quint32 )id;
 
475
 
 
476
    if ( !d->socket->waitForReadyRead(s_defaultTimeout) ) {
 
477
        setError( "Command timed out." );
 
478
        return Statement();
 
479
    }
 
480
 
 
481
    Statement statement;
 
482
    Error::Error error;
 
483
    stream >> statement >> error;
 
484
 
 
485
    setError( error );
 
486
    return statement;
 
487
}
 
488
 
 
489
 
 
490
int Soprano::Client::ClientConnection::queryIteratorType( int id )
 
491
{
 
492
//    qDebug() << "(ClientConnection::queryIteratorType)";
 
493
    QMutexLocker( &d->mutex );
 
494
 
 
495
    QDataStream stream( d->socket );
 
496
 
 
497
    stream << COMMAND_ITERATOR_QUERY_TYPE << ( quint32 )id;
 
498
 
 
499
    if ( !d->socket->waitForReadyRead(s_defaultTimeout) ) {
 
500
        setError( "Command timed out." );
 
501
        return 0;
 
502
    }
 
503
 
 
504
    quint8 type;
 
505
    Error::Error error;
 
506
    stream >> type >> error;
 
507
 
 
508
    setError( error );
 
509
    return type;
 
510
}
 
511
 
 
512
 
 
513
bool Soprano::Client::ClientConnection::queryIteratorBoolValue( int id )
 
514
{
 
515
//    qDebug() << "(ClientConnection::queryIteratorBoolValue)";
 
516
    QMutexLocker( &d->mutex );
 
517
 
 
518
    QDataStream stream( d->socket );
 
519
 
 
520
    stream << COMMAND_ITERATOR_QUERY_BOOL_VALUE << ( quint32 )id;
 
521
 
 
522
    if ( !d->socket->waitForReadyRead(s_defaultTimeout) ) {
 
523
        setError( "Command timed out." );
 
524
        return false;
 
525
    }
 
526
 
 
527
    bool v;
 
528
    Error::Error error;
 
529
    stream >> v >> error;
 
530
 
 
531
    setError( error );
 
532
    return v;
 
533
}
 
534
 
 
535
 
 
536
void Soprano::Client::ClientConnection::iteratorClose( int id )
 
537
{
 
538
//    qDebug() << "(ClientConnection::iteratorClose)";
 
539
    QMutexLocker( &d->mutex );
 
540
 
 
541
    QDataStream stream( d->socket );
 
542
 
 
543
    stream << COMMAND_ITERATOR_CLOSE << ( quint32 )id;
 
544
 
 
545
    if ( !d->socket->waitForReadyRead(s_defaultTimeout) ) {
 
546
        setError( "Command timed out." );
 
547
        return;
 
548
    }
 
549
 
 
550
    Error::Error error;
 
551
    stream >> error;
 
552
 
 
553
    setError( error );
 
554
}
 
555
 
 
556
 
 
557
bool Soprano::Client::ClientConnection::checkProtocolVersion()
 
558
{
 
559
    QMutexLocker( &d->mutex );
 
560
 
 
561
    QDataStream stream( d->socket );
 
562
    stream << COMMAND_SUPPORTS_PROTOCOL_VERSION << ( quint32 )PROTOCOL_VERSION;
 
563
 
 
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." );
 
567
        return false;
 
568
    }
 
569
 
 
570
    bool reply;
 
571
    stream >> reply;
 
572
 
 
573
    if ( reply ) {
 
574
        clearError();
 
575
    }
 
576
    else {
 
577
        setError( QString( "Server does not support protocol version %1" ).arg( PROTOCOL_VERSION ) );
 
578
    }
 
579
    return reply;
 
580
}
 
581
 
 
582
#include "clientconnection.moc"