mirror of https://github.com/oxen-io/lokinet
add python ffi example wrapper code
This commit is contained in:
parent
3f21cc5865
commit
e58bd10b23
|
@ -59,6 +59,7 @@ add_library(${SHARED_LIB} SHARED ${LIB_SRC})
|
|||
|
||||
add_executable(${EXE} ${EXE_SRC})
|
||||
target_link_libraries(${EXE} ${STATIC_LIB} ${LIBS})
|
||||
target_link_libraries(${SHARED_LIB} ${LIBS})
|
||||
|
||||
add_executable(rcutil daemon/rcutil.cpp)
|
||||
target_link_libraries(rcutil ${STATIC_LIB} ${LIBS})
|
||||
|
|
|
@ -0,0 +1,41 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
|
||||
from ctypes import *
|
||||
import signal
|
||||
import time
|
||||
import threading
|
||||
|
||||
class LLARP(threading.Thread):
|
||||
|
||||
lib = None
|
||||
ctx = None
|
||||
|
||||
def signal(self, sig):
|
||||
if self.ctx and self.lib:
|
||||
self.lib.llarp_main_signal(self.ctx, int(sig))
|
||||
|
||||
def run(self):
|
||||
code = self.lib.llarp_main_run(self.ctx)
|
||||
print ("llarp_main_run exited with status {}".format(code))
|
||||
|
||||
|
||||
def main():
|
||||
llarp = LLARP()
|
||||
llarp.lib = CDLL("./libllarp.so")
|
||||
llarp.ctx = llarp.lib.llarp_main_init(b'daemon.ini')
|
||||
if llarp.ctx:
|
||||
llarp.start()
|
||||
try:
|
||||
while True:
|
||||
print("busy loop")
|
||||
time.sleep(1)
|
||||
except KeyboardInterrupt:
|
||||
llarp.signal(signal.SIGINT)
|
||||
finally:
|
||||
llarp.lib.llarp_main_free(llarp.ctx)
|
||||
return
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
|
@ -17,10 +17,13 @@ main(int argc, char *argv[])
|
|||
const char *conffname = "daemon.ini";
|
||||
if(argc > 1)
|
||||
conffname = argv[1];
|
||||
|
||||
if(llarp_main_init(&ctx, conffname))
|
||||
ctx = llarp_main_init(conffname);
|
||||
int code = 1;
|
||||
if(ctx)
|
||||
{
|
||||
signal(SIGINT, handle_signal);
|
||||
return llarp_main_run(ctx);
|
||||
code = llarp_main_run(ctx);
|
||||
llarp_main_free(ctx);
|
||||
}
|
||||
return code;
|
||||
}
|
||||
|
|
|
@ -15,8 +15,8 @@ extern "C" {
|
|||
struct llarp_main;
|
||||
|
||||
/** initialize application context and load config */
|
||||
bool
|
||||
llarp_main_init(struct llarp_main **ptr, const char *fname);
|
||||
struct llarp_main *
|
||||
llarp_main_init(const char *fname);
|
||||
|
||||
/** handle signal for main context */
|
||||
void
|
||||
|
@ -27,7 +27,7 @@ int
|
|||
llarp_main_run(struct llarp_main *ptr);
|
||||
|
||||
void
|
||||
llarp_main_free(struct llarp_main **ptr);
|
||||
llarp_main_free(struct llarp_main *ptr);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
|
|
@ -211,11 +211,11 @@ struct llarp_main
|
|||
std::unique_ptr< llarp::Context > ctx;
|
||||
};
|
||||
|
||||
bool
|
||||
llarp_main_init(struct llarp_main **ptr, const char *fname)
|
||||
struct llarp_main *
|
||||
llarp_main_init(const char *fname)
|
||||
{
|
||||
if(!fname)
|
||||
return false;
|
||||
return nullptr;
|
||||
|
||||
llarp_main *m = new llarp_main;
|
||||
m->ctx.reset(new llarp::Context(std::cout));
|
||||
|
@ -223,10 +223,9 @@ llarp_main_init(struct llarp_main **ptr, const char *fname)
|
|||
{
|
||||
m->ctx->Close();
|
||||
delete m;
|
||||
return false;
|
||||
return nullptr;
|
||||
}
|
||||
*ptr = m;
|
||||
return true;
|
||||
return m;
|
||||
}
|
||||
|
||||
void
|
||||
|
@ -238,16 +237,13 @@ llarp_main_signal(struct llarp_main *ptr, int sig)
|
|||
int
|
||||
llarp_main_run(struct llarp_main *ptr)
|
||||
{
|
||||
auto code = ptr->ctx->Run();
|
||||
ptr->ctx->Close();
|
||||
return code;
|
||||
return ptr->ctx->Run();
|
||||
}
|
||||
|
||||
void
|
||||
llarp_main_free(struct llarp_main **ptr)
|
||||
llarp_main_free(struct llarp_main *ptr)
|
||||
{
|
||||
if(*ptr)
|
||||
delete *ptr;
|
||||
*ptr = nullptr;
|
||||
ptr->ctx->Close();
|
||||
delete ptr;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue