1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-09 00:00:00 +01:00

added new JsonBuffer class which will obsolete struct buffer

This commit is contained in:
Steffen Vogel 2018-08-24 23:23:01 +02:00
parent 08ed946831
commit 5c023df352
6 changed files with 251 additions and 1 deletions

View file

@ -0,0 +1,58 @@
/** A simple buffer for encoding streamed JSON messages.
*
* @file
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
* @copyright 2017-2018, Institute for Automation of Complex Power Systems, EONERC
* @license GNU General Public License (version 3)
*
* VILLAScommon
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*********************************************************************************/
#pragma once
#include <vector>
#include <stdlib.h>
#include <jansson.h>
namespace villas {
class Buffer : public std::vector<char> {
public:
void append(const char *data, size_t len)
{
insert(end(), data, data + len);
}
};
class JsonBuffer : public Buffer
{
protected:
static int callback(const char *data, size_t len, void *ctx);
public:
/** Encode JSON document /p j and append it to the buffer */
int encode(json_t *j);
/** Decode JSON document from the beginning of the buffer */
json_t * decode();
};
} // namespace villas

View file

@ -24,6 +24,7 @@ add_library(villas-common SHARED
advio.c
bitset.c
buffer.c
json_buffer.cpp
compat.c
crypt.c
hash_table.c

View file

@ -1,6 +1,5 @@
/** A simple growing buffer.
*
* @file
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
* @copyright 2017-2018, Institute for Automation of Complex Power Systems, EONERC
* @license GNU General Public License (version 3)

View file

@ -0,0 +1,56 @@
/** A simple buffer for encoding streamed JSON messages.
*
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
* @copyright 2017-2018, Institute for Automation of Complex Power Systems, EONERC
* @license GNU General Public License (version 3)
*
* VILLAScommon
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*********************************************************************************/
#include <villas/compat.h>
#include <villas/json_buffer.hpp>
using namespace villas;
json_t * JsonBuffer::decode()
{
json_t *j;
json_error_t err;
j = json_loadb(data(), size(), JSON_DISABLE_EOF_CHECK, &err);
if (!j)
return nullptr;
/* Remove decoded JSON document from beginning */
erase(begin(), begin() + err.position);
return j;
}
int JsonBuffer::encode(json_t *j)
{
return json_dump_callback(j, callback, this, 0);
}
int JsonBuffer::callback(const char *data, size_t len, void *ctx)
{
Buffer *b = static_cast<Buffer *>(ctx);
/* Append junk of JSON to buffer */
b->insert(b->end(), &data[0], &data[len]);
return 0;
}

View file

@ -25,6 +25,7 @@ add_executable(unit-tests-common
logging.cpp
advio.cpp
json_buffer.cpp
bitset.cpp
graph.cpp
hash_table.cpp

View file

@ -0,0 +1,135 @@
/** Unit tests for buffer
*
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
* @copyright 2017-2018, Institute for Automation of Complex Power Systems, EONERC
* @license GNU General Public License (version 3)
*
* VILLAScommon
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*********************************************************************************/
#include <cstdlib>
#include <ctime>
#include <criterion/criterion.h>
#include <jansson.h>
#include <villas/json_buffer.hpp>
using namespace villas;
void init_logging();
using villas::Buffer;
TestSuite(buffer,
.description = "Buffer datastructure",
.init = init_logging
);
Test(json_buffer, decode)
{
JsonBuffer buf;
json_t *j;
json_t *k;
const char *e = "{\"id\": \"5a786626-fbc6-4c04-98c2-48027e68c2fa\"}";
size_t len = strlen(e);
buf.insert(buf.begin(), e, e+len);
k = json_loads(e, 0, NULL);
cr_assert_not_null(k);
j = buf.decode();
cr_assert_not_null(j);
cr_assert(json_equal(j, k));
}
Test(json_buffer, encode)
{
int ret;
JsonBuffer buf;
json_t *k;
const char *e = "{\"id\": \"5a786626-fbc6-4c04-98c2-48027e68c2fa\"}";
k = json_loads(e, 0, NULL);
cr_assert_not_null(k);
ret = buf.encode(k);
cr_assert_eq(ret, 0);
char *f = buf.data();
cr_assert_not_null(f);
cr_assert_str_eq(e, f);
json_decref(k);
}
Test(json_buffer, encode_decode)
{
int ret;
JsonBuffer buf;
json_t *k;
json_t *j;
const char *e = "{\"id\": \"5a786626-fbc6-4c04-98c2-48027e68c2fa\"}";
k = json_loads(e, 0, NULL);
cr_assert_not_null(k);
ret = buf.encode(k);
cr_assert_eq(ret, 0);
j = buf.decode();
cr_assert_not_null(j);
cr_assert(json_equal(j, k));
json_decref(j);
json_decref(k);
}
Test(json_buffer, multiple)
{
int ret;
const int N = 100;
JsonBuffer buf;
json_t *k[N];
json_t *j[N];
std::srand(std::time(nullptr));
for (int i = 0; i < N; i++) {
k[i] = json_pack("{ s: i }", "id", std::rand());
cr_assert_not_null(k[i]);
ret = buf.encode(k[i]);
cr_assert_eq(ret, 0);
}
for (int i = 0; i < N; i++) {
j[i] = buf.decode();
cr_assert_not_null(j[i]);
cr_assert(json_equal(k[i], j[i]));
json_decref(k[i]);
json_decref(j[i]);
}
}