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:
|
||||
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:
|
||||
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();
|
||||
|
||||
int getShmid(int row, int col);
|
||||
tile_t * shmServerAt(int shmid);
|
||||
int getShmid();
|
||||
tile_t * shmServerAt();
|
||||
tile_t * shmClientAt(int shmid);
|
||||
void shmDt(void *shm_ptr);
|
||||
void shmServerRm(int shmid);
|
||||
|
|
|
@ -19,6 +19,4 @@ typedef struct {
|
|||
|
||||
void setupServer(int argc, char *argv[]);
|
||||
|
||||
input_server_t check_input(int argc, char *argv[]);
|
||||
|
||||
#endif
|
|
@ -16,8 +16,9 @@
|
|||
#include <errExit.h>
|
||||
#include <custom_msgq.h>
|
||||
#include <forza4.h>
|
||||
#include <client.h>
|
||||
|
||||
int game_state;
|
||||
int game_state; //FIXME: non ancora utilizzato
|
||||
|
||||
tile_t *boardClient;
|
||||
|
||||
|
@ -59,7 +60,7 @@ int main(int argc, char *argv[]){
|
|||
|
||||
printf("Il mio pid per killarmi: %d\n", getpid()); //TMP
|
||||
|
||||
checkClientinput(argc, argv);
|
||||
setupClient(argc, argv);
|
||||
|
||||
// SIGNAL
|
||||
printf(PCLIENT "Setting up signal\n");
|
||||
|
@ -107,7 +108,7 @@ int main(int argc, char *argv[]){
|
|||
|
||||
// SHM
|
||||
printf(PCLIENT "Setting up shm\n");
|
||||
int shmBoardid = getShmid(input.rows, input.collums);
|
||||
int shmBoardid = getShmid();
|
||||
boardClient = shmClientAt(shmBoardid);
|
||||
//
|
||||
|
||||
|
@ -172,19 +173,3 @@ int main(int argc, char *argv[]){
|
|||
|
||||
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);
|
||||
|
||||
|
||||
// aperta semaforo per connessione dei due client
|
||||
printf(PSERVER "Waiting for players...\n");
|
||||
semOp(_SEMID, 0, 1);
|
||||
semOp(_SEMID, 1, 1);
|
||||
semOp(_SEMID, CLIENT0, 1);
|
||||
semOp(_SEMID, CLIENT1, 1);
|
||||
|
||||
|
||||
// aspetto che si connettano i due client
|
||||
semOp(_SEMID, 2, -1);
|
||||
|
||||
player_ds player;
|
||||
char name[2][MAX_NAME];
|
||||
|
||||
// ricevuta nome primo client
|
||||
// aspetto PRIMO client
|
||||
semOp(_SEMID, SERVER, -1);
|
||||
// ricevo nome primo client
|
||||
rcvPlayer(&player);
|
||||
|
||||
strcpy(name[player.id], player.name_player);
|
||||
_PIDS[player.id] = player.pid;
|
||||
|
||||
printf(PSERVER "Player %s connected\n", name[player.id]);
|
||||
|
||||
// aspetto secondo client
|
||||
semOp(_SEMID, 2, -1);
|
||||
// aspetto SECONDO client
|
||||
semOp(_SEMID, SERVER, -1);
|
||||
// ricevo nome secondo client
|
||||
rcvPlayer(&player);
|
||||
|
||||
strcpy(name[player.id], player.name_player);
|
||||
_PIDS[player.id] = player.pid;
|
||||
|
||||
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
|
||||
printf(PSERVER "Starting game\n");
|
||||
|
||||
|
||||
int turns_left = _INPUT_S.rows * _INPUT_S.collums;
|
||||
int turn = 0;
|
||||
tile_t result;
|
||||
move_t move;
|
||||
|
||||
do {
|
||||
// apro semaforo al player
|
||||
// apro semaforo al player di turno
|
||||
semOp(_SEMID, turn, 1);
|
||||
|
||||
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
|
||||
semOp(_SEMID, turn, 1);
|
||||
|
||||
// next turn
|
||||
turn ^= 1;
|
||||
|
||||
} while (--turns_left && !(result = checkWin(_BOARD, move.move)));
|
||||
|
||||
|
||||
if (!result) { //FIXME: change printf
|
||||
if (!result) {
|
||||
printf(PSERVER "Game ended in a draw\n");
|
||||
|
||||
} 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 getShmid();
|
||||
tile_t *shmServerAt();
|
||||
|
||||
void setupServerShm() {
|
||||
_SHMID = getShmid(_ROWS, _COLLUMS);
|
||||
_BOARD = shmServerAt(_SHMID);
|
||||
_SHMID = getShmid();
|
||||
_BOARD = shmServerAt();
|
||||
|
||||
for (int i=0; i < _ROWS * _COLLUMS; i++) {
|
||||
_BOARD[i] = EMPTY;
|
||||
}
|
||||
}
|
||||
|
||||
int getShmid(int row, int col) {
|
||||
int shmid = shmget(ftok(KEYFILE, 'z'), row * col * sizeof(tile_t), IPC_CREAT | S_IRUSR | S_IWUSR);
|
||||
int getShmid() {
|
||||
int shmid = shmget(ftok(KEYFILE, 'z'), _ROWS * _COLLUMS * sizeof(tile_t), IPC_CREAT | S_IRUSR | S_IWUSR);
|
||||
printf("%d\n", shmid);
|
||||
if (shmid == -1) {
|
||||
errExit("shmget", "getShmid");
|
||||
}
|
||||
|
||||
|
||||
return shmid;
|
||||
}
|
||||
|
||||
tile_t * shmServerAt(int shmid) {
|
||||
tile_t *board = (tile_t *)shmat(shmid, NULL, 0);
|
||||
tile_t *shmServerAt() {
|
||||
tile_t *board = (tile_t *)shmat(_SHMID, NULL, 0);
|
||||
if (board == SHMERR) {
|
||||
errExit("shmat", "shmServerAt");
|
||||
}
|
||||
|
@ -44,7 +49,7 @@ tile_t * shmClientAt(int shmid) {
|
|||
return board;
|
||||
}
|
||||
|
||||
void shmDt(void *shm_ptr) {
|
||||
void shmDt(void *shm_ptr) { //FIXME: sono tutte var globali
|
||||
if (shmdt(shm_ptr) == -1) {
|
||||
errExit("shmdt", "shmDt");
|
||||
}
|
||||
|
|
|
@ -17,9 +17,11 @@
|
|||
|
||||
input_server_t _INPUT_S;
|
||||
|
||||
input_server_t checkServerInput(int argc, char *argv[]);
|
||||
|
||||
void setupServer(int argc, char *argv[]){
|
||||
// CHECK_INPUT
|
||||
_INPUT_S = check_input(argc, argv);
|
||||
_INPUT_S = checkServerInput(argc, argv);
|
||||
|
||||
_ROWS = _INPUT_S.rows;
|
||||
_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) {
|
||||
printf("\033[92m<Help>\033[39m ./F4Server ROW COL PLAYER1 PLAYER2\n"
|
||||
"\t- ROW \t\tnumber of rows\n"
|
||||
|
|
Loading…
Reference in New Issue