I really wonder what I had in mind when I put QStrings everywhere...

Of course SODEP says nothing about encoding so it's really byte arrays
that we send/receive.

MetaService being higher level I'll assume UTF8 encoding though (it's
apparently what it's using.


svn path=/branches/work/~ervin/qtjolie/; revision=975435
This commit is contained in:
Kevin Ottens 2009-05-30 08:44:34 +00:00
parent 214c0348a3
commit 177be5f6af
15 changed files with 107 additions and 110 deletions

View File

@ -3,7 +3,7 @@ include(KDE4Defaults)
include(MacroLibrary)
add_definitions(${QT_DEFINITIONS} ${KDE4_DEFINITIONS})
#include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR} ${KDE4_INCLUDES})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DQT_NO_CAST_FROM_ASCII -DQT_NO_CAST_TO_ASCII")
add_subdirectory(qtjolie)
add_subdirectory(includes)

View File

@ -20,7 +20,7 @@
#include "fault.h"
#include <QtCore/QString>
#include <QtCore/QByteArray>
#include "sodephelpers_p.h"
@ -30,7 +30,7 @@ namespace Jolie
class FaultPrivate
{
public:
QString name;
QByteArray name;
Value data;
};
@ -44,7 +44,7 @@ Fault::Fault()
}
Fault::Fault(const QString &name, const Value &data)
Fault::Fault(const QByteArray &name, const Value &data)
: d(new FaultPrivate)
{
d->name = name;
@ -69,7 +69,7 @@ Fault &Fault::operator=(const Fault &other)
return *this;
}
QString Fault::name() const
QByteArray Fault::name() const
{
return d->name;
}

View File

@ -31,7 +31,7 @@ class Q_DECL_EXPORT Fault
{
public:
Fault();
explicit Fault(const QString &name, const Value &data = Value());
explicit Fault(const QByteArray &name, const Value &data = Value());
Fault(const Fault &other);
@ -39,7 +39,7 @@ public:
Fault &operator=(const Fault &other);
QString name() const;
QByteArray name() const;
Value data() const;
bool isValid() const;

View File

@ -20,7 +20,7 @@
#include "message.h"
#include <QtCore/QString>
#include <QtCore/QByteArray>
#include "sodephelpers_p.h"
@ -33,8 +33,8 @@ public:
MessagePrivate() : id(0) {}
qint64 id;
QString resourcePath;
QString operationName;
QByteArray resourcePath;
QByteArray operationName;
Fault fault;
Value data;
};
@ -48,7 +48,7 @@ Message::Message()
{
}
Message::Message(const QString &resourcePath, const QString &operationName, qint64 id)
Message::Message(const QByteArray &resourcePath, const QByteArray &operationName, qint64 id)
: d(new MessagePrivate)
{
static qint64 lastId = 0;
@ -85,12 +85,12 @@ qint64 Message::id() const
return d->id;
}
QString Message::resourcePath() const
QByteArray Message::resourcePath() const
{
return d->resourcePath;
}
QString Message::operationName() const
QByteArray Message::operationName() const
{
return d->operationName;
}

View File

@ -32,9 +32,9 @@ class Q_DECL_EXPORT Message
{
public:
Message();
explicit Message(const QString &resourcePath,
const QString &operationName,
qint64 id = 0);
explicit Message(const QByteArray &resourcePath,
const QByteArray &operationName,
qint64 id = 0);
Message(const Message &other);
~Message();
@ -42,8 +42,8 @@ public:
qint64 id() const;
QString resourcePath() const;
QString operationName() const;
QByteArray resourcePath() const;
QByteArray operationName() const;
Fault fault() const;
void setFault(const Fault &fault);

View File

@ -50,13 +50,13 @@ MetaService::~MetaService()
bool MetaService::start()
{
d->metaserviceProcess.start("metaservice");
d->metaserviceProcess.start(QString::fromUtf8("metaservice"));
return d->metaserviceProcess.waitForStarted();
}
bool MetaService::stop()
{
Client client("localhost", 9000);
Client client(QString::fromUtf8("localhost"), 9000);
Message message("/", "shutdown");
client.callNoReply(message);
return d->metaserviceProcess.waitForFinished(30000);
@ -69,20 +69,20 @@ bool MetaService::isRunning() const
QString MetaService::loadService(const QString &name, const QString &fileName)
{
Client client("localhost", 9000);
Client client(QString::fromUtf8("localhost"), 9000);
Message message("/", "loadEmbeddedJolieService");
Value value;
value.children("resourcePrefix") << Value(name);
value.children("filepath") << Value(fileName);
value.children("resourcePrefix") << Value(name.toUtf8());
value.children("filepath") << Value(fileName.toUtf8());
message.setData(value);
Message reply = client.call(message);
return reply.data().toString();
return QString::fromUtf8(reply.data().toByteArray());
}
QStringList MetaService::loadedServices() const
{
Client client("localhost", 9000);
Client client(QString::fromUtf8("localhost"), 9000);
Message message("/", "getServices");
Message reply = client.call(message);
@ -90,7 +90,7 @@ QStringList MetaService::loadedServices() const
QStringList result;
foreach (const Value &service, services) {
result << service.children("resourceName").first().toString();
result << QString::fromUtf8(service.children("resourceName").first().toByteArray());
}
return result;
@ -98,9 +98,9 @@ QStringList MetaService::loadedServices() const
void MetaService::unloadService(const QString &name)
{
Client client("localhost", 9000);
Client client(QString::fromUtf8("localhost"), 9000);
Message message("/", "unloadEmbeddedService");
message.setData(Value(name));
message.setData(Value(name.toUtf8()));
client.call(message);
}

View File

@ -22,8 +22,7 @@
#define QTJOLIE_SODEPHELPERS_P_H
#include <QtCore/QIODevice>
#include <QtCore/QString>
#include <QtCore/QStringList>
#include <QtCore/QByteArray>
#include "value.h"
#include "fault.h"
@ -68,11 +67,10 @@ inline void sodepWrite(QIODevice &io, qint64 value)
io.write(out, 8);
}
inline void sodepWrite(QIODevice &io, const QString &value)
inline void sodepWrite(QIODevice &io, const QByteArray &value)
{
QByteArray data = value.toUtf8();
sodepWrite(io, data.size());
io.write(data);
sodepWrite(io, value.size());
io.write(value);
}
inline void sodepWrite(QIODevice &io, const Value &value)
@ -83,16 +81,16 @@ inline void sodepWrite(QIODevice &io, const Value &value)
} else if (value.isInt()) {
io.putChar(2);
sodepWrite(io, value.toInt());
} else if (value.isString()) {
} else if (value.isByteArray()) {
io.putChar(1);
sodepWrite(io, value.toString());
sodepWrite(io, value.toByteArray());
} else {
io.putChar(0);
}
sodepWrite(io, value.childrenNames().size());
foreach (const QString &name, value.childrenNames()) {
foreach (const QByteArray &name, value.childrenNames()) {
sodepWrite(io, name);
QList<Value> values = value.children(name);
@ -183,7 +181,7 @@ inline qint64 sodepReadInt64(QIODevice &io)
return i;
}
inline QString sodepReadString(QIODevice &io)
inline QByteArray sodepReadByteArray(QIODevice &io)
{
qint32 length = sodepReadInt32(io);
@ -195,7 +193,7 @@ inline QString sodepReadString(QIODevice &io)
io.read(data, length);
data[length] = '\0';
QString result = QString::fromUtf8(data);
QByteArray result(data);
delete[] data;
return result;
@ -222,7 +220,7 @@ inline Value sodepReadValue(QIODevice &io)
break;
}
case 1: {
result = Value(sodepReadString(io));
result = Value(sodepReadByteArray(io));
break;
}
default:
@ -232,7 +230,7 @@ inline Value sodepReadValue(QIODevice &io)
qint32 childrenCount = sodepReadInt32(io);
for (int i=0; i<childrenCount; ++i) {
QString name = sodepReadString(io);
QByteArray name = sodepReadByteArray(io);
qint32 valueCount = sodepReadInt32(io);
for (int j=0; j<valueCount; ++j) {
@ -256,7 +254,7 @@ inline Fault sodepReadFault(QIODevice &io)
return Fault();
}
QString name = sodepReadString(io);
QByteArray name = sodepReadByteArray(io);
Value data = sodepReadValue(io);
return Fault(name, data);
@ -265,8 +263,8 @@ inline Fault sodepReadFault(QIODevice &io)
inline Message sodepReadMessage(QIODevice &io)
{
qint64 id = sodepReadInt64(io);
QString resourcePath = sodepReadString(io);
QString operationName = sodepReadString(io);
QByteArray resourcePath = sodepReadByteArray(io);
QByteArray operationName = sodepReadByteArray(io);
Message result(resourcePath, operationName, id);

View File

@ -20,7 +20,6 @@
#include "value.h"
#include <QtCore/QStringList>
#include <QtCore/QVariant>
#include "sodephelpers_p.h"
@ -32,7 +31,7 @@ class ValuePrivate
{
public:
QVariant content;
QMap<QString, QList<Value> > children;
QMap<QByteArray, QList<Value> > children;
};
} // namespace Jolie
@ -44,7 +43,7 @@ Value::Value()
{
}
Value::Value(const QString &content)
Value::Value(const QByteArray &content)
: d(new ValuePrivate)
{
d->content = content;
@ -80,27 +79,27 @@ Value &Value::operator=(const Value &other)
return *this;
}
QStringList Value::childrenNames() const
QList<QByteArray> Value::childrenNames() const
{
return d->children.keys();
}
QList<Value> & Value::children(const QString &name)
QList<Value> & Value::children(const QByteArray &name)
{
return d->children[name];
}
const QList<Value> & Value::children(const QString &name) const
const QList<Value> & Value::children(const QByteArray &name) const
{
return d->children[name];
}
QString Value::toString() const
QByteArray Value::toByteArray() const
{
if (isString()) {
return d->content.toString();
if (isByteArray()) {
return d->content.toByteArray();
} else {
return QString();
return QByteArray();
}
}
@ -122,9 +121,9 @@ double Value::toDouble() const
}
}
bool Value::isString() const
bool Value::isByteArray() const
{
return d->content.type()==QVariant::String;
return d->content.type()==QVariant::ByteArray;
}
bool Value::isInt() const
@ -139,6 +138,6 @@ bool Value::isDouble() const
bool Value::isValid() const
{
return isString() || isInt() || isDouble();
return isByteArray() || isInt() || isDouble();
}

View File

@ -33,7 +33,7 @@ class Q_DECL_EXPORT Value
public:
Value();
explicit Value(const QString &content);
explicit Value(const QByteArray &content);
explicit Value(qint32 content);
explicit Value(double content);
@ -43,15 +43,15 @@ public:
Value &operator=(const Value &other);
QStringList childrenNames() const;
QList<Value> &children(const QString &name);
const QList<Value> &children(const QString &name) const;
QList<QByteArray> childrenNames() const;
QList<Value> &children(const QByteArray &name);
const QList<Value> &children(const QByteArray &name) const;
QString toString() const;
QByteArray toByteArray() const;
qint32 toInt() const;
double toDouble() const;
bool isString() const;
bool isByteArray() const;
bool isInt() const;
bool isDouble() const;

View File

@ -35,7 +35,7 @@ private slots:
{
Fault f;
QCOMPARE(f.name(), QString());
QCOMPARE(f.name(), QByteArray());
QVERIFY(!f.data().isValid());
QVERIFY(!f.isValid());
@ -44,16 +44,16 @@ private slots:
{
Fault f1("blup"), f2("blop", Value(42));
QCOMPARE(f1.name(), QString("blup"));
QCOMPARE(f1.name(), QByteArray("blup"));
QVERIFY(!f1.data().isValid());
QCOMPARE(f2.name(), QString("blop"));
QCOMPARE(f2.name(), QByteArray("blop"));
QVERIFY(f2.data().isValid());
QCOMPARE(f2.data().toInt(), 42);
f1 = f2;
QCOMPARE(f1.name(), QString("blop"));
QCOMPARE(f1.name(), QByteArray("blop"));
QVERIFY(f1.data().isValid());
QCOMPARE(f1.data().toInt(), 42);
}

View File

@ -34,19 +34,19 @@ inline void sodepCompare(const Jolie::Value &v1, const Jolie::Value &v2)
{
QCOMPARE(v1.isValid(), v2.isValid());
QCOMPARE(v1.isString(), v2.isString());
QCOMPARE(v1.isByteArray(), v2.isByteArray());
QCOMPARE(v1.isInt(), v2.isInt());
QCOMPARE(v1.isDouble(), v2.isDouble());
QCOMPARE(v1.toString(), v2.toString());
QCOMPARE(v1.toByteArray(), v2.toByteArray());
QCOMPARE(v1.toInt(), v2.toInt());
QCOMPARE(v1.toDouble(), v2.toDouble());
QStringList v1Names = v1.childrenNames();
QStringList v2Names = v2.childrenNames();
QList<QByteArray> v1Names = v1.childrenNames();
QList<QByteArray> v2Names = v2.childrenNames();
QCOMPARE(v1Names, v2Names);
foreach (const QString &name, v1Names) {
foreach (const QByteArray &name, v1Names) {
QList<Jolie::Value> v1Values = v1.children(name);
QList<Jolie::Value> v2Values = v2.children(name);

View File

@ -36,18 +36,18 @@ private slots:
Message m1("/foo", "bar", 1);
Message m2("/pata/pata", "pon", 2);
QCOMPARE(m1.resourcePath(), QString("/foo"));
QCOMPARE(m1.operationName(), QString("bar"));
QCOMPARE(m1.resourcePath(), QByteArray("/foo"));
QCOMPARE(m1.operationName(), QByteArray("bar"));
QCOMPARE(m1.id(), qint64(1));
QCOMPARE(m2.resourcePath(), QString("/pata/pata"));
QCOMPARE(m2.operationName(), QString("pon"));
QCOMPARE(m2.resourcePath(), QByteArray("/pata/pata"));
QCOMPARE(m2.operationName(), QByteArray("pon"));
QCOMPARE(m2.id(), qint64(2));
m1 = m2;
QCOMPARE(m1.resourcePath(), QString("/pata/pata"));
QCOMPARE(m1.operationName(), QString("pon"));
QCOMPARE(m1.resourcePath(), QByteArray("/pata/pata"));
QCOMPARE(m1.operationName(), QByteArray("pon"));
QCOMPARE(m2.id(), qint64(2));
}

View File

@ -36,15 +36,15 @@ using namespace Jolie;
void dump(const Value &value, int level)
{
QString indent;
QByteArray indent;
while (level>0) {
indent+=" ";
level--;
}
qDebug() << (indent+value.toString()) << value.toInt() << value.toDouble();
foreach (const QString &name, value.childrenNames()) {
qDebug() << (indent+value.toByteArray()) << value.toInt() << value.toDouble();
foreach (const QByteArray &name, value.childrenNames()) {
QList<Value> children = value.children(name);
qDebug() << (indent+"Children:") << name;
foreach (const Value &child, children) {
@ -83,7 +83,7 @@ private slots:
QVERIFY2(m_metaService.start(), "Looks like you don't have Jolie's metaservice command");
QTest::qWait(1000);
m_client = new Client("localhost", 9000);
m_client = new Client(QString::fromUtf8("localhost"), 9000);
}
void cleanupTestCase()
@ -97,8 +97,8 @@ private slots:
QTest::addColumn<QString>("resourcePrefix");
QTest::addColumn<QString>("fileName");
QTest::newRow("printer service") << "Printer" << "printer.ol";
QTest::newRow("math service") << "Math" << "math.ol";
QTest::newRow("printer service") << QString::fromUtf8("Printer") << QString::fromUtf8("printer.ol");
QTest::newRow("math service") << QString::fromUtf8("Math") << QString::fromUtf8("math.ol");
}
void shouldLoadService()
@ -106,32 +106,32 @@ private slots:
QFETCH(QString, resourcePrefix);
QFETCH(QString, fileName);
QCOMPARE(m_metaService.loadService(resourcePrefix, QString(DATA_DIR"/")+fileName),
QCOMPARE(m_metaService.loadService(resourcePrefix, QString::fromUtf8(DATA_DIR"/")+fileName),
resourcePrefix);
}
void shouldListServices()
{
QStringList expected;
expected << "Math" << "Printer";
expected << QString::fromUtf8("Math") << QString::fromUtf8("Printer");
QCOMPARE(m_metaService.loadedServices(), expected);
}
void shouldPlaceServiceCalls_data()
{
QTest::addColumn<QString>("path");
QTest::addColumn<QString>("method");
QTest::addColumn<QByteArray>("path");
QTest::addColumn<QByteArray>("method");
QTest::addColumn<Value>("data");
QTest::addColumn<Value>("replyData");
QTest::newRow("printer service") << "/Printer" << "printInput" << Value("Patapatapon!") << Value("success");
QTest::newRow("math service") << "/Math" << "twice" << Value(10.5) << Value(21.0);
QTest::newRow("printer service") << QByteArray("/Printer") << QByteArray("printInput") << Value("Patapatapon!") << Value("success");
QTest::newRow("math service") << QByteArray("/Math") << QByteArray("twice") << Value(10.5) << Value(21.0);
}
void shouldPlaceServiceCalls()
{
QFETCH(QString, path);
QFETCH(QString, method);
QFETCH(QByteArray, path);
QFETCH(QByteArray, method);
QFETCH(Value, data);
QFETCH(Value, replyData);
@ -150,8 +150,8 @@ private slots:
{
QTest::addColumn<QString>("serviceName");
QTest::newRow("printer service") << "PrinterService";
QTest::newRow("math service") << "MathService";
QTest::newRow("printer service") << QString::fromUtf8("PrinterService");
QTest::newRow("math service") << QString::fromUtf8("MathService");
}
void shouldUnloadService()

View File

@ -42,12 +42,12 @@ public:
layout()->addWidget(m_lineEdit);
QPushButton *button = new QPushButton(this);
button->setText("SEND");
button->setText(QString::fromUtf8("SEND"));
layout()->addWidget(button);
connect(button, SIGNAL(clicked()),
this, SLOT(sendMessage()));
m_socket.connectToHost("localhost", 10000);
m_socket.connectToHost(QString::fromUtf8("localhost"), 10000);
if (!m_socket.waitForConnected(10000)) {
qDebug("Failed to connect!");
return;
@ -59,7 +59,7 @@ private slots:
void sendMessage()
{
Message message("/", "printInput");
message.setData(Value(m_lineEdit->text()));
message.setData(Value(m_lineEdit->text().toUtf8()));
sodepWrite(m_socket, message);
qDebug("Message sent:");
@ -67,7 +67,7 @@ private slots:
buffer.open(QIODevice::WriteOnly);
sodepWrite(buffer, message);
buffer.close();
qDebug(buffer.data().toHex());
qDebug("%s", buffer.data().toHex().constData());
qDebug("Message received:");
buffer.setData(QByteArray());
@ -75,7 +75,7 @@ private slots:
message = sodepReadMessage(m_socket);
sodepWrite(buffer, message);
buffer.close();
qDebug(buffer.data().toHex());
qDebug("%s", buffer.data().toHex().constData());
}
private:
QLineEdit *m_lineEdit;

View File

@ -38,11 +38,11 @@ private slots:
QCOMPARE(v.toInt(), 0);
QCOMPARE(v.toDouble(), 0.0);
QCOMPARE(v.toString(), QString());
QCOMPARE(v.toByteArray(), QByteArray());
QVERIFY(!v.isValid());
QVERIFY(!v.isString());
QVERIFY(!v.isByteArray());
QVERIFY(!v.isInt());
QVERIFY(!v.isDouble());
}
@ -63,7 +63,7 @@ private slots:
QVERIFY(v2.isInt());
QCOMPARE(v2.toDouble(), 0.0);
QCOMPARE(v2.toString(), QString());
QCOMPARE(v2.toByteArray(), QByteArray());
}
void shouldRespectDoubleValues()
@ -82,23 +82,23 @@ private slots:
QVERIFY(v2.isDouble());
QCOMPARE(v2.toInt(), 0);
QCOMPARE(v2.toString(), QString());
QCOMPARE(v2.toByteArray(), QByteArray());
}
void shouldRespectStringValues()
void shouldRespectByteArrayValues()
{
Value v1("42"), v2;
QCOMPARE(v1.toString(), QString("42"));
QCOMPARE(v2.toString(), QString());
QCOMPARE(v1.toByteArray(), QByteArray("42"));
QCOMPARE(v2.toByteArray(), QByteArray());
QVERIFY(v1.isString());
QVERIFY(!v2.isString());
QVERIFY(v1.isByteArray());
QVERIFY(!v2.isByteArray());
v2 = v1;
QCOMPARE(v2.toString(), QString("42"));
QVERIFY(v2.isString());
QCOMPARE(v2.toByteArray(), QByteArray("42"));
QVERIFY(v2.isByteArray());
QCOMPARE(v2.toInt(), 0);
QCOMPARE(v2.toDouble(), 0.0);