initial pybind11 introspection code

pull/1184/head
Jeff Becker 4 years ago committed by Thomas Winget
parent 8d03e6dd3c
commit 1fdb8b4c94

@ -28,7 +28,6 @@ option(USE_SHELLHOOKS "enable shell hooks on compile time (dangerous)" OFF)
option(WARNINGS_AS_ERRORS "treat all warnings as errors. turn off for development, on for release" OFF)
option(TRACY_ROOT "include tracy profiler source" OFF)
option(WITH_TESTS "build unit tests" ON)
#option(WITH_SYSTEMD ...) defined below
include(cmake/target_link_libraries_system.cmake)
include(cmake/add_import_library.cmake)
@ -264,6 +263,7 @@ if(SUBMODULE_CHECK)
check_submodule(external/ghc-filesystem)
check_submodule(external/optional-lite)
check_submodule(external/date)
check_submodule(external/pybind11)
endif()
endif()
@ -277,6 +277,7 @@ add_subdirectory(external/cxxopts EXCLUDE_FROM_ALL)
add_subdirectory(external/ghc-filesystem)
add_subdirectory(external/optional-lite EXCLUDE_FROM_ALL)
add_subdirectory(external/date EXCLUDE_FROM_ALL)
add_subdirectory(external/pybind11 EXCLUDE_FROM_ALL)
if(ANDROID)
list(APPEND LIBS log)
@ -293,6 +294,9 @@ add_subdirectory(crypto)
add_subdirectory(llarp)
add_subdirectory(libabyss)
add_subdirectory(daemon)
add_subdirectory(pybind)
if (NOT SHADOW)
if(WITH_TESTS)

@ -197,8 +197,8 @@ set(LIB_SRC
service/sendcontext.cpp
service/session.cpp
service/tag_lookup_job.cpp
service/tag.cpp
)
service/tag.cpp)
if(TRACY_ROOT)
set(LIB_SRC ${LIB_SRC} ${TRACY_ROOT}/TracyClient.cpp)
endif()

@ -21,6 +21,8 @@
namespace llarp
{
<<<<<<< HEAD
bool
Context::CallSafe(std::function< void(void) > f)
{
@ -95,13 +97,21 @@ namespace llarp
{
llarp::LogInfo(llarp::VERSION_FULL, " ", llarp::RELEASE_MOTTO);
llarp::LogInfo("starting up");
mainloop = llarp_make_ev_loop();
if(m_Simulation == nullptr)
{
mainloop = m_Simulation->m_NetLoop;
}
if(mainloop == nullptr)
mainloop = llarp_make_ev_loop();
logic->set_event_loop(mainloop.get());
mainloop->set_logic(logic);
crypto = std::make_unique< sodium::CryptoLibSodium >();
cryptoManager = std::make_unique< CryptoManager >(crypto.get());
if(m_Simulation == nullptr)
{
crypto = std::make_unique< sodium::CryptoLibSodium >();
cryptoManager = std::make_unique< CryptoManager >(crypto.get());
}
router = std::make_unique< Router >(worker, mainloop, logic);
@ -145,11 +155,18 @@ namespace llarp
// run net io thread
llarp::LogInfo("running mainloop");
llarp_ev_loop_run_single_process(mainloop, logic);
if(closeWaiter)
if(m_Simulation == nullptr)
{
llarp_ev_loop_run_single_process(mainloop, logic);
if(closeWaiter)
{
// inform promise if called by CloseAsync
closeWaiter->set_value();
}
}
else
{
// inform promise if called by CloseAsync
closeWaiter->set_value();
m_Simulation->NodeUp(this);
}
return 0;
}

@ -0,0 +1,38 @@
#include <simulation/sim_context.hpp>
#include <llarp.hpp>
namespace llarp
{
namespace simulate
{
Simulation::Simulation() : m_CryptoManager(new sodium::CryptoLibSodium())
{
}
void
Simulation::NodeUp(llarp::Context *)
{
}
Node_ptr
Simulation::AddNode(const std::string &name)
{
auto itr = m_Nodes.find(name);
if(itr == m_Nodes.end())
{
itr =
m_Nodes
.emplace(name,
std::make_shared< llarp::Context >(shared_from_this()))
.first;
}
return itr->second;
}
void
Simulation::DelNode(const std::string &name)
{
m_Nodes.erase(name);
}
} // namespace simulate
} // namespace llarp

@ -0,0 +1,35 @@
#pragma once
#include <crypto/crypto_libsodium.hpp>
#include <ev/ev.h>
#include <constants/features.hpp>
namespace llarp
{
// forward declair
struct Context;
using Node_ptr = std::shared_ptr< llarp::Context >;
namespace simulate
{
struct Simulation : public std::enable_shared_from_this< Simulation >
{
Simulation();
llarp::CryptoManager m_CryptoManager;
llarp_ev_loop_ptr m_NetLoop;
std::unordered_map< std::string, Node_ptr > m_Nodes;
void
NodeUp(llarp::Context* node);
Node_ptr
AddNode(const std::string& name);
void
DelNode(const std::string& name);
};
using Sim_ptr = std::shared_ptr< Simulation >;
} // namespace simulate
} // namespace llarp

@ -0,0 +1,12 @@
set(PYTHON_EXECUTABLE "python3")
find_package(pybind11 REQUIRED)
set(LLARP_PYBIND_SRC
module.cpp
llarp/router_contact.cpp
llarp/simulation/sim_context.cpp)
pybind11_add_module(pyllarp MODULE ${LLARP_PYBIND_SRC})
target_include_directories(pyllarp PRIVATE ${CMAKE_SOURCE_DIR}/include ${CMAKE_SOURCE_DIR}/crypto/libntrup/include ${CMAKE_SOURCE_DIR}/llarp ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(pyllarp PUBLIC ${EXE_LIBS})

@ -0,0 +1,19 @@
#pragma once
#include <pybind11/pybind11.h>
namespace py = pybind11;
namespace llarp
{
void
Context_Init(py::module &mod);
void
RouterContact_Init(py::module &mod);
namespace simulate
{
void
SimContext_Init(py::module &mod);
}
} // namespace llarp

@ -0,0 +1,23 @@
#include "common.hpp"
#include <llarp.hpp>
namespace llarp
{
void
Context_Init(py::module& mod)
{
using Context_ptr = std::shared_ptr< Context >;
py::class_< Context, Context_ptr >(mod, "Context")
.def(py::init< simulate::Sim_ptr >())
.def("Setup",
[](Context_ptr self) -> bool { return self->Setup() == 0; })
.def("Run",
[](Context_ptr self) -> int {
return self->Run(llarp_main_runtime_opts{});
})
.def("IsUp", &Context::IsUp)
.def("LooksAlive", &Context::LooksAlive)
.def("Configure", &Context::Configure)
.def("CallSafe", &Context::CallSafe);
}
} // namespace llarp

@ -0,0 +1,18 @@
#include <router_contact.hpp>
#include "common.hpp"
namespace llarp
{
void
RouterContact_Init(py::module& mod)
{
py::class_< RouterContact >(mod, "RouterContact")
.def(py::init<>())
.def("ReadFile", &RouterContact::Read)
.def("WriteFile", &RouterContact::Write)
.def("Verify", [](const RouterContact rc) -> bool {
const llarp_time_t now = llarp::time_now_ms();
return rc.Verify(now);
});
}
} // namespace llarp

@ -0,0 +1,20 @@
#include "common.hpp"
#include <llarp.hpp>
namespace llarp
{
namespace simulate
{
void
SimContext_Init(py::module& mod)
{
py::class_< Simulation, Sim_ptr >(mod, "Simulation")
.def(py::init<>())
.def("AddNode", &Simulation::AddNode)
.def("DelNode", &Simulation::DelNode);
py::object context = py::cast(std::make_shared< Simulation >());
mod.attr("context") = context;
}
} // namespace simulate
} // namespace llarp

@ -0,0 +1,8 @@
#include "common.hpp"
PYBIND11_MODULE(pyllarp, m)
{
llarp::simulate::SimContext_Init(m);
llarp::RouterContact_Init(m);
}
Loading…
Cancel
Save