diff --git a/acock_comp30023_2019_project-1.zip b/acock_comp30023_2019_project-1.zip index 259cc857aeb5dd93d41b56a8cf303543506bd04c..d5dd55445870747445a2ad69002bd8926040f860 100644 Binary files a/acock_comp30023_2019_project-1.zip and b/acock_comp30023_2019_project-1.zip differ diff --git a/image_tagger.c b/image_tagger.c index 79e4e301f14971386ccb7c187ed0f96515499657..6d1e7489763d7d925699313fffe008010fd389e9 100644 --- a/image_tagger.c +++ b/image_tagger.c @@ -1,6 +1,7 @@ /* * image_tagger.c * source code from Lab 5/6 of COMP30023 +* @author: Alexander D'Arcy Cock */ #include <errno.h> @@ -21,7 +22,7 @@ #include <sys/types.h> #include <unistd.h> -// constants +// Constants for the HTTP response headers static char const * const HTTP_200_FORMAT = "HTTP/1.1 200 OK\r\n\ Content-Type: text/html\r\n\ Content-Length: %ld\r\n\r\n"; @@ -34,7 +35,7 @@ static char const * const HTTP_400 = "HTTP/1.1 400 Bad Request\r\nContent-Length 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; -//static char const * const SET_COOKIE = ; +// The html filenames are stored in constants static char* const PAGE_1 = "1_intro.html"; static char* const PAGE_2 = "2_start.html"; static char* const PAGE_3 = "3_first_turn.html"; @@ -43,17 +44,6 @@ static char* const PAGE_5 = "5_discarded.html"; static char* const PAGE_6 = "6_endgame.html"; static char* const PAGE_7 = "7_gameover.html"; -/* -static char* P1_NAME; -static char* P2_NAME; -static int P1_GAME_STATE = 0; -static int P2_GAME_STATE = 0; -static char* KEYWORD_LIST_P1[100]; -static char* KEYWORD_LIST_P2[100]; -static int NUM_KEYWORD_P1 = 0; -static int NUM_KEYWORD_P2 = 0; -*/ - // represents the types of method typedef enum { GET, @@ -61,6 +51,7 @@ typedef enum { UNKNOWN } METHOD; +// represents the states that the players can be in typedef enum { WELCOME, WAITING, @@ -69,6 +60,7 @@ typedef enum { GAMEOVER } GAMESTATE; +// represents the different attributes of each player struct Player { char* name; int nameLength; @@ -78,12 +70,11 @@ struct Player { int numKeywords; }; +// The players are global to access them from any function static struct Player player1; static struct Player player2; -//player1.playerNum = 1; -//player2.playerNum = 2; - +//checks the output from the buff to fetch the url of the request static char* getURL(char* curr) { char* url; if (strncmp(curr, "1_intro.html ", 13) == 0 || strncmp(curr, " HTTP/1.1", 9) == 0) @@ -113,6 +104,7 @@ static char* getURL(char* curr) { return url; } +// Handles storing the keywords and returns true if there is a match to an added keyword static bool addKeyword(char * keyword, int playerNum) { int i = 0; bool match = false; @@ -146,6 +138,7 @@ static bool addKeyword(char * keyword, int playerNum) { return match; } +// Returns the player that the cookie in the request header refers to static struct Player* checkCookie(char* buff) { char * cookie; if((cookie = strstr(buff,"player=")) != NULL) { @@ -176,6 +169,7 @@ static struct Player* checkCookie(char* buff) { return NULL; } +// Forms the HTTP response to the client and returns true if one is sent static bool sendResponse(int sockfd, char* file, char* post) { char buff[2049]; int n; @@ -218,20 +212,16 @@ static bool sendResponse(int sockfd, char* file, char* post) { } } // send the header first - if (write(sockfd, buff, n) < 0) - { + if (write(sockfd, buff, n) < 0) { perror("write"); return false; } // send the file int filefd = open(file, O_RDONLY); - do - { + do { n = sendfile(sockfd, filefd, NULL, 2048); - } - while (n > 0); - if (n < 0) - { + } while (n > 0); + if (n < 0) { perror("sendfile"); close(filefd); return false; @@ -240,6 +230,7 @@ static bool sendResponse(int sockfd, char* file, char* post) { return true; } +// Handles the different cases for get requests and returns true if a response is sent static bool getRequest(int sockfd, char* buff, char* url, int n) { struct Player* player = checkCookie(buff); @@ -251,12 +242,17 @@ static bool getRequest(int sockfd, char* buff, char* url, int n) { if (strcmp(url,PAGE_3) == 0 || strcmp(url,PAGE_6) == 0) { player->gameState = READY; } + } else if(player1.gameState == GAMEOVER) { + url = PAGE_7; } - sendResponse(sockfd,url,NULL); - return true; + if(sendResponse(sockfd,url,NULL)) { + return true; + } + return false; } +// Handles the different cases for post requests and returns true if a response is sent static bool postRequest(int sockfd, char* buff, char* url, int n) { char* post; char* file; @@ -281,11 +277,13 @@ static bool postRequest(int sockfd, char* buff, char* url, int n) { for(int i = 0; i < player2.numKeywords; i++) { printf("%s\n",player2.keywordList[i]); } + } else if (player1.gameState == COMPLETED && player2.gameState == COMPLETED) { + file = PAGE_6; + } else if(player1.gameState == GAMEOVER) { + file = PAGE_7; } else { file = PAGE_5; } - } else if (player1.gameState == COMPLETED || player2.gameState == COMPLETED) { - file = PAGE_6; } else if((post = strstr(buff,"quit=Quit")) != NULL || player1.gameState == GAMEOVER) { file = PAGE_7; player1.gameState = GAMEOVER; @@ -293,11 +291,29 @@ static bool postRequest(int sockfd, char* buff, char* url, int n) { file = NULL; } - sendResponse(sockfd,file,post); + if(strcmp(file,PAGE_6) == 0) { + int i = 0; + if(player1.numKeywords > 0) { + for(i = 0; i < player1.numKeywords; i++) { + free(player1.keywordList[i]); + } + player1.numKeywords = 0; + } + if(player2.numKeywords > 0) { + for(i = 0; i < player2.numKeywords; i++) { + free(player2.keywordList[i]); + } + player2.numKeywords = 0; + } + } - return true; + if(sendResponse(sockfd,file,post)) { + return true; + } + return false; } +// Attempts to handle the http requests, returns true if a response is sent static bool handle_http_request(int sockfd) { // try to read the request char buff[2049]; @@ -312,6 +328,7 @@ static bool handle_http_request(int sockfd) { return false; } printf("%s\n", buff); + // terminate the string buff[n] = 0; char * curr = buff; @@ -353,12 +370,10 @@ static bool handle_http_request(int sockfd) { // never used, just for completeness fprintf(stderr, "no other methods supported"); } - + printf("P1: %d\nP2: %d\n",player1.gameState,player2.gameState); return true; } - - int main(int argc, char * argv[]) { if (argc < 3) { @@ -466,5 +481,12 @@ int main(int argc, char * argv[]) { } } + if(player1.name != NULL) { + free(player1.name); + } + if(player2.name != NULL) { + free(player2.name); + } + return 0; } \ No newline at end of file