mirror of
https://github.com/oxen-io/lokinet
synced 2023-12-14 06:53:00 +01:00
[DRAFT] add embedded lokinet api for configuration.
feedback from jason and morgan desired.
This commit is contained in:
parent
a294c81f0d
commit
d726fc1416
93
include/lokinet/lokinet_config.h
Normal file
93
include/lokinet/lokinet_config.h
Normal file
|
@ -0,0 +1,93 @@
|
|||
#pragma once
|
||||
|
||||
#include "lokinet_export.h"
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
struct lokinet_config;
|
||||
|
||||
/// load a lokinet configuration from an open file handle.
|
||||
struct lokinet_config* EXPORT
|
||||
lokinet_config_from_file(FILE* f);
|
||||
|
||||
/// free a lokinet config we loaded.
|
||||
void EXPORT
|
||||
lokinet_config_free(struct lokinet_config*);
|
||||
|
||||
struct lokinet_config_iter;
|
||||
|
||||
/// open an iterator to iterate over all config values in a lokinet config in the section `sect`
|
||||
struct lokinet_config_iter* EXPORT
|
||||
lokinet_config_iterate_section(struct lokinet_config*, const char* sect);
|
||||
|
||||
/// fetch the next config value on this iterator.
|
||||
/// returns false if there are no more items left to iterate over.
|
||||
/// fills up opt_name and opt_val with option name and option value, or sets both to hold NULL if
|
||||
/// at the last entry.
|
||||
bool EXPORT
|
||||
lokinet_config_iter_next(
|
||||
struct lokinet_config_iter*, const char** opt_name, const char** opt_val);
|
||||
|
||||
/// close a previously openend lokinet_config_iter
|
||||
void EXPORT
|
||||
lokinet_config_iter_free(struct lokinet_config_iter*);
|
||||
|
||||
/// add to the lokinet config a config value for the section `sect` `opt_name=val`
|
||||
void EXPORT
|
||||
lokinet_config_add_opt(
|
||||
struct lokinet_config*, const char* sect, const char* opt_name, const char* val);
|
||||
|
||||
/// write current config to an open file.
|
||||
/// returns number of bytes written or -1 on error.
|
||||
ssize_t EXPORT
|
||||
lokinet_config_to_file(struct lokinet_config*, FILE* f);
|
||||
|
||||
/// helper function for reading config from file.
|
||||
static struct lokinet_config* EXPORT
|
||||
lokinet_config_read(const char* fname)
|
||||
{
|
||||
struct lokinet_config* conf;
|
||||
FILE* f;
|
||||
f = fopen(fname, "rb");
|
||||
if (f == NULL)
|
||||
{
|
||||
fprintf(stderr, "failed to open %s: %s", fname, strerror(errno));
|
||||
return NULL;
|
||||
}
|
||||
conf = lokinet_config_from_file(f);
|
||||
if (conf == NULL)
|
||||
fprintf(stderr, "failed to parse config file %s", fname);
|
||||
fclose(f);
|
||||
return conf;
|
||||
}
|
||||
|
||||
/// helper function for writing config to file.
|
||||
static bool EXPORT
|
||||
lokinet_config_write(struct lokinet_config* conf, const char* fname)
|
||||
{
|
||||
ssize_t n;
|
||||
FILE* f;
|
||||
f = fopen(fname, "wb");
|
||||
if (f == NULL)
|
||||
{
|
||||
fprintf(stderr, "failed to open %s: %s", fname, strerror(errno));
|
||||
return NULL;
|
||||
}
|
||||
n = lokinet_config_to_file(conf, f);
|
||||
if (n == -1)
|
||||
fprintf(stderr, "failed to write config file %s", fname);
|
||||
fclose(f);
|
||||
return conf;
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
|
@ -1,5 +1,6 @@
|
|||
#pragma once
|
||||
|
||||
#include "lokinet_config.h"
|
||||
#include "lokinet_export.h"
|
||||
|
||||
#include <stdbool.h>
|
||||
|
@ -13,10 +14,16 @@ extern "C"
|
|||
|
||||
struct lokinet_context;
|
||||
|
||||
/// allocate a new lokinet context
|
||||
struct lokinet_context* EXPORT
|
||||
struct lokinet_config* EXPORT
|
||||
lokinet_context_new();
|
||||
|
||||
/// allocate a new lokinet context, passing in a loaded config, finalizing the config's loaded
|
||||
/// values and injecting them into the lokinet context. if config is NULL we will load sensible
|
||||
/// defaults. after calling lokinet_context_new the config can be released by calling
|
||||
/// lokinet_config_free().
|
||||
struct lokinet_context* EXPORT
|
||||
lokinet_context_from_config(struct lokinet_config*);
|
||||
|
||||
/// free a context allocated by lokinet_context_new
|
||||
void EXPORT
|
||||
lokinet_context_free(struct lokinet_context*);
|
||||
|
|
|
@ -193,17 +193,28 @@ namespace
|
|||
};
|
||||
} // namespace
|
||||
|
||||
struct lokinet_config
|
||||
{
|
||||
std::shraed_ptr<llarp::Config> impl;
|
||||
};
|
||||
|
||||
struct lokinet_context
|
||||
{
|
||||
std::mutex m_access;
|
||||
|
||||
std::shared_ptr<llarp::Context> impl = std::make_shared<Context>();
|
||||
std::shared_ptr<llarp::Config> config = llarp::Config::EmbeddedConfig();
|
||||
std::shared_ptr<llarp::Config> config;
|
||||
|
||||
std::unique_ptr<std::thread> runner;
|
||||
|
||||
int _socket_id = 0;
|
||||
|
||||
explicit lokinet_context(const std::shared_ptr<llarp::Config>& conf = nullptr) : config{conf}
|
||||
{
|
||||
if (not config)
|
||||
config = llarp::Config::EmbeddedConfig();
|
||||
}
|
||||
|
||||
~lokinet_context()
|
||||
{
|
||||
if (runner)
|
||||
|
@ -525,6 +536,12 @@ extern "C"
|
|||
return new lokinet_context{};
|
||||
}
|
||||
|
||||
struct lokinet_context* EXPORT
|
||||
lokinet_context_from_config(struct lokinet_config* cfg)
|
||||
{
|
||||
return new lokinet_context{cfg ? cfg->impl : nullptr};
|
||||
}
|
||||
|
||||
void EXPORT
|
||||
lokinet_context_free(struct lokinet_context* ctx)
|
||||
{
|
||||
|
|
Loading…
Reference in a new issue