fix ffi example

pull/7/head
Jeff Becker 6 years ago
parent b1eddbc70f
commit f65fee38be

@ -5,13 +5,22 @@ from ctypes import *
import signal
import time
import threading
import os
lib_file = os.path.join(os.path.realpath('.'), 'liblokinet.so')
class LLARP(threading.Thread):
class LokiNET(threading.Thread):
lib = None
ctx = None
def load(self, lib, conf):
self.lib = CDLL(lib)
self.lib.llarp_ensure_config(conf)
self.ctx = self.lib.llarp_main_init(conf)
return self.ctx != 0
def signal(self, sig):
if self.ctx and self.lib:
self.lib.llarp_main_signal(self.ctx, int(sig))
@ -20,21 +29,21 @@ class LLARP(threading.Thread):
code = self.lib.llarp_main_run(self.ctx)
print("llarp_main_run exited with status {}".format(code))
def close(self):
if self.lib and self.ctx:
self.lib.llarp_main_free(self.ctx)
def main():
llarp = LLARP()
llarp.lib = CDLL("./libllarp.so")
llarp.ctx = llarp.lib.llarp_main_init(b'daemon.ini')
if llarp.ctx:
llarp.start()
loki = LokiNET()
if loki.load(lib_file, b'daemon.ini'):
loki.start()
try:
while True:
print("busy loop")
time.sleep(1)
except KeyboardInterrupt:
llarp.signal(signal.SIGINT)
finally:
llarp.lib.llarp_main_free(llarp.ctx)
loki.close()
return

@ -1,47 +1,54 @@
#ifndef LLARP_CONFIG_H_
#define LLARP_CONFIG_H_
/**
* config.h
*
* library configuration utilties
*/
struct llarp_config;
/// allocate config
void
llarp_new_config(struct llarp_config **conf);
/// deallocate config
void
llarp_free_config(struct llarp_config **conf);
/// @brief return -1 on fail otherwiwse 0
int
llarp_load_config(struct llarp_config *conf, const char *fname);
/// config iterator configuration
struct llarp_config_iterator
#ifdef __cplusplus
extern "C"
{
/// a customizable pointer to pass data to iteration functor
void *user;
/// set by llarp_config_iter
struct llarp_config *conf;
/// visit (self, section, key, value)
void (*visit)(struct llarp_config_iterator *, const char *, const char *,
const char *);
};
/// iterator over "conf" and call visit functor defined in "iter"
void
llarp_config_iter(struct llarp_config *conf,
struct llarp_config_iterator *iter);
/// ensure configuration exists
/// populate with defaults if it does not exist
/// return if this succeeded
bool
llarp_ensure_config(const char *fname);
#endif
/**
* config.h
*
* library configuration utilties
*/
struct llarp_config;
/// allocate config
void
llarp_new_config(struct llarp_config **conf);
/// deallocate config
void
llarp_free_config(struct llarp_config **conf);
/// @brief return -1 on fail otherwiwse 0
int
llarp_load_config(struct llarp_config *conf, const char *fname);
/// config iterator configuration
struct llarp_config_iterator
{
/// a customizable pointer to pass data to iteration functor
void *user;
/// set by llarp_config_iter
struct llarp_config *conf;
/// visit (self, section, key, value)
void (*visit)(struct llarp_config_iterator *, const char *, const char *,
const char *);
};
/// iterator over "conf" and call visit functor defined in "iter"
void
llarp_config_iter(struct llarp_config *conf,
struct llarp_config_iterator *iter);
/// ensure configuration exists
/// populate with defaults if it does not exist
/// return if this succeeded
bool
llarp_ensure_config(const char *fname);
#ifdef __cplusplus
}
#endif
#endif

@ -41,75 +41,79 @@ namespace llarp
} // namespace llarp
void
llarp_new_config(struct llarp_config **conf)
extern "C"
{
llarp_config *c = new llarp_config;
*conf = c;
}
void
llarp_free_config(struct llarp_config **conf)
{
if(*conf)
delete *conf;
*conf = nullptr;
}
void
llarp_new_config(struct llarp_config **conf)
{
llarp_config *c = new llarp_config;
*conf = c;
}
int
llarp_load_config(struct llarp_config *conf, const char *fname)
{
if(!conf->impl.Load(fname))
return -1;
return 0;
}
void
llarp_free_config(struct llarp_config **conf)
{
if(*conf)
delete *conf;
*conf = nullptr;
}
void
llarp_config_iter(struct llarp_config *conf, struct llarp_config_iterator *iter)
{
iter->conf = conf;
std::map< std::string, llarp::Config::section_t & > sections = {
{"network", conf->impl.network}, {"connect", conf->impl.connect},
{"bind", conf->impl.iwp_links}, {"netdb", conf->impl.netdb},
{"dns", conf->impl.dns}, {"services", conf->impl.services}};
int
llarp_load_config(struct llarp_config *conf, const char *fname)
{
if(!conf->impl.Load(fname))
return -1;
return 0;
}
for(const auto item : conf->impl.router)
iter->visit(iter, "router", item.first.c_str(), item.second.c_str());
void
llarp_config_iter(struct llarp_config *conf,
struct llarp_config_iterator *iter)
{
iter->conf = conf;
std::map< std::string, llarp::Config::section_t & > sections = {
{"network", conf->impl.network}, {"connect", conf->impl.connect},
{"bind", conf->impl.iwp_links}, {"netdb", conf->impl.netdb},
{"dns", conf->impl.dns}, {"services", conf->impl.services}};
for(const auto section : sections)
for(const auto item : section.second)
iter->visit(iter, section.first.c_str(), item.first.c_str(),
item.second.c_str());
}
for(const auto item : conf->impl.router)
iter->visit(iter, "router", item.first.c_str(), item.second.c_str());
bool
llarp_ensure_config(const char *fname)
{
std::error_code ec;
if(fs::exists(fname, ec))
return true;
if(ec)
{
llarp::LogError(ec);
return false;
for(const auto section : sections)
for(const auto item : section.second)
iter->visit(iter, section.first.c_str(), item.first.c_str(),
item.second.c_str());
}
std::ofstream f(fname);
if(!f.is_open())
bool
llarp_ensure_config(const char *fname)
{
llarp::LogError("failed to open ", fname, " for writing");
return false;
}
std::error_code ec;
if(fs::exists(fname, ec))
return true;
if(ec)
{
llarp::LogError(ec);
return false;
}
std::ofstream f(fname);
if(!f.is_open())
{
llarp::LogError("failed to open ", fname, " for writing");
return false;
}
f << "[netdb]" << std::endl;
f << "dir=netdb" << std::endl;
f << "[bind]" << std::endl;
f << "[netdb]" << std::endl;
f << "dir=netdb" << std::endl;
f << "[bind]" << std::endl;
std::string ifname;
std::string ifname;
if(llarp::GetBestNetIF(ifname, AF_INET))
f << ifname << "=1090" << std::endl;
if(llarp::GetBestNetIF(ifname, AF_INET))
f << ifname << "=1090" << std::endl;
llarp::LogInfo("Generated new config ", fname);
return true;
llarp::LogInfo("Generated new config ", fname);
return true;
}
}
Loading…
Cancel
Save