Commit 6322f7c2 authored by Steffen Vogel's avatar Steffen Vogel 🎅🏼
Browse files

buffer: merge two buffer implementations

parent db106f50
/** 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 <jansson.h>
#include <cstdlib>
#include <villas/common.hpp>
#include <jansson.h>
namespace villas {
class Buffer {
class Buffer : public std::vector<char> {
public:
std::vector<char> buf;
protected:
static int callback(const char *data, size_t len, void *ctx);
Buffer(size_t size);
public:
Buffer(const char *buf, size_type len) :
std::vector<char>(buf, buf+len)
{ }
void clear();
Buffer(size_type count = 0) :
std::vector<char>(count, 0)
{ }
int append(const char *data, size_t len);
/** Encode JSON document /p j and append it to the buffer */
int encode(json_t *j, int flags = 0);
int parseJson(json_t **j);
/** Decode JSON document from the beginning of the buffer */
json_t * decode();
int appendJson(json_t *j, int flags = 0);
void append(const char *data, size_t len)
{
insert(end(), data, data + len);
}
};
} /* namespace villas */
/** 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 */
......@@ -23,7 +23,6 @@
add_library(villas-common SHARED
advio.cpp
buffer.cpp
json_buffer.cpp
compat.cpp
hist.cpp
dsp/pid.cpp
......
......@@ -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);
......
/** 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;
int Buffer::append(const char *data, size_t l)
{
buf.insert(buf.end(), data, data+l);
j = json_loadb(data(), size(), JSON_DISABLE_EOF_CHECK, &err);
if (!j)
return nullptr;
return 0;
/* Remove decoded JSON document from beginning */
erase(begin(), begin() + err.position);
return j;
}
int Buffer::parseJson(json_t **j)
int Buffer::encode(json_t *j, int flags)
{
*j = json_loadb(buf.data(), buf.size(), 0, nullptr);
if (!*j)
return -1;
return 0;
return json_dump_callback(j, callback, this, flags);
}
int Buffer::appendJson(json_t *j, int flags)
int Buffer::callback(const char *data, size_t len, void *ctx)
{
size_t l;
Buffer *b = static_cast<Buffer *>(ctx);
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;
}
/* Append junk of JSON to buffer */
b->insert(b->end(), &data[0], &data[len]);
return 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;
}
......@@ -22,7 +22,7 @@
add_executable(unit-tests-common
advio.cpp
json_buffer.cpp
buffer.cpp
graph.cpp
hist.cpp
kernel.cpp
......
......@@ -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];
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment