Skip to content

Commit

Permalink
added a way of remembering number of cliens
Browse files Browse the repository at this point in the history
  • Loading branch information
rishithaminol committed Mar 21, 2018
1 parent 9256e9a commit 0e8b57b
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 2 deletions.
3 changes: 3 additions & 0 deletions cmarketcap.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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;
Expand Down
46 changes: 45 additions & 1 deletion httpd.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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) {
Expand All @@ -375,13 +385,41 @@ static void *__cb_read_from_client(void *cb_arg)
}
}

dec_number_of_clients();
close(sockfd);
free(_cb_arg);
DEBUG_MSG("Connection closed\n");

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)
{
Expand Down Expand Up @@ -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);
}
3 changes: 3 additions & 0 deletions httpd.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 4 additions & 1 deletion timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#include <time.h>
#include <unistd.h>

#include "httpd.h"

/* @brief Count down timer display
*
* @param[in] sec number of seconds to wait
Expand All @@ -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);
Expand Down

0 comments on commit 0e8b57b

Please sign in to comment.