cleaner API and better doxygen comment
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -23,7 +23,7 @@ extern Process * unknownip;
|
||||
static std::pair<int,int> self_pipe = std::make_pair(-1, -1);
|
||||
|
||||
static bool monitor_run_flag = false;
|
||||
typedef std::map<uint64_t, NethogsMonitorUpdate> NethogsAppUpdateMap;
|
||||
typedef std::map<void*, NethogsMonitorUpdate> NethogsAppUpdateMap;
|
||||
static NethogsAppUpdateMap monitor_update_data;
|
||||
|
||||
static int monitor_refresh_delay = 1;
|
||||
@@ -51,11 +51,6 @@ static std::pair<int, int> create_self_pipe()
|
||||
return std::make_pair(pfd[0], pfd[1]);
|
||||
}
|
||||
|
||||
static uint64_t getProcAsKey(ProcList * curproc)
|
||||
{
|
||||
return reinterpret_cast<uint64_t>(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
|
||||
|
||||
18
libnethogs.h
18
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
|
||||
*/
|
||||
|
||||
|
||||
Reference in New Issue
Block a user