Compare commits

...

2 Commits

2 changed files with 26 additions and 36 deletions

View File

@ -19,7 +19,18 @@
#include <custom_msgq.h>
#include <custom_sig.h>
int main(int argc, char *argv[]){
void waitPlayer(char name[2][MAX_NAME]) {
player_ds player;
semOp(_SEMID, SERVER, -1);
// ricevo nome primo client
rcvPlayer(&player);
strcpy(name[player.id], player.name_player);
_PIDS[player.id] = player.pid;
printf(PSERVER "Player %s connected\n", name[player.id]);
}
int main(int argc, char *argv[]) {
printf("Il mio pid per killarmi: %d\n", getpid()); //FIXME: TMP
setupServer(argc, argv);
@ -28,35 +39,19 @@ int main(int argc, char *argv[]){
printf(PSERVER "Waiting for players...\n");
semOp(_SEMID, CLIENT0, 1);
semOp(_SEMID, CLIENT1, 1);
player_ds player;
char name[2][MAX_NAME];
// aspetto PRIMO client //FIXME: codice duplicato
semOp(_SEMID, SERVER, -1);
// ricevo nome primo client
rcvPlayer(&player);
strcpy(name[player.id], player.name_player);
_PIDS[player.id] = player.pid;
printf(PSERVER "Player %s connected\n", name[player.id]);
// aspetto PRIMO client
waitPlayer(name);
// aspetto SECONDO client
semOp(_SEMID, SERVER, -1);
// ricevo nome secondo client
rcvPlayer(&player);
strcpy(name[player.id], player.name_player);
_PIDS[player.id] = player.pid;
printf(PSERVER "Player %s connected\n", name[player.id]);
waitPlayer(name);
// avvisto inizio partita
// avviso inizio partita
semOp(_SEMID, CLIENT0, 1);
semOp(_SEMID, CLIENT1, 1);
// PARTITA
printf(PSERVER "Starting game\n");
@ -91,7 +86,6 @@ int main(int argc, char *argv[]){
} while (--turns_left && (result = checkWin(move.move)) == -1);
printf("result: %d\n", result);
if (result == -1) {
printf(PSERVER "Game ended in a draw\n");

View File

@ -2,6 +2,7 @@
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <time.h>
#include <sys/msg.h>
#include <sys/sem.h>
#include <sys/shm.h>
@ -19,20 +20,15 @@ pid_t _PIDS[2];
void setServerSignal(int sig);
void sigIntHandler2(int sig) {
raise(SIGTERM);
}
void sigHandlerServer(int sig) {
if (sig == SIGINT) {
printf("\n" PSERVER "Press again Ctrl^C to exit (within %d sec)\n", TIME_TO_RESET);
signal(SIGINT, sigIntHandler2);
alarm(TIME_TO_RESET);
}
if (sig == SIGALRM) {
printf(PSERVER "Time to exit (%d sec) expired\n", TIME_TO_RESET);
signal(SIGINT, sigHandlerServer);
static int last_sigint = 0;
if (time(0) - last_sigint >= TIME_TO_RESET) {
printf("\n" PSERVER "Press again Ctrl^C to exit (within %d sec)\n", TIME_TO_RESET);
last_sigint = time(0);
} else {
raise(SIGTERM);
}
}
// abbandono di un giocatore
@ -65,7 +61,7 @@ void sigHandlerServer(int sig) {
// msgq
if (_MSGQID) {
printf(PSERVER "Deleting msg\n");
if (msgctl(_MSGQID, IPC_RMID, NULL) == -1) { //TODO: funzioni
if (msgctl(_MSGQID, IPC_RMID, NULL) == -1) {
errExit("msgctl", "sigHandlerServer");
}
}