From e444dd57db49323c778a511e20e0dead830c6a65 Mon Sep 17 00:00:00 2001 From: PolynomialDivision Date: Sun, 16 Jul 2017 20:59:32 +0200 Subject: [PATCH] Add remove thread --- src/include/datastorage.h | 2 ++ src/main.c | 8 +++++--- src/storage/datastorage.c | 24 ++++++++++++++++++++++++ 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/include/datastorage.h b/src/include/datastorage.h index edab115..1bed488 100644 --- a/src/include/datastorage.h +++ b/src/include/datastorage.h @@ -54,6 +54,7 @@ typedef struct client_s { #define ARRAY_CLIENT_LEN 1000 +#define TIME_THRESHOLD_CLIENT 60 struct client_s client_array[ARRAY_CLIENT_LEN]; pthread_mutex_t client_array_mutex; @@ -64,6 +65,7 @@ void client_array_insert(client entry); client* client_array_delete(client entry); void print_client_array(); void print_client_entry(client entry); +void *remove_client_array_thread(void *arg); #define ARRAY_LEN 1000 diff --git a/src/main.c b/src/main.c index 1b18b5f..956c32f 100644 --- a/src/main.c +++ b/src/main.c @@ -60,10 +60,12 @@ int main(int argc, char **argv) { init_socket_runopts(opt_broadcast_ip, opt_broadcast_port, 0); - pthread_t tid; - pthread_create(&tid, NULL, &remove_array_thread, NULL); - + pthread_t tid_probe; + pthread_create(&tid_probe, NULL, &remove_array_thread, NULL); + pthread_t tid_client; + pthread_create(&tid_client, NULL, &remove_client_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 aa09117..0d92f40 100644 --- a/src/storage/datastorage.c +++ b/src/storage/datastorage.c @@ -15,6 +15,7 @@ int client_array_go_next(char sort_order[], int i, client entry, client next_entry); int client_array_go_next_help(char sort_order[], int i, client entry, client next_entry); +void remove_old_client_entries(time_t current_time, long long int threshold); int probe_entry_last = -1; int client_entry_last = -1; @@ -232,6 +233,17 @@ void insert_to_array(probe_entry entry, int inc_counter) pthread_mutex_unlock(&probe_array_mutex); } +void remove_old_client_entries(time_t current_time, long long int threshold) +{ + for(int i = 0; i < probe_entry_last; i++) + { + if (client_array[i].time < current_time - threshold) + { + client_array_delete(client_array[i]); + } + } +} + void remove_old_probe_entries(time_t current_time, long long int threshold) { for(int i = 0; i < probe_entry_last; i++) @@ -254,6 +266,18 @@ void *remove_array_thread(void *arg) { return 0; } +void *remove_client_array_thread(void *arg) { + while (1) { + sleep(TIME_THRESHOLD_CLIENT); + pthread_mutex_lock(&client_array_mutex); + printf("[Thread] : Removing old client entries!\n"); + remove_old_client_entries(time(0), TIME_THRESHOLD_CLIENT); + pthread_mutex_unlock(&client_array_mutex); + } + return 0; +} + + void insert_client_to_array(client entry) { pthread_mutex_lock(&client_array_mutex);