Elaborato_SO/src/F4Server.c

113 lines
2.6 KiB
C
Raw Normal View History

2023-05-14 18:03:50 +02:00
#include <sys/types.h>
#include <sys/ipc.h>
2023-05-14 21:44:03 +02:00
#include <sys/shm.h>
2023-05-16 14:55:12 +02:00
#include <sys/stat.h>
2023-05-15 16:50:48 +02:00
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
2023-05-16 15:55:05 +02:00
#include <sys/msg.h>
2023-05-16 16:32:24 +02:00
#include <sys/sem.h>
2023-05-17 14:24:46 +02:00
#include <string.h>
#include <unistd.h>
2023-05-14 18:03:50 +02:00
2023-05-14 18:10:44 +02:00
#include <server.h>
#include <structures.h>
2023-05-15 10:31:50 +02:00
#include <custom_sem.h>
2023-05-15 10:43:25 +02:00
#include <custom_shm.h>
2023-05-15 16:50:48 +02:00
#include <errExit.h>
2023-05-16 16:53:23 +02:00
#include <forza4.h>
2023-05-19 15:21:15 +02:00
#include <custom_msgq.h>
2023-05-21 11:53:23 +02:00
#include <custom_sig.h>
2023-05-14 18:03:50 +02:00
2023-05-14 17:07:34 +02:00
int main(int argc, char *argv[]){
2023-05-25 14:28:45 +02:00
printf("Il mio pid per killarmi: %d\n", getpid()); //FIXME: TMP
2023-05-29 11:37:43 +02:00
setupServer(argc, argv);
2023-05-16 16:10:43 +02:00
2023-05-26 15:16:26 +02:00
// aperta semaforo per connessione dei due client
2023-05-22 11:03:36 +02:00
printf(PSERVER "Waiting for players...\n");
2023-05-29 12:16:36 +02:00
semOp(_SEMID, CLIENT0, 1);
semOp(_SEMID, CLIENT1, 1);
2023-05-16 23:27:27 +02:00
2023-05-14 22:13:42 +02:00
2023-05-14 17:07:34 +02:00
2023-05-17 15:57:10 +02:00
player_ds player;
2023-05-17 14:24:46 +02:00
char name[2][MAX_NAME];
2023-05-16 23:27:27 +02:00
2023-05-31 11:13:23 +02:00
// aspetto PRIMO client //FIXME: codice duplicato
2023-05-29 12:16:36 +02:00
semOp(_SEMID, SERVER, -1);
// ricevo nome primo client
2023-05-19 15:21:15 +02:00
rcvPlayer(&player);
2023-05-17 15:57:10 +02:00
strcpy(name[player.id], player.name_player);
_PIDS[player.id] = player.pid;
2023-05-22 17:22:17 +02:00
printf(PSERVER "Player %s connected\n", name[player.id]);
2023-05-16 23:27:27 +02:00
2023-05-29 12:16:36 +02:00
// aspetto SECONDO client
semOp(_SEMID, SERVER, -1);
2023-05-29 11:49:35 +02:00
// ricevo nome secondo client
2023-05-19 15:21:15 +02:00
rcvPlayer(&player);
2023-05-17 15:57:10 +02:00
strcpy(name[player.id], player.name_player);
_PIDS[player.id] = player.pid;
2023-05-22 17:22:17 +02:00
printf(PSERVER "Player %s connected\n", name[player.id]);
2023-05-16 23:27:27 +02:00
2023-05-29 12:16:36 +02:00
// avvisto inizio partita
semOp(_SEMID, CLIENT0, 1);
semOp(_SEMID, CLIENT1, 1);
2023-05-16 23:27:27 +02:00
2023-05-29 11:49:35 +02:00
2023-05-16 23:27:27 +02:00
// PARTITA
2023-05-22 11:03:36 +02:00
printf(PSERVER "Starting game\n");
2023-05-15 11:57:30 +02:00
int turns_left = _INPUT_S.rows * _INPUT_S.collums;
2023-05-16 17:07:35 +02:00
int turn = 0;
2023-06-01 14:39:04 +02:00
tile_t result = -1;
2023-05-16 16:53:23 +02:00
move_t move;
do {
2023-06-01 14:39:04 +02:00
//FIXME: tmp
printf("result: %d\n", result);
2023-05-29 12:16:36 +02:00
// apro semaforo al player di turno
semOp(_SEMID, turn, 1);
2023-05-16 16:53:23 +02:00
2023-05-22 11:03:36 +02:00
printf(PSERVER "%s's turn\n", name[turn]);
2023-05-16 18:43:56 +02:00
2023-05-29 11:49:35 +02:00
// aspetto che mi mandi una mossa
2023-05-19 15:21:15 +02:00
rcvMove(&move);
2023-05-16 16:53:23 +02:00
2023-05-31 11:00:59 +02:00
insertMove(move.move, turn);
2023-05-29 11:49:35 +02:00
// info
printf("move: %d\n", move.move);
2023-05-16 23:27:27 +02:00
printf("turns left: %d\n", turns_left - 1);
2023-05-29 11:49:35 +02:00
// dico al player che la mossa è stata effettuata per fargli stampare il campo
2023-05-26 10:58:57 +02:00
semOp(_SEMID, turn, 1);
2023-05-29 12:16:36 +02:00
// next turn
2023-05-26 10:58:57 +02:00
turn ^= 1;
2023-05-29 11:49:35 +02:00
2023-06-01 14:39:04 +02:00
} while (--turns_left && (result = checkWin(move.move)) == -1);
2023-05-16 16:53:23 +02:00
2023-05-15 11:57:30 +02:00
2023-06-01 12:05:00 +02:00
printf("result: %d\n", result);
2023-06-01 14:39:04 +02:00
if (result == -1) {
2023-05-22 11:03:36 +02:00
printf(PSERVER "Game ended in a draw\n");
2023-05-16 17:07:35 +02:00
} else {
2023-06-01 12:05:00 +02:00
printf(PSERVER "%s won\n", name[result]);
2023-05-16 17:07:35 +02:00
}
2023-05-22 17:22:17 +02:00
2023-05-31 11:52:32 +02:00
game_end_t game_end = {.mtype = GAME_END, .winner = result};
2023-05-19 15:21:15 +02:00
sndGame_end(&game_end);
sndGame_end(&game_end);
2023-06-01 12:05:00 +02:00
printf("%d %d\n", _PIDS[0], _PIDS[1]);
2023-06-01 14:39:04 +02:00
kill(_PIDS[0], SIGUSR1);
kill(_PIDS[1], SIGUSR1);
2023-05-16 17:07:35 +02:00
raise(SIGTERM);
2023-05-16 18:43:56 +02:00
}