From fcacd7efdb241c5b8712c3c940a38ac445494017 Mon Sep 17 00:00:00 2001 From: jimmylomro Date: Wed, 16 Mar 2022 18:58:28 +0000 Subject: [PATCH] added nethogsmonitor_loop_devices and moved code to python dir --- {contrib => python}/README.md | 0 {src => python}/bindings.cpp | 36 ++++++++++++++++++++++++--- {contrib => python}/python-wrapper.py | 0 setup.py | 2 +- 4 files changed, 34 insertions(+), 4 deletions(-) rename {contrib => python}/README.md (100%) rename {src => python}/bindings.cpp (66%) rename {contrib => python}/python-wrapper.py (100%) diff --git a/contrib/README.md b/python/README.md similarity index 100% rename from contrib/README.md rename to python/README.md diff --git a/src/bindings.cpp b/python/bindings.cpp similarity index 66% rename from src/bindings.cpp rename to python/bindings.cpp index 8c20ca6..0413798 100644 --- a/src/bindings.cpp +++ b/python/bindings.cpp @@ -1,5 +1,6 @@ -#include #include +#include +#include #include #include @@ -13,14 +14,17 @@ std::set pidsToWatch; //--- hacky way to get callbacks working and handle signals std::function empty_callback; std::function loop_callback; -void loop_callback_wrapper(int arg1, NethogsMonitorRecord const *arg2){ +void loop_callback_wrapper( + int action, + NethogsMonitorRecord const *record) +{ py::gil_scoped_acquire acquire; if (PyErr_CheckSignals() != 0) { nethogsmonitor_breakloop(); PyErr_Clear(); } else if (loop_callback) { - loop_callback(arg1, arg2); + loop_callback(action, record); } } @@ -39,6 +43,29 @@ int nethogsmonitor_loop_py( return retval; } +int nethogsmonitor_loop_devices_py( + std::function &cb, + char *filter, + std::vector __devicenames, + bool all, + int to_ms) +{ + // this is ok because we only use the vector here + std::vector _devicenames; + for (auto _dn : __devicenames) _devicenames.push_back(const_cast(_dn.c_str())); + int devc = _devicenames.size(); + char **devicenames = (_devicenames.empty()) ? NULL : _devicenames.data(); + + int retval; + loop_callback = cb; + { + py::gil_scoped_release release; + retval = nethogsmonitor_loop_devices(loop_callback_wrapper, filter, devc, devicenames, all, to_ms); + } + loop_callback = empty_callback; + return retval; +} + //--- python module binding PYBIND11_MODULE(nethogs, m) { py::class_(m, "NethogsMonitorRecord") @@ -55,6 +82,9 @@ PYBIND11_MODULE(nethogs, m) { m.def("nethogsmonitor_loop", &nethogsmonitor_loop_py, R"pbdoc( Nethogs monitor loop )pbdoc"); + m.def("nethogsmonitor_loop_devices", &nethogsmonitor_loop_devices_py, R"pbdoc( + Nethogs monitor loop + )pbdoc"); m.def("nethogsmonitor_breakloop", &nethogsmonitor_breakloop, R"pbdoc( Nethogs monitor loop break )pbdoc"); diff --git a/contrib/python-wrapper.py b/python/python-wrapper.py similarity index 100% rename from contrib/python-wrapper.py rename to python/python-wrapper.py diff --git a/setup.py b/setup.py index 0ed8487..8b134b8 100644 --- a/setup.py +++ b/setup.py @@ -11,7 +11,7 @@ _version_info = subprocess.run(['bash', "./determineVersion.sh"], stdout=subproc __version__ = _version_info.stdout.decode("utf-8").rstrip("\n").split("-")[0] if _version_info else "0.0.0" OBJS = [ - "src/bindings.cpp", + "python/bindings.cpp", "src/libnethogs.cpp", "src/packet.cpp", "src/connection.cpp",