Zappy - Year end project
2
This is a project that Epitech asked us to create in order to allow us to reveiw the notions of the current year.
Loading...
Searching...
No Matches
src
server
src
sub_main.c
Go to the documentation of this file.
1
/*
2
** EPITECH PROJECT, 2023
3
** my_zappy [fed34_prox_vm]
4
** File description:
5
** sub_main.c
6
*/
7
8
#include <time.h>
9
#include <stdio.h>
10
#include <string.h>
11
#include <sys/socket.h>
12
13
#include "
utils.h
"
14
#include "
arg_parse.h
"
15
#include "
client_management.h
"
16
17
static
int
display_helper(
void
)
18
{
19
printf(
"USAGE:\n"
20
"\t./zappy_server -p port -x width -y height -n name1 name2 ... "
21
"-c clientNb -f freq\n\n"
22
"DESCRIPTION:\n"
23
"\t-p port [MANDATORY] port number\n"
24
"\t-x width [MANDATORY] width of the world\n"
25
"\t-y height [MANDATORY] height of the world\n"
26
"\t-n name1 name2 ... [MANDATORY] name of the team\n"
27
"\t-c clientNb [MANDATORY] number of authorized clients per team\n"
28
"\t-f freq [OPTIONAL] reciprocal of time unit "
29
"for execution of actions\n"
);
30
return
(0);
31
}
32
33
static
int
arguments_handler(
int
argc,
char
**argv,
server_handler_t
*server)
34
{
35
struct
arg_s
**arguments = NULL;
36
int
nb_param =
get_nb_parameter
(argc, (
const
char
**)argv);
37
38
arguments =
get_zappy_args
(argc, (
const
char
**)argv, SERVER_OPTION);
39
if
(arguments == NULL) {
40
return
write_error_msg
(
"See the helper with '-help' option.\n"
);
41
}
42
if
(
check_values_validity
((
const
struct
arg_s
**)arguments,
43
nb_param) == -1) {
44
return
(-1);
45
}
46
if
(
server_initialization
(server, arguments) == -1) {
47
return
(-1);
48
}
49
free_args
(arguments, nb_param);
50
return
(0);
51
}
52
53
static
int
shutdown_server(
server_handler_t
*server)
54
{
55
if
(server->
game_data
.
map
!= NULL) {
56
free_array
(server->
game_data
.
map
);
57
}
58
if
(server->
game_data
.
teams
!= NULL) {
59
for
(
int
i = 0; server->
game_data
.
teams
[i].
team_name
!= NULL; i++) {
60
free(server->
game_data
.
teams
[i].
team_name
);
61
}
62
free(server->
game_data
.
teams
);
63
}
64
delete_all_client
(server->
game_data
.
clients
);
65
if
(shutdown(server->
socket
, SHUT_RDWR) == -1) {
66
return
(
ERROR
);
67
}
68
return
(0);
69
}
70
71
int
sub_main
(
int
argc,
char
**argv)
72
{
73
server_handler_t
server;
74
75
srand(time(NULL));
76
if
(argc == 2 && strcmp(argv[1],
"-help"
) == 0) {
77
return
display_helper();
78
}
79
if
(arguments_handler(argc, argv, &server) == -1) {
80
return
(
ERROR
);
81
}
82
server_loop
(&server);
83
return
(shutdown_server(&server));
84
}
arg_parse.h
get_nb_parameter
int get_nb_parameter(int ac, const char **av)
Get the number of parameter passed to the program.
Definition
parameter_utils.c:38
get_zappy_args
struct arg_s ** get_zappy_args(int ac, const char **av, const struct option_list_s *opt_l)
The principal function to get every arguments.
Definition
arg_parse.c:89
check_values_validity
int check_values_validity(const struct arg_s **arguments, const size_t size)
Check if the values of the parameters are correct.
Definition
check_values_validity.c:106
client_management.h
delete_all_client
void delete_all_client(cli_t client[MAX_CLIENT])
The function to delete a every client from the cli_t structure.
Definition
client_management.c:45
ERROR
#define ERROR
! GLOBAL CONSTANTS !!
Definition
constants.h:13
server_initialization
int server_initialization(server_handler_t *server, struct arg_s **arguments)
The function to initialize every server data.
Definition
server_initialization.c:47
server_loop
void server_loop(server_handler_t *server)
The server loop where communication client/server can be done.
Definition
server_loop.c:67
arg_s
Definition
arg_parse.h:16
game_data_s::teams
team_t * teams
Definition
server_handler.h:124
game_data_s::clients
cli_t clients[MAX_CLIENT]
Definition
server_handler.h:125
game_data_s::map
map_t ** map
Definition
server_handler.h:126
server_handler_s
A structure to store server general data.
Definition
server_handler.h:137
server_handler_s::socket
int socket
Definition
server_handler.h:138
server_handler_s::game_data
game_data_t game_data
Definition
server_handler.h:141
team_s::team_name
char * team_name
Definition
server_handler.h:104
sub_main
int sub_main(int argc, char **argv)
This is the main function of the program (this is the function called by the main used for compiling)
Definition
sub_main.c:71
utils.h
free_args
void free_args(struct arg_s **args, const size_t size)
Free struct args_s of lenght size.
Definition
free.c:35
free_array
void free_array(void *to_free)
! FREE !!
Definition
free.c:13
write_error_msg
int write_error_msg(const char *str)
! WRITTER !!
Definition
writer.c:11