mirror of
https://github.com/ratspeak/microReticulum
synced 2026-11-22 23:10:38 +00:00
WIP: Major improvements in memory management
This commit is contained in:
1 parent
1c2525b588
commit
cc25dbab8f
28 files changed
+914
-398
No files matched your search
File renamed without changes.
File renamed without changes.
@@ -295,7 +295,7 @@ void testBytesConversion() {
|
||||
|
||||
{
|
||||
RNS::Bytes bytes("Hello World");
|
||||
std::string hex = bytes.toHex();
|
||||
std::string hex = bytes.toHex(true);
|
||||
RNS::extreme("text: \"" + bytes.toString() + "\" upper hex: \"" + hex + "\"");
|
||||
assert(hex.length() == 22);
|
||||
assert(hex.compare("48656C6C6F20576F726C64") == 0);
|
||||
@@ -340,11 +340,33 @@ void testBytesConversion() {
|
||||
|
||||
}
|
||||
|
||||
void testBytesResize() {
|
||||
RNS::extreme("Testing downsize of Bytes with initial capacity");
|
||||
|
||||
RNS::Bytes bytes(1024);
|
||||
assert(!bytes);
|
||||
assert(bytes.size() == 0);
|
||||
assert(bytes.capacity() == 1024);
|
||||
assert(bytes.data() != nullptr);
|
||||
|
||||
uint8_t* buffer = bytes.writable(bytes.capacity());
|
||||
assert(bytes);
|
||||
assert(bytes.size() == 1024);
|
||||
assert(bytes.capacity() == 1024);
|
||||
|
||||
assert(buffer != nullptr);
|
||||
memcpy(buffer, "Hello World", 11);
|
||||
bytes.resize(11);
|
||||
assert(bytes.size() == 11);
|
||||
assert(bytes.capacity() == 1024);
|
||||
}
|
||||
|
||||
void testBytes() {
|
||||
RNS::head("Running testBytes...", RNS::LOG_EXTREME);
|
||||
testBytesMain();
|
||||
testCowBytes();
|
||||
testBytesConversion();
|
||||
testBytesResize();
|
||||
}
|
||||
|
||||
/*
|
||||
File renamed without changes.
File renamed without changes.
@@ -1,5 +1,6 @@
|
||||
#include "TestFilesystem.h"
|
||||
|
||||
#include <Utilities/OS.h>
|
||||
#include <Log.h>
|
||||
|
||||
#ifdef ARDUINO
|
||||
@@ -65,7 +66,7 @@ TestFilesystem::TestFilesystem() : Filesystem() {
|
||||
Serial.println(" MB");
|
||||
// ensure filesystem is writable and format if not
|
||||
Bytes test;
|
||||
if (!OS::write_file(test, "/test")) {
|
||||
if (!OS::write_file("/test", test)) {
|
||||
info("SPIFFS filesystem is being formatted, please wait...");
|
||||
SPIFFS.format();
|
||||
}
|
||||
@@ -94,8 +95,7 @@ TestFilesystem::TestFilesystem() : Filesystem() {
|
||||
if (file) {
|
||||
#elif NRF52
|
||||
File file(InternalFS);
|
||||
file.open(file_path, FILE_O_READ);
|
||||
if (file) {
|
||||
if (file.open(file_path, FILE_O_READ)) {
|
||||
#else
|
||||
if (false) {
|
||||
#endif
|
||||
@@ -121,24 +121,22 @@ TestFilesystem::TestFilesystem() : Filesystem() {
|
||||
}
|
||||
}
|
||||
|
||||
/*virtual*/ const Bytes TestFilesystem::read_file(const char* file_path) {
|
||||
Bytes data;
|
||||
/*virtual*/ size_t TestFilesystem::read_file(const char* file_path, Bytes& data) {
|
||||
size_t read = 0;
|
||||
#ifdef ARDUINO
|
||||
#ifdef ESP32
|
||||
File file = SPIFFS.open(file_path, FILE_READ);
|
||||
if (file) {
|
||||
size_t size = file.size();
|
||||
size_t read = file.readBytes((char*)data.writable(size), size);
|
||||
read = file.readBytes((char*)data.writable(size), size);
|
||||
#elif NRF52
|
||||
File file(InternalFS);
|
||||
file.open(file_path, FILE_O_READ);
|
||||
if (file) {
|
||||
if (file.open(file_path, FILE_O_READ)) {
|
||||
size_t size = file.size();
|
||||
size_t read = file.readBytes((char*)data.writable(size), size);
|
||||
read = file.readBytes((char*)data.writable(size), size);
|
||||
#else
|
||||
if (false) {
|
||||
size_t size = 0;
|
||||
size_t read = 0;
|
||||
#endif
|
||||
#else
|
||||
FILE* file = fopen(file_path, "r");
|
||||
@@ -168,24 +166,22 @@ TestFilesystem::TestFilesystem() : Filesystem() {
|
||||
else {
|
||||
error("read_file: failed to open input file " + std::string(file_path));
|
||||
}
|
||||
return data;
|
||||
return read;
|
||||
}
|
||||
|
||||
/*virtual*/ bool TestFilesystem::write_file(const Bytes& data, const char* file_path) {
|
||||
bool success = false;
|
||||
/*virtual*/ size_t TestFilesystem::write_file(const char* file_path, const Bytes& data) {
|
||||
size_t wrote = 0;
|
||||
#ifdef ARDUINO
|
||||
#ifdef ESP32
|
||||
File file = SPIFFS.open(file_path, FILE_WRITE);
|
||||
if (file) {
|
||||
size_t wrote = file.write(data.data(), data.size());
|
||||
wrote = file.write(data.data(), data.size());
|
||||
#elif NRF52
|
||||
File file(InternalFS);
|
||||
file.open(file_path, FILE_O_WRITE);
|
||||
if (file) {
|
||||
size_t wrote = file.write(data.data(), data.size());
|
||||
if (file.open(file_path, FILE_O_WRITE)) {
|
||||
wrote = file.write(data.data(), data.size());
|
||||
#else
|
||||
if (false) {
|
||||
size_t wrote = 0;
|
||||
#endif
|
||||
#else
|
||||
FILE* file = fopen(file_path, "w");
|
||||
@@ -194,11 +190,8 @@ TestFilesystem::TestFilesystem() : Filesystem() {
|
||||
size_t wrote = fwrite(data.data(), 1, data.size(), file);
|
||||
#endif
|
||||
extreme("write_file: wrote " + std::to_string(wrote) + " Bytes to file " + std::string(file_path));
|
||||
if (wrote == data.size()) {
|
||||
success = true;
|
||||
}
|
||||
else {
|
||||
error("write_file: failed to write file " + std::string(file_path));
|
||||
if (wrote < data.size()) {
|
||||
warning("write_file: not all data was written to file " + std::string(file_path));
|
||||
}
|
||||
//extreme("write_file: closing output file");
|
||||
#ifdef ARDUINO
|
||||
@@ -214,7 +207,7 @@ TestFilesystem::TestFilesystem() : Filesystem() {
|
||||
else {
|
||||
error("write_file: failed to open output file " + std::string(file_path));
|
||||
}
|
||||
return success;
|
||||
return wrote;
|
||||
}
|
||||
|
||||
/*virtual*/ bool TestFilesystem::remove_file(const char* file_path) {
|
||||
|
||||
@@ -11,8 +11,8 @@ public:
|
||||
|
||||
protected:
|
||||
virtual bool file_exists(const char* file_path);
|
||||
virtual const RNS::Bytes read_file(const char* file_path);
|
||||
virtual bool write_file(const RNS::Bytes& data, const char* file_path);
|
||||
virtual size_t read_file(const char* file_path, RNS::Bytes& data);
|
||||
virtual size_t write_file(const char* file_path, const RNS::Bytes& data);
|
||||
virtual bool remove_file(const char* file_path);
|
||||
virtual bool rename_file(const char* from_file_path, const char* to_file_path);
|
||||
virtual bool create_directory(const char* directory_path);
|
||||
|
||||
File renamed without changes.
@@ -81,7 +81,7 @@ const char test_file_data[] = "test data";
|
||||
|
||||
void testWrite() {
|
||||
RNS::Bytes data(test_file_data);
|
||||
if (RNS::Utilities::OS::write_file(data, test_file_path)) {
|
||||
if (RNS::Utilities::OS::write_file(test_file_path, data) == data.size()) {
|
||||
RNS::extreme("wrote: " + std::to_string(data.size()) + " bytes");
|
||||
}
|
||||
else {
|
||||
@@ -91,8 +91,8 @@ void testWrite() {
|
||||
}
|
||||
|
||||
void testRead() {
|
||||
RNS::Bytes data = RNS::Utilities::OS::read_file(test_file_path);
|
||||
if (data) {
|
||||
RNS::Bytes data;
|
||||
if (RNS::Utilities::OS::read_file(test_file_path, data) > 0) {
|
||||
RNS::extreme("read: " + std::to_string(data.size()) + " bytes");
|
||||
RNS::extreme("data: " + data.toString());
|
||||
assert(data.size() == strlen(test_file_data));
|
||||
@@ -129,7 +129,7 @@ void testSerializeObject() {
|
||||
}
|
||||
RNS::extreme("serialized " + std::to_string(length) + " bytes");
|
||||
if (length > 0) {
|
||||
if (RNS::Utilities::OS::write_file(data, test_object_path)) {
|
||||
if (RNS::Utilities::OS::write_file(test_object_path, data) == data.size()) {
|
||||
RNS::extreme("wrote: " + std::to_string(data.size()) + " bytes");
|
||||
}
|
||||
else {
|
||||
@@ -147,9 +147,10 @@ void testSerializeObject() {
|
||||
void testDeserializeObject() {
|
||||
|
||||
//StaticJsonDocument<8192> doc;
|
||||
DynamicJsonDocument doc(8192);
|
||||
DynamicJsonDocument doc(1024);
|
||||
|
||||
RNS::Bytes data = RNS::Utilities::OS::read_file(test_object_path);
|
||||
RNS::Bytes data;
|
||||
if (RNS::Utilities::OS::read_file(test_object_path, data) > 0) {
|
||||
if (data) {
|
||||
RNS::extreme("read: " + std::to_string(data.size()) + " bytes");
|
||||
//RNS::extreme("data: " + data.toString());
|
||||
@@ -192,7 +193,7 @@ void testSerializeObject() {
|
||||
}
|
||||
RNS::extreme("serialized " + std::to_string(length) + " bytes");
|
||||
if (length > 0) {
|
||||
if (RNS::Utilities::OS::write_file(data, test_object_path)) {
|
||||
if (RNS::Utilities::OS::write_file(test_object_path, data) == data.size()) {
|
||||
RNS::extreme("wrote: " + std::to_string(data.size()) + " bytes");
|
||||
}
|
||||
else {
|
||||
@@ -210,10 +211,10 @@ void testSerializeObject() {
|
||||
void testDeserializeObject() {
|
||||
|
||||
//StaticJsonDocument<8192> doc;
|
||||
DynamicJsonDocument doc(8192);
|
||||
DynamicJsonDocument doc(1024);
|
||||
|
||||
RNS::Bytes data = RNS::Utilities::OS::read_file(test_object_path);
|
||||
if (data) {
|
||||
RNS::Bytes data;
|
||||
if (RNS::Utilities::OS::read_file(test_object_path, data) > 0) {
|
||||
RNS::extreme("read: " + std::to_string(data.size()) + " bytes");
|
||||
//RNS::extreme("data: " + data.toString());
|
||||
DeserializationError error = deserializeJson(doc, data.data());
|
||||
@@ -290,7 +291,7 @@ void testSerializeVector() {
|
||||
}
|
||||
RNS::extreme("testSerializeVector: serialized " + std::to_string(length) + " bytes");
|
||||
if (length > 0) {
|
||||
if (RNS::Utilities::OS::write_file(data, test_vector_path)) {
|
||||
if (RNS::Utilities::OS::write_file(test_vector_path, data) == data.size()) {
|
||||
RNS::extreme("testSerializeVector: wrote: " + std::to_string(data.size()) + " bytes");
|
||||
}
|
||||
else {
|
||||
@@ -314,10 +315,10 @@ void testDeserializeVector() {
|
||||
4*JSON_OBJECT_SIZE(1);
|
||||
|
||||
//StaticJsonDocument<8192> doc;
|
||||
DynamicJsonDocument doc(8192);
|
||||
DynamicJsonDocument doc(1024);
|
||||
|
||||
RNS::Bytes data = RNS::Utilities::OS::read_file(test_vector_path);
|
||||
if (data) {
|
||||
RNS::Bytes data;
|
||||
if (RNS::Utilities::OS::read_file(test_vector_path, data) > 0) {
|
||||
RNS::extreme("testDeserializeVector: read: " + std::to_string(data.size()) + " bytes");
|
||||
//RNS::extreme("testDeserializeVector: data: " + data.toString());
|
||||
DeserializationError error = deserializeJson(doc, data.data());
|
||||
@@ -387,7 +388,7 @@ void testSerializeSet() {
|
||||
}
|
||||
RNS::extreme("testSerializeSet: serialized " + std::to_string(length) + " bytes");
|
||||
if (length > 0) {
|
||||
if (RNS::Utilities::OS::write_file(data, test_set_path)) {
|
||||
if (RNS::Utilities::OS::write_file(test_set_path, data) == data.size()) {
|
||||
RNS::extreme("testSerializeSet: wrote: " + std::to_string(data.size()) + " bytes");
|
||||
}
|
||||
else {
|
||||
@@ -411,10 +412,10 @@ void testDeserializeSet() {
|
||||
4*JSON_OBJECT_SIZE(1);
|
||||
|
||||
//StaticJsonDocument<8192> doc;
|
||||
DynamicJsonDocument doc(8192);
|
||||
DynamicJsonDocument doc(1024);
|
||||
|
||||
RNS::Bytes data = RNS::Utilities::OS::read_file(test_set_path);
|
||||
if (data) {
|
||||
RNS::Bytes data;
|
||||
if (RNS::Utilities::OS::read_file(test_set_path, data) > 0) {
|
||||
RNS::extreme("testDeserializeSet: read: " + std::to_string(data.size()) + " bytes");
|
||||
//RNS::extreme("testDeserializeSet: data: " + data.toString());
|
||||
DeserializationError error = deserializeJson(doc, data.data());
|
||||
@@ -476,7 +477,7 @@ void testSerializeMap() {
|
||||
}
|
||||
RNS::extreme("testSerializeMap: serialized " + std::to_string(length) + " bytes");
|
||||
if (length > 0) {
|
||||
if (RNS::Utilities::OS::write_file(data, test_map_path)) {
|
||||
if (RNS::Utilities::OS::write_file(test_map_path, data) == data.size()) {
|
||||
RNS::extreme("testSerializeMap: wrote: " + std::to_string(data.size()) + " bytes");
|
||||
}
|
||||
else {
|
||||
@@ -500,10 +501,10 @@ void testDeserializeMap() {
|
||||
4*JSON_OBJECT_SIZE(1);
|
||||
|
||||
//StaticJsonDocument<8192> doc;
|
||||
DynamicJsonDocument doc(8192);
|
||||
DynamicJsonDocument doc(1024);
|
||||
|
||||
RNS::Bytes data = RNS::Utilities::OS::read_file(test_map_path);
|
||||
if (data) {
|
||||
RNS::Bytes data;
|
||||
if (RNS::Utilities::OS::read_file(test_map_path, data) > 0) {
|
||||
RNS::extreme("testDeserializeMap: read: " + std::to_string(data.size()) + " bytes");
|
||||
//RNS::extreme("testDeserializeMap: data: " + data.toString());
|
||||
DeserializationError error = deserializeJson(doc, data.data());
|
||||
@@ -573,15 +574,51 @@ void testSerializeDestinationTable() {
|
||||
two.assignHex("2222222222222222");
|
||||
map.insert({two, entry_two});
|
||||
|
||||
for (int n = 0; n < 20; n++) {
|
||||
RNS::Transport::DestinationEntry entry;
|
||||
entry._timestamp = 1.0;
|
||||
entry._received_from = received;
|
||||
entry._receiving_interface = test_interface;
|
||||
entry._random_blobs = blobs;
|
||||
RNS::Bytes hash;
|
||||
hash.assign(std::to_string(1000000000000001 + n).c_str());
|
||||
map.insert({hash, entry});
|
||||
}
|
||||
|
||||
for (auto& [hash, test] : map) {
|
||||
RNS::extreme("testSerializeDestinationTable: entry: " + hash.toHex() + " = " + test.debugString());
|
||||
}
|
||||
|
||||
StaticJsonDocument<4096> doc;
|
||||
doc = map;
|
||||
//StaticJsonDocument<1024> doc;
|
||||
//doc = map;
|
||||
RNS::extreme("testSerializeDestinationTable: JSON_OBJECT_SIZE " + std::to_string(JSON_OBJECT_SIZE(1)) + " bytes");
|
||||
RNS::extreme("testSerializeDestinationTable: JSON_ARRAY_SIZE " + std::to_string(JSON_ARRAY_SIZE(1)) + " bytes");
|
||||
int calcsize = 0;
|
||||
for (auto& [destination_hash, destination_entry] : map) {
|
||||
// entry
|
||||
calcsize += JSON_ARRAY_SIZE(1);
|
||||
// map key
|
||||
calcsize += JSON_OBJECT_SIZE(1);
|
||||
// entry (+1)
|
||||
calcsize += JSON_OBJECT_SIZE(7+1);
|
||||
// blobs
|
||||
calcsize += JSON_ARRAY_SIZE(destination_entry._random_blobs.size());
|
||||
}
|
||||
RNS::extreme("testSerializeDestinationTable: calcsize " + std::to_string(calcsize) + " bytes");
|
||||
const int BUFFER_SIZE = (JSON_OBJECT_SIZE(9) + JSON_ARRAY_SIZE(2)) * map.size() + JSON_ARRAY_SIZE(map.size());
|
||||
RNS::extreme("testSerializeDestinationTable: BUFFER_SIZE " + std::to_string(BUFFER_SIZE) + " bytes");
|
||||
//DynamicJsonDocument doc(32768);
|
||||
//DynamicJsonDocument doc(BUFFER_SIZE);
|
||||
DynamicJsonDocument doc(calcsize);
|
||||
doc.set(map);
|
||||
RNS::extreme("testSerializeDestinationTable: doc size " + std::to_string(doc.size()));
|
||||
RNS::extreme("testSerializeDestinationTable: doc memory " + std::to_string(doc.memoryUsage()));
|
||||
|
||||
RNS::Bytes data;
|
||||
size_t size = 4096;
|
||||
// /size_t size = 1024;
|
||||
//size_t size = calcsize;
|
||||
size_t size = doc.memoryUsage();
|
||||
RNS::extreme("testSerializeDestinationTable: buffer size " + std::to_string(size) + " bytes");
|
||||
size_t length = serializeJson(doc, data.writable(size), size);
|
||||
//size_t length = serializeMsgPack(doc, data.writable(size), size);
|
||||
if (length < size) {
|
||||
@@ -589,7 +626,8 @@ void testSerializeDestinationTable() {
|
||||
}
|
||||
RNS::extreme("testSerializeDestinationTable: serialized " + std::to_string(length) + " bytes");
|
||||
if (length > 0) {
|
||||
if (RNS::Utilities::OS::write_file(data, test_destination_table_path)) {
|
||||
RNS::extreme("json: " + data.toString());
|
||||
if (RNS::Utilities::OS::write_file(test_destination_table_path, data) == data.size()) {
|
||||
RNS::extreme("testSerializeDestinationTable: wrote: " + std::to_string(data.size()) + " bytes");
|
||||
}
|
||||
else {
|
||||
@@ -606,23 +644,22 @@ void testSerializeDestinationTable() {
|
||||
|
||||
void testDeserializeDestinationTable() {
|
||||
|
||||
// Compute the required size
|
||||
const int capacity =
|
||||
JSON_ARRAY_SIZE(2) +
|
||||
2*JSON_OBJECT_SIZE(3) +
|
||||
4*JSON_OBJECT_SIZE(1);
|
||||
RNS::Bytes data;
|
||||
if (RNS::Utilities::OS::read_file(test_destination_table_path, data) > 0) {
|
||||
//StaticJsonDocument<8192> doc;
|
||||
//DynamicJsonDocument doc(1024);
|
||||
int calcsize = data.size() * 1.5;
|
||||
DynamicJsonDocument doc(calcsize);
|
||||
|
||||
//StaticJsonDocument<8192> doc;
|
||||
DynamicJsonDocument doc(8192);
|
||||
|
||||
RNS::Bytes data = RNS::Utilities::OS::read_file(test_destination_table_path);
|
||||
if (data) {
|
||||
RNS::extreme("testDeserializeDestinationTable: read: " + std::to_string(data.size()) + " bytes");
|
||||
//RNS::extreme("testDeserializeVector: data: " + data.toString());
|
||||
DeserializationError error = deserializeJson(doc, data.data());
|
||||
//DeserializationError error = deserializeMsgPack(doc, data.data());
|
||||
RNS::extreme("testDeserializeDestinationTable: doc size " + std::to_string(doc.size()));
|
||||
RNS::extreme("testDeserializeDestinationTable: doc memory " + std::to_string(doc.memoryUsage()));
|
||||
if (!error) {
|
||||
RNS::extreme("testDeserializeDestinationTable: successfully deserialized");
|
||||
RNS::extreme("testDeserializeDestinationTable: successfully deserialized " + std::to_string(data.size()) + " bytes");
|
||||
RNS::extreme("json: " + data.toString());
|
||||
|
||||
static std::map<RNS::Bytes, RNS::Transport::DestinationEntry> map(doc.as<std::map<RNS::Bytes, RNS::Transport::DestinationEntry>>());
|
||||
for (auto& [hash, test] : map) {
|
||||
File renamed without changes.
@@ -2,7 +2,7 @@
|
||||
|
||||
#include "TestFilesystem.h"
|
||||
|
||||
#include <Test/Test.h>
|
||||
#include "Test.h"
|
||||
|
||||
#include "Log.h"
|
||||
#include "Utilities/OS.h"
|
||||
@@ -45,10 +45,16 @@ void setup() {
|
||||
RNS::LogLevel loglevel = RNS::loglevel();
|
||||
//RNS::loglevel(RNS::LOG_WARNING);
|
||||
RNS::loglevel(RNS::LOG_EXTREME);
|
||||
|
||||
// all tests
|
||||
//test();
|
||||
|
||||
// individiual tests
|
||||
//testReference();
|
||||
//testCrypto();
|
||||
testPersistence();
|
||||
//testPersistence();
|
||||
testBytes();
|
||||
|
||||
RNS::loglevel(loglevel);
|
||||
RNS::extreme("Finished running tests");
|
||||
//return;
|
||||
|
||||
@@ -19,6 +19,8 @@ src_dir = .
|
||||
platform = native
|
||||
build_flags =
|
||||
-std=c++11
|
||||
-g3
|
||||
-ggdb
|
||||
-Wall
|
||||
-Wextra
|
||||
-Wno-missing-field-initializers
|
||||
@@ -32,12 +34,17 @@ lib_deps =
|
||||
microReticulum=symlink://../..
|
||||
lib_extra_dirs = ../
|
||||
lib_compat_mode = off
|
||||
;debug_init_break =
|
||||
;debug_init_break = tbreak
|
||||
;debug_tool =
|
||||
|
||||
[env:native17]
|
||||
platform = native
|
||||
build_unflags = -std=gnu++11
|
||||
build_flags =
|
||||
-std=c++17
|
||||
-g3
|
||||
-ggdb
|
||||
-Wall
|
||||
-Wextra
|
||||
-Wno-missing-field-initializers
|
||||
|
||||
@@ -1 +1 @@
|
||||
{"1111111111111111":{"timestamp":1,"received_from":"deadbeef","announce_hops":0,"expires":0,"random_blobs":["b10bb10b","deadbeef"],"receiving_interface_hash":"aa6c2640df3ddc1c2194a3de35e784587191b82da5feeb50c1082ca9b45ecc95","packet_hash":null},"2222222222222222":{"timestamp":2,"received_from":"deadbeef","announce_hops":0,"expires":0,"random_blobs":["b10bb10b","deadbeef"],"receiving_interface_hash":"aa6c2640df3ddc1c2194a3de35e784587191b82da5feeb50c1082ca9b45ecc95","packet_hash":null}}
|
||||
{"1111111111111111":{"timestamp":1,"received_from":"deadbeef","announce_hops":0,"expires":0,"random_blobs":["b10bb10b","deadbeef"],"receiving_interface_hash":"aa6c2640df3ddc1c2194a3de35e784587191b82da5feeb50c1082ca9b45ecc95","packet_hash":null},"2222222222222222":{"timestamp":2,"received_from":"deadbeef","announce_hops":0,"expires":0,"random_blobs":["b10bb10b","deadbeef"],"receiving_interface_hash":"aa6c2640df3ddc1c2194a3de35e784587191b82da5feeb50c1082ca9b45ecc95","packet_hash":null},"31303030303030303030303030303031":{"timestamp":1,"received_from":"deadbeef","announce_hops":0,"expires":0,"random_blobs":["b10bb10b","deadbeef"],"receiving_interface_hash":"aa6c2640df3ddc1c2194a3de35e784587191b82da5feeb50c1082ca9b45ecc95","packet_hash":null},"31303030303030303030303030303032":{"timestamp":1,"received_from":"deadbeef","announce_hops":0,"expires":0,"random_blobs":["b10bb10b","deadbeef"],"receiving_interface_hash":"aa6c2640df3ddc1c2194a3de35e784587191b82da5feeb50c1082ca9b45ecc95","packet_hash":null},"31303030303030303030303030303033":{"timestamp":1,"received_from":"deadbeef","announce_hops":0,"expires":0,"random_blobs":["b10bb10b","deadbeef"],"receiving_interface_hash":"aa6c2640df3ddc1c2194a3de35e784587191b82da5feeb50c1082ca9b45ecc95","packet_hash":null},"31303030303030303030303030303034":{"timestamp":1,"received_from":"deadbeef","announce_hops":0,"expires":0,"random_blobs":["b10bb10b","deadbeef"],"receiving_interface_hash":"aa6c2640df3ddc1c2194a3de35e784587191b82da5feeb50c1082ca9b45ecc95","packet_hash":null},"31303030303030303030303030303035":{"timestamp":1,"received_from":"deadbeef","announce_hops":0,"expires":0,"random_blobs":["b10bb10b","deadbeef"],"receiving_interface_hash":"aa6c2640df3ddc1c2194a3de35e784587191b82da5feeb50c1082ca9b45ecc95","packet_hash":null},"31303030303030303030303030303036":{"timestamp":1,"received_from":"deadbeef","announce_hops":0,"expires":0,"random_blobs":["b10bb10b","deadbeef"],"receiving_interface_hash":"aa6c2640df3ddc1c2194a3de35e784587191b82da5feeb50c1082ca9b45ecc95","packet_hash":null},"31303030303030303030303030303037":{"timestamp":1,"received_from":"deadbeef","announce_hops":0,"expires":0,"random_blobs":["b10bb10b","deadbeef"],"receiving_interface_hash":"aa6c2640df3ddc1c2194a3de35e784587191b82da5feeb50c1082ca9b45ecc95","packet_hash":null},"31303030303030303030303030303038":{"timestamp":1,"received_from":"deadbeef","announce_hops":0,"expires":0,"random_blobs":["b10bb10b","deadbeef"],"receiving_interface_hash":"aa6c2640df3ddc1c2194a3de35e784587191b82da5feeb50c1082ca9b45ecc95","packet_hash":null},"31303030303030303030303030303039":{"timestamp":1,"received_from":"deadbeef","announce_hops":0,"expires":0,"random_blobs":["b10bb10b","deadbeef"],"receiving_interface_hash":"aa6c2640df3ddc1c2194a3de35e784587191b82da5feeb50c1082ca9b45ecc95","packet_hash":null},"31303030303030303030303030303130":{"timestamp":1,"received_from":"deadbeef","announce_hops":0,"expires":0,"random_blobs":["b10bb10b","deadbeef"],"receiving_interface_hash":"aa6c2640df3ddc1c2194a3de35e784587191b82da5feeb50c1082ca9b45ecc95","packet_hash":null},"31303030303030303030303030303131":{"timestamp":1,"received_from":"deadbeef","announce_hops":0,"expires":0,"random_blobs":["b10bb10b","deadbeef"],"receiving_interface_hash":"aa6c2640df3ddc1c2194a3de35e784587191b82da5feeb50c1082ca9b45ecc95","packet_hash":null},"31303030303030303030303030303132":{"timestamp":1,"received_from":"deadbeef","announce_hops":0,"expires":0,"random_blobs":["b10bb10b","deadbeef"],"receiving_interface_hash":"aa6c2640df3ddc1c2194a3de35e784587191b82da5feeb50c1082ca9b45ecc95","packet_hash":null},"31303030303030303030303030303133":{"timestamp":1,"received_from":"deadbeef","announce_hops":0,"expires":0,"random_blobs":["b10bb10b","deadbeef"],"receiving_interface_hash":"aa6c2640df3ddc1c2194a3de35e784587191b82da5feeb50c1082ca9b45ecc95","packet_hash":null},"31303030303030303030303030303134":{"timestamp":1,"received_from":"deadbeef","announce_hops":0,"expires":0,"random_blobs":["b10bb10b","deadbeef"],"receiving_interface_hash":"aa6c2640df3ddc1c2194a3de35e784587191b82da5feeb50c1082ca9b45ecc95","packet_hash":null},"31303030303030303030303030303135":{"timestamp":1,"received_from":"deadbeef","announce_hops":0,"expires":0,"random_blobs":["b10bb10b","deadbeef"],"receiving_interface_hash":"aa6c2640df3ddc1c2194a3de35e784587191b82da5feeb50c1082ca9b45ecc95","packet_hash":null},"31303030303030303030303030303136":{"timestamp":1,"received_from":"deadbeef","announce_hops":0,"expires":0,"random_blobs":["b10bb10b","deadbeef"],"receiving_interface_hash":"aa6c2640df3ddc1c2194a3de35e784587191b82da5feeb50c1082ca9b45ecc95","packet_hash":null},"31303030303030303030303030303137":{"timestamp":1,"received_from":"deadbeef","announce_hops":0,"expires":0,"random_blobs":["b10bb10b","deadbeef"],"receiving_interface_hash":"aa6c2640df3ddc1c2194a3de35e784587191b82da5feeb50c1082ca9b45ecc95","packet_hash":null},"31303030303030303030303030303138":{"timestamp":1,"received_from":"deadbeef","announce_hopLine truncated
|
||||
@@ -22,6 +22,7 @@ build_flags =
|
||||
-Wno-unused-parameter
|
||||
-Isrc
|
||||
-DNATIVE
|
||||
;-DNDEBUG
|
||||
lib_deps =
|
||||
ArduinoJson@^6.21.3
|
||||
https://github.com/attermann/arduinolibs.git
|
||||
@@ -39,6 +40,7 @@ build_flags =
|
||||
-Wno-unused-parameter
|
||||
-Isrc
|
||||
-DNATIVE
|
||||
;-DNDEBUG
|
||||
lib_deps =
|
||||
ArduinoJson@^6.21.3
|
||||
https://github.com/attermann/arduinolibs.git
|
||||
@@ -111,6 +113,7 @@ build_flags =
|
||||
-Wno-missing-field-initializers
|
||||
-Wno-format
|
||||
-Isrc
|
||||
;-DNDEBUG
|
||||
lib_deps =
|
||||
ArduinoJson@^6.21.3
|
||||
https://github.com/attermann/arduinolibs.git
|
||||
@@ -128,6 +131,7 @@ build_flags =
|
||||
-Wno-missing-field-initializers
|
||||
-Wno-format
|
||||
-Isrc
|
||||
;-DNDEBUG
|
||||
lib_deps =
|
||||
ArduinoJson@^6.21.3
|
||||
https://github.com/attermann/arduinolibs.git
|
||||
+57
-10
@@ -2,22 +2,69 @@
|
||||
|
||||
using namespace RNS;
|
||||
|
||||
void Bytes::ownData() {
|
||||
if (!_data) {
|
||||
newData();
|
||||
// Creates new shared data for instance
|
||||
// - If size is specified (>0) then create shared data with initial size
|
||||
// - If size is not specified (<=0) then create empty shared data
|
||||
void Bytes::newData(size_t size /*= 0*/) {
|
||||
//mem("Bytes is creating its own data");
|
||||
if (size > 0) {
|
||||
// CBA Note that this method of construction creates vector with specified and initializes all eleemtnets with default values
|
||||
Data* data = new Data(size);
|
||||
if (data == nullptr) {
|
||||
error("Bytes failed to allocate data buffer");
|
||||
throw std::runtime_error("Failed to allocate data buffer");
|
||||
}
|
||||
_data = SharedData(data);
|
||||
}
|
||||
else if (!_owner) {
|
||||
else {
|
||||
Data* data = new Data();
|
||||
if (data == nullptr) {
|
||||
error("Bytes failed to allocate empty data buffer");
|
||||
throw std::runtime_error("Failed to allocate empty data buffer");
|
||||
}
|
||||
_data = SharedData(data);
|
||||
}
|
||||
_exclusive = true;
|
||||
}
|
||||
|
||||
// Ensures that instance has exclusive shared data
|
||||
// - If instance has no shared data then create new shared data
|
||||
// - If instance does not have exclusive on shared data that is not empty then make a copy of shared data (if requests) and resize (if requested)
|
||||
// - If instance does not have exclusive on shared data that is empty then then create new shared data
|
||||
// - If instance already has exclusive on shared data then do nothing except resize (if requested)
|
||||
void Bytes::exclusiveData(bool copy /*= true*/, size_t size /*= 0*/) {
|
||||
if (!_data) {
|
||||
newData(size);
|
||||
}
|
||||
else if (!_exclusive) {
|
||||
Data *data;
|
||||
if (!_data->empty()) {
|
||||
if (copy && !_data->empty()) {
|
||||
//extreme("Bytes is creating a writable copy of its shared data");
|
||||
data = new Data(*_data.get());
|
||||
if (data == nullptr) {
|
||||
error("Bytes failed to duplicate data buffer");
|
||||
throw std::runtime_error("Failed to duplicate data buffer");
|
||||
}
|
||||
if (size > 0) {
|
||||
data->resize(size);
|
||||
}
|
||||
_data = SharedData(data);
|
||||
_exclusive = true;
|
||||
}
|
||||
else {
|
||||
//extreme("Bytes is creating its own data because shared is empty");
|
||||
data = new Data();
|
||||
//data = new Data();
|
||||
//if (data == nullptr) {
|
||||
// error("Bytes failed to allocate empty data buffer");
|
||||
// throw std::runtime_error("Failed to allocate empty data buffer");
|
||||
//}
|
||||
//_data = SharedData(data);
|
||||
//_exclusive = true;
|
||||
newData(size);
|
||||
}
|
||||
_data = SharedData(data);
|
||||
_owner = true;
|
||||
}
|
||||
else if (size > 0) {
|
||||
_data->resize(size);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,7 +93,7 @@ void Bytes::assignHex(const char* hex) {
|
||||
// if assignment is empty then clear data and don't bother creating new
|
||||
if (hex == nullptr || hex[0] == 0) {
|
||||
_data = nullptr;
|
||||
_owner = true;
|
||||
_exclusive = true;
|
||||
return;
|
||||
}
|
||||
newData();
|
||||
@@ -62,7 +109,7 @@ void Bytes::appendHex(const char* hex) {
|
||||
if (hex == nullptr || hex[0] == 0) {
|
||||
return;
|
||||
}
|
||||
ownData();
|
||||
exclusiveData();
|
||||
size_t len = strlen(hex);
|
||||
for (size_t i = 0; i < len; i += 2) {
|
||||
uint8_t byte = (hex[i] % 32 + 9) % 25 * 16 + (hex[i+1] % 32 + 9) % 25;
|
||||
|
||||
+40
-31
@@ -53,6 +53,11 @@ namespace RNS {
|
||||
assign(string);
|
||||
//mem(std::string("Bytes object created from std::string \"") + toString() + "\", this: " + std::to_string((uintptr_t)this) + ", data: " + std::to_string((unsigned long)_data.get()));
|
||||
}
|
||||
Bytes(size_t capacity) {
|
||||
newData();
|
||||
reserve(capacity);
|
||||
//mem(std::string("Bytes object created with capacity ") + std::to_string(capacity) + ", this: " + std::to_string((uintptr_t)this) + ", data: " + std::to_string((unsigned long)_data.get()));
|
||||
}
|
||||
virtual ~Bytes() {
|
||||
//mem(std::string("Bytes object destroyed \"") + toString() + "\", this: " + std::to_string((uintptr_t)this) + ", data: " + std::to_string((unsigned long)_data.get()));
|
||||
}
|
||||
@@ -90,39 +95,31 @@ namespace RNS {
|
||||
private:
|
||||
inline SharedData shareData() const {
|
||||
//mem("Bytes is sharing its own data");
|
||||
_owner = false;
|
||||
_exclusive = false;
|
||||
return _data;
|
||||
}
|
||||
inline void newData(size_t size = 0) {
|
||||
//mem("Bytes is creating its own data");
|
||||
if (size > 0) {
|
||||
_data = SharedData(new Data(size));
|
||||
}
|
||||
else {
|
||||
_data = SharedData(new Data());
|
||||
}
|
||||
_owner = true;
|
||||
}
|
||||
void ownData();
|
||||
void newData(size_t size = 0);
|
||||
void exclusiveData(bool copy = true, size_t size = 0);
|
||||
|
||||
public:
|
||||
inline void clear() {
|
||||
_data = nullptr;
|
||||
_owner = true;
|
||||
_exclusive = true;
|
||||
}
|
||||
|
||||
inline void assign(const Bytes& bytes) {
|
||||
#ifdef COW
|
||||
_data = bytes.shareData();
|
||||
_owner = false;
|
||||
_exclusive = false;
|
||||
#else
|
||||
// if assignment is empty then clear data and don't bother creating new
|
||||
if (bytes.size() <= 0) {
|
||||
_data = nullptr;
|
||||
_owner = true;
|
||||
_exclusive = true;
|
||||
return;
|
||||
}
|
||||
newData();
|
||||
//newData();
|
||||
exclusiveData(false);
|
||||
_data->insert(_data->begin(), bytes._data->begin(), bytes._data->end());
|
||||
#endif
|
||||
}
|
||||
@@ -130,20 +127,22 @@ namespace RNS {
|
||||
// if assignment is empty then clear data and don't bother creating new
|
||||
if (chunk == nullptr || size <= 0) {
|
||||
_data = nullptr;
|
||||
_owner = true;
|
||||
_exclusive = true;
|
||||
return;
|
||||
}
|
||||
newData();
|
||||
//newData();
|
||||
exclusiveData(false);
|
||||
_data->insert(_data->begin(), chunk, chunk + size);
|
||||
}
|
||||
inline void assign(const char* string) {
|
||||
// if assignment is empty then clear data and don't bother creating new
|
||||
if (string == nullptr || string[0] == 0) {
|
||||
_data = nullptr;
|
||||
_owner = true;
|
||||
_exclusive = true;
|
||||
return;
|
||||
}
|
||||
newData();
|
||||
//newData();
|
||||
exclusiveData(false);
|
||||
_data->insert(_data->begin(), (uint8_t* )string, (uint8_t* )string + strlen(string));
|
||||
}
|
||||
inline void assign(const std::string& string) { assign(string.c_str()); }
|
||||
@@ -154,7 +153,7 @@ namespace RNS {
|
||||
if (bytes.size() <= 0) {
|
||||
return;
|
||||
}
|
||||
ownData();
|
||||
exclusiveData(true);
|
||||
_data->insert(_data->end(), bytes._data->begin(), bytes._data->end());
|
||||
}
|
||||
inline void append(const uint8_t* chunk, size_t size) {
|
||||
@@ -162,7 +161,7 @@ namespace RNS {
|
||||
if (chunk == nullptr || size <= 0) {
|
||||
return;
|
||||
}
|
||||
ownData();
|
||||
exclusiveData(true);
|
||||
_data->insert(_data->end(), chunk, chunk + size);
|
||||
}
|
||||
inline void append(const char* string) {
|
||||
@@ -170,35 +169,45 @@ namespace RNS {
|
||||
if (string == nullptr || string[0] == 0) {
|
||||
return;
|
||||
}
|
||||
ownData();
|
||||
exclusiveData(true);
|
||||
_data->insert(_data->end(), (uint8_t* )string, (uint8_t* )string + strlen(string));
|
||||
}
|
||||
inline void append(const std::string& string) { append(string.c_str()); }
|
||||
inline void append(uint8_t byte) {
|
||||
ownData();
|
||||
exclusiveData(true);
|
||||
_data->push_back(byte);
|
||||
}
|
||||
void appendHex(const char* hex);
|
||||
|
||||
inline uint8_t* writable(size_t size) {
|
||||
if (size > 0) {
|
||||
//newData(size);
|
||||
exclusiveData(false, size);
|
||||
return _data->data();
|
||||
}
|
||||
else if (_data) {
|
||||
//newData(_data->size());
|
||||
exclusiveData(false, _data->size());
|
||||
return _data->data();
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
inline void resize(size_t newsize) {
|
||||
// if size is unchanged then do nothing
|
||||
if (newsize == size()) {
|
||||
return;
|
||||
}
|
||||
ownData();
|
||||
exclusiveData(true);
|
||||
_data->resize(newsize);
|
||||
}
|
||||
|
||||
inline uint8_t* writable(size_t size) {
|
||||
newData(size);
|
||||
return _data->data();
|
||||
}
|
||||
|
||||
public:
|
||||
int8_t compare(const Bytes& bytes) const;
|
||||
inline size_t size() const { if (!_data) return 0; return _data->size(); }
|
||||
inline bool empty() const { if (!_data) return true; return _data->empty(); }
|
||||
inline size_t capacity() const { if (!_data) return 0; return _data->capacity(); }
|
||||
inline void reserve(size_t capacity) const { if (!_data) return; _data->reserve(capacity); }
|
||||
inline const uint8_t* data() const { if (!_data) return nullptr; return _data->data(); }
|
||||
|
||||
inline std::string toString() const { if (!_data) return ""; return {(const char*)data(), size()}; }
|
||||
@@ -232,7 +241,7 @@ namespace RNS {
|
||||
|
||||
private:
|
||||
SharedData _data;
|
||||
mutable bool _owner = true;
|
||||
mutable bool _exclusive = true;
|
||||
|
||||
};
|
||||
|
||||
|
||||
+8
-8
@@ -69,19 +69,19 @@ namespace RNS {
|
||||
protected:
|
||||
// CBA These should NOT be called internally, and should be protected (only to be overridden and called by Object)
|
||||
virtual bool file_exists(const char* file_path) { return false; }
|
||||
virtual const Bytes read_file(const char* file_path) { return {Bytes::NONE}; }
|
||||
virtual bool write_file(const Bytes& data, const char* file_path) { return false; }
|
||||
virtual size_t read_file(const char* file_path, Bytes& data) { return 0; }
|
||||
virtual size_t write_file(const char* file_path, const Bytes& data) { return 0; }
|
||||
virtual bool remove_file(const char* file_path) { return false; }
|
||||
virtual bool rename_file(const char* from_file_path, const char* to_file_path) { return false; }
|
||||
virtual bool directory_exists(const char* directory_path) { return false; }
|
||||
virtual bool create_directory(const char* directory_path) { return false; }
|
||||
virtual bool remove_directory(const char* directory_path) { return false; }
|
||||
virtual std::list<std::string> list_directory(const char* directory_path) { return _empty; }
|
||||
virtual std::list<std::string> list_directory(const char* directory_path) { extreme("list_directory: empty"); return _empty; }
|
||||
|
||||
public:
|
||||
inline bool do_file_exists(const char* file_path) { assert(_object); return _object->do_file_exists(file_path); }
|
||||
inline const Bytes do_read_file(const char* file_path) { assert(_object); return _object->do_read_file(file_path); }
|
||||
inline bool do_write_file(const Bytes& data, const char* file_path) { assert(_object); return _object->do_write_file(data, file_path); }
|
||||
inline size_t do_read_file(const char* file_path, Bytes& data) { assert(_object); return _object->do_read_file(file_path, data); }
|
||||
inline size_t do_write_file(const char* file_path, const Bytes& data) { assert(_object); return _object->do_write_file(file_path, data); }
|
||||
inline bool do_remove_file(const char* file_path) { assert(_object); return _object->do_remove_file(file_path); }
|
||||
inline bool do_rename_file(const char* from_file_path, const char* to_file_path) { assert(_object); return _object->do_rename_file(from_file_path, to_file_path); }
|
||||
inline bool do_directory_exists(const char* directory_path) { assert(_object); return _object->do_directory_exists(directory_path); }
|
||||
@@ -112,14 +112,14 @@ namespace RNS {
|
||||
virtual ~Object() { mem("Filesystem::Data object destroyed, this: " + std::to_string((uintptr_t)this)); }
|
||||
private:
|
||||
virtual inline bool do_file_exists(const char* file_path) { if (_parent == nullptr) return false; return _parent->file_exists(file_path); }
|
||||
virtual inline const Bytes do_read_file(const char* file_path) { if (_parent == nullptr) return {Bytes::NONE}; return _parent->read_file(file_path); }
|
||||
virtual inline bool do_write_file(const Bytes& data, const char* file_path) { if (_parent == nullptr) return false; return _parent->write_file(data, file_path); }
|
||||
virtual inline size_t do_read_file(const char* file_path, Bytes& data) { if (_parent == nullptr) return {Bytes::NONE}; return _parent->read_file(file_path, data); }
|
||||
virtual inline size_t do_write_file(const char* file_path, const Bytes& data) { if (_parent == nullptr) return false; return _parent->write_file(file_path, data); }
|
||||
virtual inline bool do_remove_file(const char* file_path) { if (_parent == nullptr) return false; return _parent->remove_file(file_path); }
|
||||
virtual inline bool do_rename_file(const char* from_file_path, const char* to_file_path) { if (_parent == nullptr) return false; return _parent->rename_file(from_file_path, to_file_path); }
|
||||
virtual inline bool do_directory_exists(const char* directory_path) { if (_parent == nullptr) return false; return _parent->directory_exists(directory_path); }
|
||||
virtual inline bool do_create_directory(const char* directory_path) { if (_parent == nullptr) return false; return _parent->create_directory(directory_path); }
|
||||
virtual inline bool do_remove_directory(const char* directory_path) { if (_parent == nullptr) return false; return _parent->remove_directory(directory_path); }
|
||||
virtual inline std::list<std::string> do_list_directory(const char* directory_path) { if (_parent == nullptr) return _empty; return _parent->do_list_directory(directory_path); }
|
||||
virtual inline std::list<std::string> do_list_directory(const char* directory_path) { if (_parent == nullptr) return _empty; return _parent->list_directory(directory_path); }
|
||||
private:
|
||||
Filesystem* _parent = nullptr;
|
||||
std::list<std::string> _empty;
|
||||
|
||||
+3
-3
@@ -129,8 +129,8 @@ void Identity::load_public_key(const Bytes& pub_bytes) {
|
||||
bool Identity::load(const char* path) {
|
||||
extreme("Reading identity key from storage...");
|
||||
try {
|
||||
Bytes prv_bytes = OS::read_file(path);
|
||||
if (prv_bytes) {
|
||||
Bytes prv_bytes;
|
||||
if (OS::read_file(path, prv_bytes) > 0) {
|
||||
return load_private_key(prv_bytes);
|
||||
}
|
||||
else {
|
||||
@@ -155,7 +155,7 @@ communication for the identity. Be very careful with this method.
|
||||
bool Identity::to_file(const char* path) {
|
||||
extreme("Writing identity key to storage...");
|
||||
try {
|
||||
return OS::write_file(get_private_key(), path);
|
||||
return (OS::write_file(path, get_private_key()) == get_private_key().size());
|
||||
}
|
||||
catch (std::exception& e) {
|
||||
error("Error while saving identity to " + std::string(path));
|
||||
|
||||
@@ -6,6 +6,16 @@
|
||||
|
||||
#include <string>
|
||||
|
||||
#ifndef NDEBUG
|
||||
#define DEBUG(msg) (RNS::debug(msg))
|
||||
#define TRACE(msg) (RNS::extreme(msg))
|
||||
#define MEM(msg) (RNS::mem(msg))
|
||||
#else
|
||||
#define DEBUG(ignore) ((void)0)
|
||||
#define TRACE(ignore) ((void)0)
|
||||
#define MEM(ignore) ((void)0)
|
||||
#endif
|
||||
|
||||
namespace RNS {
|
||||
|
||||
enum LogLevel {
|
||||
|
||||
+26
-5
@@ -86,8 +86,8 @@ Reticulum::Reticulum() : _object(new Object()) {
|
||||
try {
|
||||
std::string time_offset_path = Reticulum::_storagepath + "/time_offset";
|
||||
if (OS::file_exists(time_offset_path.c_str())) {
|
||||
Bytes buf = OS::read_file(time_offset_path.c_str());
|
||||
if (buf.size() == 8) {
|
||||
Bytes buf;
|
||||
if (OS::read_file(time_offset_path.c_str(), buf) == 8) {
|
||||
uint64_t offset = *(uint64_t*)buf.data();
|
||||
debug("Read time offset of " + std::to_string(offset) + " from file");
|
||||
OS::setTimeOffset(offset);
|
||||
@@ -220,8 +220,6 @@ void Reticulum::jobs() {
|
||||
if (now > _object->_last_data_persist + PERSIST_INTERVAL) {
|
||||
persist_data();
|
||||
}
|
||||
|
||||
debug("Available Memory: " + std::to_string(OS::freeMemory()) + " Available Storage: " + std::to_string(OS::storage_available()));
|
||||
}
|
||||
|
||||
void Reticulum::should_persist_data() {
|
||||
@@ -242,7 +240,7 @@ void Reticulum::persist_data() {
|
||||
uint64_t offset = OS::ltime();
|
||||
debug("Writing time offset of " + std::to_string(offset) + " to file");
|
||||
Bytes buf((uint8_t*)&offset, sizeof(offset));
|
||||
OS::write_file(buf, time_offset_path.c_str());
|
||||
OS::write_file(time_offset_path.c_str(), buf);
|
||||
}
|
||||
catch (std::exception& e) {
|
||||
error("Failed to write time offset, the contained exception was: " + std::string(e.what()));
|
||||
@@ -291,4 +289,27 @@ void Reticulum::clean_caches() {
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
Transport::clean_caches();
|
||||
|
||||
}
|
||||
|
||||
void Reticulum::clear_caches() {
|
||||
extreme("Clearing resource and packet caches...");
|
||||
|
||||
try {
|
||||
std::string destination_table_path = _storagepath + "/destination_table";
|
||||
OS::remove_file(destination_table_path.c_str());
|
||||
|
||||
std::string packet_cache_path = _cachepath;
|
||||
OS::remove_directory(packet_cache_path.c_str());
|
||||
|
||||
#ifdef ARDUINO
|
||||
std::string time_offset_path = _storagepath + "/time_offset";
|
||||
OS::remove_file(time_offset_path.c_str());
|
||||
#endif
|
||||
}
|
||||
catch (std::exception& e) {
|
||||
error("Failed to clear cache file(s), the contained exception was: " + std::string(e.what()));
|
||||
}
|
||||
}
|
||||
@@ -65,6 +65,7 @@ namespace RNS {
|
||||
void should_persist_data();
|
||||
void persist_data();
|
||||
void clean_caches();
|
||||
void clear_caches();
|
||||
|
||||
/*
|
||||
Returns whether proofs sent are explicit or implicit.
|
||||
|
||||
+394
-192
@@ -59,6 +59,9 @@ extern uint8_t crypto_crc8(uint8_t tag, const void *data, unsigned size);
|
||||
|
||||
/*static*/ std::map<Bytes, const Interface&> Transport::_pending_local_path_requests;
|
||||
|
||||
// CBA
|
||||
/*static*/ std::map<Bytes, Transport::PacketEntry> Transport::_packet_table;
|
||||
|
||||
/*static*/ uint16_t Transport::_LOCAL_CLIENT_CACHE_MAXSIZE = 512;
|
||||
|
||||
/*static*/ double Transport::_start_time = 0.0;
|
||||
@@ -85,7 +88,8 @@ extern uint8_t crypto_crc8(uint8_t tag, const void *data, unsigned size);
|
||||
/*static*/ uint16_t Transport::_max_pr_tags = 320;
|
||||
|
||||
// CBA
|
||||
/*static*/ uint16_t Transport::_path_table_maxsize = 200;
|
||||
/*static*/ uint16_t Transport::_path_table_maxsize = 10;
|
||||
/*static*/ uint16_t Transport::_path_table_maxpersist = 10;
|
||||
/*static*/ double Transport::_last_saved = 0.0;
|
||||
/*static*/ float Transport::_save_interval = 3600.0;
|
||||
/*static*/ uint8_t Transport::_destination_table_crc = 0;
|
||||
@@ -96,6 +100,11 @@ extern uint8_t crypto_crc8(uint8_t tag, const void *data, unsigned size);
|
||||
// CBA
|
||||
/*static*/ Transport::Callbacks Transport::_callbacks;
|
||||
|
||||
// CBA Stats
|
||||
/*static*/ uint32_t Transport::_packets_sent = 0;
|
||||
/*static*/ uint32_t Transport::_packets_received = 0;
|
||||
/*static*/ uint32_t Transport::_destinations_added = 0;
|
||||
|
||||
/*static*/ void Transport::start(const Reticulum& reticulum_instance) {
|
||||
info("Transport starting...");
|
||||
_jobs_running = true;
|
||||
@@ -181,139 +190,12 @@ extern uint8_t crypto_crc8(uint8_t tag, const void *data, unsigned size);
|
||||
if (Reticulum::transport_enabled()) {
|
||||
info("Transport mode is enabled");
|
||||
|
||||
std::string destination_table_path = Reticulum::_storagepath + "/destination_table";
|
||||
if (!_owner.is_connected_to_shared_instance() && OS::file_exists(destination_table_path.c_str())) {
|
||||
/*p
|
||||
serialised_destinations = []
|
||||
try:
|
||||
file = open(destination_table_path, "rb")
|
||||
serialised_destinations = umsgpack.unpackb(file.read())
|
||||
file.close()
|
||||
// Read in path table and then write and clean in case any entries are invalid
|
||||
read_path_table();
|
||||
write_path_table();
|
||||
clean_caches();
|
||||
|
||||
for serialised_entry in serialised_destinations:
|
||||
destination_hash = serialised_entry[0]
|
||||
|
||||
if len(destination_hash) == RNS.Reticulum.TRUNCATED_HASHLENGTH//8:
|
||||
timestamp = serialised_entry[1]
|
||||
received_from = serialised_entry[2]
|
||||
hops = serialised_entry[3]
|
||||
expires = serialised_entry[4]
|
||||
random_blobs = serialised_entry[5]
|
||||
receiving_interface = Transport.find_interface_from_hash(serialised_entry[6])
|
||||
announce_packet = Transport.get_cached_packet(serialised_entry[7])
|
||||
|
||||
if announce_packet != None and receiving_interface != None:
|
||||
announce_packet.unpack()
|
||||
// We increase the hops, since reading a packet
|
||||
// from cache is equivalent to receiving it again
|
||||
// over an interface. It is cached with it's non-
|
||||
// increased hop-count.
|
||||
announce_packet.hops += 1
|
||||
Transport.destination_table[destination_hash] = [timestamp, received_from, hops, expires, random_blobs, receiving_interface, announce_packet]
|
||||
RNS.log("Loaded path table entry for "+RNS.prettyhexrep(destination_hash)+" from storage", RNS.LOG_DEBUG)
|
||||
else:
|
||||
RNS.log("Could not reconstruct path table entry from storage for "+RNS.prettyhexrep(destination_hash), RNS.LOG_DEBUG)
|
||||
if announce_packet == None:
|
||||
RNS.log("The announce packet could not be loaded from cache", RNS.LOG_DEBUG)
|
||||
if receiving_interface == None:
|
||||
RNS.log("The interface is no longer available", RNS.LOG_DEBUG)
|
||||
*/
|
||||
|
||||
try {
|
||||
//StaticJsonDocument<1024> doc;
|
||||
DynamicJsonDocument doc(1024);
|
||||
|
||||
RNS::Bytes data = RNS::Utilities::OS::read_file(destination_table_path.c_str());
|
||||
if (data) {
|
||||
RNS::extreme("Transport::start: read: " + std::to_string(data.size()) + " bytes");
|
||||
//RNS::extreme("testDeserializeVector: data: " + data.toString());
|
||||
DeserializationError error = deserializeJson(doc, data.data());
|
||||
//DeserializationError error = deserializeMsgPack(doc, data.data());
|
||||
if (!error) {
|
||||
_destination_table = doc.as<std::map<Bytes, DestinationEntry>>();
|
||||
RNS::extreme("Transport::start: successfully deserialized path table with " + std::to_string(_destination_table.size()) + " entries");
|
||||
// Calculate crc for dirty-checking before write
|
||||
_destination_table_crc = crypto_crc8(0, data.data(), data.size());
|
||||
std::vector<Bytes> invalid_paths;
|
||||
for (auto& [destination_hash, destination_entry] : _destination_table) {
|
||||
RNS::extreme("Transport::start: entry: " + destination_hash.toHex() + " = " + destination_entry.debugString());
|
||||
// CBA TODO If announce packet load fails then remove destination en try (it's useless without announce packet)
|
||||
if (!destination_entry._announce_packet) {
|
||||
// remove destination
|
||||
RNS::extreme("Transport::start: removing invalid path " + destination_hash.toHex());
|
||||
invalid_paths.push_back(destination_hash);
|
||||
}
|
||||
}
|
||||
for (const auto& destination_hash : invalid_paths) {
|
||||
_destination_table.erase(destination_hash);
|
||||
}
|
||||
}
|
||||
else {
|
||||
RNS::extreme("Transport::start: failed to deserialize");
|
||||
}
|
||||
}
|
||||
else {
|
||||
RNS::extreme("Transport::start: destination table read failed");
|
||||
}
|
||||
verbose("Loaded " + std::to_string(_destination_table.size()) + " path table entries from storage");
|
||||
|
||||
}
|
||||
catch (std::exception& e) {
|
||||
error("Could not load destination table from storage, the contained exception was: " + std::string(e.what()));
|
||||
}
|
||||
}
|
||||
|
||||
// TODO
|
||||
/*
|
||||
tunnel_table_path = RNS.Reticulum.storagepath+"/tunnels"
|
||||
if os.path.isfile(tunnel_table_path) and not Transport.owner.is_connected_to_shared_instance:
|
||||
serialised_tunnels = []
|
||||
try:
|
||||
file = open(tunnel_table_path, "rb")
|
||||
serialised_tunnels = umsgpack.unpackb(file.read())
|
||||
file.close()
|
||||
|
||||
for serialised_tunnel in serialised_tunnels:
|
||||
tunnel_id = serialised_tunnel[0]
|
||||
interface_hash = serialised_tunnel[1]
|
||||
serialised_paths = serialised_tunnel[2]
|
||||
expires = serialised_tunnel[3]
|
||||
|
||||
tunnel_paths = {}
|
||||
for serialised_entry in serialised_paths:
|
||||
destination_hash = serialised_entry[0]
|
||||
timestamp = serialised_entry[1]
|
||||
received_from = serialised_entry[2]
|
||||
hops = serialised_entry[3]
|
||||
expires = serialised_entry[4]
|
||||
random_blobs = serialised_entry[5]
|
||||
receiving_interface = Transport.find_interface_from_hash(serialised_entry[6])
|
||||
announce_packet = Transport.get_cached_packet(serialised_entry[7])
|
||||
|
||||
if announce_packet != None:
|
||||
announce_packet.unpack()
|
||||
// We increase the hops, since reading a packet
|
||||
// from cache is equivalent to receiving it again
|
||||
// over an interface. It is cached with it's non-
|
||||
// increased hop-count.
|
||||
announce_packet.hops += 1
|
||||
|
||||
tunnel_path = [timestamp, received_from, hops, expires, random_blobs, receiving_interface, announce_packet]
|
||||
tunnel_paths[destination_hash] = tunnel_path
|
||||
|
||||
tunnel = [tunnel_id, None, tunnel_paths, expires]
|
||||
Transport.tunnels[tunnel_id] = tunnel
|
||||
|
||||
if len(Transport.destination_table) == 1:
|
||||
specifier = "entry"
|
||||
else:
|
||||
specifier = "entries"
|
||||
|
||||
RNS.log("Loaded "+str(len(Transport.tunnels))+" tunnel table "+specifier+" from storage", RNS.LOG_VERBOSE)
|
||||
|
||||
except Exception as e:
|
||||
RNS.log("Could not load tunnel table from storage, the contained exception was: "+str(e), RNS.LOG_ERROR)
|
||||
*/
|
||||
read_tunnel_table();
|
||||
|
||||
// Create transport-specific destination for probe requests
|
||||
if (Reticulum::probe_destination_enabled()) {
|
||||
@@ -337,6 +219,11 @@ extern uint8_t crypto_crc8(uint8_t tag, const void *data, unsigned size);
|
||||
if hasattr(interface, "wants_tunnel") and interface.wants_tunnel:
|
||||
Transport.synthesize_tunnel(interface)
|
||||
*/
|
||||
|
||||
#ifndef NDEBUG
|
||||
// CBA DEBUG
|
||||
dump_stats();
|
||||
#endif
|
||||
}
|
||||
|
||||
/*static*/ void Transport::loop() {
|
||||
@@ -528,12 +415,7 @@ extern uint8_t crypto_crc8(uint8_t tag, const void *data, unsigned size);
|
||||
}
|
||||
|
||||
// Cull the path table if it has reached its max size
|
||||
if (_destination_table.size() > _path_table_maxsize) {
|
||||
// TODO prune by age, or better yet by last use
|
||||
std::map<Bytes, DestinationEntry>::iterator iter = _destination_table.begin();
|
||||
std::advance(iter, _destination_table.size() - _path_table_maxsize);
|
||||
_destination_table.erase(_destination_table.begin(), iter);
|
||||
}
|
||||
cull_path_table();
|
||||
|
||||
if (OS::time() > (_tables_last_culled + _tables_cull_interval)) {
|
||||
|
||||
@@ -620,7 +502,7 @@ extern uint8_t crypto_crc8(uint8_t tag, const void *data, unsigned size);
|
||||
// Cull the path table
|
||||
std::vector<Bytes> stale_paths;
|
||||
for (const auto& [destination_hash, destination_entry] : _destination_table) {
|
||||
const auto& attached_interface = destination_entry._receiving_interface;
|
||||
const Interface& attached_interface = destination_entry.receiving_interface();
|
||||
double destination_expiry;
|
||||
if (attached_interface && attached_interface.mode() == Type::Interface::MODE_ACCESS_POINT) {
|
||||
destination_expiry = destination_entry._timestamp + AP_PATH_TIME;
|
||||
@@ -725,6 +607,10 @@ extern uint8_t crypto_crc8(uint8_t tag, const void *data, unsigned size);
|
||||
extreme("Removed " + std::to_string(count) + " tunnels");
|
||||
}
|
||||
|
||||
#ifndef NDEBUG
|
||||
dump_stats();
|
||||
#endif
|
||||
|
||||
_tables_last_culled = OS::time();
|
||||
}
|
||||
|
||||
@@ -821,6 +707,7 @@ extern uint8_t crypto_crc8(uint8_t tag, const void *data, unsigned size);
|
||||
|
||||
/*static*/ bool Transport::outbound(Packet& packet) {
|
||||
extreme("Transport::outbound()");
|
||||
++_packets_sent;
|
||||
|
||||
if (!packet.destination()) {
|
||||
//throw std::invalid_argument("Can not send packet with no destination.");
|
||||
@@ -846,7 +733,7 @@ extern uint8_t crypto_crc8(uint8_t tag, const void *data, unsigned size);
|
||||
extreme("Transport::outbound: Path to destination is known");
|
||||
//outbound_interface = Transport.destination_table[packet.destination_hash][5]
|
||||
DestinationEntry destination_entry = (*_destination_table.find(packet.destination_hash())).second;
|
||||
Interface& outbound_interface = destination_entry._receiving_interface;
|
||||
Interface outbound_interface = destination_entry.receiving_interface();
|
||||
|
||||
// If there's more than one hop to the destination, and we know
|
||||
// a path, we insert the packet into transport by adding the next
|
||||
@@ -1197,7 +1084,7 @@ extern uint8_t crypto_crc8(uint8_t tag, const void *data, unsigned size);
|
||||
_receipts.push_back(receipt);
|
||||
}
|
||||
|
||||
cache(packet);
|
||||
cache_packet(packet);
|
||||
}
|
||||
|
||||
_jobs_locked = false;
|
||||
@@ -1282,6 +1169,7 @@ extern uint8_t crypto_crc8(uint8_t tag, const void *data, unsigned size);
|
||||
|
||||
/*static*/ void Transport::inbound(const Bytes& raw, const Interface& interface /*= {Type::NONE}*/) {
|
||||
extreme("Transport::inbound()");
|
||||
++_packets_received;
|
||||
// CBA
|
||||
if (_callbacks._receive_packet) {
|
||||
try {
|
||||
@@ -1376,7 +1264,9 @@ extern uint8_t crypto_crc8(uint8_t tag, const void *data, unsigned size);
|
||||
warning("Transport::inbound: Packet unpack failed!");
|
||||
return;
|
||||
}
|
||||
#ifndef NDEBUG
|
||||
extreme("Transport::inbound: packet: " + packet.debugString());
|
||||
#endif
|
||||
|
||||
extreme("Transport::inbound: destination=" + packet.destination_hash().toHex() + " hops=" + std::to_string(packet.hops()));
|
||||
|
||||
@@ -1432,7 +1322,7 @@ extern uint8_t crypto_crc8(uint8_t tag, const void *data, unsigned size);
|
||||
if (accept) {
|
||||
extreme("Transport::inbound: Packet accepted by filter");
|
||||
_packet_hashlist.insert(packet.packet_hash());
|
||||
cache(packet);
|
||||
cache_packet(packet);
|
||||
|
||||
// Check special conditions for local clients connected
|
||||
// through a shared Reticulum instance
|
||||
@@ -1597,7 +1487,7 @@ extern uint8_t crypto_crc8(uint8_t tag, const void *data, unsigned size);
|
||||
new_raw << packet.raw().mid(2);
|
||||
}
|
||||
|
||||
Interface& outbound_interface = destination_entry._receiving_interface;
|
||||
Interface outbound_interface = destination_entry.receiving_interface();
|
||||
|
||||
if (packet.packet_type() == Type::Packet::LINKREQUEST) {
|
||||
extreme("Transport::inbound: Packet is next-hop LINKREQUEST");
|
||||
@@ -2071,6 +1961,15 @@ extern uint8_t crypto_crc8(uint8_t tag, const void *data, unsigned size);
|
||||
new_announce.send();
|
||||
}
|
||||
|
||||
// CBA Culling before adding to esnure table does not exceed maxsize
|
||||
extreme("Caching packet " + packet.get_hash().toHex());
|
||||
if (RNS::Transport::cache_packet(packet, true)) {
|
||||
packet.cached(true);
|
||||
}
|
||||
//extreme("Adding packet " + packet.get_hash().toHex() + " to packet table");
|
||||
//PacketEntry packet_entry(packet);
|
||||
//_packet_table.insert({packet.get_hash(), packet_entry});
|
||||
extreme("Adding destination " + packet.destination_hash().toHex() + " to path table");
|
||||
DestinationEntry destination_table_entry(
|
||||
now,
|
||||
received_from,
|
||||
@@ -2078,10 +1977,15 @@ extern uint8_t crypto_crc8(uint8_t tag, const void *data, unsigned size);
|
||||
expires,
|
||||
random_blobs,
|
||||
//packet.receiving_interface(),
|
||||
const_cast<Interface&>(packet.receiving_interface()),
|
||||
packet
|
||||
//const_cast<Interface&>(packet.receiving_interface()),
|
||||
packet.receiving_interface().get_hash(),
|
||||
//packet
|
||||
packet.get_hash()
|
||||
);
|
||||
_destination_table.insert({packet.destination_hash(), destination_table_entry});
|
||||
if (_destination_table.insert({packet.destination_hash(), destination_table_entry}).second) {
|
||||
++_destinations_added;
|
||||
cull_path_table();
|
||||
}
|
||||
|
||||
debug("Destination " + packet.destination_hash().toHex() + " is now " + std::to_string(announce_hops) + " hops away via " + received_from.toHex() + " on " + packet.receiving_interface().toString());
|
||||
//extreme("Transport::inbound: Destination " + packet.destination_hash().toHex() + " has data: " + packet.data().toHex());
|
||||
@@ -2666,7 +2570,7 @@ Deregisters an announce handler.
|
||||
return {Type::NONE};
|
||||
}
|
||||
|
||||
/*static*/ bool Transport::should_cache(const Packet& packet) {
|
||||
/*static*/ bool Transport::should_cache_packet(const Packet& packet) {
|
||||
// TODO: Rework the caching system. It's currently
|
||||
// not very useful to even cache Resource proofs,
|
||||
// disabling it for now, until redesigned.
|
||||
@@ -2681,13 +2585,13 @@ Deregisters an announce handler.
|
||||
// means that they have not had their hop count
|
||||
// increased yet! Take note of this when reading from
|
||||
// the packet cache.
|
||||
/*static*/ bool Transport::cache(const Packet& packet, bool force_cache /*= false*/) {
|
||||
extreme("Conditionally saving packet " + packet.get_hash().toHex() + " to storage");
|
||||
if (should_cache(packet) || force_cache) {
|
||||
/*static*/ bool Transport::cache_packet(const Packet& packet, bool force_cache /*= false*/) {
|
||||
extreme("Checking to see if packet " + packet.get_hash().toHex() + " should be cached");
|
||||
if (should_cache_packet(packet) || force_cache) {
|
||||
extreme("Saving packet " + packet.get_hash().toHex() + " to storage");
|
||||
try {
|
||||
std::string packet_cache_path = Reticulum::_cachepath + "/" + packet.get_hash().toHex();
|
||||
return Persistence::serialize(packet, packet_cache_path.c_str(), 1024);
|
||||
return Persistence::serialize(packet, packet_cache_path.c_str());
|
||||
}
|
||||
catch (std::exception& e) {
|
||||
error("Error writing packet to cache. The contained exception was: " + std::string(e.what()));
|
||||
@@ -2723,6 +2627,7 @@ Deregisters an announce handler.
|
||||
//return Persistence::deserialize(packet_cache_path.c_str());
|
||||
Packet packet({Type::NONE});
|
||||
Persistence::deserialize(packet, packet_cache_path.c_str());
|
||||
packet.update_hash();
|
||||
return packet;
|
||||
}
|
||||
catch (std::exception& e) {
|
||||
@@ -2847,7 +2752,7 @@ Deregisters an announce handler.
|
||||
auto iter = _destination_table.find(destination_hash);
|
||||
if (iter != _destination_table.end()) {
|
||||
DestinationEntry destination_entry = (*iter).second;
|
||||
return destination_entry._receiving_interface;
|
||||
return destination_entry.receiving_interface();
|
||||
}
|
||||
else {
|
||||
return {Type::NONE};
|
||||
@@ -3023,7 +2928,7 @@ will announce it.
|
||||
if (iter != _destination_table.end()) {
|
||||
extreme("Transport::path_request_handler: entry found for destination " + destination_hash.toHex());
|
||||
DestinationEntry& destination_entry = (*iter).second;
|
||||
if (is_local_client_interface(destination_entry._receiving_interface)) {
|
||||
if (is_local_client_interface(destination_entry.receiving_interface())) {
|
||||
destination_exists_on_local_client = true;
|
||||
_pending_local_path_requests.insert({destination_hash, attached_interface});
|
||||
}
|
||||
@@ -3057,11 +2962,11 @@ will announce it.
|
||||
else if ((Reticulum::transport_enabled() || is_from_local_client) && destination_iter != _destination_table.end()) {
|
||||
extreme("Transport::path_request_handler: entry found for destination " + destination_hash.toHex());
|
||||
DestinationEntry& destination_entry = (*destination_iter).second;
|
||||
const Packet& packet = destination_entry._announce_packet;
|
||||
const Packet& announce_packet = destination_entry.announce_packet();
|
||||
const Bytes& next_hop = destination_entry._received_from;
|
||||
const Interface& received_from = destination_entry._receiving_interface;
|
||||
const Interface& receiving_interface = destination_entry.receiving_interface();
|
||||
|
||||
if (attached_interface.mode() == Type::Interface::MODE_ROAMING && attached_interface == destination_entry._receiving_interface) {
|
||||
if (attached_interface.mode() == Type::Interface::MODE_ROAMING && attached_interface == receiving_interface) {
|
||||
debug("Not answering path request on roaming-mode interface, since next hop is on same roaming-mode interface");
|
||||
}
|
||||
else {
|
||||
@@ -3081,7 +2986,8 @@ will announce it.
|
||||
uint8_t retries = Type::Transport::PATHFINDER_R;
|
||||
uint8_t local_rebroadcasts = 0;
|
||||
bool block_rebroadcasts = true;
|
||||
uint8_t announce_hops = destination_entry._announce_packet.hops();
|
||||
// CBA TODO Determine if okay to take hops directly from DestinationEntry
|
||||
uint8_t announce_hops = announce_packet.hops();
|
||||
|
||||
double retransmit_timeout = 0;
|
||||
if (is_from_local_client) {
|
||||
@@ -3098,14 +3004,15 @@ will announce it.
|
||||
// rebroadcast locally. In such a case the actual announce
|
||||
// is temporarily held, and then reinserted when the path
|
||||
// request has been served to the peer.
|
||||
auto announce_iter = _announce_table.find(destination_entry._announce_packet.destination_hash());
|
||||
const Packet& announce_packet = announce_packet;
|
||||
auto announce_iter = _announce_table.find(announce_packet.destination_hash());
|
||||
if (announce_iter != _announce_table.end()) {
|
||||
AnnounceEntry& held_entry = (*announce_iter).second;
|
||||
_held_announces.insert({destination_entry._announce_packet.destination_hash(), held_entry});
|
||||
_held_announces.insert({announce_packet.destination_hash(), held_entry});
|
||||
}
|
||||
|
||||
/*
|
||||
_announce_table.insert({destination_entry._announce_packet.destination_hash(), {
|
||||
_announce_table.insert({announce_packet.destination_hash(), {
|
||||
now,
|
||||
retransmit_timeout,
|
||||
retries,
|
||||
@@ -3113,7 +3020,7 @@ will announce it.
|
||||
//destination_entry.receiving_interface,
|
||||
destination_entry._received_from,
|
||||
announce_hops,
|
||||
destination_entry._announce_packet,
|
||||
announce_packet,
|
||||
local_rebroadcasts,
|
||||
block_rebroadcasts,
|
||||
attached_interface
|
||||
@@ -3127,12 +3034,12 @@ will announce it.
|
||||
//destination_entry.receiving_interface,
|
||||
destination_entry._received_from,
|
||||
announce_hops,
|
||||
destination_entry._announce_packet,
|
||||
announce_packet,
|
||||
local_rebroadcasts,
|
||||
block_rebroadcasts,
|
||||
attached_interface
|
||||
);
|
||||
_announce_table.insert({destination_entry._announce_packet.destination_hash(), announce_entry});
|
||||
_announce_table.insert({announce_packet.destination_hash(), announce_entry});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3323,7 +3230,7 @@ will announce it.
|
||||
return false;
|
||||
}
|
||||
|
||||
/*static*/ void Transport::save_packet_hashlist() {
|
||||
/*static*/ void Transport::write_packet_hashlist() {
|
||||
// TODO
|
||||
/*
|
||||
if not Transport.owner.is_connected_to_shared_instance:
|
||||
@@ -3365,8 +3272,105 @@ will announce it.
|
||||
*/
|
||||
}
|
||||
|
||||
/*static*/ bool Transport::save_path_table() {
|
||||
debug("Transport::save_path_table");
|
||||
/*static*/ bool Transport::read_path_table() {
|
||||
debug("Transport::read_path_table");
|
||||
std::string destination_table_path = Reticulum::_storagepath + "/destination_table";
|
||||
if (!_owner.is_connected_to_shared_instance() && OS::file_exists(destination_table_path.c_str())) {
|
||||
/*p
|
||||
serialised_destinations = []
|
||||
try:
|
||||
file = open(destination_table_path, "rb")
|
||||
serialised_destinations = umsgpack.unpackb(file.read())
|
||||
file.close()
|
||||
|
||||
for serialised_entry in serialised_destinations:
|
||||
destination_hash = serialised_entry[0]
|
||||
|
||||
if len(destination_hash) == RNS.Reticulum.TRUNCATED_HASHLENGTH//8:
|
||||
timestamp = serialised_entry[1]
|
||||
received_from = serialised_entry[2]
|
||||
hops = serialised_entry[3]
|
||||
expires = serialised_entry[4]
|
||||
random_blobs = serialised_entry[5]
|
||||
receiving_interface = Transport.find_interface_from_hash(serialised_entry[6])
|
||||
announce_packet = Transport.get_cached_packet(serialised_entry[7])
|
||||
|
||||
if announce_packet != None and receiving_interface != None:
|
||||
announce_packet.unpack()
|
||||
// We increase the hops, since reading a packet
|
||||
// from cache is equivalent to receiving it again
|
||||
// over an interface. It is cached with it's non-
|
||||
// increased hop-count.
|
||||
announce_packet.hops += 1
|
||||
Transport.destination_table[destination_hash] = [timestamp, received_from, hops, expires, random_blobs, receiving_interface, announce_packet]
|
||||
RNS.log("Loaded path table entry for "+RNS.prettyhexrep(destination_hash)+" from storage", RNS.LOG_DEBUG)
|
||||
else:
|
||||
RNS.log("Could not reconstruct path table entry from storage for "+RNS.prettyhexrep(destination_hash), RNS.LOG_DEBUG)
|
||||
if announce_packet == None:
|
||||
RNS.log("The announce packet could not be loaded from cache", RNS.LOG_DEBUG)
|
||||
if receiving_interface == None:
|
||||
RNS.log("The interface is no longer available", RNS.LOG_DEBUG)
|
||||
*/
|
||||
|
||||
try {
|
||||
extreme("Transport::start: buffer capacity " + std::to_string(Persistence::_buffer.capacity()) + " bytes");
|
||||
if (RNS::Utilities::OS::read_file(destination_table_path.c_str(), Persistence::_buffer) > 0) {
|
||||
extreme("Transport::start: read: " + std::to_string(Persistence::_buffer.size()) + " bytes");
|
||||
#ifndef NDEBUG
|
||||
// CBA DEBUG Dump path table
|
||||
extreme("Transport::start: buffer addr: " + std::to_string((long)Persistence::_buffer.data()));
|
||||
extreme("Transport::start: buffer size " + std::to_string(Persistence::_buffer.size()) + " bytes");
|
||||
//extreme("SERIALIZED: destination_table");
|
||||
//extreme(Persistence::_buffer.toString());
|
||||
#endif
|
||||
#ifdef USE_MSGPACK
|
||||
DeserializationError error = deserializeMsgPack(Persistence::_document, Persistence::_buffer.data());
|
||||
#else
|
||||
DeserializationError error = deserializeJson(Persistence::_document, Persistence::_buffer.data());
|
||||
#endif
|
||||
extreme("Transport::start: doc size: " + std::to_string(Persistence::_buffer.size()) + " bytes");
|
||||
if (!error) {
|
||||
_destination_table = Persistence::_document.as<std::map<Bytes, DestinationEntry>>();
|
||||
extreme("Transport::start: successfully deserialized path table with " + std::to_string(_destination_table.size()) + " entries");
|
||||
// Calculate crc for dirty-checking before write
|
||||
_destination_table_crc = crypto_crc8(0, Persistence::_buffer.data(), Persistence::_buffer.size());
|
||||
std::vector<Bytes> invalid_paths;
|
||||
for (auto& [destination_hash, destination_entry] : _destination_table) {
|
||||
#ifndef NDEBUG
|
||||
extreme("Transport::start: entry: " + destination_hash.toHex() + " = " + destination_entry.debugString());
|
||||
#endif
|
||||
// CBA TODO If announce packet load fails then remove destination entry (it's useless without announce packet)
|
||||
if (!destination_entry.announce_packet()) {
|
||||
// remove destination
|
||||
RNS::warning("Transport::start: removing invalid path to " + destination_hash.toHex());
|
||||
invalid_paths.push_back(destination_hash);
|
||||
}
|
||||
}
|
||||
for (const auto& destination_hash : invalid_paths) {
|
||||
_destination_table.erase(destination_hash);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
extreme("Transport::start: failed to deserialize");
|
||||
}
|
||||
}
|
||||
else {
|
||||
extreme("Transport::start: destination table read failed");
|
||||
}
|
||||
verbose("Loaded " + std::to_string(_destination_table.size()) + " path table entries from storage");
|
||||
|
||||
}
|
||||
catch (std::exception& e) {
|
||||
error("Could not load destination table from storage, the contained exception was: " + std::string(e.what()));
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/*static*/ bool Transport::write_path_table() {
|
||||
debug("Transport::write_path_table");
|
||||
//return false;
|
||||
|
||||
if (Transport::_owner.is_connected_to_shared_instance()) {
|
||||
return true;
|
||||
@@ -3434,36 +3438,57 @@ will announce it.
|
||||
|
||||
std::string destination_table_path = Reticulum::_storagepath + "/destination_table";
|
||||
|
||||
//StaticJsonDocument<1024> doc;
|
||||
//doc = _destination_table;
|
||||
DynamicJsonDocument doc(1024);
|
||||
doc.set(_destination_table);
|
||||
{
|
||||
Persistence::_document.set(_destination_table);
|
||||
extreme("Transport::write_path_table: doc size " + std::to_string(Persistence::_document.memoryUsage()) + " bytes");
|
||||
|
||||
RNS::Bytes data;
|
||||
size_t size = 4096;
|
||||
size_t length = serializeJson(doc, data.writable(size), size);
|
||||
//size_t length = serializeMsgPack(doc, data.writable(size), size);
|
||||
if (length < size) {
|
||||
data.resize(length);
|
||||
}
|
||||
RNS::extreme("Transport::save_path_table: serialized " + std::to_string(length) + " bytes");
|
||||
if (length > 0) {
|
||||
// Check crc to see if data has changed before writing
|
||||
uint8_t crc = crypto_crc8(0, data.data(), data.size());
|
||||
if (_destination_table_crc > 0 && crc == _destination_table_crc) {
|
||||
RNS::extreme("Transport::save_path_table: no change detected, skipping write");
|
||||
//size_t size = 8192;
|
||||
size_t size = Persistence::_buffer.capacity();
|
||||
extreme("Transport::write_path_table: obtaining buffer size " + std::to_string(size) + " bytes");
|
||||
uint8_t* buffer = Persistence::_buffer.writable(size);
|
||||
extreme("Transport::write_path_table: buffer addr: " + std::to_string((long)buffer));
|
||||
#ifdef USE_MSGPACK
|
||||
size_t length = serializeMsgPack(Persistence::_document, buffer, size);
|
||||
#else
|
||||
size_t length = serializeJson(Persistence::_document, buffer, size);
|
||||
#endif
|
||||
extreme("Transport::write_path_table: serialized " + std::to_string(length) + " bytes");
|
||||
if (length < size) {
|
||||
Persistence::_buffer.resize(length);
|
||||
}
|
||||
else if (RNS::Utilities::OS::write_file(data, destination_table_path.c_str())) {
|
||||
RNS::extreme("Transport::save_path_table: wrote " + std::to_string(_destination_table.size()) + " entries, " + std::to_string(data.size()) + " bytes");
|
||||
}
|
||||
if (Persistence::_buffer.size() > 0) {
|
||||
#ifndef NDEBUG
|
||||
// CBA DEBUG Dump path table
|
||||
extreme("Transport::write_path_table: buffer addr: " + std::to_string((long)Persistence::_buffer.data()));
|
||||
extreme("Transport::write_path_table: buffer size " + std::to_string(Persistence::_buffer.size()) + " bytes");
|
||||
//extreme("SERIALIZED: destination_table");
|
||||
//extreme(Persistence::_buffer.toString());
|
||||
#endif
|
||||
// Check crc to see if data has changed before writing
|
||||
uint8_t crc = crypto_crc8(0, Persistence::_buffer.data(), Persistence::_buffer.size());
|
||||
if (_destination_table_crc > 0 && crc == _destination_table_crc) {
|
||||
extreme("Transport::write_path_table: no change detected, skipping write");
|
||||
}
|
||||
else if (RNS::Utilities::OS::write_file(destination_table_path.c_str(), Persistence::_buffer) == Persistence::_buffer.size()) {
|
||||
extreme("Transport::write_path_table: wrote " + std::to_string(_destination_table.size()) + " entries, " + std::to_string(Persistence::_buffer.size()) + " bytes");
|
||||
_destination_table_crc = crc;
|
||||
success = true;
|
||||
|
||||
#ifndef NDEBUG
|
||||
// CBA DEBUG Dump path table
|
||||
//extreme("FILE: destination_table");
|
||||
//if (OS::read_file("/destination_table", Persistence::_buffer) > 0) {
|
||||
// extreme(Persistence::_buffer.toString());
|
||||
//}
|
||||
#endif
|
||||
}
|
||||
else {
|
||||
RNS::extreme("Transport::save_path_table: write failed");
|
||||
extreme("Transport::write_path_table: write failed");
|
||||
}
|
||||
}
|
||||
else {
|
||||
RNS::extreme("Transport::save_path_table: failed to serialize");
|
||||
extreme("Transport::write_path_table: failed to serialize");
|
||||
}
|
||||
|
||||
if (success) {
|
||||
@@ -3487,7 +3512,62 @@ will announce it.
|
||||
return success;
|
||||
}
|
||||
|
||||
/*static*/ void Transport::save_tunnel_table() {
|
||||
/*static*/ void Transport::read_tunnel_table() {
|
||||
debug("Transport::read_tunnel_table");
|
||||
// TODO
|
||||
/*
|
||||
tunnel_table_path = RNS.Reticulum.storagepath+"/tunnels"
|
||||
if os.path.isfile(tunnel_table_path) and not Transport.owner.is_connected_to_shared_instance:
|
||||
serialised_tunnels = []
|
||||
try:
|
||||
file = open(tunnel_table_path, "rb")
|
||||
serialised_tunnels = umsgpack.unpackb(file.read())
|
||||
file.close()
|
||||
|
||||
for serialised_tunnel in serialised_tunnels:
|
||||
tunnel_id = serialised_tunnel[0]
|
||||
interface_hash = serialised_tunnel[1]
|
||||
serialised_paths = serialised_tunnel[2]
|
||||
expires = serialised_tunnel[3]
|
||||
|
||||
tunnel_paths = {}
|
||||
for serialised_entry in serialised_paths:
|
||||
destination_hash = serialised_entry[0]
|
||||
timestamp = serialised_entry[1]
|
||||
received_from = serialised_entry[2]
|
||||
hops = serialised_entry[3]
|
||||
expires = serialised_entry[4]
|
||||
random_blobs = serialised_entry[5]
|
||||
receiving_interface = Transport.find_interface_from_hash(serialised_entry[6])
|
||||
announce_packet = Transport.get_cached_packet(serialised_entry[7])
|
||||
|
||||
if announce_packet != None:
|
||||
announce_packet.unpack()
|
||||
// We increase the hops, since reading a packet
|
||||
// from cache is equivalent to receiving it again
|
||||
// over an interface. It is cached with it's non-
|
||||
// increased hop-count.
|
||||
announce_packet.hops += 1
|
||||
|
||||
tunnel_path = [timestamp, received_from, hops, expires, random_blobs, receiving_interface, announce_packet]
|
||||
tunnel_paths[destination_hash] = tunnel_path
|
||||
|
||||
tunnel = [tunnel_id, None, tunnel_paths, expires]
|
||||
Transport.tunnels[tunnel_id] = tunnel
|
||||
|
||||
if len(Transport.destination_table) == 1:
|
||||
specifier = "entry"
|
||||
else:
|
||||
specifier = "entries"
|
||||
|
||||
RNS.log("Loaded "+str(len(Transport.tunnels))+" tunnel table "+specifier+" from storage", RNS.LOG_VERBOSE)
|
||||
|
||||
except Exception as e:
|
||||
RNS.log("Could not load tunnel table from storage, the contained exception was: "+str(e), RNS.LOG_ERROR)
|
||||
*/
|
||||
}
|
||||
|
||||
/*static*/ void Transport::write_tunnel_table() {
|
||||
// TODO
|
||||
/*
|
||||
if not Transport.owner.is_connected_to_shared_instance:
|
||||
@@ -3568,9 +3648,60 @@ will announce it.
|
||||
|
||||
/*static*/ void Transport::persist_data() {
|
||||
extreme("Transport::persist_data()");
|
||||
save_packet_hashlist();
|
||||
save_path_table();
|
||||
save_tunnel_table();
|
||||
write_packet_hashlist();
|
||||
write_path_table();
|
||||
write_tunnel_table();
|
||||
}
|
||||
|
||||
/*static*/ void Transport::clean_caches() {
|
||||
extreme("Transport::clean_caches()");
|
||||
// CBA Remove cached packets no longer in path list
|
||||
std::list<std::string> files = OS::list_directory(Reticulum::_cachepath.c_str());
|
||||
for (auto& file : files) {
|
||||
extreme("Transport::clean_caches: Checking for use of cached packet " + file);
|
||||
bool found = false;
|
||||
for (auto& [destination_hash, destination_entry] : _destination_table) {
|
||||
if (file.compare(destination_entry._announce_packet.toHex()) == 0) {
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!found) {
|
||||
extreme("Transport::clean_caches: Removing cached packet " + file);
|
||||
std::string packet_cache_path = Reticulum::_cachepath + "/" + file;
|
||||
OS::remove_file(packet_cache_path.c_str());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*static*/ void Transport::dump_stats() {
|
||||
|
||||
#ifndef NDEBUG
|
||||
// memory
|
||||
// storage
|
||||
// _destinations
|
||||
// _destination_table
|
||||
// _reverse_table
|
||||
// _announce_table
|
||||
// _held_announces
|
||||
head("mem: " + std::to_string(OS::memory_available()) + " flash: " + std::to_string(OS::storage_available()) + " paths: " + std::to_string(_destination_table.size()) + " dsts: " + std::to_string(_destinations.size()) + " revr: " + std::to_string(_reverse_table.size()) + " annc: " + std::to_string(_announce_table.size()) + " held: " + std::to_string(_held_announces.size()), LOG_EXTREME);
|
||||
// _path_requests
|
||||
// _discovery_path_requests
|
||||
// _pending_local_path_requests
|
||||
// _discovery_pr_tags
|
||||
// _control_destinations
|
||||
// _control_hashes
|
||||
extreme("preqs: " + std::to_string(_path_requests.size()) + " dpreqs: " + std::to_string(_discovery_path_requests.size()) + " ppreqs: " + std::to_string(_pending_local_path_requests.size()) + " dprt: " + std::to_string(_discovery_pr_tags.size()) + " cdsts: " + std::to_string(_control_destinations.size()) + " chshs: " + std::to_string(_control_hashes.size()));
|
||||
// _packet_hashlist
|
||||
// _receipts
|
||||
// _link_table
|
||||
// _pending_links
|
||||
// _active_links
|
||||
// _tunnels
|
||||
extreme("phl: " + std::to_string(_packet_hashlist.size()) + " rcp: " + std::to_string(_receipts.size()) + " lt: " + std::to_string(_link_table.size()) + " pl: " + std::to_string(_pending_links.size()) + " al: " + std::to_string(_active_links.size()) + " tun: " + std::to_string(_tunnels.size()));
|
||||
extreme("pin: " + std::to_string(_packets_received) + " pout: " + std::to_string(_packets_sent) + " dadd: " + std::to_string(_destinations_added) + "\r\n");
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
/*static*/ void Transport::exit_handler() {
|
||||
@@ -3599,3 +3730,74 @@ will announce it.
|
||||
|
||||
return {Type::NONE};
|
||||
}
|
||||
|
||||
/*static*/ void Transport::cull_path_table() {
|
||||
if (_destination_table.size() > _path_table_maxsize) {
|
||||
// TODO prune by age, or better yet by last use
|
||||
/*
|
||||
std::map<Bytes, DestinationEntry>::iterator iter = _destination_table.begin();
|
||||
// naively erase from front of table
|
||||
std::advance(iter, _destination_table.size() - _path_table_maxsize + 1);
|
||||
_destination_table.erase(_destination_table.begin(), iter);
|
||||
*/
|
||||
/*
|
||||
uint16_t count = 0;
|
||||
std::set<DestinationEntry> sorted_values;
|
||||
MapToValues(_destination_table, sorted_values);
|
||||
for (auto& destination_entry : sorted_values) {
|
||||
Packet announce_packet = destination_entry.announce_packet();
|
||||
extreme("Transport::cull_path_table: Removing destination " + announce_packet.destination_hash().toHex() + " from path table");
|
||||
// Remove destination from path table
|
||||
if (_destination_table.erase(announce_packet.destination_hash()) < 1) {
|
||||
warning("Failed to remove destination " + announce_packet.destination_hash().toHex() + " from path table");
|
||||
}
|
||||
// Remove announce packet from packet table
|
||||
if (_packet_table.erase(destination_entry._announce_packet) < 1) {
|
||||
warning("Failed to remove packet " + destination_entry._announce_packet.toHex() + " from packet table");
|
||||
}
|
||||
// Remove cached packet file
|
||||
std::string packet_cache_path = Reticulum::_cachepath + "/" + destination_entry._announce_packet.toHex();
|
||||
if (OS::file_exists(packet_cache_path.c_str())) {
|
||||
OS::remove_file(packet_cache_path.c_str());
|
||||
}
|
||||
++count;
|
||||
if (_destination_table.size() <= _path_table_maxsize) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
debug("Removed " + std::to_string(count) + " path(s) from path table");
|
||||
*/
|
||||
uint16_t count = 0;
|
||||
std::vector<std::pair<Bytes,DestinationEntry>> sorted_pairs;
|
||||
// Copy key/value pairs from map into vector
|
||||
std::for_each(_destination_table.begin(), _destination_table.end(), [&](const std::pair<const Bytes, DestinationEntry>& ref) {
|
||||
sorted_pairs.push_back(ref);
|
||||
});
|
||||
// Sort vector using specified comparator
|
||||
std::sort(sorted_pairs.begin(), sorted_pairs.end(), [](const std::pair<Bytes,DestinationEntry> &left, const std::pair<Bytes,DestinationEntry> &right) {
|
||||
return left.second._timestamp < right.second._timestamp;
|
||||
});
|
||||
// Iterate vector of sorted values
|
||||
for (auto& [destination_hash, destination_entry] : sorted_pairs) {
|
||||
extreme("Transport::cull_path_table: Removing destination " + destination_hash.toHex() + " from path table");
|
||||
// Remove destination from path table
|
||||
if (_destination_table.erase(destination_hash) < 1) {
|
||||
warning("Failed to remove destination " + destination_hash.toHex() + " from path table");
|
||||
}
|
||||
// Remove announce packet from packet table
|
||||
if (_packet_table.erase(destination_entry._announce_packet) < 1) {
|
||||
warning("Failed to remove packet " + destination_entry._announce_packet.toHex() + " from packet table");
|
||||
}
|
||||
// Remove cached packet file
|
||||
std::string packet_cache_path = Reticulum::_cachepath + "/" + destination_entry._announce_packet.toHex();
|
||||
if (OS::file_exists(packet_cache_path.c_str())) {
|
||||
OS::remove_file(packet_cache_path.c_str());
|
||||
}
|
||||
++count;
|
||||
if (_destination_table.size() <= _path_table_maxsize) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
debug("Removed " + std::to_string(count) + " path(s) from path table");
|
||||
}
|
||||
}
|
||||
+83
-10
@@ -69,12 +69,43 @@ namespace RNS {
|
||||
};
|
||||
|
||||
public:
|
||||
|
||||
class PacketEntry {
|
||||
public:
|
||||
PacketEntry() {}
|
||||
PacketEntry(const Bytes& raw, double sent_at, const Bytes& destination_hash) :
|
||||
_raw(raw),
|
||||
_sent_at(sent_at),
|
||||
_destination_hash(destination_hash)
|
||||
{
|
||||
}
|
||||
PacketEntry(const Packet& packet) :
|
||||
_raw(packet.raw()),
|
||||
_sent_at(packet.sent_at()),
|
||||
_destination_hash(packet.destination_hash())
|
||||
{
|
||||
}
|
||||
public:
|
||||
Bytes _raw;
|
||||
double _sent_at = 0;
|
||||
Bytes _destination_hash;
|
||||
bool _cached = false;
|
||||
#ifndef NDEBUG
|
||||
inline std::string debugString() const {
|
||||
std::string dump;
|
||||
dump = "PacketEntry: destination_hash=" + _destination_hash.toHex() +
|
||||
" sent_at=" + std::to_string(_sent_at);
|
||||
return dump;
|
||||
}
|
||||
#endif
|
||||
};
|
||||
|
||||
// CBA TODO Analyze safety of using Inrerface references here
|
||||
// CBA TODO Analyze safety of using Packet references here
|
||||
class DestinationEntry {
|
||||
public:
|
||||
DestinationEntry() {}
|
||||
DestinationEntry(double timestamp, const Bytes& received_from, uint8_t announce_hops, double expires, const std::set<Bytes>& random_blobs, Interface& receiving_interface, const Packet& packet) :
|
||||
DestinationEntry(double timestamp, const Bytes& received_from, uint8_t announce_hops, double expires, const std::set<Bytes>& random_blobs, const Bytes& receiving_interface, const Bytes& packet) :
|
||||
_timestamp(timestamp),
|
||||
_received_from(received_from),
|
||||
_hops(announce_hops),
|
||||
@@ -84,15 +115,24 @@ namespace RNS {
|
||||
_announce_packet(packet)
|
||||
{
|
||||
}
|
||||
public:
|
||||
inline Interface receiving_interface() const { return find_interface_from_hash(_receiving_interface); }
|
||||
inline Packet announce_packet() const { return get_cached_packet(_announce_packet); }
|
||||
public:
|
||||
double _timestamp = 0;
|
||||
Bytes _received_from;
|
||||
uint8_t _hops = 0;
|
||||
double _expires = 0;
|
||||
std::set<Bytes> _random_blobs;
|
||||
Interface _receiving_interface = {Type::NONE};
|
||||
//Interface _receiving_interface = {Type::NONE};
|
||||
Bytes _receiving_interface;
|
||||
//const Packet& _announce_packet;
|
||||
Packet _announce_packet = {Type::NONE};
|
||||
//Packet _announce_packet = {Type::NONE};
|
||||
Bytes _announce_packet;
|
||||
inline bool operator < (const DestinationEntry& entry) const {
|
||||
// sort by ascending timestamp (oldest entries at the top)
|
||||
return _timestamp < entry._timestamp;
|
||||
}
|
||||
#ifndef NDEBUG
|
||||
inline std::string debugString() const {
|
||||
std::string dump;
|
||||
@@ -101,8 +141,8 @@ namespace RNS {
|
||||
" hops=" + std::to_string(_hops) +
|
||||
" expires=" + std::to_string(_expires) +
|
||||
//" random_blobs=" + _random_blobs +
|
||||
" receiving_interface=" + _receiving_interface.toString() +
|
||||
" announce_packet=" + _announce_packet.toString();
|
||||
" receiving_interface=" + _receiving_interface.toHex() +
|
||||
" announce_packet=" + _announce_packet.toHex();
|
||||
dump += " random_blobs=(";
|
||||
for (auto& blob : _random_blobs) {
|
||||
dump += blob.toHex() + ",";
|
||||
@@ -265,8 +305,8 @@ namespace RNS {
|
||||
static void register_announce_handler(HAnnounceHandler handler);
|
||||
static void deregister_announce_handler(HAnnounceHandler handler);
|
||||
static Interface find_interface_from_hash(const Bytes& interface_hash);
|
||||
static bool should_cache(const Packet& packet);
|
||||
static bool cache(const Packet& packet, bool force_cache = false);
|
||||
static bool should_cache_packet(const Packet& packet);
|
||||
static bool cache_packet(const Packet& packet, bool force_cache = false);
|
||||
static Packet get_cached_packet(const Bytes& packet_hash);
|
||||
static bool clear_cached_packet(const Bytes& packet_hash);
|
||||
static bool cache_request_packet(const Packet& packet);
|
||||
@@ -290,21 +330,31 @@ namespace RNS {
|
||||
static void shared_connection_reappeared();
|
||||
static void drop_announce_queues();
|
||||
static bool announce_emitted(const Packet& packet);
|
||||
static void save_packet_hashlist();
|
||||
static bool save_path_table();
|
||||
static void save_tunnel_table();
|
||||
static void write_packet_hashlist();
|
||||
static bool read_path_table();
|
||||
static bool write_path_table();
|
||||
static void read_tunnel_table();
|
||||
static void write_tunnel_table();
|
||||
static void persist_data();
|
||||
static void clean_caches();
|
||||
static void dump_stats();
|
||||
static void exit_handler();
|
||||
|
||||
static Destination find_destination_from_hash(const Bytes& destination_hash);
|
||||
|
||||
// CBA
|
||||
static void cull_path_table();
|
||||
|
||||
// getters/setters
|
||||
static inline void set_receive_packet_callback(Callbacks::receive_packet callback) { _callbacks._receive_packet = callback; }
|
||||
static inline void set_transmit_packet_callback(Callbacks::transmit_packet callback) { _callbacks._transmit_packet = callback; }
|
||||
static inline void set_filter_packet_callback(Callbacks::filter_packet callback) { _callbacks._filter_packet = callback; }
|
||||
static inline const Reticulum& reticulum() { return _owner; }
|
||||
static inline const Identity& identity() { return _identity; }
|
||||
inline static uint16_t path_table_maxsize() { return _path_table_maxsize; }
|
||||
inline static void path_table_maxsize(uint16_t path_table_maxsize) { _path_table_maxsize = path_table_maxsize; }
|
||||
inline static uint16_t probe_destination_enabled() { return _path_table_maxpersist; }
|
||||
inline static void path_table_maxpersist(uint16_t path_table_maxpersist) { _path_table_maxpersist = path_table_maxpersist; }
|
||||
|
||||
private:
|
||||
// CBA MUST use references to interfaces here in order for virtul overrides for send/receive to work
|
||||
@@ -359,6 +409,9 @@ namespace RNS {
|
||||
|
||||
static std::map<Bytes, const Interface&> _pending_local_path_requests;
|
||||
|
||||
// CBA
|
||||
static std::map<Bytes, PacketEntry> _packet_table; // A lookup table containing announce packets for known paths
|
||||
|
||||
//z _local_client_rssi_cache = []
|
||||
//z _local_client_snr_cache = []
|
||||
static uint16_t _LOCAL_CLIENT_CACHE_MAXSIZE;
|
||||
@@ -382,6 +435,7 @@ namespace RNS {
|
||||
|
||||
// CBA
|
||||
static uint16_t _path_table_maxsize;
|
||||
static uint16_t _path_table_maxpersist;
|
||||
static double _last_saved;
|
||||
static float _save_interval;
|
||||
static uint8_t _destination_table_crc;
|
||||
@@ -391,6 +445,25 @@ namespace RNS {
|
||||
|
||||
// CBA
|
||||
static Callbacks _callbacks;
|
||||
|
||||
// CBA Stats
|
||||
static uint32_t _packets_sent;
|
||||
static uint32_t _packets_received;
|
||||
static uint32_t _destinations_added;
|
||||
};
|
||||
|
||||
template <typename M, typename S>
|
||||
void MapToValues(const M& m, S& s) {
|
||||
for (typename M::const_iterator it = m.begin(); it != m.end(); ++it) {
|
||||
s.insert(it->second);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename M, typename S>
|
||||
void MapToPairs(const M& m, S& s) {
|
||||
for (typename M::const_iterator it = m.begin(); it != m.end(); ++it) {
|
||||
s.push_back(*it);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+20
-6
@@ -12,11 +12,15 @@ namespace RNS { namespace Type {
|
||||
NONE
|
||||
};
|
||||
|
||||
namespace Persistence {
|
||||
|
||||
static const uint16_t DOCUMENT_MAXSIZE = 8192;
|
||||
static const uint16_t BUFFER_MAXSIZE = Persistence::DOCUMENT_MAXSIZE * 1.5; // Json write buffer of 1.5 times document seems to be sufficient
|
||||
|
||||
}
|
||||
|
||||
namespace Reticulum {
|
||||
|
||||
// Future minimum will probably be locked in at 251 bytes to support
|
||||
// networks with segments of different MTUs. Absolute minimum is 219.
|
||||
static const uint16_t MTU = 500;
|
||||
/*
|
||||
The MTU that Reticulum adheres to, and will expect other peers to
|
||||
adhere to. By default, the MTU is 507 bytes. In custom RNS network
|
||||
@@ -26,7 +30,11 @@ namespace RNS { namespace Type {
|
||||
|
||||
Unless you really know what you are doing, the MTU should be left at
|
||||
the default value.
|
||||
|
||||
Future minimum will probably be locked in at 251 bytes to support
|
||||
networks with segments of different MTUs. Absolute minimum is 219.
|
||||
*/
|
||||
static const uint16_t MTU = 500;
|
||||
|
||||
static const uint16_t MAX_QUEUED_ANNOUNCES = 16384;
|
||||
static const uint32_t QUEUED_ANNOUNCE_LIFE = 60*60*24;
|
||||
@@ -71,11 +79,17 @@ namespace RNS { namespace Type {
|
||||
static const uint16_t MDU = MTU - HEADER_MAXSIZE - IFAC_MIN_SIZE;
|
||||
|
||||
static const uint32_t RESOURCE_CACHE = 60*60*24;
|
||||
static const uint16_t JOB_INTERVAL = 60*5;
|
||||
static const uint16_t CLEAN_INTERVAL = 60*15;
|
||||
// CBA TEST
|
||||
//static const uint16_t JOB_INTERVAL = 60*5;
|
||||
static const uint16_t JOB_INTERVAL = 60;
|
||||
// CBA TEST
|
||||
//static const uint16_t CLEAN_INTERVAL = 60*15;
|
||||
static const uint16_t CLEAN_INTERVAL = 60;
|
||||
// CBA
|
||||
//static const uint16_t PERSIST_INTERVAL = 60*60*12;
|
||||
static const uint16_t PERSIST_INTERVAL = 60*60;
|
||||
// CBA TEST
|
||||
//static const uint16_t PERSIST_INTERVAL = 60*60;
|
||||
static const uint16_t PERSIST_INTERVAL = 60;
|
||||
static const uint16_t GRACIOUS_PERSIST_INTERVAL = 60*5;
|
||||
|
||||
static const uint8_t DESTINATION_LENGTH = TRUNCATED_HASHLENGTH/8; // In bytes
|
||||
|
||||
+4
-19
@@ -18,21 +18,6 @@ using namespace RNS::Utilities;
|
||||
/*static*/ Filesystem OS::filesystem = {Type::NONE};
|
||||
/*static*/ uint64_t OS::timeOffset = 0;
|
||||
|
||||
/*static*/ int OS::freeMemory() {
|
||||
//#ifdef ARDUINO
|
||||
#if 0
|
||||
char top;
|
||||
#ifdef __arm__
|
||||
return &top - reinterpret_cast<char*>(sbrk(0));
|
||||
#elif defined(CORE_TEENSY) || (ARDUINO > 103 && ARDUINO != 151)
|
||||
return &top - __brkval;
|
||||
#else
|
||||
return __brkval ? &top - __brkval : &top - __malloc_heap_start;
|
||||
#endif
|
||||
return -1;
|
||||
#endif
|
||||
}
|
||||
|
||||
/*static*/ void OS::register_filesystem(Filesystem& filesystem) {
|
||||
extreme("Registering filesystem...");
|
||||
OS::filesystem = filesystem;
|
||||
@@ -51,18 +36,18 @@ using namespace RNS::Utilities;
|
||||
return filesystem.do_file_exists(file_path);
|
||||
}
|
||||
|
||||
/*static*/ const Bytes OS::read_file(const char* file_path) {
|
||||
/*static*/ size_t OS::read_file(const char* file_path, Bytes& data) {
|
||||
if (!filesystem) {
|
||||
throw std::runtime_error("Filesystem has not been registered");
|
||||
}
|
||||
return filesystem.do_read_file(file_path);
|
||||
return filesystem.do_read_file(file_path, data);
|
||||
}
|
||||
|
||||
/*static*/ bool OS::write_file(const Bytes& data, const char* file_path) {
|
||||
/*static*/ size_t OS::write_file(const char* file_path, const Bytes& data) {
|
||||
if (!filesystem) {
|
||||
throw std::runtime_error("Filesystem has not been registered");
|
||||
}
|
||||
return filesystem.do_write_file(data, file_path);
|
||||
return filesystem.do_write_file(file_path, data);
|
||||
}
|
||||
|
||||
/*static*/ bool OS::remove_file(const char* file_path) {
|
||||
|
||||
+41
-4
@@ -27,8 +27,6 @@ namespace RNS { namespace Utilities {
|
||||
static inline uint64_t getTimeOffset() { return timeOffset; }
|
||||
static inline void setTimeOffset(uint64_t offset) { timeOffset = offset; }
|
||||
|
||||
static int freeMemory();
|
||||
|
||||
#ifdef ARDUINO
|
||||
// return current time in milliseconds since startup
|
||||
static inline uint64_t ltime() {
|
||||
@@ -70,8 +68,8 @@ namespace RNS { namespace Utilities {
|
||||
static void deregister_filesystem();
|
||||
|
||||
static bool file_exists(const char* file_path);
|
||||
static const RNS::Bytes read_file(const char* file_path);
|
||||
static bool write_file(const RNS::Bytes& data, const char* file_path);
|
||||
static size_t read_file(const char* file_path, RNS::Bytes& data);
|
||||
static size_t write_file(const char* file_path, const RNS::Bytes& data);
|
||||
static bool remove_file(const char* file_path);
|
||||
static bool rename_file(const char* from_file_path, const char* to_file_path);
|
||||
static bool directory_exists(const char* directory_path);
|
||||
@@ -87,6 +85,45 @@ namespace RNS { namespace Utilities {
|
||||
return -1;
|
||||
}
|
||||
|
||||
static inline uint32_t memory_size() {
|
||||
}
|
||||
|
||||
/*
|
||||
static inline uint32_t memory_available()
|
||||
{
|
||||
uint32_t i;
|
||||
uint32_t len;
|
||||
uint8_t* ptr;
|
||||
|
||||
for(i=1;;i++) {
|
||||
len = i * 512;
|
||||
ptr = (uint8_t*)malloc(len);
|
||||
if (ptr == nullptr) {
|
||||
break;
|
||||
}
|
||||
free(ptr);
|
||||
}
|
||||
|
||||
len -= 512; //(i-1)*512 is aviliable size
|
||||
|
||||
return len;
|
||||
}
|
||||
*/
|
||||
|
||||
static inline size_t memory_available()
|
||||
{
|
||||
const size_t block_size = 256;
|
||||
size_t block_count;
|
||||
for(block_count = 1; ; block_count++) {
|
||||
void* ptr = malloc(block_count * block_size);
|
||||
if (ptr == nullptr) {
|
||||
break;
|
||||
}
|
||||
free(ptr);
|
||||
}
|
||||
return (block_count - 1) * block_size;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
} }
|
||||
@@ -0,0 +1,8 @@
|
||||
#include "Persistence.h"
|
||||
|
||||
#include "Bytes.h"
|
||||
|
||||
using namespace RNS;
|
||||
|
||||
/*static*/ DynamicJsonDocument _document(Type::Persistence::DOCUMENT_MAXSIZE);
|
||||
/*static*/ Bytes _buffer(Type::Persistence::BUFFER_MAXSIZE);
|
||||
+83
-43
@@ -3,6 +3,7 @@
|
||||
// CBA NOTE If headers for classes referenced in this file are not included here,
|
||||
// then they MUST be included BEFORE this header is included.
|
||||
#include "Transport.h"
|
||||
#include "Type.h"
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
@@ -152,8 +153,8 @@ namespace ArduinoJson {
|
||||
return dst.set(nullptr);
|
||||
}
|
||||
RNS::extreme("<<< Serializing packet hash " + src.get_hash().toHex());
|
||||
// Whenever a reference to a packet is serilized we must ensure that packet itself also gets serialized separately
|
||||
RNS::Transport::cache(src, true);
|
||||
// Whenever a reference to a packet is serialized we must ensure that packet itself also gets serialized separately
|
||||
RNS::Transport::cache_packet(src, true);
|
||||
return dst.set(src.get_hash().toHex());
|
||||
}
|
||||
static RNS::Packet fromJson(JsonVariantConst src) {
|
||||
@@ -178,23 +179,16 @@ namespace ArduinoJson {
|
||||
template <>
|
||||
struct Converter<RNS::Packet> {
|
||||
static bool toJson(const RNS::Packet& src, JsonVariant dst) {
|
||||
RNS::extreme("<<< Serializing packet");
|
||||
dst["hash"] = src.get_hash();
|
||||
RNS::extreme("<<< Serializing Packet");
|
||||
//dst["hash"] = src.get_hash();
|
||||
dst["raw"] = src.raw();
|
||||
dst["sent_at"] = src.sent_at();
|
||||
dst["destination_hash"] = src.get_hash();
|
||||
RNS::extreme("<<< Finished serializing packet");
|
||||
RNS::extreme("<<< Finished serializing Packet");
|
||||
return true;
|
||||
}
|
||||
static RNS::Packet fromJson(JsonVariantConst src) {
|
||||
RNS::extreme(">>> Deserializing packet");
|
||||
/*
|
||||
RNS::Packet packet;
|
||||
//packet.set_hash(src["hash"]);
|
||||
packet.sent_at(src["sent_at"]);
|
||||
RNS::Bytes destination_hash = src["destination_hash"];
|
||||
*/
|
||||
/**/
|
||||
RNS::extreme(">>> Deserializing Packet");
|
||||
RNS::Bytes raw = src["raw"];
|
||||
RNS::Packet packet(
|
||||
{RNS::Type::NONE},
|
||||
@@ -205,13 +199,45 @@ namespace ArduinoJson {
|
||||
RNS::Bytes destination_hash = src["destination_hash"];
|
||||
// set cached flag since pcket was read from cache
|
||||
packet.cached(true);
|
||||
/**/
|
||||
RNS::extreme(">>> Finished deserializing packet");
|
||||
RNS::extreme(">>> Finished deserializing Packet");
|
||||
return packet;
|
||||
}
|
||||
static bool checkJson(JsonVariantConst src) {
|
||||
return
|
||||
src["hash"].is<RNS::Bytes>() &&
|
||||
//src["hash"].is<RNS::Bytes>() &&
|
||||
src["raw"].is<RNS::Bytes>() &&
|
||||
src["sent_at"].is<double>() &&
|
||||
src["destination_hash"].is<RNS::Bytes>();
|
||||
}
|
||||
};
|
||||
|
||||
// ArduinoJSON serialization support for RNS::Transport::PacketEntry
|
||||
template <>
|
||||
struct Converter<RNS::Transport::PacketEntry> {
|
||||
static bool toJson(const RNS::Transport::PacketEntry& src, JsonVariant dst) {
|
||||
RNS::extreme("<<< Serializing Transport::PacketEntry");
|
||||
//dst["hash"] = src._hash;
|
||||
dst["raw"] = src._raw;
|
||||
dst["sent_at"] = src._sent_at;
|
||||
dst["destination_hash"] = src._destination_hash;
|
||||
RNS::extreme("<<< Finished serializing Transport::PacketEntry");
|
||||
return true;
|
||||
}
|
||||
static RNS::Transport::PacketEntry fromJson(JsonVariantConst src) {
|
||||
RNS::extreme(">>> Deserializing Transport::PacketEntry");
|
||||
RNS::Transport::PacketEntry dst;
|
||||
//dst._hash = src["hash"];
|
||||
dst._raw = src["raw"];
|
||||
dst._sent_at = src["sent_at"];
|
||||
dst._destination_hash = src["destination_hash"];
|
||||
// set cached flag since pcket was read from cache
|
||||
dst._cached = true;
|
||||
RNS::extreme(">>> Finished deserializing Transport::PacketEntry");
|
||||
return dst;
|
||||
}
|
||||
static bool checkJson(JsonVariantConst src) {
|
||||
return
|
||||
//src["hash"].is<RNS::Bytes>() &&
|
||||
src["raw"].is<RNS::Bytes>() &&
|
||||
src["sent_at"].is<double>() &&
|
||||
src["destination_hash"].is<RNS::Bytes>();
|
||||
@@ -228,20 +254,23 @@ namespace ArduinoJson {
|
||||
dst["announce_hops"] = src._hops;
|
||||
dst["expires"] = src._expires;
|
||||
dst["random_blobs"] = src._random_blobs;
|
||||
//dst["receiving_interface"] = src._receiving_interface;
|
||||
/*
|
||||
//dst["interface_hash"] = src._receiving_interface;
|
||||
if (src._receiving_interface) {
|
||||
dst["receiving_interface_hash"] = src._receiving_interface.get_hash();
|
||||
dst["interface_hash"] = src._receiving_interface.get_hash();
|
||||
}
|
||||
else {
|
||||
dst["receiving_interface_hash"] = nullptr;
|
||||
dst["interface_hash"] = nullptr;
|
||||
}
|
||||
// CBA TODO Move packet serialization to *after* destination table serialization since packets are useless
|
||||
// anyway if there's no space to left to write destination table.
|
||||
// Whenever a reference to a packet is serialized we must ensure that packet itself also gets serialized separately
|
||||
//dst["packet"] = src._announce_packet;
|
||||
if (src._announce_packet) {
|
||||
dst["packet_hash"] = src._announce_packet.get_hash();
|
||||
// Only cache packet if not already cached
|
||||
if (!src._announce_packet.cached()) {
|
||||
if (RNS::Transport::cache(src._announce_packet, true)) {
|
||||
if (RNS::Transport::cache_packet(src._announce_packet, true)) {
|
||||
const_cast<RNS::Packet&>(src._announce_packet).cached(true);
|
||||
}
|
||||
}
|
||||
@@ -252,20 +281,23 @@ namespace ArduinoJson {
|
||||
else {
|
||||
dst["packet_hash"] = nullptr;
|
||||
}
|
||||
*/
|
||||
dst["interface_hash"] = src._receiving_interface;
|
||||
dst["packet_hash"] = src._announce_packet;
|
||||
RNS::extreme("<<< Finished Serializing Transport::DestinationEntry");
|
||||
return true;
|
||||
}
|
||||
static RNS::Transport::DestinationEntry fromJson(JsonVariantConst src) {
|
||||
RNS::extreme(">>> Deserializing Transport::DestinationEntry");
|
||||
/**/
|
||||
RNS::Transport::DestinationEntry dst;
|
||||
dst._timestamp = src["timestamp"];
|
||||
dst._received_from = src["received_from"];
|
||||
dst._hops = src["announce_hops"];
|
||||
dst._expires = src["expires"];
|
||||
dst._random_blobs = src["random_blobs"].as<std::set<RNS::Bytes>>();
|
||||
//dst._receiving_interface = src["receiving_interface"];
|
||||
RNS::Bytes interface_hash = src["receiving_interface_hash"];
|
||||
/*
|
||||
//dst._receiving_interface = src["interface_hash"];
|
||||
RNS::Bytes interface_hash = src["interface_hash"];
|
||||
if (interface_hash) {
|
||||
// Query transport for matching interface
|
||||
dst._receiving_interface = RNS::Transport::find_interface_from_hash(interface_hash);
|
||||
@@ -276,7 +308,9 @@ namespace ArduinoJson {
|
||||
// Query transport for matching packet
|
||||
dst._announce_packet = RNS::Transport::get_cached_packet(packet_hash);
|
||||
}
|
||||
/**/
|
||||
*/
|
||||
dst._receiving_interface = src["interface_hash"];
|
||||
dst._announce_packet = src["packet_hash"];
|
||||
/*
|
||||
//RNS::Transport::DestinationEntry dst(src["timestamp"], src["received_from"], src["announce_hops"], src["expires"], src["random_blobs"], src["receiving_interface"], src["packet"]);
|
||||
RNS::Transport::DestinationEntry dst(
|
||||
@@ -299,7 +333,7 @@ namespace ArduinoJson {
|
||||
src["announce_hops"].is<int>() &&
|
||||
src["expires"].is<double>() &&
|
||||
src["random_blobs"].is<std::set<RNS::Bytes>>() &&
|
||||
src["receiving_interface_hash"].is<RNS::Bytes>() &&
|
||||
src["interface_hash"].is<RNS::Bytes>() &&
|
||||
src["packet_hash"].is<RNS::Bytes>();
|
||||
}
|
||||
};
|
||||
@@ -308,19 +342,24 @@ namespace ArduinoJson {
|
||||
|
||||
namespace RNS { namespace Persistence {
|
||||
|
||||
template <typename T> bool serialize(const T& obj, const char* file_path, size_t max_size) {
|
||||
DynamicJsonDocument doc(1024);
|
||||
doc.set(obj);
|
||||
RNS::Bytes data;
|
||||
size_t length = serializeJson(doc, data.writable(max_size), max_size);
|
||||
//size_t length = serializeMsgPack(doc, data.writable(max_size), max_size);
|
||||
if (length < max_size) {
|
||||
data.resize(length);
|
||||
static DynamicJsonDocument _document(Type::Persistence::DOCUMENT_MAXSIZE);
|
||||
static Bytes _buffer(Type::Persistence::BUFFER_MAXSIZE);
|
||||
|
||||
template <typename T> bool serialize(const T& obj, const char* file_path) {
|
||||
_document.set(obj);
|
||||
size_t size = _buffer.capacity();
|
||||
#ifdef USE_MSGPACK
|
||||
size_t length = serializeMsgPack(_document, _buffer.writable(size), size);
|
||||
#else
|
||||
size_t length = serializeJson(_document, _buffer.writable(size), size);
|
||||
#endif
|
||||
if (length < size) {
|
||||
_buffer.resize(length);
|
||||
}
|
||||
RNS::extreme("Persistence::serialize: serialized " + std::to_string(length) + " bytes");
|
||||
if (length > 0) {
|
||||
if (RNS::Utilities::OS::write_file(data, file_path)) {
|
||||
RNS::extreme("Persistence::serialize: wrote " + std::to_string(data.size()) + " bytes");
|
||||
if (RNS::Utilities::OS::write_file(file_path, _buffer) == _buffer.size()) {
|
||||
RNS::extreme("Persistence::serialize: wrote " + std::to_string(_buffer.size()) + " bytes");
|
||||
}
|
||||
else {
|
||||
RNS::extreme("Persistence::serialize: write failed");
|
||||
@@ -334,16 +373,17 @@ namespace RNS { namespace Persistence {
|
||||
return true;
|
||||
}
|
||||
template <typename T> bool deserialize(T& obj, const char* file_path) {
|
||||
DynamicJsonDocument doc(1024);
|
||||
RNS::Bytes data = RNS::Utilities::OS::read_file(file_path);
|
||||
if (data) {
|
||||
RNS::extreme("Persistence::deserialize: read: " + std::to_string(data.size()) + " bytes");
|
||||
//RNS::extreme("testDeserializeVector: data: " + data.toString());
|
||||
DeserializationError error = deserializeJson(doc, data.data());
|
||||
//DeserializationError error = deserializeMsgPack(doc, data.data());
|
||||
if (RNS::Utilities::OS::read_file(file_path, _buffer) > 0) {
|
||||
RNS::extreme("Persistence::deserialize: read: " + std::to_string(_buffer.size()) + " bytes");
|
||||
//RNS::extreme("testDeserializeVector: data: " + _buffer.toString());
|
||||
#ifdef USE_MSGPACK
|
||||
DeserializationError error = deserializeMsgPack(_document, _buffer.data());
|
||||
#else
|
||||
DeserializationError error = deserializeJson(_document, _buffer.data());
|
||||
#endif
|
||||
if (!error) {
|
||||
RNS::extreme("Persistence::deserialize: successfully deserialized");
|
||||
obj = doc.as<T>();
|
||||
obj = _document.as<T>();
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
|
||||
Reference in new issue
Block a user