#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include int main(int argc, char *argv[]){ // CHECK_INPUT input_server_t input = check_input(argc, argv); // SIGNAL printfServer("Setting up signals\n"); setServerSignalHandler(); // // MSGQ printfServer("Setting up msgq\n"); //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'); _MSGQID = msgget(msgKey, IPC_CREAT | S_IRUSR | S_IWUSR); if (_MSGQID == -1){ errExit("msgget", "F4Server"); } // msg msg_t msg = { .mtype = 1, .server_in = input, .player_id = 0 }; // msgsnd sndMsg(&msg); msg.player_id = 1; sndMsg(&msg); // // SHM printfServer("Setting up shm\n"); shmid = getShmid(input.rows, input.collums); board = shmServerAt(shmid); // // printfServer("Setting up sem\n"); key_t semKey = ftok(KEYFILE, 'S'); semid = semget(semKey, 3, IPC_CREAT | S_IRUSR | S_IWUSR); if (semid == -1) { errExit("getsem", "F4Server"); } resetServerSem(semid); // // aperurta semaforo per connessione dei due client printfServer("Waiting for players...\n"); 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); 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; printfServer("Player "); printf("%s connected\n", name[player.id]); // aspetto secondo client semOp(semid, 2, -1); // ricevuta nome secondo client rcvPlayer(&player); strcpy(name[player.id], player.name_player); pids[player.id] = player.pid; printfServer("Player "); printf("%s connected\n", name[player.id]); semOp(semid, 0, 1); semOp(semid, 1, 1); // PARTITA printfServer("Starting game\n"); setCollums(input.collums); setRows(input.rows); int turns_left = input.rows * input.collums; int turn = 0; tile_t result; move_t move; do { semOp(semid, turn, 1); turn ^= 1; printfServer(""); printf("%s's turn\n", name[turn]); rcvMove(&move); //TMP printf("move: %d\n", move.move); printf("turns left: %d\n", turns_left - 1); } while (--turns_left && !(result = checkWin(board, move.move))); if (!result) { //FIXME: change printf printfServer("Game ended in a draw\n"); } else { printfServer("Game ended\n"); } game_end_t game_end = {.mtype = 4, .winner = result}; sndGame_end(&game_end); sndGame_end(&game_end); kill(pids[0], SIGUSR1); kill(pids[1], SIGUSR1); //TODO: end of game return 0; }