From a2e1a5579f9f9bad1ba4016aff99c05b6a06266c Mon Sep 17 00:00:00 2001 From: Xiaoxuan Li <x.li215@student.unimelb.edu.au> Date: Mon, 29 Apr 2019 23:18:04 +1000 Subject: [PATCH] final update before due --- image_tagger.c | 203 +++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 187 insertions(+), 16 deletions(-) diff --git a/image_tagger.c b/image_tagger.c index 39467b1..730b207 100644 --- a/image_tagger.c +++ b/image_tagger.c @@ -25,6 +25,18 @@ static int const HTTP_400_LENGTH = 47; static char const * const HTTP_404 = "HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\n\r\n"; static int const HTTP_404_LENGTH = 45; +int player1; +int player2; + +bool player1_ready; +bool player2_ready; + +char player1_k[2005][205]; +char player2_k[2005][205]; + +int player1_knum; +int player2_knum; + // represents the types of method typedef enum { @@ -33,11 +45,12 @@ typedef enum UNKNOWN } METHOD; -static bool handle_http_request(int sockfd, int player) +static bool handle_http_request(int sockfd) { // try to read the request char buff[2049]; int n = read(sockfd, buff, 2049); + //fwrite(buff,1,2049,stdout); if (n <= 0) { if (n < 0) @@ -46,6 +59,12 @@ static bool handle_http_request(int sockfd, int player) printf("socket %d close the connection\n", sockfd); return false; } + if( player1 <0 && player2 != sockfd){ + player1 = sockfd; + } + if( player2 <0 && player1 != sockfd){ + player2 = sockfd; + } // terminate the string buff[n] = 0; @@ -82,6 +101,7 @@ static bool handle_http_request(int sockfd, int player) if (strncmp(curr, "?start", 6) == 0){ if (method == GET) { + // get the size of the file struct stat st; stat("3_first_turn.html", &st); @@ -112,7 +132,14 @@ if (strncmp(curr, "?start", 6) == 0){ char * quit = strstr(buff, "quit="); char * guess = strstr(buff, "guess="); if (quit) { - player -=1; + bool player1_ready = false; + bool player2_ready = false; + + int player1 = -1; + int player2 = -1; + + int player1_knum = 0; + int player2_knum = 0; // get the size of the file struct stat st; stat("7_gameover.html", &st); @@ -140,7 +167,134 @@ if (strncmp(curr, "?start", 6) == 0){ } if(guess){ // get the size of the file - if(player < 2){ + if(player1_ready&&player2_ready){ + char * keyword = strstr(buff, "keyword=") + 8; + int keyword_length = strlen(keyword); + if(player1 == sockfd){ + strncpy(player1_k[player1_knum], keyword, keyword_length); + player1_knum += 1; + int flag =0; + for(int i=0; i<=player2_knum;i++){ + if(strcmp(keyword,player2_k[i])==0){ + flag = 1; + } + } + if(flag){ + struct stat st; + stat("6_endgame.html", &st); + n = sprintf(buff, HTTP_200_FORMAT, st.st_size); + // send the header first + if (write(sockfd, buff, n) < 0) + { + perror("write"); + return false; + } + // send the file + int filefd = open("6_endgame.html", O_RDONLY); + do + { + n = sendfile(sockfd, filefd, NULL, 2048); + } + while (n > 0); + if (n < 0) + { + perror("sendfile"); + close(filefd); + return false; + } + close(filefd); + } + else{ + struct stat st; + stat("4_accepted.html", &st); + n = sprintf(buff, HTTP_200_FORMAT, st.st_size); + // send the header first + if (write(sockfd, buff, n) < 0) + { + perror("write"); + return false; + } + // send the file + int filefd = open("4_accepted.html", O_RDONLY); + do + { + n = sendfile(sockfd, filefd, NULL, 2048); + } + while (n > 0); + if (n < 0) + { + perror("sendfile"); + close(filefd); + return false; + } + close(filefd); + } + } + + if(player2 == sockfd){ + strncpy(player2_k[player2_knum], keyword, keyword_length); + player2_knum += 1; + int flag =0; + for(int i=0; i<=player1_knum;i++){ + if(strcmp(keyword,player1_k[i])==0){ + flag = 1; + } + } + if(flag){ + struct stat st; + stat("6_endgame.html", &st); + n = sprintf(buff, HTTP_200_FORMAT, st.st_size); + // send the header first + if (write(sockfd, buff, n) < 0) + { + perror("write"); + return false; + } + // send the file + int filefd = open("6_endgame.html", O_RDONLY); + do + { + n = sendfile(sockfd, filefd, NULL, 2048); + } + while (n > 0); + if (n < 0) + { + perror("sendfile"); + close(filefd); + return false; + } + close(filefd); + } + else{ + struct stat st; + stat("4_accepted.html", &st); + n = sprintf(buff, HTTP_200_FORMAT, st.st_size); + // send the header first + if (write(sockfd, buff, n) < 0) + { + perror("write"); + return false; + } + // send the file + int filefd = open("4_accepted.html", O_RDONLY); + do + { + n = sendfile(sockfd, filefd, NULL, 2048); + } + while (n > 0); + if (n < 0) + { + perror("sendfile"); + close(filefd); + return false; + } + close(filefd); + } + } + + } + + else{ struct stat st; stat("5_discarded.html", &st); n = sprintf(buff, HTTP_200_FORMAT, st.st_size); @@ -164,7 +318,7 @@ if (strncmp(curr, "?start", 6) == 0){ return false; } close(filefd); - } + } } } @@ -172,13 +326,15 @@ if (strncmp(curr, "?start", 6) == 0){ +if (*curr == ' '){ - - - if (*curr == ' '){ - - if (method == GET) - { + if (method == GET){ + if(player1 == sockfd){ + player1_ready = true; + } + if(player2 == sockfd){ + player2_ready = true; + } // get the size of the file struct stat st; stat("1_intro.html", &st); @@ -212,7 +368,14 @@ if (strncmp(curr, "?start", 6) == 0){ char * quit = strstr(buff, "quit="); if (quit) { - player -=1; + bool player1_ready = false; + bool player2_ready = false; + + int player1 = -1; + int player2 = -1; + + int player1_knum = 0; + int player2_knum = 0; // get the size of the file struct stat st; stat("7_gameover.html", &st); @@ -269,7 +432,7 @@ if (strncmp(curr, "?start", 6) == 0){ close(filefd); //move the trailing part backward int p1, p2; - for (p1 = size - 1, p2 = p1 - username_length; p1 >= size - 17; --p1, --p2) + for (p1 = size - 1, p2 = p1 - username_length; p1 >= size - 212; --p1, --p2) buff[p1] = buff[p2]; ++p2; @@ -306,13 +469,23 @@ if (strncmp(curr, "?start", 6) == 0){ int main(int argc, char * argv[]) { + player1_ready = false; + player2_ready = false; + + player1 = -1; + player2 = -1; + + player1_knum = 0; + player2_knum = 0; - int player = 0; if (argc < 3) { fprintf(stderr, "usage: %s ip port\n", argv[0]); return 0; } + else if(argc == 3) { + fprintf(stderr, "image_tagger server is now running at IP: %s on port %s\n", + argv[1], argv[2]);} // create TCP socket which only accept IPv4 int sockfd = socket(AF_INET, SOCK_STREAM, 0); @@ -365,7 +538,6 @@ int main(int argc, char * argv[]) exit(EXIT_FAILURE); } - int player = 0; // loop all possible descriptor for (int i = 0; i <= maxfd; ++i){ // determine if the current file descriptor is active @@ -396,11 +568,10 @@ int main(int argc, char * argv[]) ); } - player += 1; } // a request is sent from the client - else if (!handle_http_request(i, player)) + else if (!handle_http_request(i)) { close(i); -- GitLab