diff --git a/common/include/villas/buffer.hpp b/common/include/villas/buffer.hpp index 98d24f579..a6ad1256c 100644 --- a/common/include/villas/buffer.hpp +++ b/common/include/villas/buffer.hpp @@ -1,4 +1,4 @@ -/** A simple growing buffer. +/** A simple buffer for encoding streamed JSON messages. * * @file * @author Steffen Vogel @@ -23,29 +23,38 @@ #pragma once -#include #include +#include + #include -#include - namespace villas { -class Buffer { +class Buffer : public std::vector { + +protected: + static int callback(const char *data, size_t len, void *ctx); public: - std::vector buf; + Buffer(const char *buf, size_type len) : + std::vector(buf, buf+len) + { } - Buffer(size_t size); + Buffer(size_type count = 0) : + std::vector(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 */ diff --git a/common/include/villas/json_buffer.hpp b/common/include/villas/json_buffer.hpp deleted file mode 100644 index 187561aee..000000000 --- a/common/include/villas/json_buffer.hpp +++ /dev/null @@ -1,53 +0,0 @@ -/** A simple buffer for encoding streamed JSON messages. - * - * @file - * @author Steffen Vogel - * @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 . - *********************************************************************************/ - -#pragma once - -#include - -#include - -#include - -namespace villas { - -class JsonBuffer : public std::vector -{ - -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 */ diff --git a/common/lib/CMakeLists.txt b/common/lib/CMakeLists.txt index b8e86b439..cb0062299 100644 --- a/common/lib/CMakeLists.txt +++ b/common/lib/CMakeLists.txt @@ -23,7 +23,6 @@ add_library(villas-common SHARED advio.cpp buffer.cpp - json_buffer.cpp compat.cpp hist.cpp dsp/pid.cpp diff --git a/common/lib/advio.cpp b/common/lib/advio.cpp index 5c757dce6..fb2238ed8 100644 --- a/common/lib/advio.cpp +++ b/common/lib/advio.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); diff --git a/common/lib/buffer.cpp b/common/lib/buffer.cpp index 318021bf8..25380f4ae 100644 --- a/common/lib/buffer.cpp +++ b/common/lib/buffer.cpp @@ -1,4 +1,4 @@ -/** A simple growing buffer. +/** A simple buffer for encoding streamed JSON messages. * * @author Steffen Vogel * @copyright 2014-2020, Institute for Automation of Complex Power Systems, EONERC @@ -20,49 +20,37 @@ * along with this program. If not, see . *********************************************************************************/ -#include - #include #include -#include -#include 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(ctx); + + /* Append junk of JSON to buffer */ + b->insert(b->end(), &data[0], &data[len]); return 0; } diff --git a/common/lib/json_buffer.cpp b/common/lib/json_buffer.cpp deleted file mode 100644 index e3f521955..000000000 --- a/common/lib/json_buffer.cpp +++ /dev/null @@ -1,56 +0,0 @@ -/** A simple buffer for encoding streamed JSON messages. - * - * @author Steffen Vogel - * @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 . - *********************************************************************************/ - -#include -#include - -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(ctx); - - /* Append junk of JSON to buffer */ - b->insert(b->end(), &data[0], &data[len]); - - return 0; -} diff --git a/common/tests/unit/CMakeLists.txt b/common/tests/unit/CMakeLists.txt index 921e944cb..27658725d 100644 --- a/common/tests/unit/CMakeLists.txt +++ b/common/tests/unit/CMakeLists.txt @@ -22,7 +22,7 @@ add_executable(unit-tests-common advio.cpp - json_buffer.cpp + buffer.cpp graph.cpp hist.cpp kernel.cpp diff --git a/common/tests/unit/json_buffer.cpp b/common/tests/unit/buffer.cpp similarity index 91% rename from common/tests/unit/json_buffer.cpp rename to common/tests/unit/buffer.cpp index e90863654..ff2b1471b 100644 --- a/common/tests/unit/json_buffer.cpp +++ b/common/tests/unit/buffer.cpp @@ -26,18 +26,16 @@ #include #include -#include +#include 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];