diff --git a/http-server.c b/http-server.c index 2feee982a75e597bb056a5f70efcb464d2c7d218..63c567c599ffc765c9f938bea587a0fe431c26ca 100644 --- a/http-server.c +++ b/http-server.c @@ -93,7 +93,7 @@ bool get_request(char* buff, int sockfd, char* file_name){ return true; } -bool post_request(char *buff, int sockfd, char* file_name, char* response){ +bool post_request(char *buff, int sockfd, char* file_name){ // locate the username, it is safe to do so in this sample code, but usually the result is expected to be // copied to another buffer using strcpy or strncpy to ensure that it will not be overwritten. printf("USERNAME IS %s\n\n", buff); @@ -107,7 +107,7 @@ bool post_request(char *buff, int sockfd, char* file_name, char* response){ stat(file_name, &st); // increase file size to accommodate the username long size = st.st_size + added_length; - int n = sprintf(buff, response, size); + int n = sprintf(buff, HTTP_200_FORMAT, size); // send the header first if (write(sockfd, buff, n) < 0) { @@ -146,6 +146,7 @@ bool post_request(char *buff, int sockfd, char* file_name, char* response){ static bool handle_http_request(int sockfd, User_list* users) { // try to read the request + printf("THE NUMBER OF USERS IS (in http req) %d\n", users->n_users); char buff[2049]; int n = read(sockfd, buff, 2049); if (n <= 0) @@ -165,36 +166,50 @@ static bool handle_http_request(int sockfd, User_list* users) // parse the method Request* req = parse_request(curr); printf("REQUEST BODY IS \n\n%s\n", req->body); - if (strncmp(req->url, "/welcome_page?start=Start", 24) == 0){ + if(strncmp(req->body, "quit=Quit", 9) == 0){ + change_player_status(sockfd,users, QUIT); + post_request(buff,sockfd, "7_gameover.html"); + } + if (strncmp(req->url, "/?start=Start", 24) == 0){ printf("matches start"); if (req->method == GET){ - get_request(buff,sockfd, "3_first_turn.html"); + change_player_status(sockfd, users, READY); + get_request(buff,sockfd, "3_first_turn.html"); } if (req->method == POST){ - Response* response = redirect(req, "gameover_page"); - - char *resp = parse_response(response); - post_request(buff,sockfd, "7_gameover.html",resp); - free(response); - } - } - else if (strncmp(req->url, "/gameover_page", 14) == 0){ - if(req->method == GET){ - get_request(buff,sockfd, "7_gameover.html"); - printf("i return false\n"); + if(strncmp(req->body, "keyword=", 8) == 0){ + printf("strncmp with keywoprds is successful"); + printf("the numer of users is %d\n", users->n_users); + // for(int i=0; i < users->n_users; i++){ + // printf("USER ID %d", users->users[i]->id); + // if(users->users[i]->status == READY){ + // printf("is ready\n"); + // } + // if(users->users[i]->status == WAIT){ + // printf("is wait\n"); + // } + // if(users->users[i]->status == QUIT){ + // printf("is quit\n"); + // } + // } + if(players_ready(users)){ + post_request(buff,sockfd, "5_discarded.html"); + } + else if(should_player_quit(users)){ + change_player_status(sockfd,users, QUIT); + post_request(buff,sockfd, "7_gameover.html"); + } + else{ + add_keyword(sockfd, users, req->body); + post_request(buff,sockfd, "4_accepted.html"); + } + } } } else if (strncmp(req->url, "/welcome_page", 13) == 0){ if(req->method == GET){ get_request(buff,sockfd, "2_start.html"); } - if (req->method == POST){ - Response* response = redirect(req, "gameover_page"); - - char *resp = parse_response(response); - post_request(buff,sockfd, "7_gameover.html",resp); - free(response); - } } else if (*req->url == '/' && (strlen(req->url) == 1)){ if (req->method == POST) @@ -202,16 +217,12 @@ static bool handle_http_request(int sockfd, User_list* users) char *name = strchr(req->body, '=')+1; printf("**%s**\n", name); if (name != NULL){ - // for (int i=0; i < users->n_users; i++){ - // if(users->users[i]->name == NULL){ - // users->users[i]->name = name; - // } - // } - Response* response = redirect(req, "welcome_page"); - char *resp = parse_response(response); - post_request(buff,sockfd, "2_start.html",resp); - free(resp); - free(response); + for (int i=0; i < users->n_users; i++){ + if(users->users[i]->id == sockfd){ + users->users[i]->name = name; + } + } + post_request(buff,sockfd, "2_start.html"); } } else if (req->method == GET) @@ -271,13 +282,6 @@ int main(int argc, char * argv[]) // if ip parameter is not specified serv_addr.sin_addr.s_addr = inet_addr(argv[1]); serv_addr.sin_port = htons(atoi(argv[2])); - char *user_id = malloc(sizeof(char)*(strlen(argv[1])+strlen(argv[2]))); - user_id[0] = '\0'; - strcat(user_id,argv[1]); - strcat(user_id,argv[2]); - printf("%s\n\n\\n", user_id); - assert(user_id); - free(user_id); // bind address to socket if (bind(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) { @@ -324,8 +328,12 @@ int main(int argc, char * argv[]) // update the maximum tracker if (newsockfd > maxfd) maxfd = newsockfd; + // print out the IP and the socket number char ip[INET_ADDRSTRLEN]; + User* new_player = new_user(sockfd); + add_user(new_player, users); + printf("THE NUMBER OF USERS IS %d\n", users->n_users); printf( "new connection from %s on socket %d\n", // convert to human readable string @@ -342,8 +350,7 @@ int main(int argc, char * argv[]) } } } - free(users); + free(users); } - return 0; } diff --git a/http-server.o b/http-server.o index b00f6d1b747122f3ddabb921837e564fbc1277d4..ce050eae728c5840758ba35cf9fd718b08cf3c24 100644 Binary files a/http-server.o and b/http-server.o differ diff --git a/image_tagger b/image_tagger index e75f06810a1abd98dc04ec442587bdfd46469f44..058f8c6173d82bfd8264946dc1acfcf47c65e547 100644 Binary files a/image_tagger and b/image_tagger differ diff --git a/user.c b/user.c index 99db6a2b163ac896c4418b49519d8594df32131b..89a79637d5be4d000edb22a2cf79e8d90d135425 100644 --- a/user.c +++ b/user.c @@ -2,12 +2,13 @@ #include <stdlib.h> #include <assert.h> #include <stdbool.h> +#include <string.h> #include "user.h" -User* new_user(char* id){ - User *user = malloc(sizeof(User*)); +User* new_user(int id){ + User *user = malloc(sizeof *user); assert(user); user->id = id; user->n_capacity = 5; @@ -22,11 +23,35 @@ User_list* initialise_player_list(){ User_list *users = malloc(sizeof(User_list)); assert(users); users->n_users = 0; - users->users = malloc(sizeof(User*)*3); + users->users = malloc(sizeof(User*)*5); assert(users->users); return users; } +void resize_keywords(User* user, char* keyword){ + if(user->n_capacity == user->n_keywords){ + user->n_capacity *= 2; + user->keywords = realloc(user->keywords,sizeof(char*) * (user->n_capacity)); + } + user->keywords[user->n_keywords] = keyword; + user->n_keywords++; +} + + +void add_keyword(int id, User_list* users, char* query){ + const char s[2] = "&"; + char * curr = query; + char * token; + curr += 8; + token = strtok(curr, s); + printf("the keywod is %s\n", token); + for(int i=0; i < users->n_users; i++){ + if(users->users[i]->id == id){ + resize_keywords(users->users[i], token); + } + } +} + void add_user(User* user, User_list* users){ users->users[users->n_users] = user; users->n_users++; @@ -48,4 +73,28 @@ bool is_players_ready(User_list* users){ } } return true; +} + +bool should_player_quit(User_list* users){ + for(int i=0; i < users->n_users; i++){ + if (users->users[i]->status == QUIT){ + return true; + } + } + return false; +} +bool players_ready(User_list* users){ + for(int i=0; i < users->n_users; i++){ + if (users->users[i]->status != READY){ + return false; + } + } + return true; +} +void change_player_status(int user_id, User_list* users, STATUS status){ + for(int i=0; i < users->n_users; i++){ + if (users->users[i]->id == user_id){ + users->users[i]->status = status; + } + } } \ No newline at end of file diff --git a/user.h b/user.h index 6131fe7d28dc715e1318dacf67de1a556b1c87c0..81ce36ff438ba10ff84db178c9993bcbfc6b37e9 100644 --- a/user.h +++ b/user.h @@ -2,13 +2,14 @@ typedef enum { READY, - WAIT + WAIT, + QUIT } STATUS; typedef struct User { char* name; - char* id; + int id; char** keywords; int n_keywords; STATUS status; @@ -20,12 +21,22 @@ typedef struct User_list{ User **users; }User_list; -User* new_user(char* id); +User* new_user(int id); User_list* initialise_player_list(); +void resize_keywords(User* user, char* keyword); + +void add_keyword(int id, User_list* users, char* query); + void add_user(User* user, User_list* users); void free_users(User_list* users); -bool is_players_ready(User_list* users); \ No newline at end of file +bool is_players_ready(User_list* users); + +bool should_player_quit(User_list* users); + +bool players_ready(User_list* users); + +void change_player_status(int user_id, User_list* users, STATUS status); \ No newline at end of file diff --git a/user.o b/user.o index d10234ac9ebf35864a3e8532e0781a1ba07fdaeb..4561529bef21b14459e05815a19a720799bf6d0f 100644 Binary files a/user.o and b/user.o differ