Elaborato_SO/src/F4Client.c

113 lines
2.5 KiB
C
Raw Normal View History

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