server_setup

This commit is contained in:
Lorenzo Bianchi 2023-05-29 11:37:43 +02:00
parent 8e51cc1ab4
commit 14267e3aee
10 changed files with 34 additions and 42 deletions

View File

@ -9,4 +9,4 @@
- define per i semafori invece di usare 1/-1
- ctrl+c con time
- abbandono player

View File

@ -5,6 +5,8 @@
extern int _SHMID;
void setupServerShm();
int getShmid(int row, int col);
tile_t * shmServerAt(int shmid);
tile_t * shmClientAt(int shmid);

View File

@ -11,11 +11,11 @@
extern tile_t *_BOARD;
// global variable
static int _ROWS;
static int _COLLUMS;
extern int _ROWS;
extern int _COLLUMS;
static char _TOKEN1;
static char _TOKEN2;
extern char _TOKEN1;
extern char _TOKEN2;
void setCollums(int collums);
void setRows(int rows);

View File

@ -7,6 +7,8 @@
#define PCLIENT "\033[94m<Client>\033[39m "
#define PHELP "\033[92m<Help>\033[39m "
#define CLEAR 1
// semaphore
union semun {
int val;

View File

@ -150,7 +150,6 @@ int main(int argc, char *argv[]){
while (1) { //FIXME: potenzialmente da cambiare questo while (ma forse bastano i segnali)
// aspetto il mio turno
semOp(semid, msg.player_id, -1); // mettere id eccetera in var
printBoard(boardClient);
// input e controllo

View File

@ -22,17 +22,7 @@
int main(int argc, char *argv[]){
printf("Il mio pid per killarmi: %d\n", getpid()); //FIXME: TMP
setupServer(argc, argv); //TODO: passare struct con id
//FIXME: fixare usando le var globali prima di mettere in setupServer
// SHM
//TODO: forse bisogna fare il reset della shm, perche in teoria mette a 0 quando crea
//ma nel caso in cui il campo non si fosse chiuso per qualche motivo forse quando lo riapre non lo azzera
printf(PSERVER "Setting up shm\n");
_SHMID = getShmid(_INPUT_S.rows, _INPUT_S.collums);
_BOARD = shmServerAt(_SHMID);
//
setupServer(argc, argv);
// aperta semaforo per connessione dei due client
@ -42,7 +32,6 @@ int main(int argc, char *argv[]){
// aspetto che si connettano i due client
//FIXME: forse non serve perche tanto deve aspettare che i player gli mandono un messaggio con il loro nome (a cose servono i semafori se ci sono i messaggi che fanno l'attesa non in polling aaaaaaaaa)
semOp(_SEMID, 2, -1);
player_ds player;
@ -73,8 +62,6 @@ int main(int argc, char *argv[]){
// PARTITA
printf(PSERVER "Starting game\n");
_ROWS = _INPUT_S.rows;
_COLLUMS = _INPUT_S.collums;
printf("%d %d\n", _ROWS, _COLLUMS);
for (int i=0; i < _ROWS * _COLLUMS; i++) { //FIXME: tmp
_BOARD[i] = EMPTY;

View File

@ -8,6 +8,11 @@
int _SHMID;
void setupServerShm() {
_SHMID = getShmid(_ROWS, _COLLUMS);
_BOARD = shmServerAt(_SHMID);
}
int getShmid(int row, int col) {
int shmid = shmget(ftok(KEYFILE, 'z'), row * col * sizeof(tile_t), IPC_CREAT | S_IRUSR | S_IWUSR);
if (shmid == -1) {

View File

@ -23,25 +23,6 @@ void sigIntHandler2(int sig) {
}
void sigHandlerServer(int sig) {
// if (sig == SIGINT) { // FIXME: non riesco a farlo funzionare, quando premo ctr+c è come se gli arrivasse al server la mossa di un giocatore
// UPDATE: funzia ma è piu bella la mia sol?
// sigint_count--;
// if (sigint_count > 0) {
// printf(PSERVER "Press again Ctrl^C to exit ");
// printf("(whitin %d sec)\n", TIME_TO_RESET);
// alarm(TIME_TO_RESET);
// } else {
// alarm(0); // toglie l'allarme
// sig = SIGTERM;
// }
// }
// if (sig == SIGALRM) {
// printf(PSERVER "");
// printf("Time to exit (%d sec) expired", TIME_TO_RESET);
// sigint_count = 2;
// }
if (sig == SIGINT) {
printf(PSERVER "Press again Ctrl^C to exit (whitin %d sec)\n", TIME_TO_RESET);
signal(SIGINT, sigIntHandler2);
@ -63,7 +44,6 @@ void sigHandlerServer(int sig) {
printf(PSERVER "Terminating player two\n");
kill(_PIDS[1], SIGTERM);
}
//
// msgq
if (_MSGQID) {

View File

@ -1,10 +1,18 @@
#include <stdio.h>
#include <stdlib.h>
#include <forza4.h>
#include <errExit.h>
#include <structures.h>
tile_t *_BOARD;
int _ROWS;
int _COLLUMS;
char _TOKEN1;
char _TOKEN2;
void setRows(int rows) {
_ROWS = rows;
}
@ -81,6 +89,10 @@ int checkWinAll(tile_t *board){
}
void printBoard(tile_t *board) {
if (CLEAR) {
system("clear");
}
for (int i=0; i < _ROWS; i++) {
printf("|");
for (int j=0; j < _COLLUMS; j++) {

View File

@ -13,7 +13,7 @@
#include <custom_sem.h>
#include <custom_shm.h>
#include <custom_sig.h>
#include <forza4.h>
input_server_t _INPUT_S;
@ -21,6 +21,9 @@ void setupServer(int argc, char *argv[]){
// CHECK_INPUT
_INPUT_S = check_input(argc, argv);
_ROWS = _INPUT_S.rows;
_COLLUMS = _INPUT_S.collums;
// MSGQ
printf(PSERVER "Setting up msgq\n");
setupServerMsgq();
@ -33,7 +36,9 @@ void setupServer(int argc, char *argv[]){
printf(PSERVER "Setting up sem\n");
setupServerSem();
// SHM
printf(PSERVER "Setting up shm\n");
setupServerShm();
}