printf\n
This commit is contained in:
parent
348243fbad
commit
aac74b0893
|
@ -1,9 +1,9 @@
|
|||
#ifndef SIGNAL_H
|
||||
#define SIGNAL_H
|
||||
|
||||
#define TIME_TO_RESET 10
|
||||
#define TIME_TO_RESET 2
|
||||
|
||||
void serverSigHandler(int sig);
|
||||
void sigHandlerServer(int sig);
|
||||
void setServerSignalHandler();
|
||||
|
||||
#endif
|
|
@ -36,15 +36,11 @@ void sigHandlerClient(int sig) {
|
|||
if (sig == SIGTERM || sig == SIGHUP) {
|
||||
// shm
|
||||
if (boardClient) {
|
||||
printf("\n");
|
||||
printfClient("Detathing shm");
|
||||
if (shmdt(boardClient) == -1) {
|
||||
errExit("shmdt", "sigHandlerServer");
|
||||
}
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
|
||||
exit(0);
|
||||
}
|
||||
|
||||
|
@ -87,7 +83,7 @@ int main(int argc, char *argv[]){
|
|||
//
|
||||
|
||||
// MSGQ
|
||||
printfClient("Setting up msgq\n");
|
||||
printfClient("Setting up msgq");
|
||||
key_t msgKey = ftok(KEYFILE, 'M');
|
||||
_MSGQID = msgget(msgKey, IPC_CREAT | S_IRUSR | S_IWUSR);
|
||||
if (_MSGQID == -1) {
|
||||
|
@ -99,7 +95,7 @@ int main(int argc, char *argv[]){
|
|||
msg_t msg;
|
||||
|
||||
// msgrcv
|
||||
printfClient("Waiting for message...\n");
|
||||
printfClient("Waiting for message...");
|
||||
rcvMsg(&msg);
|
||||
|
||||
input = msg.server_in;
|
||||
|
@ -107,13 +103,13 @@ int main(int argc, char *argv[]){
|
|||
|
||||
|
||||
// SHM
|
||||
printfClient("Setting up shm\n");
|
||||
printfClient("Setting up shm");
|
||||
int shmBoardid = getShmid(input.rows, input.collums);
|
||||
boardClient = shmClientAt(shmBoardid);
|
||||
//
|
||||
|
||||
// SEM
|
||||
printfClient("Setting up sem\n");
|
||||
printfClient("Setting up sem");
|
||||
key_t semKey = ftok(KEYFILE, 'S');
|
||||
int semid;
|
||||
if ((semid = semget(semKey, 3, S_IRUSR | S_IWUSR | IPC_CREAT)) == -1) {
|
||||
|
@ -125,7 +121,7 @@ int main(int argc, char *argv[]){
|
|||
|
||||
// aspetto che il server mi dia il permesso di collegarmi e segnalo collegamento al server
|
||||
semOp(semid, msg.player_id, -1);
|
||||
printfClient("Connecting to server...\n");
|
||||
printfClient("Connecting to server...");
|
||||
semOp(semid, 2, 1);
|
||||
|
||||
|
||||
|
@ -134,11 +130,11 @@ int main(int argc, char *argv[]){
|
|||
strcpy(player.name_player, argv[1]);
|
||||
sndPlayer(&player);
|
||||
|
||||
printfClient("Searching for oponent...\n");
|
||||
printfClient("Searching for oponent...");
|
||||
|
||||
semOp(semid, msg.player_id, -1);
|
||||
|
||||
printfClient("Opponent found\n");
|
||||
printfClient("Opponent found");
|
||||
|
||||
|
||||
|
||||
|
@ -150,7 +146,7 @@ int main(int argc, char *argv[]){
|
|||
semOp(semid, msg.player_id, -1);
|
||||
|
||||
// input e controllo
|
||||
printfServer("Your Turn: \n");
|
||||
printfServer("Your Turn: ");
|
||||
do {
|
||||
scanf("%d", &move.move);
|
||||
move.move--;
|
||||
|
@ -171,7 +167,7 @@ void checkClientinput(int argc, char *argv[]){
|
|||
);
|
||||
exit(EXIT_SUCCESS);
|
||||
} else if (argc > 2) {
|
||||
printfClient("To many arguments!\n");
|
||||
printfClient("To many arguments!");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
|
|
|
@ -24,13 +24,13 @@ int main(int argc, char *argv[]){
|
|||
input_server_t input = check_input(argc, argv);
|
||||
|
||||
// SIGNAL
|
||||
printfServer("Setting up signals\n");
|
||||
printfServer("Setting up signals");
|
||||
setServerSignalHandler();
|
||||
//
|
||||
|
||||
|
||||
// MSGQ
|
||||
printfServer("Setting up msgq\n");
|
||||
printfServer("Setting up msgq");
|
||||
//TODO: forse bisogna fare il reset della shm, perche in teoria mette a 0 quando crea
|
||||
//ma nel caso in cui il campo non si fosse chiuso per qualche motivo forse quando lo riapre non lo azzera
|
||||
key_t msgKey = ftok(KEYFILE, 'M');
|
||||
|
@ -52,13 +52,13 @@ int main(int argc, char *argv[]){
|
|||
//
|
||||
|
||||
// SHM
|
||||
printfServer("Setting up shm\n");
|
||||
printfServer("Setting up shm");
|
||||
shmid = getShmid(input.rows, input.collums);
|
||||
board = shmServerAt(shmid);
|
||||
//
|
||||
|
||||
//
|
||||
printfServer("Setting up sem\n");
|
||||
printfServer("Setting up sem");
|
||||
key_t semKey = ftok(KEYFILE, 'S');
|
||||
semid = semget(semKey, 3, IPC_CREAT | S_IRUSR | S_IWUSR);
|
||||
if (semid == -1) {
|
||||
|
@ -70,7 +70,7 @@ int main(int argc, char *argv[]){
|
|||
|
||||
|
||||
// aperurta semaforo per connessione dei due client
|
||||
printfServer("Waiting for players...\n");
|
||||
printfServer("Waiting for players...");
|
||||
semOp(semid, 0, 1);
|
||||
semOp(semid, 1, 1);
|
||||
|
||||
|
@ -90,7 +90,7 @@ int main(int argc, char *argv[]){
|
|||
pids[player.id] = player.pid;
|
||||
|
||||
printfServer("Player ");
|
||||
printf("%s connected\n", name[player.id]);
|
||||
printf("%s connected", name[player.id]);
|
||||
|
||||
// aspetto secondo client
|
||||
semOp(semid, 2, -1);
|
||||
|
@ -101,14 +101,14 @@ int main(int argc, char *argv[]){
|
|||
pids[player.id] = player.pid;
|
||||
|
||||
printfServer("Player ");
|
||||
printf("%s connected\n", name[player.id]);
|
||||
printf("%s connected", name[player.id]);
|
||||
|
||||
semOp(semid, 0, 1);
|
||||
semOp(semid, 1, 1);
|
||||
|
||||
|
||||
// PARTITA
|
||||
printfServer("Starting game\n");
|
||||
printfServer("Starting game");
|
||||
setCollums(input.collums);
|
||||
setRows(input.rows);
|
||||
|
||||
|
@ -135,10 +135,10 @@ int main(int argc, char *argv[]){
|
|||
|
||||
|
||||
if (!result) { //FIXME: change printf
|
||||
printfServer("Game ended in a draw\n");
|
||||
printfServer("Game ended in a draw");
|
||||
|
||||
} else {
|
||||
printfServer("Game ended\n");
|
||||
printfServer("Game ended");
|
||||
}
|
||||
game_end_t game_end = {.mtype = 4, .winner = result};
|
||||
sndGame_end(&game_end);
|
||||
|
|
|
@ -16,7 +16,6 @@ void sigHandlerServer(int sig) {
|
|||
if (sig == SIGINT) {
|
||||
sigint_count--;
|
||||
if (sigint_count > 0) {
|
||||
printf("\n");
|
||||
printfServer("Press again Ctrl^C to exit ");
|
||||
printf("(whitin %d sec)\n", TIME_TO_RESET);
|
||||
alarm(TIME_TO_RESET);
|
||||
|
@ -26,25 +25,22 @@ void sigHandlerServer(int sig) {
|
|||
}
|
||||
|
||||
if (sig == SIGALRM) {
|
||||
printf("<Server> Time to exit (%d sec) expired\n", TIME_TO_RESET);
|
||||
printfServer("Time to exit (%d sec) expired", TIME_TO_RESET);
|
||||
sigint_count = 2;
|
||||
}
|
||||
|
||||
if (sig == SIGTERM || sig == SIGHUP) {
|
||||
if (pids[0] > 0) {
|
||||
printf("\n");
|
||||
printfServer("Terminating player one");
|
||||
kill(pids[0], SIGTERM);
|
||||
}
|
||||
if (pids[1] > 0){
|
||||
printf("\n");
|
||||
if (pids[1] > 0) {
|
||||
printfServer("Terminating player two");
|
||||
kill(pids[1], SIGTERM);
|
||||
}
|
||||
|
||||
// msgq
|
||||
if (_MSGQID) {
|
||||
printf("\n");
|
||||
printfServer("Deleting msg");
|
||||
if (msgctl(_MSGQID, IPC_RMID, NULL) == -1) { //TODO: funzione
|
||||
errExit("msgctl", "sigHandlerServer");
|
||||
|
@ -53,7 +49,6 @@ void sigHandlerServer(int sig) {
|
|||
|
||||
// sem
|
||||
if (semid) {
|
||||
printf("\n");
|
||||
printfServer("Deleting sem");
|
||||
if (semctl(semid, 0, IPC_RMID, 0) == -1) {
|
||||
errExit("semctl", "sigHandlerServer");
|
||||
|
@ -62,26 +57,21 @@ void sigHandlerServer(int sig) {
|
|||
|
||||
// shm
|
||||
if (board) {
|
||||
printf("\n");
|
||||
printfServer("Detathing shm");
|
||||
if (shmdt(board) == -1) {
|
||||
errExit("shmdt", "sigHandlerServer");
|
||||
}
|
||||
}
|
||||
if (shmid){
|
||||
printf("\n");
|
||||
printfServer("Deleting shm");
|
||||
if (shmctl(shmid, IPC_RMID, NULL) == -1) {
|
||||
errExit("shmctl", "SigHandlerSever");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
exit(0);
|
||||
exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
void setServerSignalHandler() {
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
#include <stdio.h>
|
||||
|
||||
void printfServer(char *string){
|
||||
printf("\033[34m<Server>\033[39m %s", string);
|
||||
printf("\n\033[34m<Server>\033[39m %s", string);
|
||||
}
|
||||
|
||||
void printfClient(char *string){
|
||||
printf("\033[94m<Client>\033[39m %s", string);
|
||||
printf("\n\033[94m<Client>\033[39m %s", string);
|
||||
}
|
Loading…
Reference in New Issue