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-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-16 23:27:27 +02:00
|
|
|
#include <nonsodovemetterle.h>
|
2023-05-16 18:43:56 +02:00
|
|
|
|
|
|
|
tile_t *boardClient = NULL;
|
2023-05-16 16:10:43 +02:00
|
|
|
|
|
|
|
void sigHandlerClient(int sig) {
|
|
|
|
if (sig == SIGINT) {
|
|
|
|
//TODO:
|
2023-05-16 18:43:56 +02:00
|
|
|
|
|
|
|
sig = SIGTERM;
|
2023-05-16 16:10:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (sig == SIGTERM) {
|
2023-05-16 18:43:56 +02:00
|
|
|
// shm
|
|
|
|
if (boardClient) {
|
2023-05-16 23:27:27 +02:00
|
|
|
printf("\n");
|
2023-05-16 18:43:56 +02:00
|
|
|
printfClient("Detathing shm");
|
|
|
|
if (shmdt(boardClient) == -1) {
|
|
|
|
errExit("shmdt", "sigHandlerServer");
|
|
|
|
}
|
|
|
|
}
|
2023-05-16 23:27:27 +02:00
|
|
|
|
|
|
|
printf("\n");
|
2023-05-16 16:10:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2023-05-16 23:27:27 +02:00
|
|
|
//TODO: controllo input
|
|
|
|
|
2023-05-16 16:10:43 +02:00
|
|
|
input_server_t input;
|
|
|
|
|
|
|
|
// SIGNAL
|
2023-05-16 18:43:56 +02:00
|
|
|
printfClient("Setting up signal\n");
|
2023-05-16 16:10:43 +02:00
|
|
|
sigset_t mySet;
|
|
|
|
sigfillset(&mySet);
|
|
|
|
sigdelset(&mySet, SIGINT);
|
|
|
|
sigdelset(&mySet, SIGTERM);
|
|
|
|
sigprocmask(SIG_SETMASK, &mySet, NULL);
|
|
|
|
|
|
|
|
if (signal(SIGINT, sigHandlerClient) == SIG_ERR) {
|
|
|
|
errExit("signal SIGINT", "F4Client");
|
|
|
|
}
|
|
|
|
if (signal(SIGTERM, sigHandlerClient) == SIG_ERR) {
|
|
|
|
errExit("signal SIGTERM", "F4Client");
|
|
|
|
}
|
|
|
|
//
|
|
|
|
|
2023-05-16 15:55:05 +02:00
|
|
|
// MSGQ
|
2023-05-16 18:43:56 +02:00
|
|
|
printfClient("Setting up msgq\n");
|
2023-05-16 15:55:05 +02:00
|
|
|
key_t msgKey = ftok(KEYFILE, 'M');
|
2023-05-16 16:10:43 +02:00
|
|
|
int msgid = msgget(msgKey, IPC_CREAT | S_IRUSR | S_IWUSR);
|
|
|
|
if (msgid == -1) {
|
|
|
|
errExit("msgget", "F4Client");
|
2023-05-16 15:55:05 +02:00
|
|
|
}
|
|
|
|
|
2023-05-16 16:10:43 +02:00
|
|
|
// msg
|
|
|
|
msg_t msg;
|
|
|
|
|
2023-05-16 16:53:23 +02:00
|
|
|
// msgrcv
|
2023-05-16 18:43:56 +02:00
|
|
|
printfClient("Waiting for message...\n");
|
2023-05-16 16:10:43 +02:00
|
|
|
if (msgrcv(msgid, &msg, sizeof(msg_t) - sizeof(long), 1, 0) == -1) {
|
|
|
|
errExit("msgrcv", "F4Client");
|
|
|
|
}
|
|
|
|
|
|
|
|
input = msg.server_in;
|
2023-05-16 15:55:05 +02:00
|
|
|
//
|
|
|
|
|
|
|
|
|
2023-05-16 16:10:43 +02:00
|
|
|
// SHM
|
2023-05-16 18:43:56 +02:00
|
|
|
printfClient("Setting up shm\n");
|
2023-05-16 16:10:43 +02:00
|
|
|
int shmBoardid = getShmid(input.rows, input.collums);
|
2023-05-16 18:43:56 +02:00
|
|
|
boardClient = shmClientAt(shmBoardid);
|
2023-05-16 16:10:43 +02:00
|
|
|
//
|
|
|
|
|
|
|
|
// SEM
|
2023-05-16 18:43:56 +02:00
|
|
|
printfClient("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-15 11:24:39 +02:00
|
|
|
|
2023-05-16 23:27:27 +02:00
|
|
|
semOp(semid, msg.player_id, -1); // aspetta che il server gli dia il permesso di connettersi
|
2023-05-16 18:43:56 +02:00
|
|
|
printfClient("Connecting to server...\n");
|
2023-05-16 23:27:27 +02:00
|
|
|
semOp(semid, 2, 1); // finito operazioni di connessione
|
2023-05-15 11:24:39 +02:00
|
|
|
|
|
|
|
|
2023-05-16 23:27:27 +02:00
|
|
|
// mando il mio nome al server
|
|
|
|
printf("%s\n", argv[1]);
|
|
|
|
player_names_t name = {.mtype = 3, .id = msg.player_id, .name_player = argv[1]};
|
|
|
|
if (msgsnd(msgid, &name, sizeof(player_names_t) - sizeof(long), 0) == -1) {
|
|
|
|
errExit("msgsnd", "mandare nome");
|
|
|
|
}
|
2023-05-16 17:07:35 +02:00
|
|
|
|
2023-05-16 18:43:56 +02:00
|
|
|
printfClient("Searching for oponent...\n");
|
|
|
|
|
|
|
|
|
2023-05-16 23:27:27 +02:00
|
|
|
|
|
|
|
|
|
|
|
move_t move = {.mtype = 2};
|
|
|
|
// PARTITA
|
|
|
|
while (1) { //FIXME: potenzialmente da cambiare questo while (ma forse bastano i segnali)
|
2023-05-16 17:07:35 +02:00
|
|
|
semOp(semid, msg.player_id, -1); // aspetto il mio turno
|
|
|
|
|
2023-05-16 23:27:27 +02:00
|
|
|
printfServer("Tocca a te: \n");
|
|
|
|
scanf("%d", &move.move);
|
|
|
|
|
|
|
|
if (msgsnd(msgid, &move, sizeof(move_t) - sizeof(long), 0) == -1) {
|
|
|
|
errExit("msgsnd", "partita_client");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-05-16 17:07:35 +02:00
|
|
|
}
|
|
|
|
|
2023-05-15 11:24:39 +02:00
|
|
|
return 0;
|
|
|
|
}
|