Elaborato_SO/src/server.c

97 lines
2.2 KiB
C
Raw Normal View History

2023-05-14 17:07:34 +02:00
#include <stdio.h>
#include <stdlib.h>
2023-05-14 18:44:47 +02:00
#include <ctype.h>
2023-05-14 18:10:44 +02:00
#include <server.h>
2023-05-14 22:19:52 +02:00
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/stat.h>
2023-05-14 21:44:03 +02:00
2023-05-14 18:10:44 +02:00
#include <errExit.h>
2023-05-26 15:16:26 +02:00
#include <signal.h>
2023-05-26 15:23:59 +02:00
#include <custom_msgq.h>
#include <custom_sem.h>
#include <custom_shm.h>
#include <custom_sig.h>
2023-05-29 11:37:43 +02:00
#include <forza4.h>
2023-05-14 17:07:34 +02:00
2023-05-23 15:47:34 +02:00
input_server_t _INPUT_S;
2023-06-17 10:45:51 +02:00
int turn = 0;
2023-05-23 15:47:34 +02:00
2023-05-29 12:16:36 +02:00
input_server_t checkServerInput(int argc, char *argv[]);
2023-05-26 15:17:22 +02:00
void setupServer(int argc, char *argv[]){
2023-05-29 12:35:04 +02:00
// CHECK INPUT
2023-05-29 12:16:36 +02:00
_INPUT_S = checkServerInput(argc, argv);
2023-05-26 15:16:26 +02:00
2023-05-29 11:37:43 +02:00
_ROWS = _INPUT_S.rows;
_COLLUMS = _INPUT_S.collums;
2023-05-26 15:16:26 +02:00
// SIGNAL
printf(PSERVER "Setting up signals\n");
setupServerSignalHandler();
2023-05-29 15:01:11 +02:00
// MSGQ
2023-06-18 15:54:51 +02:00
printf(PSERVER "Setting up msgq\n");
2023-05-29 15:01:11 +02:00
setupServerMsgq();
printf("msgq: %d\n", _MSGQID);
2023-05-26 15:16:26 +02:00
// SEM
printf(PSERVER "Setting up sem\n");
setupServerSem();
2023-05-29 15:01:11 +02:00
printf("sem: %d\n", _SEMID);
2023-05-26 15:16:26 +02:00
2023-05-29 11:37:43 +02:00
// SHM
printf(PSERVER "Setting up shm\n");
setupServerShm();
2023-05-29 15:01:11 +02:00
printf("shm: %d\n", _SHMID);
2023-05-26 15:16:26 +02:00
}
2023-05-29 12:16:36 +02:00
input_server_t checkServerInput(int argc, char *argv[]){
2023-05-14 17:07:34 +02:00
if (argc < 5) {
2023-06-18 15:32:52 +02:00
printf(PHELP "./F4Server ROW COL PLAYER1 PLAYER2\n"
2023-05-14 18:44:47 +02:00
"\t- ROW \t\tnumber of rows\n"
"\t- COL \t\tnumber of rows\n"
"\t- PLAYER1 \tplayer one token\n"
"\t- PLAYER2 \tplayer two token\n"
);
2023-05-14 17:31:00 +02:00
exit(EXIT_SUCCESS);
2023-05-14 17:07:34 +02:00
} else if (argc > 5) {
2023-05-14 17:31:00 +02:00
errExitMsg("To many arguments");
2023-05-14 17:07:34 +02:00
}
2023-05-14 18:08:10 +02:00
input_server_t input = {
2023-05-14 17:53:38 +02:00
.collums = atoi(argv[1]),
.rows = atoi(argv[2]),
2023-05-14 18:03:12 +02:00
.player1Token = argv[3][0],
2023-05-14 18:44:47 +02:00
.player2Token = argv[4][0]
2023-05-14 17:53:38 +02:00
};
2023-05-14 17:07:34 +02:00
2023-06-01 15:03:34 +02:00
if (input.collums < 5 || input.rows < 5) {
2023-05-14 17:07:34 +02:00
errExitMsg("To few rows or collums");
}
2023-05-14 18:44:47 +02:00
if (!isprint(input.player1Token) || !isprint(input.player2Token)) {
errExitMsg("Invalid player token");
2023-05-14 17:07:34 +02:00
}
2023-05-14 17:53:38 +02:00
return input;
2023-06-17 10:45:51 +02:00
}
void end_game(int winner){
game_end_t game_end = {.mtype = GAME_END, .winner = winner};
sndGame_end(&game_end);
sndGame_end(&game_end);
kill(_PIDS[0], SIGUSR1);
kill(_PIDS[1], SIGUSR1);
semOp(_SEMID, SERVER, -1);
semOp(_SEMID, SERVER, -1);
_PIDS[0] = 0;
_PIDS[1] = 0;
raise(SIGTERM);
2023-05-14 17:07:34 +02:00
}