From 1fdb8b4c9450222c92700f6610a25dd7eb2e6543 Mon Sep 17 00:00:00 2001 From: Jeff Becker Date: Fri, 17 Jan 2020 08:22:08 -0500 Subject: [PATCH] initial pybind11 introspection code --- CMakeLists.txt | 6 +++- llarp/CMakeLists.txt | 4 +-- llarp/context.cpp | 31 +++++++++++++++----- llarp/simulation/sim_context.cpp | 38 +++++++++++++++++++++++++ llarp/simulation/sim_context.hpp | 35 +++++++++++++++++++++++ pybind/CMakeLists.txt | 12 ++++++++ pybind/common.hpp | 19 +++++++++++++ pybind/llarp/context.cpp | 23 +++++++++++++++ pybind/llarp/router_contact.cpp | 18 ++++++++++++ pybind/llarp/simulation/sim_context.cpp | 20 +++++++++++++ pybind/module.cpp | 8 ++++++ 11 files changed, 204 insertions(+), 10 deletions(-) create mode 100644 llarp/simulation/sim_context.cpp create mode 100644 llarp/simulation/sim_context.hpp create mode 100644 pybind/CMakeLists.txt create mode 100644 pybind/common.hpp create mode 100644 pybind/llarp/context.cpp create mode 100644 pybind/llarp/router_contact.cpp create mode 100644 pybind/llarp/simulation/sim_context.cpp create mode 100644 pybind/module.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 49ddba226..e2a3caa7b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) diff --git a/llarp/CMakeLists.txt b/llarp/CMakeLists.txt index 33aaeb66e..fa6feccf0 100644 --- a/llarp/CMakeLists.txt +++ b/llarp/CMakeLists.txt @@ -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() diff --git a/llarp/context.cpp b/llarp/context.cpp index facbba774..ce056feb7 100644 --- a/llarp/context.cpp +++ b/llarp/context.cpp @@ -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; } diff --git a/llarp/simulation/sim_context.cpp b/llarp/simulation/sim_context.cpp new file mode 100644 index 000000000..0b6f64175 --- /dev/null +++ b/llarp/simulation/sim_context.cpp @@ -0,0 +1,38 @@ +#include +#include + +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 diff --git a/llarp/simulation/sim_context.hpp b/llarp/simulation/sim_context.hpp new file mode 100644 index 000000000..88f6c2258 --- /dev/null +++ b/llarp/simulation/sim_context.hpp @@ -0,0 +1,35 @@ +#pragma once +#include +#include +#include + +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 diff --git a/pybind/CMakeLists.txt b/pybind/CMakeLists.txt new file mode 100644 index 000000000..c592d27b4 --- /dev/null +++ b/pybind/CMakeLists.txt @@ -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}) + diff --git a/pybind/common.hpp b/pybind/common.hpp new file mode 100644 index 000000000..179d4d16f --- /dev/null +++ b/pybind/common.hpp @@ -0,0 +1,19 @@ +#pragma once +#include + +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 diff --git a/pybind/llarp/context.cpp b/pybind/llarp/context.cpp new file mode 100644 index 000000000..363a2fe99 --- /dev/null +++ b/pybind/llarp/context.cpp @@ -0,0 +1,23 @@ +#include "common.hpp" +#include + +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 diff --git a/pybind/llarp/router_contact.cpp b/pybind/llarp/router_contact.cpp new file mode 100644 index 000000000..9b472b22f --- /dev/null +++ b/pybind/llarp/router_contact.cpp @@ -0,0 +1,18 @@ +#include +#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 diff --git a/pybind/llarp/simulation/sim_context.cpp b/pybind/llarp/simulation/sim_context.cpp new file mode 100644 index 000000000..75294ffd7 --- /dev/null +++ b/pybind/llarp/simulation/sim_context.cpp @@ -0,0 +1,20 @@ +#include "common.hpp" +#include + +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 diff --git a/pybind/module.cpp b/pybind/module.cpp new file mode 100644 index 000000000..961a6f77f --- /dev/null +++ b/pybind/module.cpp @@ -0,0 +1,8 @@ + +#include "common.hpp" + +PYBIND11_MODULE(pyllarp, m) +{ + llarp::simulate::SimContext_Init(m); + llarp::RouterContact_Init(m); +}