Elaborato_SO/src/F4Client.c

113 lines
2.5 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/stat.h>
#include <sys/msg.h>
#include <signal.h>
#include <sys/sem.h>
#include <sys/shm.h>
#include <string.h>
#include <unistd.h>
#include <custom_sem.h>
#include <structures.h>
#include <custom_shm.h>
#include <errExit.h>
#include <custom_msgq.h>
#include <forza4.h>
#include <client.h>
void checkClientinput(int argc, char *argv[]);
//TODO: mettere main ma non l'ho fatto per makefile
int main(int argc, char *argv[]){
printf("Il mio pid per killarmi: %d\n", getpid()); //TMP
setupClient(argc, argv);
//
// MSGQ
// msg
input_server_t input;
msg_t msg;
// msgrcv
printf(PCLIENT "Waiting for message...\n");
rcvMsg(&msg);
input = msg.server_in;
//
// SHM
printf(PCLIENT "Setting up shm\n");
int shmBoardid = getShmid();
_BOARD = shmClientAt(shmBoardid);
//
// SEM
printf(PCLIENT "Setting up sem\n");
key_t semKey = ftok(KEYFILE, 'S');
int semid;
if ((semid = semget(semKey, 3, S_IRUSR | S_IWUSR | IPC_CREAT)) == -1) {
errExit("getsem", "F4Client");
}
//
// aspetto che il server mi dia il permesso di collegarmi e segnalo collegamento al server
semOp(semid, msg.player_id, -1);
printf(PCLIENT "Connecting to server...\n");
semOp(semid, 2, 1);
// mando i miei dati al server
player_ds player = {.mtype = 3, .id = msg.player_id, .pid = getpid()};
strcpy(player.name_player, argv[1]);
sndPlayer(&player);
printf(PCLIENT "Searching for oponent...\n");
semOp(semid, msg.player_id, -1);
printf(PCLIENT "Opponent found\n");
// PARTITA
move_t move = {.mtype = 2};
setRows(input.rows); //FIXME: da mettere in un setup
setCollums(input.collums);
setTokens(msg.server_in.player1Token, msg.server_in.player2Token);
while (1) {
// aspetto il mio turno
semOp(semid, msg.player_id, -1); // mettere id eccetera in var
printBoard(_BOARD);
// input e controllo
printf(PCLIENT "Your Turn: ");
int pos;
do {
scanf("%d", &move.move);
move.move = checkMove(_BOARD, move.move - 1);
} while (move.move == -1);
// mando mossa al server
sndMove(&move);
semOp(semid, msg.player_id, -1);
printBoard(_BOARD);
printf(PCLIENT "Waiting for oponent\n"); //FIXME: solo se la partita non è finita
}
return 0;
}