Compare commits
2 Commits
f89c162652
...
d8ff25fa84
Author | SHA1 | Date |
---|---|---|
Edi De Candido | d8ff25fa84 | |
Edi De Candido | b75279c7fa |
|
@ -19,6 +19,17 @@
|
|||
#include <custom_msgq.h>
|
||||
#include <custom_sig.h>
|
||||
|
||||
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
|
||||
|
||||
|
@ -29,34 +40,18 @@ int main(int argc, char *argv[]){
|
|||
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");
|
||||
|
|
|
@ -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) {
|
||||
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);
|
||||
signal(SIGINT, sigIntHandler2);
|
||||
alarm(TIME_TO_RESET);
|
||||
last_sigint = time(0);
|
||||
} else {
|
||||
raise(SIGTERM);
|
||||
}
|
||||
|
||||
if (sig == SIGALRM) {
|
||||
printf(PSERVER "Time to exit (%d sec) expired\n", TIME_TO_RESET);
|
||||
signal(SIGINT, sigHandlerServer);
|
||||
}
|
||||
|
||||
// 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");
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue