From 0e8b57b8673614742947ca9501e5d916734594ad Mon Sep 17 00:00:00 2001 From: Rishitha Minol Date: Wed, 21 Mar 2018 14:00:32 +0530 Subject: [PATCH] added a way of remembering number of cliens --- cmarketcap.c | 3 +++ httpd.c | 46 +++++++++++++++++++++++++++++++++++++++++++++- httpd.h | 3 +++ timer.c | 5 ++++- 4 files changed, 55 insertions(+), 2 deletions(-) diff --git a/cmarketcap.c b/cmarketcap.c index 97a3070..9500992 100644 --- a/cmarketcap.c +++ b/cmarketcap.c @@ -12,6 +12,7 @@ #include "timer.h" pthread_mutex_t shift_column_locker = PTHREAD_MUTEX_INITIALIZER; + struct global_data_handle global_data_handle; /* @brief coin_history column map */ @@ -106,6 +107,8 @@ int main(int argc, char *argv[]) pthread_mutex_init(&mysql_db_access, NULL); pthread_mutex_init(&shift_column_locker, NULL); + init_httpd_mutexes(); + global_data_handle.httpd_sockfd = 0; prog_name = *argv; diff --git a/httpd.c b/httpd.c index 890d3bd..14d1289 100644 --- a/httpd.c +++ b/httpd.c @@ -19,11 +19,20 @@ #define CLIENT_MAX 10 #define MAX_URI_SIZE 2048 +pthread_mutex_t number_of_clients_var_locker = PTHREAD_MUTEX_INITIALIZER; +size_t number_of_clients = 0; +#define LOCK_NUM_OF_CLIENTS_LOCKER pthread_mutex_lock( \ + &number_of_clients_var_locker) +#define UNLOCK_NUM_OF_CLIENTS_LOCKER pthread_mutex_unlock( \ + &number_of_clients_var_locker) + static void parse_http_header(char *buff, struct myhttp_header *header); static void *__cb_read_from_client(void *cb_arg); static int check_http_header(struct myhttp_header *header); static void send_header(int sockfd, char *code, char *type); static void send_json_response(int sockfd, struct myhttp_header *header, MYSQL *db); +static void inc_number_of_clients(); /*! Increment number of clients */ +static void dec_number_of_clients(); /*! Decrement number of clients */ /** @brief Parse http headers into a data structure. * @@ -354,6 +363,7 @@ static void *__cb_read_from_client(void *cb_arg) pthread_detach(pthread_self()); DEBUG_MSG("waiting for client to write...\n"); + inc_number_of_clients(); while ((nbytes = read(sockfd, buffer, RD_BUFF_MAX))) { DEBUG_MSG("client wrote %d bytes\n", nbytes); if (nbytes < 0) { @@ -375,6 +385,7 @@ static void *__cb_read_from_client(void *cb_arg) } } + dec_number_of_clients(); close(sockfd); free(_cb_arg); DEBUG_MSG("Connection closed\n"); @@ -382,6 +393,33 @@ static void *__cb_read_from_client(void *cb_arg) pthread_exit(NULL); } +/* @brief returns number of clients at the moment */ +size_t num_of_clients() +{ + size_t num; + LOCK_NUM_OF_CLIENTS_LOCKER; + num = number_of_clients; + UNLOCK_NUM_OF_CLIENTS_LOCKER; + + return num; +} + +/* @brief Increas number of clients at the moment */ +static void inc_number_of_clients() +{ + LOCK_NUM_OF_CLIENTS_LOCKER; + number_of_clients++; + UNLOCK_NUM_OF_CLIENTS_LOCKER; +} + +/* @brief Decrease number of clients at the moment */ +static void dec_number_of_clients() +{ + LOCK_NUM_OF_CLIENTS_LOCKER; + number_of_clients--; + UNLOCK_NUM_OF_CLIENTS_LOCKER; +} + /* @brief needs openned database */ int __cb_main_thread(MYSQL *db) { @@ -453,4 +491,10 @@ int __cb_main_thread(MYSQL *db) //pthread_exit(NULL); return 0; -} /* main */ +} /* __cb_main_thread */ + +/* @brief Initialize mutex functionalities of 'httpd.c' section */ +void init_httpd_mutexes() +{ + pthread_mutex_init(&number_of_clients_var_locker, NULL); +} diff --git a/httpd.h b/httpd.h index e18059f..e7e21f9 100644 --- a/httpd.h +++ b/httpd.h @@ -41,4 +41,7 @@ extern void print_uri_base(struct uri_base *ub); extern void free_uri_base(struct uri_base *ub); /*! @} */ /* uri_tokenization */ +extern size_t num_of_clients(); +extern void init_httpd_mutexes(); + #endif \ No newline at end of file diff --git a/timer.c b/timer.c index c931730..e828a78 100644 --- a/timer.c +++ b/timer.c @@ -2,6 +2,8 @@ #include #include +#include "httpd.h" + /* @brief Count down timer display * * @param[in] sec number of seconds to wait @@ -14,7 +16,8 @@ void wait_countdown_timer(const char *wait_str, int sec) i = sec; do { - printf("%s%3d(s)", wait_str, i); + printf("%s%3d(s) number_of_clients = %lu", wait_str, i, + num_of_clients()); fflush(stdout); i--; sleep(1);