modifica variabili globali (nome e static)

This commit is contained in:
Edi De Candido 2023-05-23 12:37:44 +02:00
parent df06c131a2
commit be6bc918ea
6 changed files with 39 additions and 40 deletions

View File

@ -5,7 +5,7 @@
#define MAX_NAME 16
int _MSGQID;
static int _MSGQID;
#define MSG 1
#define MOVE 2

View File

@ -32,13 +32,13 @@ typedef enum {
PLAYER2
} tile_t;
int semid;
int shmid;
static int _SEMID;
static int _SHMID;
tile_t *board;
static tile_t *_BOARD;
pid_t pids[2];
static pid_t _PIDS[2];
input_server_t input;
static input_server_t _INPUT_S;
#endif

View File

@ -23,7 +23,7 @@ int main(int argc, char *argv[]){
printf("Il mio pid per killarmi: %d\n", getpid()); //TMP
// CHECK_INPUT
input = check_input(argc, argv);
_INPUT_S = check_input(argc, argv);
// SIGNAL
printf(PSERVER "Setting up signals\n");
@ -37,8 +37,8 @@ int main(int argc, char *argv[]){
//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
printf(PSERVER "Setting up shm\n");
shmid = getShmid(input.rows, input.collums);
board = shmServerAt(shmid);
_SHMID = getShmid(_INPUT_S.rows, _INPUT_S.collums);
_BOARD = shmServerAt(_SHMID);
//
// SEM
@ -49,52 +49,51 @@ int main(int argc, char *argv[]){
// aperurta semaforo per connessione dei due client
printf(PSERVER "Waiting for players...\n");
semOp(semid, 0, 1);
semOp(semid, 1, 1);
semOp(_SEMID, 0, 1);
semOp(_SEMID, 1, 1);
// aspetto che si connettano i due client
//FIXME: forse non serve perche tanto deve aspettare che i player gli mandono un messaggio con il loro nome (a cose servono i semafori se ci sono i messaggi che fanno l'attesa non in polling aaaaaaaaa)
semOp(semid, 2, -1);
semOp(_SEMID, 2, -1);
player_ds player;
char name[2][MAX_NAME];
pids[2];
// ricevuta nome primo client
rcvPlayer(&player);
strcpy(name[player.id], player.name_player);
pids[player.id] = player.pid;
_PIDS[player.id] = player.pid;
printf(PSERVER "Player %s connected\n", name[player.id]);
// aspetto secondo client
semOp(semid, 2, -1);
semOp(_SEMID, 2, -1);
// ricevuta nome secondo client
rcvPlayer(&player);
strcpy(name[player.id], player.name_player);
pids[player.id] = player.pid;
_PIDS[player.id] = player.pid;
printf(PSERVER "Player %s connected\n", name[player.id]);
semOp(semid, 0, 1);
semOp(semid, 1, 1);
semOp(_SEMID, 0, 1);
semOp(_SEMID, 1, 1);
// PARTITA
printf(PSERVER "Starting game\n");
setCollums(input.collums);
setRows(input.rows);
setCollums(_INPUT_S.collums);
setRows(_INPUT_S.rows);
int turns_left = input.rows * input.collums;
int turns_left = _INPUT_S.rows * _INPUT_S.collums;
int turn = 0;
tile_t result;
move_t move;
do {
semOp(semid, turn, 1);
semOp(_SEMID, turn, 1);
turn ^= 1;
printf(PSERVER "%s's turn\n", name[turn]);
@ -104,12 +103,12 @@ int main(int argc, char *argv[]){
//TMP
printf("move: %d\n", move.move);
insertMove(board, move.move, turn);
insertMove(_BOARD, move.move, turn);
printf("turns left: %d\n", turns_left - 1);
} while (--turns_left && !(result = checkWin(board, move.move)));
} while (--turns_left && !(result = checkWin(_BOARD, move.move)));
if (!result) { //FIXME: change printf
@ -123,8 +122,8 @@ int main(int argc, char *argv[]){
sndGame_end(&game_end);
sndGame_end(&game_end);
kill(pids[0], SIGUSR1);
kill(pids[1], SIGUSR1);
kill(_PIDS[0], SIGUSR1);
kill(_PIDS[1], SIGUSR1);
//TODO: end of game

View File

@ -27,7 +27,7 @@ void openMsgq() {
void sndId() {
msg_t msg = {
.mtype = 1,
.server_in = input,
.server_in = _INPUT_S,
.player_id = 0
};

View File

@ -10,12 +10,12 @@ void resetServerSem(int semid);
void setupServerSem() {
key_t semKey = ftok(KEYFILE, 'S');
semid = semget(semKey, 3, IPC_CREAT | S_IRUSR | S_IWUSR);
if (semid == -1) {
_SEMID = semget(semKey, 3, IPC_CREAT | S_IRUSR | S_IWUSR);
if (_SEMID == -1) {
errExit("getsem", "F4Server");
}
resetServerSem(semid);
resetServerSem(_SEMID);
}
void semOp(int semid, short sem_num, short sem_op) {

View File

@ -52,13 +52,13 @@ void sigHandlerServer(int sig) {
if (sig == SIGTERM || sig == SIGHUP) {
if (pids[0] > 0) {
if (_PIDS[0] > 0) {
printf(PSERVER "Terminating player one\n");
kill(pids[0], SIGTERM);
kill(_PIDS[0], SIGTERM);
}
if (pids[1] > 0) {
if (_PIDS[1] > 0) {
printf(PSERVER "Terminating player two\n");
kill(pids[1], SIGTERM);
kill(_PIDS[1], SIGTERM);
}
// msgq
@ -70,23 +70,23 @@ void sigHandlerServer(int sig) {
}
// sem
if (semid) {
if (_SEMID) {
printf(PSERVER "Deleting sem\n");
if (semctl(semid, 0, IPC_RMID, 0) == -1) {
if (semctl(_SEMID, 0, IPC_RMID, 0) == -1) {
errExit("semctl", "sigHandlerServer");
}
}
// shm
if (board) {
if (_BOARD) {
printf(PSERVER "Detathing shm\n");
if (shmdt(board) == -1) {
if (shmdt(_BOARD) == -1) {
errExit("shmdt", "sigHandlerServer");
}
}
if (shmid){
if (_SHMID){
printf(PSERVER "Deleting shm\n");
if (shmctl(shmid, IPC_RMID, NULL) == -1) {
if (shmctl(_SHMID, IPC_RMID, NULL) == -1) {
errExit("shmctl", "SigHandlerSever");
}
}