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-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>
|
|
|
|
|
|
|
|
|
|
|
|
void sigHandlerClient(int sig) {
|
|
|
|
if (sig == SIGINT) {
|
|
|
|
//TODO:
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sig == SIGTERM) {
|
2023-05-16 16:27:20 +02:00
|
|
|
//TODO: in teoria non fa niente di speciale qua, in quanto il server dovrebbe chiudere tutto
|
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
|
|
|
|
int client(){
|
2023-05-16 15:55:05 +02:00
|
|
|
|
2023-05-16 16:10:43 +02:00
|
|
|
input_server_t input;
|
|
|
|
|
|
|
|
// SIGNAL
|
|
|
|
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
|
|
|
|
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 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
|
|
|
|
int shmBoardid = getShmid(input.rows, input.collums);
|
2023-05-16 16:27:20 +02:00
|
|
|
tile_t *board = shmClientAt(shmBoardid);
|
2023-05-16 16:10:43 +02:00
|
|
|
//
|
|
|
|
|
|
|
|
// SEM
|
|
|
|
key_t semKey = ftok(KEYFILE, 'S');
|
2023-05-16 16:29:49 +02:00
|
|
|
int semid;
|
2023-05-16 16:10:43 +02:00
|
|
|
if ((semid = semget(semKey, 2, S_IRUSR | S_IWUSR | IPC_CREAT)) == -1) {
|
|
|
|
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
|
|
|
|
|
|
|
semOp(semid, 0, -1); // aspetta che il server gli dia il permesso di connettersi
|
|
|
|
|
|
|
|
printf("\033[94m<Client>\033[39m Connecting to server...\n");
|
|
|
|
|
|
|
|
|
|
|
|
semOp(semid, 1, -1); // finito operazioni di connessione
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|