From 99e8d618738b506d3d4e33777bff289cb4075d17 Mon Sep 17 00:00:00 2001 From: Mohamed Boussaffa Date: Sun, 13 Mar 2016 21:36:27 +0800 Subject: [PATCH] cleaner API and better doxygen comment --- MakeLib.mk | 2 +- libnethogs.cpp | 21 +++++++-------------- libnethogs.h | 18 +++++++++++++----- 3 files changed, 21 insertions(+), 20 deletions(-) diff --git a/MakeLib.mk b/MakeLib.mk index c7d0725..c719eba 100644 --- a/MakeLib.mk +++ b/MakeLib.mk @@ -52,7 +52,7 @@ install: $(LIBNAME) ldconfig install_dev: install - @ln -s $(DESTDIR)$(libdir)/$(LIBNAME) $(DESTDIR)$(libdir)/$(LIBRARY) + @ln -f -s $(DESTDIR)$(libdir)/$(LIBNAME) $(DESTDIR)$(libdir)/$(LIBRARY) install -m 755 libnethogs.a $(DESTDIR)$(libdir) @echo "Installed libnethogs.a to $(DESTDIR)$(libdir)" install -d -m 755 $(DESTDIR)$(incdir) diff --git a/libnethogs.cpp b/libnethogs.cpp index 2752253..1f43164 100644 --- a/libnethogs.cpp +++ b/libnethogs.cpp @@ -23,7 +23,7 @@ extern Process * unknownip; static std::pair self_pipe = std::make_pair(-1, -1); static bool monitor_run_flag = false; -typedef std::map NethogsAppUpdateMap; +typedef std::map NethogsAppUpdateMap; static NethogsAppUpdateMap monitor_update_data; static int monitor_refresh_delay = 1; @@ -51,11 +51,6 @@ static std::pair create_self_pipe() return std::make_pair(pfd[0], pfd[1]); } -static uint64_t getProcAsKey(ProcList * curproc) -{ - return reinterpret_cast(curproc); -} - static bool wait_for_next_trigger() { if( pc_loop_use_select ) @@ -211,13 +206,12 @@ static void nethogsmonitor_handle_update(NethogsMonitorCallback cb) if (DEBUG) std::cout << "PROC: Deleting process\n"; - NethogsAppUpdateMap::iterator it = monitor_update_data.find(getProcAsKey(curproc)); + NethogsAppUpdateMap::iterator it = monitor_update_data.find(curproc); if( it != monitor_update_data.end() ) { NethogsMonitorUpdate& data = it->second; - data.action = NETHOGS_APP_ACTION_REMOVE; - (*cb)(&data); - monitor_update_data.erase(getProcAsKey(curproc)); + (*cb)(NETHOGS_APP_ACTION_REMOVE, &data); + monitor_update_data.erase(curproc); } ProcList * todelete = curproc; @@ -247,8 +241,8 @@ static void nethogsmonitor_handle_update(NethogsMonitorCallback cb) curproc->getVal()->gettotal (&recv_bytes, &sent_bytes); //notify update - bool const new_data = (monitor_update_data.find(getProcAsKey(curproc)) == monitor_update_data.end()); - NethogsMonitorUpdate &data = monitor_update_data[getProcAsKey(curproc)]; + bool const new_data = (monitor_update_data.find(curproc) == monitor_update_data.end()); + NethogsMonitorUpdate &data = monitor_update_data[curproc]; bool data_change = false; if( new_data ) @@ -273,8 +267,7 @@ static void nethogsmonitor_handle_update(NethogsMonitorCallback cb) if( data_change ) { - data.action = NETHOGS_APP_ACTION_SET; - (*cb)(&data); + (*cb)(NETHOGS_APP_ACTION_SET, &data); } //next diff --git a/libnethogs.h b/libnethogs.h index 3f6792d..269a567 100644 --- a/libnethogs.h +++ b/libnethogs.h @@ -20,8 +20,6 @@ extern "C" { typedef struct NethogsMonitorUpdate { - int action; // NETHOGS_APP_ACTION_SET or NETHOGS_APP_ACTION_REMOVE - uint64_t key; //A unique key for the record, used with set/remove actions const char* name; int pid; uint32_t uid; @@ -32,12 +30,22 @@ typedef struct NethogsMonitorUpdate float recv_kbs; } NethogsMonitorUpdate; -typedef void(*NethogsMonitorCallback)(NethogsMonitorUpdate const*); - + +/** + * @brief Defines a callback to handle updates about applications + * @param action NETHOGS_APP_ACTION_SET if data is beeing added or updated, + * NETHOGS_APP_ACTION_REMOVE if data is beeing removed. + * the 'data' pointer is used to uniquely identify the data beeing update or removed. + * @param data a pointer to an application usage data. the pointer remains valid until + * the callback is called with NETHOGS_APP_ACTION_REMOVE for the same pointer. + * the user should not modify the content of the structure pointed by data. + */ +typedef void(*NethogsMonitorCallback)(int action, NethogsMonitorUpdate const* data); + /** * @brief Enter the process monitoring loop and reports updates using the * callback provided as parameter. - * This call will block until nethogsmonitor_breakloop is called or a failure occurs. + * This call will block until nethogsmonitor_breakloop() is called or a failure occurs. * @param cb A pointer to a callback function following the NethogsMonitorCallback definition */