Skip to content
Snippets Groups Projects
Commit de94b606 authored by Abhisha Nirmalathas's avatar Abhisha Nirmalathas
Browse files

navigating with checking the keywords, adding keywords to users list

parent eee8e8b6
No related branches found
No related tags found
No related merge requests found
...@@ -93,7 +93,7 @@ bool get_request(char* buff, int sockfd, char* file_name){ ...@@ -93,7 +93,7 @@ bool get_request(char* buff, int sockfd, char* file_name){
return true; 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 // 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. // copied to another buffer using strcpy or strncpy to ensure that it will not be overwritten.
printf("USERNAME IS %s\n\n", buff); printf("USERNAME IS %s\n\n", buff);
...@@ -107,7 +107,7 @@ bool post_request(char *buff, int sockfd, char* file_name, char* response){ ...@@ -107,7 +107,7 @@ bool post_request(char *buff, int sockfd, char* file_name, char* response){
stat(file_name, &st); stat(file_name, &st);
// increase file size to accommodate the username // increase file size to accommodate the username
long size = st.st_size + added_length; 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 // send the header first
if (write(sockfd, buff, n) < 0) if (write(sockfd, buff, n) < 0)
{ {
...@@ -146,6 +146,7 @@ bool post_request(char *buff, int sockfd, char* file_name, char* response){ ...@@ -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) static bool handle_http_request(int sockfd, User_list* users)
{ {
// try to read the request // try to read the request
printf("THE NUMBER OF USERS IS (in http req) %d\n", users->n_users);
char buff[2049]; char buff[2049];
int n = read(sockfd, buff, 2049); int n = read(sockfd, buff, 2049);
if (n <= 0) if (n <= 0)
...@@ -165,36 +166,50 @@ static bool handle_http_request(int sockfd, User_list* users) ...@@ -165,36 +166,50 @@ static bool handle_http_request(int sockfd, User_list* users)
// parse the method // parse the method
Request* req = parse_request(curr); Request* req = parse_request(curr);
printf("REQUEST BODY IS \n\n%s\n", req->body); 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"); printf("matches start");
if (req->method == GET){ if (req->method == GET){
change_player_status(sockfd, users, READY);
get_request(buff,sockfd, "3_first_turn.html"); get_request(buff,sockfd, "3_first_turn.html");
} }
if (req->method == POST){ if (req->method == POST){
Response* response = redirect(req, "gameover_page"); if(strncmp(req->body, "keyword=", 8) == 0){
printf("strncmp with keywoprds is successful");
char *resp = parse_response(response); printf("the numer of users is %d\n", users->n_users);
post_request(buff,sockfd, "7_gameover.html",resp); // for(int i=0; i < users->n_users; i++){
free(response); // 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, "/gameover_page", 14) == 0){
if(req->method == GET){
get_request(buff,sockfd, "7_gameover.html");
printf("i return false\n");
} }
} }
else if (strncmp(req->url, "/welcome_page", 13) == 0){ else if (strncmp(req->url, "/welcome_page", 13) == 0){
if(req->method == GET){ if(req->method == GET){
get_request(buff,sockfd, "2_start.html"); 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)){ else if (*req->url == '/' && (strlen(req->url) == 1)){
if (req->method == POST) if (req->method == POST)
...@@ -202,16 +217,12 @@ static bool handle_http_request(int sockfd, User_list* users) ...@@ -202,16 +217,12 @@ static bool handle_http_request(int sockfd, User_list* users)
char *name = strchr(req->body, '=')+1; char *name = strchr(req->body, '=')+1;
printf("**%s**\n", name); printf("**%s**\n", name);
if (name != NULL){ if (name != NULL){
// for (int i=0; i < users->n_users; i++){ for (int i=0; i < users->n_users; i++){
// if(users->users[i]->name == NULL){ if(users->users[i]->id == sockfd){
// users->users[i]->name = name; users->users[i]->name = name;
// } }
// } }
Response* response = redirect(req, "welcome_page"); post_request(buff,sockfd, "2_start.html");
char *resp = parse_response(response);
post_request(buff,sockfd, "2_start.html",resp);
free(resp);
free(response);
} }
} }
else if (req->method == GET) else if (req->method == GET)
...@@ -271,13 +282,6 @@ int main(int argc, char * argv[]) ...@@ -271,13 +282,6 @@ int main(int argc, char * argv[])
// if ip parameter is not specified // if ip parameter is not specified
serv_addr.sin_addr.s_addr = inet_addr(argv[1]); serv_addr.sin_addr.s_addr = inet_addr(argv[1]);
serv_addr.sin_port = htons(atoi(argv[2])); 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 // bind address to socket
if (bind(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) if (bind(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
{ {
...@@ -324,8 +328,12 @@ int main(int argc, char * argv[]) ...@@ -324,8 +328,12 @@ int main(int argc, char * argv[])
// update the maximum tracker // update the maximum tracker
if (newsockfd > maxfd) if (newsockfd > maxfd)
maxfd = newsockfd; maxfd = newsockfd;
// print out the IP and the socket number // print out the IP and the socket number
char ip[INET_ADDRSTRLEN]; 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( printf(
"new connection from %s on socket %d\n", "new connection from %s on socket %d\n",
// convert to human readable string // convert to human readable string
...@@ -344,6 +352,5 @@ int main(int argc, char * argv[]) ...@@ -344,6 +352,5 @@ int main(int argc, char * argv[])
} }
free(users); free(users);
} }
return 0; return 0;
} }
No preview for this file type
No preview for this file type
...@@ -2,12 +2,13 @@ ...@@ -2,12 +2,13 @@
#include <stdlib.h> #include <stdlib.h>
#include <assert.h> #include <assert.h>
#include <stdbool.h> #include <stdbool.h>
#include <string.h>
#include "user.h" #include "user.h"
User* new_user(char* id){ User* new_user(int id){
User *user = malloc(sizeof(User*)); User *user = malloc(sizeof *user);
assert(user); assert(user);
user->id = id; user->id = id;
user->n_capacity = 5; user->n_capacity = 5;
...@@ -22,11 +23,35 @@ User_list* initialise_player_list(){ ...@@ -22,11 +23,35 @@ User_list* initialise_player_list(){
User_list *users = malloc(sizeof(User_list)); User_list *users = malloc(sizeof(User_list));
assert(users); assert(users);
users->n_users = 0; users->n_users = 0;
users->users = malloc(sizeof(User*)*3); users->users = malloc(sizeof(User*)*5);
assert(users->users); assert(users->users);
return 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){ void add_user(User* user, User_list* users){
users->users[users->n_users] = user; users->users[users->n_users] = user;
users->n_users++; users->n_users++;
...@@ -49,3 +74,27 @@ bool is_players_ready(User_list* users){ ...@@ -49,3 +74,27 @@ bool is_players_ready(User_list* users){
} }
return true; 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
...@@ -2,13 +2,14 @@ ...@@ -2,13 +2,14 @@
typedef enum typedef enum
{ {
READY, READY,
WAIT WAIT,
QUIT
} STATUS; } STATUS;
typedef struct User { typedef struct User {
char* name; char* name;
char* id; int id;
char** keywords; char** keywords;
int n_keywords; int n_keywords;
STATUS status; STATUS status;
...@@ -20,12 +21,22 @@ typedef struct User_list{ ...@@ -20,12 +21,22 @@ typedef struct User_list{
User **users; User **users;
}User_list; }User_list;
User* new_user(char* id); User* new_user(int id);
User_list* initialise_player_list(); 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 add_user(User* user, User_list* users);
void free_users(User_list* users); void free_users(User_list* users);
bool is_players_ready(User_list* users); 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
No preview for this file type
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment