Comply to the new version of SODEP.

svn path=/branches/work/~ervin/sodep/; revision=880515
This commit is contained in:
Kevin Ottens 2008-11-05 18:48:16 +00:00
parent 43448adfc3
commit f29bbbab07
5 changed files with 635 additions and 0 deletions

View File

@ -0,0 +1,123 @@
/**
* This file is part of the KDE project
* Copyright (C) 2008 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.
*/
#ifndef SODEPHELPERS_P_H
#define SODEPHELPERS_P_H
#include <QtCore/QIODevice>
#include <QtCore/QString>
inline void sodepWrite(QIODevice &io, double value)
{
const char *in = (const char*)&value;
char out[8];
for (int i=0; i<8; ++i) {
out[i] = in[7-i];
}
io.write(out, 8);
}
inline void sodepWrite(QIODevice &io, qint32 value)
{
const char *in = (const char*)&value;
char out[4];
for (int i=0; i<4; ++i) {
out[i] = in[3-i];
}
io.write(out, 4);
}
inline void sodepWrite(QIODevice &io, qint64 value)
{
const char *in = (const char*)&value;
char out[8];
for (int i=0; i<8; ++i) {
out[i] = in[7-i];
}
io.write(out, 8);
}
inline void sodepWrite(QIODevice &io, const QString &value)
{
QByteArray data = value.toUtf8();
sodepWrite(io, data.size());
io.write(data);
}
inline double sodepReadDouble(QIODevice &io)
{
double d;
char *out = (char*)&d;
char in[8];
io.read(in, 8);
for (int i=0; i<8; ++i) {
out[i] = in[7-i];
}
return d;
}
inline qint32 sodepReadInt32(QIODevice &io)
{
qint32 i;
char *out = (char*)&i;
char in[4];
io.read(in, 4);
for (int j=0; j<4; ++j) {
out[j] = in[3-j];
}
return i;
}
inline qint64 sodepReadInt64(QIODevice &io)
{
qint64 i;
char *out = (char*)&i;
char in[8];
io.read(in, 8);
for (int j=0; j<8; ++j) {
out[j] = in[7-j];
}
return i;
}
inline QString sodepReadString(QIODevice &io)
{
qint32 length = sodepReadInt32(io);
QByteArray data = io.read(length);
return QString::fromUtf8(data);
}
#endif

View File

@ -0,0 +1,131 @@
/**
* This file is part of the KDE project
* Copyright (C) 2008 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 "sodepmessage.h"
#include <QtCore/QString>
#include "sodephelpers_p.h"
class SodepMessagePrivate
{
public:
SodepMessagePrivate() : id(0) {}
qint64 id;
QString resourcePath;
QString operationName;
SodepFault fault;
SodepValue data;
};
SodepMessage::SodepMessage()
: d(new SodepMessagePrivate)
{
}
SodepMessage::SodepMessage(const QString &resourcePath, const QString &operationName, qint64 id)
: d(new SodepMessagePrivate)
{
d->id = id;
d->resourcePath = resourcePath;
d->operationName = operationName;
}
SodepMessage::SodepMessage(const SodepMessage &other)
: d(new SodepMessagePrivate)
{
*d = *other.d;
}
SodepMessage::~SodepMessage()
{
delete d;
}
SodepMessage &SodepMessage::operator=(const SodepMessage &other)
{
*d = *other.d;
return *this;
}
qint64 SodepMessage::id() const
{
return d->id;
}
QString SodepMessage::resourcePath() const
{
return d->resourcePath;
}
QString SodepMessage::operationName() const
{
return d->operationName;
}
SodepFault SodepMessage::fault() const
{
return d->fault;
}
void SodepMessage::setFault(const SodepFault &fault)
{
d->fault = fault;
}
SodepValue SodepMessage::data() const
{
return d->data;
}
void SodepMessage::setData(const SodepValue &data)
{
d->data = data;
}
bool SodepMessage::isValid()
{
return !d->resourcePath.isEmpty() && !d->operationName.isEmpty();
}
void SodepMessage::writeTo(QIODevice &io) const
{
sodepWrite(io, d->id);
sodepWrite(io, d->resourcePath);
sodepWrite(io, d->operationName);
d->fault.writeTo(io);
d->data.writeTo(io);
}
SodepMessage SodepMessage::readFrom(QIODevice &io)
{
SodepMessage result;
result.d->id = sodepReadInt64(io);
result.d->resourcePath = sodepReadString(io);
result.d->operationName = sodepReadString(io);
result.d->fault = SodepFault::readFrom(io);
result.d->data = SodepValue::readFrom(io);
return result;
}

View File

@ -0,0 +1,61 @@
/**
* This file is part of the KDE project
* Copyright (C) 2008 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.
*/
#ifndef SODEPMESSAGE_H
#define SODEPMESSAGE_H
#include <sodepvalue.h>
#include <sodepfault.h>
class SodepMessagePrivate;
class Q_DECL_EXPORT SodepMessage
{
public:
SodepMessage();
explicit SodepMessage(const QString &resourcePath,
const QString &operationName,
qint64 id = 0);
SodepMessage(const SodepMessage &other);
~SodepMessage();
SodepMessage &operator=(const SodepMessage &other);
qint64 id() const;
QString resourcePath() const;
QString operationName() const;
SodepFault fault() const;
void setFault(const SodepFault &fault);
SodepValue data() const;
void setData(const SodepValue &data);
bool isValid();
void writeTo(QIODevice &io) const;
static SodepMessage readFrom(QIODevice &io);
private:
SodepMessagePrivate * const d;
};
#endif

View File

@ -0,0 +1,206 @@
/**
* This file is part of the KDE project
* Copyright (C) 2008 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 "sodepvalue.h"
#include <QtCore/QStringList>
#include <QtCore/QVariant>
#include "sodephelpers_p.h"
class SodepValuePrivate
{
public:
QVariant content;
QMap<QString, QList<SodepValue> > children;
};
SodepValue::SodepValue()
: d(new SodepValuePrivate)
{
}
SodepValue::SodepValue(const QString &content)
: d(new SodepValuePrivate)
{
d->content = content;
}
SodepValue::SodepValue(qint32 content)
: d(new SodepValuePrivate)
{
d->content = content;
}
SodepValue::SodepValue(double content)
: d(new SodepValuePrivate)
{
d->content = content;
}
SodepValue::SodepValue(const SodepValue &other)
: d(new SodepValuePrivate)
{
*d = *other.d;
}
SodepValue::~SodepValue()
{
delete d;
}
SodepValue &SodepValue::operator=(const SodepValue &other)
{
*d = *other.d;
return *this;
}
QStringList SodepValue::childrenNames() const
{
return d->children.keys();
}
QList<SodepValue> & SodepValue::children(const QString &name)
{
return d->children[name];
}
const QList<SodepValue> & SodepValue::children(const QString &name) const
{
return d->children[name];
}
QString SodepValue::toString() const
{
if (isString()) {
return d->content.toString();
} else {
return QString();
}
}
qint32 SodepValue::toInt() const
{
if (isInt()) {
return d->content.toInt();
} else {
return 0;
}
}
double SodepValue::toDouble() const
{
if (isDouble()) {
return d->content.toDouble();
} else {
return 0.0;
}
}
bool SodepValue::isString() const
{
return d->content.type()==QVariant::String;
}
bool SodepValue::isInt() const
{
return d->content.type()==QVariant::Int;
}
bool SodepValue::isDouble() const
{
return d->content.type()==QVariant::Double;
}
bool SodepValue::isValid() const
{
return isString() || isInt() || isDouble();
}
void SodepValue::writeTo(QIODevice &io) const
{
if (isDouble()) {
io.putChar(3);
sodepWrite(io, toDouble());
} else if (isInt()) {
io.putChar(2);
sodepWrite(io, toInt());
} else if (isString()) {
io.putChar(1);
sodepWrite(io, toString());
} else {
io.putChar(0);
}
sodepWrite(io, d->children.size());
foreach (const QString &name, d->children.keys()) {
sodepWrite(io, name);
QList<SodepValue> values = d->children[name];
qint32 valueCount = values.size();
sodepWrite(io, valueCount);
for (int j=0; j<valueCount; ++j) {
values[j].writeTo(io);
}
}
}
SodepValue SodepValue::readFrom(QIODevice &io)
{
SodepValue result;
char code;
io.getChar(&code);
switch(code) {
case 3: {
result = SodepValue(sodepReadDouble(io));
break;
}
case 2: {
result = SodepValue(sodepReadInt32(io));
break;
}
case 1: {
result = SodepValue(sodepReadString(io));
break;
}
default:
break;
}
qint32 childrenCount = sodepReadInt32(io);
for (int i=0; i<childrenCount; ++i) {
QString name = sodepReadString(io);
QList<SodepValue> values;
qint32 valueCount = sodepReadInt32(io);
for (int j=0; j<valueCount; ++j) {
values << readFrom(io);
}
result.d->children[name] = values;
}
return result;
}

View File

@ -0,0 +1,114 @@
/**
* This file is part of the KDE project
* Copyright (C) 2008 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 <QtCore/QObject>
#include <QtTest/QtTest>
#include <sodepmessage.h>
#include "sodeptesthelpers.h"
class SodepMessageTest : public QObject
{
Q_OBJECT
private slots:
void shouldVerifyInitialState()
{
SodepMessage m1("/foo", "bar");
SodepMessage m2("/pata/pata", "pon", 2);
QCOMPARE(m1.resourcePath(), QString("/foo"));
QCOMPARE(m1.operationName(), QString("bar"));
QCOMPARE(m1.id(), qint64(0));
QCOMPARE(m2.resourcePath(), QString("/pata/pata"));
QCOMPARE(m2.operationName(), QString("pon"));
QCOMPARE(m2.id(), qint64(2));
m1 = m2;
QCOMPARE(m1.resourcePath(), QString("/pata/pata"));
QCOMPARE(m1.operationName(), QString("pon"));
QCOMPARE(m2.id(), qint64(2));
}
void shouldBeSerializable_data()
{
SodepValue v(42);
QByteArray vSerial = QByteArray::fromHex("020000002A00000000");
SodepFault f("foo");
QByteArray fSerial = QByteArray::fromHex("0100000003")+QByteArray("foo")
+ QByteArray::fromHex("0000000000");
QTest::addColumn<SodepMessage>("original");
QTest::addColumn<QByteArray>("serialized");
QTest::newRow("no payload message") << SodepMessage("/pata", "pon")
<< QByteArray::fromHex("0000000000000000")
+ QByteArray::fromHex("00000005")+QByteArray("/pata")
+ QByteArray::fromHex("00000003")+QByteArray("pon")
+ QByteArray::fromHex("00")
+ QByteArray::fromHex("0000000000");
SodepMessage payload("/pata", "pon");
payload.setFault(f);
payload.setData(v);
QTest::newRow("payload message") << payload
<< QByteArray::fromHex("0000000000000000")
+ QByteArray::fromHex("00000005")+QByteArray("/pata")
+ QByteArray::fromHex("00000003")+QByteArray("pon")
+ fSerial
+ vSerial;
SodepMessage payloadId("/pata", "pon", 42);
payloadId.setFault(f);
payloadId.setData(v);
QTest::newRow("payload and id message") << payloadId
<< QByteArray::fromHex("000000000000002A")
+ QByteArray::fromHex("00000005")+QByteArray("/pata")
+ QByteArray::fromHex("00000003")+QByteArray("pon")
+ fSerial
+ vSerial;
}
void shouldBeSerializable()
{
QBuffer buffer;
QFETCH(SodepMessage, original);
QFETCH(QByteArray, serialized);
SodepMessage result;
buffer.open(QIODevice::WriteOnly);
original.writeTo(buffer);
buffer.close();
buffer.open(QIODevice::ReadOnly);
result = SodepMessage::readFrom(buffer);
buffer.close();
sodepCompare(result, original);
QCOMPARE(buffer.data(), serialized);
}
};
QTEST_MAIN(SodepMessageTest)
#include "sodepmessagetest.moc"