server_setup
This commit is contained in:
parent
b1ab3a883f
commit
ae091eef3e
2
Makefile
2
Makefile
|
@ -82,4 +82,4 @@ comp: server_c client_c
|
||||||
server_c:
|
server_c:
|
||||||
gcc src/F4Server.c src/custom_sem.c src/custom_shm.c src/custom_sig.c src/custom_msgq.c src/errExit.c src/forza4.c src/server.c -o bin/F4Server -I inc/
|
gcc src/F4Server.c src/custom_sem.c src/custom_shm.c src/custom_sig.c src/custom_msgq.c src/errExit.c src/forza4.c src/server.c -o bin/F4Server -I inc/
|
||||||
client_c:
|
client_c:
|
||||||
gcc src/F4Client.c src/custom_sem.c src/custom_shm.c src/custom_sig.c src/custom_msgq.c src/errExit.c src/forza4.c src/server.c -o bin/F4Client -I inc/
|
gcc src/F4Client.c src/custom_sem.c src/custom_shm.c src/custom_sig.c src/custom_msgq.c src/errExit.c src/forza4.c src/client.c src/server.c -o bin/F4Client -I inc/
|
|
@ -0,0 +1,6 @@
|
||||||
|
#ifndef CLIENT_H
|
||||||
|
#define CLIENT_H
|
||||||
|
|
||||||
|
void setupClient(int argc, char *argv[]);
|
||||||
|
|
||||||
|
#endif
|
|
@ -7,8 +7,8 @@ extern int _SHMID;
|
||||||
|
|
||||||
void setupServerShm();
|
void setupServerShm();
|
||||||
|
|
||||||
int getShmid(int row, int col);
|
int getShmid();
|
||||||
tile_t * shmServerAt(int shmid);
|
tile_t * shmServerAt();
|
||||||
tile_t * shmClientAt(int shmid);
|
tile_t * shmClientAt(int shmid);
|
||||||
void shmDt(void *shm_ptr);
|
void shmDt(void *shm_ptr);
|
||||||
void shmServerRm(int shmid);
|
void shmServerRm(int shmid);
|
||||||
|
|
|
@ -19,6 +19,4 @@ typedef struct {
|
||||||
|
|
||||||
void setupServer(int argc, char *argv[]);
|
void setupServer(int argc, char *argv[]);
|
||||||
|
|
||||||
input_server_t check_input(int argc, char *argv[]);
|
|
||||||
|
|
||||||
#endif
|
#endif
|
|
@ -16,8 +16,9 @@
|
||||||
#include <errExit.h>
|
#include <errExit.h>
|
||||||
#include <custom_msgq.h>
|
#include <custom_msgq.h>
|
||||||
#include <forza4.h>
|
#include <forza4.h>
|
||||||
|
#include <client.h>
|
||||||
|
|
||||||
int game_state;
|
int game_state; //FIXME: non ancora utilizzato
|
||||||
|
|
||||||
tile_t *boardClient;
|
tile_t *boardClient;
|
||||||
|
|
||||||
|
@ -59,7 +60,7 @@ int main(int argc, char *argv[]){
|
||||||
|
|
||||||
printf("Il mio pid per killarmi: %d\n", getpid()); //TMP
|
printf("Il mio pid per killarmi: %d\n", getpid()); //TMP
|
||||||
|
|
||||||
checkClientinput(argc, argv);
|
setupClient(argc, argv);
|
||||||
|
|
||||||
// SIGNAL
|
// SIGNAL
|
||||||
printf(PCLIENT "Setting up signal\n");
|
printf(PCLIENT "Setting up signal\n");
|
||||||
|
@ -107,7 +108,7 @@ int main(int argc, char *argv[]){
|
||||||
|
|
||||||
// SHM
|
// SHM
|
||||||
printf(PCLIENT "Setting up shm\n");
|
printf(PCLIENT "Setting up shm\n");
|
||||||
int shmBoardid = getShmid(input.rows, input.collums);
|
int shmBoardid = getShmid();
|
||||||
boardClient = shmClientAt(shmBoardid);
|
boardClient = shmClientAt(shmBoardid);
|
||||||
//
|
//
|
||||||
|
|
||||||
|
@ -171,20 +172,4 @@ int main(int argc, char *argv[]){
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
|
||||||
|
|
||||||
void checkClientinput(int argc, char *argv[]){
|
|
||||||
if (argc < 2) {
|
|
||||||
printf(PHELP "./F4Client PLAYER_NAME\n"
|
|
||||||
"\t- PLAYER_NAME \t\tplayer name\n"
|
|
||||||
);
|
|
||||||
exit(EXIT_SUCCESS);
|
|
||||||
} else if (argc > 2) {
|
|
||||||
printf(PCLIENT "To many arguments!");
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!argv[1] || strlen(argv[1]) > MAX_NAME) {
|
|
||||||
errExitMsg("Invalid player name");
|
|
||||||
}
|
|
||||||
}
|
}
|
|
@ -24,54 +24,49 @@ int main(int argc, char *argv[]){
|
||||||
|
|
||||||
setupServer(argc, argv);
|
setupServer(argc, argv);
|
||||||
|
|
||||||
|
|
||||||
// aperta semaforo per connessione dei due client
|
// aperta semaforo per connessione dei due client
|
||||||
printf(PSERVER "Waiting for players...\n");
|
printf(PSERVER "Waiting for players...\n");
|
||||||
semOp(_SEMID, 0, 1);
|
semOp(_SEMID, CLIENT0, 1);
|
||||||
semOp(_SEMID, 1, 1);
|
semOp(_SEMID, CLIENT1, 1);
|
||||||
|
|
||||||
|
|
||||||
// aspetto che si connettano i due client
|
|
||||||
semOp(_SEMID, 2, -1);
|
|
||||||
|
|
||||||
player_ds player;
|
player_ds player;
|
||||||
char name[2][MAX_NAME];
|
char name[2][MAX_NAME];
|
||||||
|
|
||||||
// ricevuta nome primo client
|
// aspetto PRIMO client
|
||||||
|
semOp(_SEMID, SERVER, -1);
|
||||||
|
// ricevo nome primo client
|
||||||
rcvPlayer(&player);
|
rcvPlayer(&player);
|
||||||
|
|
||||||
strcpy(name[player.id], player.name_player);
|
strcpy(name[player.id], player.name_player);
|
||||||
_PIDS[player.id] = player.pid;
|
_PIDS[player.id] = player.pid;
|
||||||
|
|
||||||
printf(PSERVER "Player %s connected\n", name[player.id]);
|
printf(PSERVER "Player %s connected\n", name[player.id]);
|
||||||
|
|
||||||
// aspetto secondo client
|
// aspetto SECONDO client
|
||||||
semOp(_SEMID, 2, -1);
|
semOp(_SEMID, SERVER, -1);
|
||||||
// ricevo nome secondo client
|
// ricevo nome secondo client
|
||||||
rcvPlayer(&player);
|
rcvPlayer(&player);
|
||||||
|
|
||||||
strcpy(name[player.id], player.name_player);
|
strcpy(name[player.id], player.name_player);
|
||||||
_PIDS[player.id] = player.pid;
|
_PIDS[player.id] = player.pid;
|
||||||
|
|
||||||
printf(PSERVER "Player %s connected\n", name[player.id]);
|
printf(PSERVER "Player %s connected\n", name[player.id]);
|
||||||
|
|
||||||
semOp(_SEMID, 0, 1);
|
|
||||||
semOp(_SEMID, 1, 1);
|
|
||||||
|
|
||||||
|
// avvisto inizio partita
|
||||||
|
semOp(_SEMID, CLIENT0, 1);
|
||||||
|
semOp(_SEMID, CLIENT1, 1);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// PARTITA
|
// PARTITA
|
||||||
printf(PSERVER "Starting game\n");
|
printf(PSERVER "Starting game\n");
|
||||||
|
|
||||||
|
|
||||||
int turns_left = _INPUT_S.rows * _INPUT_S.collums;
|
int turns_left = _INPUT_S.rows * _INPUT_S.collums;
|
||||||
int turn = 0;
|
int turn = 0;
|
||||||
tile_t result;
|
tile_t result;
|
||||||
move_t move;
|
move_t move;
|
||||||
|
|
||||||
do {
|
do {
|
||||||
// apro semaforo al player
|
// apro semaforo al player di turno
|
||||||
semOp(_SEMID, turn, 1);
|
semOp(_SEMID, turn, 1);
|
||||||
|
|
||||||
printf(PSERVER "%s's turn\n", name[turn]);
|
printf(PSERVER "%s's turn\n", name[turn]);
|
||||||
|
@ -88,12 +83,13 @@ int main(int argc, char *argv[]){
|
||||||
// dico al player che la mossa è stata effettuata per fargli stampare il campo
|
// dico al player che la mossa è stata effettuata per fargli stampare il campo
|
||||||
semOp(_SEMID, turn, 1);
|
semOp(_SEMID, turn, 1);
|
||||||
|
|
||||||
|
// next turn
|
||||||
turn ^= 1;
|
turn ^= 1;
|
||||||
|
|
||||||
} while (--turns_left && !(result = checkWin(_BOARD, move.move)));
|
} while (--turns_left && !(result = checkWin(_BOARD, move.move)));
|
||||||
|
|
||||||
|
|
||||||
if (!result) { //FIXME: change printf
|
if (!result) {
|
||||||
printf(PSERVER "Game ended in a draw\n");
|
printf(PSERVER "Game ended in a draw\n");
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -0,0 +1,39 @@
|
||||||
|
#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 <string.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>
|
||||||
|
|
||||||
|
void checkClientinput(int argc, char *argv[]);
|
||||||
|
|
||||||
|
void setupClient(int argc, char *argv[]) {
|
||||||
|
checkClientinput(argc, argv);
|
||||||
|
}
|
||||||
|
|
||||||
|
void checkClientinput(int argc, char *argv[]){
|
||||||
|
if (argc < 2) {
|
||||||
|
printf(PHELP "./F4Client PLAYER_NAME\n"
|
||||||
|
"\t- PLAYER_NAME \t\tplayer name\n"
|
||||||
|
);
|
||||||
|
exit(EXIT_SUCCESS);
|
||||||
|
} else if (argc > 2) {
|
||||||
|
printf(PCLIENT "To many arguments!");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!argv[1] || strlen(argv[1]) > MAX_NAME) {
|
||||||
|
errExitMsg("Invalid player name");
|
||||||
|
}
|
||||||
|
}
|
|
@ -8,26 +8,31 @@
|
||||||
|
|
||||||
int _SHMID;
|
int _SHMID;
|
||||||
|
|
||||||
|
int getShmid();
|
||||||
|
tile_t *shmServerAt();
|
||||||
|
|
||||||
void setupServerShm() {
|
void setupServerShm() {
|
||||||
_SHMID = getShmid(_ROWS, _COLLUMS);
|
_SHMID = getShmid();
|
||||||
_BOARD = shmServerAt(_SHMID);
|
_BOARD = shmServerAt();
|
||||||
|
|
||||||
for (int i=0; i < _ROWS * _COLLUMS; i++) {
|
for (int i=0; i < _ROWS * _COLLUMS; i++) {
|
||||||
_BOARD[i] = EMPTY;
|
_BOARD[i] = EMPTY;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int getShmid(int row, int col) {
|
int getShmid() {
|
||||||
int shmid = shmget(ftok(KEYFILE, 'z'), row * col * sizeof(tile_t), IPC_CREAT | S_IRUSR | S_IWUSR);
|
int shmid = shmget(ftok(KEYFILE, 'z'), _ROWS * _COLLUMS * sizeof(tile_t), IPC_CREAT | S_IRUSR | S_IWUSR);
|
||||||
|
printf("%d\n", shmid);
|
||||||
if (shmid == -1) {
|
if (shmid == -1) {
|
||||||
errExit("shmget", "getShmid");
|
errExit("shmget", "getShmid");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
return shmid;
|
return shmid;
|
||||||
}
|
}
|
||||||
|
|
||||||
tile_t * shmServerAt(int shmid) {
|
tile_t *shmServerAt() {
|
||||||
tile_t *board = (tile_t *)shmat(shmid, NULL, 0);
|
tile_t *board = (tile_t *)shmat(_SHMID, NULL, 0);
|
||||||
if (board == SHMERR) {
|
if (board == SHMERR) {
|
||||||
errExit("shmat", "shmServerAt");
|
errExit("shmat", "shmServerAt");
|
||||||
}
|
}
|
||||||
|
@ -44,7 +49,7 @@ tile_t * shmClientAt(int shmid) {
|
||||||
return board;
|
return board;
|
||||||
}
|
}
|
||||||
|
|
||||||
void shmDt(void *shm_ptr) {
|
void shmDt(void *shm_ptr) { //FIXME: sono tutte var globali
|
||||||
if (shmdt(shm_ptr) == -1) {
|
if (shmdt(shm_ptr) == -1) {
|
||||||
errExit("shmdt", "shmDt");
|
errExit("shmdt", "shmDt");
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,9 +17,11 @@
|
||||||
|
|
||||||
input_server_t _INPUT_S;
|
input_server_t _INPUT_S;
|
||||||
|
|
||||||
|
input_server_t checkServerInput(int argc, char *argv[]);
|
||||||
|
|
||||||
void setupServer(int argc, char *argv[]){
|
void setupServer(int argc, char *argv[]){
|
||||||
// CHECK_INPUT
|
// CHECK_INPUT
|
||||||
_INPUT_S = check_input(argc, argv);
|
_INPUT_S = checkServerInput(argc, argv);
|
||||||
|
|
||||||
_ROWS = _INPUT_S.rows;
|
_ROWS = _INPUT_S.rows;
|
||||||
_COLLUMS = _INPUT_S.collums;
|
_COLLUMS = _INPUT_S.collums;
|
||||||
|
@ -42,7 +44,7 @@ void setupServer(int argc, char *argv[]){
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
input_server_t check_input(int argc, char *argv[]){
|
input_server_t checkServerInput(int argc, char *argv[]){
|
||||||
if (argc < 5) {
|
if (argc < 5) {
|
||||||
printf("\033[92m<Help>\033[39m ./F4Server ROW COL PLAYER1 PLAYER2\n"
|
printf("\033[92m<Help>\033[39m ./F4Server ROW COL PLAYER1 PLAYER2\n"
|
||||||
"\t- ROW \t\tnumber of rows\n"
|
"\t- ROW \t\tnumber of rows\n"
|
||||||
|
|
Loading…
Reference in New Issue