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

buffer: merge two buffer implementations

This commit is contained in:
Steffen Vogel 2020-10-20 22:13:35 +02:00
parent e81785b2b6
commit 1dfbc291c7
8 changed files with 55 additions and 170 deletions

View file

@ -1,4 +1,4 @@
/** A simple growing buffer.
/** A simple buffer for encoding streamed JSON messages.
*
* @file
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
@ -23,29 +23,38 @@
#pragma once
#include <cstdlib>
#include <vector>
#include <cstdlib>
#include <jansson.h>
#include <villas/common.hpp>
namespace villas {
class Buffer {
class Buffer : public std::vector<char> {
protected:
static int callback(const char *data, size_t len, void *ctx);
public:
std::vector<char> buf;
Buffer(const char *buf, size_type len) :
std::vector<char>(buf, buf+len)
{ }
Buffer(size_t size);
Buffer(size_type count = 0) :
std::vector<char>(count, 0)
{ }
void clear();
/** Encode JSON document /p j and append it to the buffer */
int encode(json_t *j, int flags = 0);
int append(const char *data, size_t len);
/** Decode JSON document from the beginning of the buffer */
json_t * decode();
int parseJson(json_t **j);
int appendJson(json_t *j, int flags = 0);
void append(const char *data, size_t len)
{
insert(end(), data, data + len);
}
};
} /* namespace villas */

View file

@ -1,53 +0,0 @@
/** A simple buffer for encoding streamed JSON messages.
*
* @file
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
* @copyright 2014-2020, 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 <cstdlib>
#include <jansson.h>
namespace villas {
class JsonBuffer : public std::vector<char>
{
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, int flags = 0);
/** Decode JSON document from the beginning of the buffer */
json_t * decode();
void append(const char *data, size_t len)
{
insert(end(), data, data + len);
}
};
} /* namespace villas */

View file

@ -23,7 +23,6 @@
add_library(villas-common SHARED
advio.cpp
buffer.cpp
json_buffer.cpp
compat.cpp
hist.cpp
dsp/pid.cpp

View file

@ -245,7 +245,7 @@ AFILE * afopen(const char *uri, const char *mode)
/* Setup libcurl handle */
curl_easy_setopt(af->curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(af->curl, CURLOPT_UPLOAD, 0L);
curl_easy_setopt(af->curl, CURLOPT_USERAGENT, USER_AGENT);
curl_easy_setopt(af->curl, CURLOPT_USERAGENT, HTTP_USER_AGENT);
curl_easy_setopt(af->curl, CURLOPT_URL, af->uri);
curl_easy_setopt(af->curl, CURLOPT_WRITEDATA, af->file);
curl_easy_setopt(af->curl, CURLOPT_READDATA, af->file);

View file

@ -1,4 +1,4 @@
/** A simple growing buffer.
/** A simple buffer for encoding streamed JSON messages.
*
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
* @copyright 2014-2020, Institute for Automation of Complex Power Systems, EONERC
@ -20,49 +20,37 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*********************************************************************************/
#include <cstring>
#include <villas/compat.hpp>
#include <villas/buffer.hpp>
#include <villas/common.hpp>
#include <villas/exceptions.hpp>
using namespace villas;
Buffer::Buffer(size_t sz) :
buf(sz, 0)
{ }
void Buffer::clear()
json_t * Buffer::decode()
{
buf.clear();
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 Buffer::append(const char *data, size_t l)
int Buffer::encode(json_t *j, int flags)
{
buf.insert(buf.end(), data, data+l);
return 0;
}
int Buffer::parseJson(json_t **j)
{
*j = json_loadb(buf.data(), buf.size(), 0, nullptr);
if (!*j)
return -1;
return 0;
}
int Buffer::appendJson(json_t *j, int flags)
{
size_t l;
retry: l = json_dumpb(j, buf.data() + buf.size(), buf.capacity() - buf.size(), flags);
if (buf.capacity() < buf.size() + l) {
buf.reserve(buf.size() + l);
goto retry;
}
return json_dump_callback(j, callback, this, flags);
}
int Buffer::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

@ -1,56 +0,0 @@
/** A simple buffer for encoding streamed JSON messages.
*
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
* @copyright 2014-2020, 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.hpp>
#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, int flags)
{
return json_dump_callback(j, callback, this, flags);
}
int JsonBuffer::callback(const char *data, size_t len, void *ctx)
{
JsonBuffer *b = static_cast<JsonBuffer *>(ctx);
/* Append junk of JSON to buffer */
b->insert(b->end(), &data[0], &data[len]);
return 0;
}

View file

@ -22,7 +22,7 @@
add_executable(unit-tests-common
advio.cpp
json_buffer.cpp
buffer.cpp
graph.cpp
hist.cpp
kernel.cpp

View file

@ -26,18 +26,16 @@
#include <criterion/criterion.h>
#include <jansson.h>
#include <villas/json_buffer.hpp>
#include <villas/buffer.hpp>
using namespace villas;
using villas::JsonBuffer;
// cppcheck-suppress unknownMacro
TestSuite(buffer, .description = "Buffer datastructure");
Test(json_buffer, decode)
Test(buffer, decode)
{
JsonBuffer buf;
Buffer buf;
json_t *j;
json_t *k;
@ -54,10 +52,10 @@ Test(json_buffer, decode)
cr_assert(json_equal(j, k));
}
Test(json_buffer, encode)
Test(buffer, encode)
{
int ret;
JsonBuffer buf;
Buffer buf;
json_t *k;
const char *e = "{\"id\": \"5a786626-fbc6-4c04-98c2-48027e68c2fa\"}";
@ -76,10 +74,10 @@ Test(json_buffer, encode)
json_decref(k);
}
Test(json_buffer, encode_decode)
Test(buffer, encode_decode)
{
int ret;
JsonBuffer buf;
Buffer buf;
json_t *k;
json_t *j;
@ -100,12 +98,12 @@ Test(json_buffer, encode_decode)
json_decref(k);
}
Test(json_buffer, multiple)
Test(buffer, multiple)
{
int ret;
const int N = 100;
JsonBuffer buf;
Buffer buf;
json_t *k[N];
json_t *j[N];