2023-05-14 18:03:50 +02:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/ipc.h>
|
2023-05-14 21:44:03 +02:00
|
|
|
#include <sys/shm.h>
|
2023-05-16 14:55:12 +02:00
|
|
|
#include <sys/stat.h>
|
2023-05-15 16:50:48 +02:00
|
|
|
#include <signal.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
2023-05-16 15:55:05 +02:00
|
|
|
#include <sys/msg.h>
|
2023-05-14 18:03:50 +02:00
|
|
|
|
2023-05-14 18:10:44 +02:00
|
|
|
#include <server.h>
|
|
|
|
#include <structures.h>
|
2023-05-15 10:31:50 +02:00
|
|
|
#include <custom_sem.h>
|
2023-05-15 10:43:25 +02:00
|
|
|
#include <custom_shm.h>
|
2023-05-15 16:50:48 +02:00
|
|
|
#include <errExit.h>
|
|
|
|
|
|
|
|
void sigHandler(int sig) {
|
2023-05-16 15:26:58 +02:00
|
|
|
if (sig = SIGINT) {
|
|
|
|
//TODO:
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sig = SIGTERM) {
|
|
|
|
//TODO:
|
|
|
|
}
|
|
|
|
|
2023-05-15 16:50:48 +02:00
|
|
|
exit(0);
|
|
|
|
}
|
2023-05-14 17:07:34 +02:00
|
|
|
|
2023-05-14 18:03:50 +02:00
|
|
|
|
2023-05-14 17:07:34 +02:00
|
|
|
int main(int argc, char *argv[]){
|
2023-05-14 18:08:10 +02:00
|
|
|
input_server_t input = check_input(argc, argv);
|
2023-05-14 18:03:50 +02:00
|
|
|
|
2023-05-16 14:55:12 +02:00
|
|
|
|
|
|
|
// SHM
|
2023-05-15 10:43:25 +02:00
|
|
|
int shmBoardid = getShmid(input.rows, input.collums);
|
2023-05-14 22:04:24 +02:00
|
|
|
tile_t *board = shmServerAt(shmBoardid);
|
2023-05-16 14:55:12 +02:00
|
|
|
//
|
2023-05-14 22:04:24 +02:00
|
|
|
|
2023-05-16 14:55:12 +02:00
|
|
|
// SEM
|
|
|
|
key_t semKey = ftok(KEYFILE, 'S');
|
|
|
|
int semid = getSemid(semKey, 2);
|
2023-05-16 15:26:58 +02:00
|
|
|
|
|
|
|
resetServerSem(semid);
|
2023-05-16 14:55:12 +02:00
|
|
|
//
|
2023-05-15 10:31:50 +02:00
|
|
|
|
2023-05-16 14:55:12 +02:00
|
|
|
// SIGNAL
|
2023-05-15 16:50:48 +02:00
|
|
|
sigset_t mySet;
|
|
|
|
sigfillset(&mySet);
|
|
|
|
sigdelset(&mySet, SIGINT);
|
2023-05-16 15:26:58 +02:00
|
|
|
sigdelset(&mySet, SIGTERM);
|
2023-05-15 16:50:48 +02:00
|
|
|
sigprocmask(SIG_SETMASK, &mySet, NULL);
|
|
|
|
|
2023-05-16 15:55:05 +02:00
|
|
|
if (signal(SIGINT, sigHandler) == SIG_ERR) {
|
2023-05-16 15:26:58 +02:00
|
|
|
errExit("signal SIGINT", "f4Server");
|
2023-05-16 15:55:05 +02:00
|
|
|
}
|
|
|
|
if (signal(SIGTERM, sigHandler) == SIG_ERR) {
|
2023-05-16 15:26:58 +02:00
|
|
|
errExit("signal SIGTERM", "f4Server");
|
2023-05-16 15:55:05 +02:00
|
|
|
}
|
2023-05-16 14:55:12 +02:00
|
|
|
//
|
|
|
|
|
|
|
|
// MSGQ
|
|
|
|
key_t msgKey = ftok(KEYFILE, 'M');
|
2023-05-16 15:55:05 +02:00
|
|
|
int msgid = msgget(msgKey, IPC_CREAT | S_IRUSR | S_IWUSR);
|
|
|
|
if (msgid == -1) {
|
2023-05-16 14:55:12 +02:00
|
|
|
errExit("msgq", "F4Server");
|
2023-05-16 15:55:05 +02:00
|
|
|
}
|
|
|
|
// msg_t
|
|
|
|
msg_t msg = {
|
|
|
|
.mtype = 1,
|
|
|
|
.server_in = input
|
|
|
|
};
|
|
|
|
|
|
|
|
// msgsnd
|
|
|
|
if (msgsnd(msgid, &msg, sizeof(msg_t)-sizeof(long), 0) == -1) {
|
|
|
|
errExit("msgsnd", "F4Server");
|
|
|
|
}
|
|
|
|
|
2023-05-16 14:55:12 +02:00
|
|
|
//
|
2023-05-15 16:50:48 +02:00
|
|
|
|
|
|
|
|
2023-05-15 10:43:25 +02:00
|
|
|
|
2023-05-15 10:52:18 +02:00
|
|
|
semOp(semid, 0, 2); // aperurta semaforo per connessione dei due client
|
|
|
|
semOp(semid, 0, 0); // aspetto che si connettano i due client
|
2023-05-14 22:13:42 +02:00
|
|
|
|
2023-05-14 17:07:34 +02:00
|
|
|
|
2023-05-15 11:57:30 +02:00
|
|
|
// faccio quello che devo fare
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-05-14 17:07:34 +02:00
|
|
|
return 0;
|
|
|
|
}
|