Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
C
comp30023-2019-project-1
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Alexander Cock
comp30023-2019-project-1
Commits
8a6a6c1f
Commit
8a6a6c1f
authored
6 years ago
by
Alexander Cock
Browse files
Options
Downloads
Patches
Plain Diff
FInalised Server for submitting, added some comments
parent
98779e83
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
acock_comp30023_2019_project-1.zip
+0
-0
0 additions, 0 deletions
acock_comp30023_2019_project-1.zip
image_tagger.c
+55
-33
55 additions, 33 deletions
image_tagger.c
with
55 additions
and
33 deletions
acock_comp30023_2019_project-1.zip
+
0
−
0
View file @
8a6a6c1f
No preview for this file type
This diff is collapsed.
Click to expand it.
image_tagger.c
+
55
−
33
View file @
8a6a6c1f
/*
* 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>
//
c
onstants
//
C
onstants
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\n
Content-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
)
;
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
;
}
}
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
\n
P2: %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
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment