added a record_id member to the update struct

This commit is contained in:
Mohamed Boussaffa
2016-03-14 01:32:23 +08:00
parent 99e8d61873
commit 12d578e9e2
2 changed files with 17 additions and 13 deletions

View File

@@ -23,8 +23,8 @@ extern Process * unknownip;
static std::pair<int,int> self_pipe = std::make_pair(-1, -1);
static bool monitor_run_flag = false;
typedef std::map<void*, NethogsMonitorUpdate> NethogsAppUpdateMap;
static NethogsAppUpdateMap monitor_update_data;
typedef std::map<void*, NethogsMonitorRecord> NethogsRecordMap;
static NethogsRecordMap monitor_record_map;
static int monitor_refresh_delay = 1;
static time_t monitor_last_refresh_time = 0;
@@ -206,12 +206,12 @@ static void nethogsmonitor_handle_update(NethogsMonitorCallback cb)
if (DEBUG)
std::cout << "PROC: Deleting process\n";
NethogsAppUpdateMap::iterator it = monitor_update_data.find(curproc);
if( it != monitor_update_data.end() )
NethogsRecordMap::iterator it = monitor_record_map.find(curproc);
if( it != monitor_record_map.end() )
{
NethogsMonitorUpdate& data = it->second;
NethogsMonitorRecord& data = it->second;
(*cb)(NETHOGS_APP_ACTION_REMOVE, &data);
monitor_update_data.erase(curproc);
monitor_record_map.erase(curproc);
}
ProcList * todelete = curproc;
@@ -241,14 +241,17 @@ static void nethogsmonitor_handle_update(NethogsMonitorCallback cb)
curproc->getVal()->gettotal (&recv_bytes, &sent_bytes);
//notify update
bool const new_data = (monitor_update_data.find(curproc) == monitor_update_data.end());
NethogsMonitorUpdate &data = monitor_update_data[curproc];
bool const new_data = (monitor_record_map.find(curproc) == monitor_record_map.end());
NethogsMonitorRecord &data = monitor_record_map[curproc];
bool data_change = false;
if( new_data )
{
data_change = true;
static int record_id = 0;
++record_id;
memset(&data, 0, sizeof(data));
data.record_id = record_id;
data.name = curproc->getVal()->name;
data.pid = curproc->getVal()->pid;
}

View File

@@ -18,8 +18,9 @@ extern "C" {
#define NETHOGS_STATUS_FAILURE 1 //generic error
#define NETHOGS_STATUS_NO_DEVICE 2 //no device foundr
typedef struct NethogsMonitorUpdate
{
typedef struct NethogsMonitorRecord
{
int record_id;
const char* name;
int pid;
uint32_t uid;
@@ -28,19 +29,19 @@ typedef struct NethogsMonitorUpdate
uint32_t recv_bytes;
float sent_kbs;
float recv_kbs;
} NethogsMonitorUpdate;
} NethogsMonitorRecord;
/**
* @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.
* the record_id member 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);
typedef void(*NethogsMonitorCallback)(int action, NethogsMonitorRecord const* data);
/**
* @brief Enter the process monitoring loop and reports updates using the