lokinet/jni/lokinet_config.cpp

76 lines
1.9 KiB
C++
Raw Normal View History

#include "lokinet_jni_common.hpp"
2019-10-08 14:52:01 +00:00
#include "network_loki_lokinet_LokinetConfig.h"
2019-10-08 14:52:01 +00:00
#include <llarp.hpp>
#include <llarp/config/config.hpp>
2019-10-08 14:52:01 +00:00
2019-10-09 12:42:05 +00:00
extern "C"
2019-10-08 14:52:01 +00:00
{
JNIEXPORT jobject JNICALL
Java_network_loki_lokinet_LokinetConfig_Obtain(JNIEnv* env, jclass, jstring dataDir)
2019-10-08 14:52:01 +00:00
{
auto conf = VisitStringAsStringView<llarp::Config*>(
env, dataDir, [](std::string_view val) -> llarp::Config* {
return new llarp::Config{val};
});
if (conf == nullptr)
2019-10-08 14:52:01 +00:00
return nullptr;
return env->NewDirectByteBuffer(conf, sizeof(llarp::Config));
2019-10-08 14:52:01 +00:00
}
JNIEXPORT void JNICALL
Java_network_loki_lokinet_LokinetConfig_Free(JNIEnv* env, jclass, jobject buf)
2019-10-08 14:52:01 +00:00
{
auto ptr = FromBuffer<llarp::Config>(env, buf);
delete ptr;
2019-10-08 14:52:01 +00:00
}
JNIEXPORT jboolean JNICALL
Java_network_loki_lokinet_LokinetConfig_Load(JNIEnv* env, jobject self)
{
auto conf = GetImpl<llarp::Config>(env, self);
if (conf == nullptr)
return JNI_FALSE;
if (conf->Load())
{
return JNI_TRUE;
}
return JNI_FALSE;
}
JNIEXPORT jboolean JNICALL
Java_network_loki_lokinet_LokinetConfig_Save(JNIEnv* env, jobject self)
2019-10-08 14:52:01 +00:00
{
auto conf = GetImpl<llarp::Config>(env, self);
if (conf == nullptr)
2019-10-08 14:52:01 +00:00
return JNI_FALSE;
try
{
conf->Save();
}
catch (...)
{
return JNI_FALSE;
}
return JNI_TRUE;
}
JNIEXPORT void JNICALL
Java_network_loki_lokinet_LokinetConfig_AddDefaultValue(
JNIEnv* env, jobject self, jstring section, jstring key, jstring value)
{
auto convert = [](std::string_view str) -> std::string { return std::string{str}; };
const auto sect = VisitStringAsStringView<std::string>(env, section, convert);
const auto k = VisitStringAsStringView<std::string>(env, key, convert);
const auto v = VisitStringAsStringView<std::string>(env, value, convert);
auto conf = GetImpl<llarp::Config>(env, self);
if (conf)
{
conf->AddDefault(sect, k, v);
}
2019-10-08 14:52:01 +00:00
}
}