added nethogsmonitor_loop_devices and moved code to python dir
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
#include <pybind11/pybind11.h>
|
|
||||||
#include <pybind11/functional.h>
|
#include <pybind11/functional.h>
|
||||||
|
#include <pybind11/pybind11.h>
|
||||||
|
#include <pybind11/stl.h>
|
||||||
#include <set>
|
#include <set>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
@@ -13,14 +14,17 @@ std::set<pid_t> pidsToWatch;
|
|||||||
//--- hacky way to get callbacks working and handle signals
|
//--- hacky way to get callbacks working and handle signals
|
||||||
std::function<void(int, NethogsMonitorRecord const *)> empty_callback;
|
std::function<void(int, NethogsMonitorRecord const *)> empty_callback;
|
||||||
std::function<void(int, NethogsMonitorRecord const *)> loop_callback;
|
std::function<void(int, NethogsMonitorRecord const *)> 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;
|
py::gil_scoped_acquire acquire;
|
||||||
if (PyErr_CheckSignals() != 0) {
|
if (PyErr_CheckSignals() != 0) {
|
||||||
nethogsmonitor_breakloop();
|
nethogsmonitor_breakloop();
|
||||||
PyErr_Clear();
|
PyErr_Clear();
|
||||||
}
|
}
|
||||||
else if (loop_callback) {
|
else if (loop_callback) {
|
||||||
loop_callback(arg1, arg2);
|
loop_callback(action, record);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -39,6 +43,29 @@ int nethogsmonitor_loop_py(
|
|||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int nethogsmonitor_loop_devices_py(
|
||||||
|
std::function<void(int, NethogsMonitorRecord const *)> &cb,
|
||||||
|
char *filter,
|
||||||
|
std::vector<std::string> __devicenames,
|
||||||
|
bool all,
|
||||||
|
int to_ms)
|
||||||
|
{
|
||||||
|
// this is ok because we only use the vector here
|
||||||
|
std::vector<char*> _devicenames;
|
||||||
|
for (auto _dn : __devicenames) _devicenames.push_back(const_cast<char*>(_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
|
//--- python module binding
|
||||||
PYBIND11_MODULE(nethogs, m) {
|
PYBIND11_MODULE(nethogs, m) {
|
||||||
py::class_<NethogsMonitorRecord>(m, "NethogsMonitorRecord")
|
py::class_<NethogsMonitorRecord>(m, "NethogsMonitorRecord")
|
||||||
@@ -55,6 +82,9 @@ PYBIND11_MODULE(nethogs, m) {
|
|||||||
m.def("nethogsmonitor_loop", &nethogsmonitor_loop_py, R"pbdoc(
|
m.def("nethogsmonitor_loop", &nethogsmonitor_loop_py, R"pbdoc(
|
||||||
Nethogs monitor loop
|
Nethogs monitor loop
|
||||||
)pbdoc");
|
)pbdoc");
|
||||||
|
m.def("nethogsmonitor_loop_devices", &nethogsmonitor_loop_devices_py, R"pbdoc(
|
||||||
|
Nethogs monitor loop
|
||||||
|
)pbdoc");
|
||||||
m.def("nethogsmonitor_breakloop", &nethogsmonitor_breakloop, R"pbdoc(
|
m.def("nethogsmonitor_breakloop", &nethogsmonitor_breakloop, R"pbdoc(
|
||||||
Nethogs monitor loop break
|
Nethogs monitor loop break
|
||||||
)pbdoc");
|
)pbdoc");
|
||||||
2
setup.py
2
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"
|
__version__ = _version_info.stdout.decode("utf-8").rstrip("\n").split("-")[0] if _version_info else "0.0.0"
|
||||||
|
|
||||||
OBJS = [
|
OBJS = [
|
||||||
"src/bindings.cpp",
|
"python/bindings.cpp",
|
||||||
"src/libnethogs.cpp",
|
"src/libnethogs.cpp",
|
||||||
"src/packet.cpp",
|
"src/packet.cpp",
|
||||||
"src/connection.cpp",
|
"src/connection.cpp",
|
||||||
|
|||||||
Reference in New Issue
Block a user