2008-11-05 19:48:16 +01:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2009-04-25 15:50:35 +02:00
|
|
|
#ifndef QTJOLIE_SODEPHELPERS_P_H
|
|
|
|
#define QTJOLIE_SODEPHELPERS_P_H
|
2008-11-05 19:48:16 +01:00
|
|
|
|
|
|
|
#include <QtCore/QIODevice>
|
2009-05-30 10:44:34 +02:00
|
|
|
#include <QtCore/QByteArray>
|
2009-02-08 16:35:56 +01:00
|
|
|
|
2009-04-25 15:50:35 +02:00
|
|
|
#include "value.h"
|
|
|
|
#include "fault.h"
|
|
|
|
#include "message.h"
|
|
|
|
|
|
|
|
namespace Jolie
|
|
|
|
{
|
2008-11-05 19:48:16 +01:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2009-05-30 10:44:34 +02:00
|
|
|
inline void sodepWrite(QIODevice &io, const QByteArray &value)
|
2008-11-05 19:48:16 +01:00
|
|
|
{
|
2009-05-30 10:44:34 +02:00
|
|
|
sodepWrite(io, value.size());
|
|
|
|
io.write(value);
|
2008-11-05 19:48:16 +01:00
|
|
|
}
|
|
|
|
|
2009-04-25 15:50:35 +02:00
|
|
|
inline void sodepWrite(QIODevice &io, const Value &value)
|
2009-02-08 16:35:56 +01:00
|
|
|
{
|
|
|
|
if (value.isDouble()) {
|
|
|
|
io.putChar(3);
|
|
|
|
sodepWrite(io, value.toDouble());
|
|
|
|
} else if (value.isInt()) {
|
|
|
|
io.putChar(2);
|
|
|
|
sodepWrite(io, value.toInt());
|
2009-05-30 10:44:34 +02:00
|
|
|
} else if (value.isByteArray()) {
|
2009-02-08 16:35:56 +01:00
|
|
|
io.putChar(1);
|
2009-05-30 10:44:34 +02:00
|
|
|
sodepWrite(io, value.toByteArray());
|
2009-02-08 16:35:56 +01:00
|
|
|
} else {
|
|
|
|
io.putChar(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
sodepWrite(io, value.childrenNames().size());
|
|
|
|
|
2009-05-30 10:44:34 +02:00
|
|
|
foreach (const QByteArray &name, value.childrenNames()) {
|
2009-02-08 16:35:56 +01:00
|
|
|
sodepWrite(io, name);
|
|
|
|
|
2009-04-25 15:50:35 +02:00
|
|
|
QList<Value> values = value.children(name);
|
2009-02-08 16:35:56 +01:00
|
|
|
qint32 valueCount = values.size();
|
|
|
|
sodepWrite(io, valueCount);
|
|
|
|
for (int j=0; j<valueCount; ++j) {
|
|
|
|
sodepWrite(io, values[j]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2009-04-25 15:50:35 +02:00
|
|
|
inline void sodepWrite(QIODevice &io, const Fault &fault)
|
2009-02-08 16:35:56 +01:00
|
|
|
{
|
|
|
|
if (!fault.isValid()) {
|
|
|
|
io.putChar(0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
io.putChar(1);
|
|
|
|
sodepWrite(io, fault.name());
|
|
|
|
sodepWrite(io, fault.data());
|
|
|
|
}
|
|
|
|
|
2009-04-25 15:50:35 +02:00
|
|
|
inline void sodepWrite(QIODevice &io, const Message &message)
|
2009-02-08 16:35:56 +01:00
|
|
|
{
|
|
|
|
sodepWrite(io, message.id());
|
|
|
|
sodepWrite(io, message.resourcePath());
|
|
|
|
sodepWrite(io, message.operationName());
|
|
|
|
sodepWrite(io, message.fault());
|
|
|
|
sodepWrite(io, message.data());
|
|
|
|
}
|
|
|
|
|
2008-11-05 19:48:16 +01:00
|
|
|
inline double sodepReadDouble(QIODevice &io)
|
|
|
|
{
|
2009-02-08 18:31:13 +01:00
|
|
|
while (io.bytesAvailable()<8) {
|
|
|
|
io.waitForReadyRead(-1);
|
|
|
|
}
|
|
|
|
|
2008-11-05 19:48:16 +01:00
|
|
|
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)
|
|
|
|
{
|
2009-02-08 18:31:13 +01:00
|
|
|
while (io.bytesAvailable()<4) {
|
|
|
|
io.waitForReadyRead(-1);
|
|
|
|
}
|
|
|
|
|
2008-11-05 19:48:16 +01:00
|
|
|
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)
|
|
|
|
{
|
2009-02-08 18:31:13 +01:00
|
|
|
while (io.bytesAvailable()<8) {
|
|
|
|
io.waitForReadyRead(-1);
|
|
|
|
}
|
|
|
|
|
2008-11-05 19:48:16 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2009-05-30 10:44:34 +02:00
|
|
|
inline QByteArray sodepReadByteArray(QIODevice &io)
|
2008-11-05 19:48:16 +01:00
|
|
|
{
|
|
|
|
qint32 length = sodepReadInt32(io);
|
2009-02-08 16:35:56 +01:00
|
|
|
|
2009-02-08 18:31:13 +01:00
|
|
|
while (io.bytesAvailable()<length) {
|
|
|
|
io.waitForReadyRead(-1);
|
|
|
|
}
|
|
|
|
|
2009-02-08 16:35:56 +01:00
|
|
|
char *data = new char[length+1];
|
|
|
|
io.read(data, length);
|
|
|
|
|
2009-09-02 04:27:16 +02:00
|
|
|
QByteArray result(data, length);
|
2009-02-08 16:35:56 +01:00
|
|
|
delete[] data;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2009-04-25 15:50:35 +02:00
|
|
|
inline Value sodepReadValue(QIODevice &io)
|
2009-02-08 16:35:56 +01:00
|
|
|
{
|
2009-04-25 15:50:35 +02:00
|
|
|
Value result;
|
2009-02-08 16:35:56 +01:00
|
|
|
|
2009-02-08 18:31:13 +01:00
|
|
|
while (io.bytesAvailable()<1) {
|
|
|
|
io.waitForReadyRead(-1);
|
|
|
|
}
|
|
|
|
|
2009-02-08 16:35:56 +01:00
|
|
|
char code;
|
|
|
|
io.getChar(&code);
|
|
|
|
|
|
|
|
switch(code) {
|
|
|
|
case 3: {
|
2009-04-25 15:50:35 +02:00
|
|
|
result = Value(sodepReadDouble(io));
|
2009-02-08 16:35:56 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 2: {
|
2009-04-25 15:50:35 +02:00
|
|
|
result = Value(sodepReadInt32(io));
|
2009-02-08 16:35:56 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 1: {
|
2009-05-30 10:44:34 +02:00
|
|
|
result = Value(sodepReadByteArray(io));
|
2009-02-08 16:35:56 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
qint32 childrenCount = sodepReadInt32(io);
|
|
|
|
|
|
|
|
for (int i=0; i<childrenCount; ++i) {
|
2009-05-30 10:44:34 +02:00
|
|
|
QByteArray name = sodepReadByteArray(io);
|
2009-02-08 16:35:56 +01:00
|
|
|
|
|
|
|
qint32 valueCount = sodepReadInt32(io);
|
|
|
|
for (int j=0; j<valueCount; ++j) {
|
2009-02-09 12:28:34 +01:00
|
|
|
result.children(name) << sodepReadValue(io);
|
2009-02-08 16:35:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2008-11-05 19:48:16 +01:00
|
|
|
}
|
|
|
|
|
2009-04-25 15:50:35 +02:00
|
|
|
inline Fault sodepReadFault(QIODevice &io)
|
2009-02-08 16:35:56 +01:00
|
|
|
{
|
2009-02-08 18:31:13 +01:00
|
|
|
while (io.bytesAvailable()<1) {
|
|
|
|
io.waitForReadyRead(-1);
|
|
|
|
}
|
|
|
|
|
2009-02-08 16:35:56 +01:00
|
|
|
char code;
|
|
|
|
io.getChar(&code);
|
|
|
|
|
|
|
|
if (code!=1) {
|
2009-04-25 15:50:35 +02:00
|
|
|
return Fault();
|
2009-02-08 16:35:56 +01:00
|
|
|
}
|
|
|
|
|
2009-05-30 10:44:34 +02:00
|
|
|
QByteArray name = sodepReadByteArray(io);
|
2009-04-25 15:50:35 +02:00
|
|
|
Value data = sodepReadValue(io);
|
2009-02-08 16:35:56 +01:00
|
|
|
|
2009-04-25 15:50:35 +02:00
|
|
|
return Fault(name, data);
|
2009-02-08 16:35:56 +01:00
|
|
|
}
|
|
|
|
|
2009-04-25 15:50:35 +02:00
|
|
|
inline Message sodepReadMessage(QIODevice &io)
|
2009-02-08 16:35:56 +01:00
|
|
|
{
|
|
|
|
qint64 id = sodepReadInt64(io);
|
2009-05-30 10:44:34 +02:00
|
|
|
QByteArray resourcePath = sodepReadByteArray(io);
|
|
|
|
QByteArray operationName = sodepReadByteArray(io);
|
2009-02-08 16:35:56 +01:00
|
|
|
|
2009-04-25 15:50:35 +02:00
|
|
|
Message result(resourcePath, operationName, id);
|
2009-02-08 16:35:56 +01:00
|
|
|
|
|
|
|
result.setFault(sodepReadFault(io));
|
|
|
|
result.setData(sodepReadValue(io));
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2008-11-05 19:48:16 +01:00
|
|
|
|
2009-04-25 15:50:35 +02:00
|
|
|
} // namespace Jolie
|
|
|
|
|
2008-11-05 19:48:16 +01:00
|
|
|
#endif
|
2009-04-25 15:50:35 +02:00
|
|
|
|