From a1d488b633761e7b43f209c477e11a0c5620b040 Mon Sep 17 00:00:00 2001 From: PolynomialDivision Date: Sun, 16 Jul 2017 11:09:09 +0200 Subject: [PATCH] Remove old entries --- src/include/datastorage.h | 3 ++- src/main.c | 3 ++- src/storage/datastorage.c | 26 +++++++++++++++++++++++--- 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/src/include/datastorage.h b/src/include/datastorage.h index 9415135..70c8592 100644 --- a/src/include/datastorage.h +++ b/src/include/datastorage.h @@ -14,7 +14,7 @@ #endif #define SORT_NUM 5 -#define TIME_THRESHOLD 60 // every minute +#define TIME_THRESHOLD 5 // every minute // Probe entrys typedef struct probe_entry_s { @@ -38,6 +38,7 @@ void insert_to_array(probe_entry entry, int inc_counter); void probe_array_insert(probe_entry entry); probe_entry* probe_array_delete(probe_entry entry); void print_array(); +void *remove_array_thread(void *arg); pthread_mutex_t probe_array_mutex; diff --git a/src/main.c b/src/main.c index e32a8a8..e03d91e 100644 --- a/src/main.c +++ b/src/main.c @@ -55,7 +55,8 @@ int main(int argc, char **argv) { init_socket_runopts(opt_broadcast_ip, opt_broadcast_port, 0); - //pthread_t tid; + pthread_t tid; + pthread_create(&tid, NULL, &remove_array_thread, NULL); //pthread_create(&tid, NULL, &remove_thread, NULL); dawn_init_ubus(ubus_socket, opt_hostapd_dir); diff --git a/src/storage/datastorage.c b/src/storage/datastorage.c index 75ca2e1..31d6788 100644 --- a/src/storage/datastorage.c +++ b/src/storage/datastorage.c @@ -9,6 +9,7 @@ int go_next(char sort_order[], int i, probe_entry entry, int mac_is_equal(uint8_t addr1[], uint8_t addr2[]); int mac_is_greater(uint8_t addr1[], uint8_t addr2[]); void print_probe_entry(probe_entry entry); +void remove_old_probe_entries(time_t current_time, long long int threshold); int probe_entry_last = -1; @@ -93,7 +94,7 @@ void print_array() void insert_to_array(probe_entry entry, int inc_counter) { pthread_mutex_lock(&probe_array_mutex); - + entry.time = time(0); entry.counter = 0; probe_entry* tmp = probe_array_delete(entry); @@ -112,9 +113,28 @@ void insert_to_array(probe_entry entry, int inc_counter) pthread_mutex_unlock(&probe_array_mutex); } + +void remove_old_probe_entries(time_t current_time, long long int threshold) +{ + for(int i = 0; i < probe_entry_last; i++) + { + if (probe_array[i].time < current_time - threshold) + { + probe_array_delete(probe_array[i]); + } + } +} - - +void *remove_array_thread(void *arg) { + while (1) { + sleep(TIME_THRESHOLD); + pthread_mutex_lock(&probe_array_mutex); + printf("[Thread] : Removing old entries!\n"); + remove_old_probe_entries(time(0), TIME_THRESHOLD); + pthread_mutex_unlock(&probe_array_mutex); + } + return 0; +}