97 lines
2.2 KiB
C
97 lines
2.2 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <ctype.h>
|
|
#include <server.h>
|
|
#include <sys/types.h>
|
|
#include <sys/ipc.h>
|
|
#include <sys/sem.h>
|
|
#include <sys/stat.h>
|
|
|
|
#include <errExit.h>
|
|
#include <signal.h>
|
|
#include <custom_msgq.h>
|
|
#include <custom_sem.h>
|
|
#include <custom_shm.h>
|
|
#include <custom_sig.h>
|
|
#include <forza4.h>
|
|
|
|
input_server_t _INPUT_S;
|
|
int turn = 0;
|
|
|
|
input_server_t checkServerInput(int argc, char *argv[]);
|
|
|
|
void setupServer(int argc, char *argv[]){
|
|
// CHECK INPUT
|
|
_INPUT_S = checkServerInput(argc, argv);
|
|
|
|
_ROWS = _INPUT_S.rows;
|
|
_COLLUMS = _INPUT_S.collums;
|
|
|
|
// SIGNAL
|
|
printf(PSERVER "Setting up signals\n");
|
|
setupServerSignalHandler();
|
|
|
|
// MSGQ
|
|
printf(PSERVER "Setting up msgq\n");
|
|
setupServerMsgq();
|
|
printf("msgq: %d\n", _MSGQID);
|
|
|
|
// SEM
|
|
printf(PSERVER "Setting up sem\n");
|
|
setupServerSem();
|
|
printf("sem: %d\n", _SEMID);
|
|
|
|
// SHM
|
|
printf(PSERVER "Setting up shm\n");
|
|
setupServerShm();
|
|
printf("shm: %d\n", _SHMID);
|
|
}
|
|
|
|
|
|
input_server_t checkServerInput(int argc, char *argv[]){
|
|
if (argc < 5) {
|
|
printf(PHELP "./F4Server ROW COL PLAYER1 PLAYER2\n"
|
|
"\t- ROW \t\tnumber of rows\n"
|
|
"\t- COL \t\tnumber of rows\n"
|
|
"\t- PLAYER1 \tplayer one token\n"
|
|
"\t- PLAYER2 \tplayer two token\n"
|
|
);
|
|
exit(EXIT_SUCCESS);
|
|
} else if (argc > 5) {
|
|
errExitMsg("To many arguments");
|
|
}
|
|
|
|
input_server_t input = {
|
|
.collums = atoi(argv[1]),
|
|
.rows = atoi(argv[2]),
|
|
.player1Token = argv[3][0],
|
|
.player2Token = argv[4][0]
|
|
};
|
|
|
|
if (input.collums < 5 || input.rows < 5) {
|
|
errExitMsg("To few rows or collums");
|
|
}
|
|
|
|
if (!isprint(input.player1Token) || !isprint(input.player2Token)) {
|
|
errExitMsg("Invalid player token");
|
|
}
|
|
|
|
return input;
|
|
}
|
|
|
|
void end_game(int winner){
|
|
game_end_t game_end = {.mtype = GAME_END, .winner = winner};
|
|
sndGame_end(&game_end);
|
|
sndGame_end(&game_end);
|
|
|
|
kill(_PIDS[0], SIGUSR1);
|
|
kill(_PIDS[1], SIGUSR1);
|
|
|
|
semOp(_SEMID, SERVER, -1);
|
|
semOp(_SEMID, SERVER, -1);
|
|
|
|
_PIDS[0] = 0;
|
|
_PIDS[1] = 0;
|
|
|
|
raise(SIGTERM);
|
|
} |