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
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
17static 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
33static 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
53static 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 }
65 if (shutdown(server->socket, SHUT_RDWR) == -1) {
66 return (ERROR);
67 }
68 return (0);
69}
70
71int 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}
int get_nb_parameter(int ac, const char **av)
Get the number of parameter passed to the program.
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
int check_values_validity(const struct arg_s **arguments, const size_t size)
Check if the values of the parameters are correct.
void delete_all_client(cli_t client[MAX_CLIENT])
The function to delete a every client from the cli_t structure.
#define ERROR
! GLOBAL CONSTANTS !!
Definition constants.h:13
int server_initialization(server_handler_t *server, struct arg_s **arguments)
The function to initialize every server data.
void server_loop(server_handler_t *server)
The server loop where communication client/server can be done.
Definition server_loop.c:67
cli_t clients[MAX_CLIENT]
A structure to store server general data.
game_data_t game_data
char * team_name
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
void free_args(struct arg_s **args, const size_t size)
Free struct args_s of lenght size.
Definition free.c:35
void free_array(void *to_free)
! FREE !!
Definition free.c:13
int write_error_msg(const char *str)
! WRITTER !!
Definition writer.c:11