WIP: Enhancements to persistence

This commit is contained in:
Chad Attermann committed 2024-06-03 08:13:45 -06:00
1 parent 63a16a5391
commit f415b5e0b5
14 files changed
+693 -288

No files matched your search

+106 -33
View File
@@ -2,7 +2,6 @@
#include "Transport.h"
#include "Log.h"
#include "Utilities/OS.h"
#include <RNG.h>
@@ -13,6 +12,7 @@
using namespace RNS;
using namespace RNS::Type::Reticulum;
using namespace RNS::Utilities;
/*static*/ std::string Reticulum::_storagepath;
/*static*/ std::string Reticulum::_cachepath;
@@ -78,43 +78,13 @@ Reticulum::Reticulum() : _object(new Object()) {
// CBA MOCK
#ifdef ARDUINO
_storagepath = "";
_cachepath = "";
_cachepath = "/cache";
#else
_storagepath = ".";
_cachepath = ".";
_cachepath = "./cache";
#endif
/* TODO
Reticulum.__transport_enabled = False
Reticulum.__use_implicit_proof = True
Reticulum.__allow_probes = False
Reticulum.panic_on_interface_error = False
self.local_interface_port = 37428
self.local_control_port = 37429
self.share_instance = True
self.rpc_listener = None
self.ifac_salt = Reticulum.IFAC_SALT
self.requested_loglevel = loglevel
self.requested_verbosity = verbosity
if self.requested_loglevel != None:
if self.requested_loglevel > RNS.LOG_EXTREME:
self.requested_loglevel = RNS.LOG_EXTREME
if self.requested_loglevel < RNS.LOG_CRITICAL:
self.requested_loglevel = RNS.LOG_CRITICAL
RNS.loglevel = self.requested_loglevel
self.is_shared_instance = False
self.is_connected_to_shared_instance = False
self.is_standalone_instance = False
self.jobs_thread = None
self.last_data_persist = time.time()
self.last_cache_clean = 0
if not os.path.isdir(Reticulum.storagepath):
os.makedirs(Reticulum.storagepath)
@@ -167,6 +137,36 @@ Reticulum::Reticulum() : _object(new Object()) {
mem("Reticulum default object created, this: " + std::to_string((uintptr_t)this) + ", data: " + std::to_string((uintptr_t)_object.get()));
}
/*p TODO
@staticmethod
def exit_handler():
# This exit handler is called whenever Reticulum is asked to
# shut down, and will in turn call exit handlers in other
# classes, saving necessary information to disk and carrying
# out cleanup operations.
RNS.Transport.exit_handler()
RNS.Identity.exit_handler()
@staticmethod
def sigint_handler(signal, frame):
RNS.Transport.detach_interfaces()
RNS.exit()
@staticmethod
def sigterm_handler(signal, frame):
RNS.Transport.detach_interfaces()
RNS.exit()
@staticmethod
def get_instance():
"""
Return the currently running Reticulum instance
"""
return Reticulum.__instance
*/
void Reticulum::start() {
Transport::start(*this);
@@ -175,8 +175,81 @@ void Reticulum::start() {
void Reticulum::loop() {
assert(_object);
if (!_object->_is_connected_to_shared_instance) {
if (OS::time() > (_jobs_last_run + JOB_INTERVAL)) {
jobs();
_jobs_last_run = OS::time();
}
RNS::Transport::loop();
}
// Perform random number gnerator housekeeping
RNG.loop();
}
void Reticulum::jobs() {
double now = OS::time();
if (now > _last_cache_clean + CLEAN_INTERVAL) {
clean_caches();
_last_cache_clean = OS::time();
}
if (now > _last_data_persist + PERSIST_INTERVAL) {
persist_data();
}
}
void Reticulum::should_persist_data() {
if (OS::time() > _last_data_persist + GRACIOUS_PERSIST_INTERVAL) {
persist_data();
}
}
void Reticulum::persist_data() {
Transport::persist_data();
Identity::persist_data();
_last_data_persist = OS::time();
}
void Reticulum::clean_caches() {
extreme("Cleaning resource and packet caches...");
double now = OS::time();
/*
// Clean resource caches
for (auto& filename : OS::list_directory(resourcepath) {
try {
if (filename.length() == (Type::Identity::HASHLENGTH//8)*2) {
std::string filepath = _resourcepath + "/" + filename;
//p mtime = os.path.getmtime(filepath)
//p age = now - mtime
//p if (age > Types::Reticulum.RESOURCE_CACHE) {
//p OS::remove_file(filepath);
//p }
}
}
catch (std::exception& e) {
error("Error while cleaning resources cache, the contained exception was: " + std::string(e.what()));
}
}
// Clean packet caches
for (auto& filename : OS::list_directory(_cachepath.c_str())) {
try {
if (filename.length() == (Type::Identity::HASHLENGTH/8)*2) {
std::string filepath = _cachepath + "/" + filename;
//p mtime = os.path.getmtime(filepath)
//p age = now - mtime
//p if (age > Types::Transport::DESTINATION_TIMEOUT) {
//p OS::remove_file(filepath);
//p }
}
}
catch (std::exception& e) {
error("Error while cleaning packet cache, the contained exception was: " + std::string(e.what()));
}
}
*/
}