From eb4c32c110a6879f832fd0358c013e86e336add9 Mon Sep 17 00:00:00 2001 From: Lorenzo Bianchi Date: Mon, 29 May 2023 13:06:22 +0200 Subject: [PATCH] cliet_setup --- TODO.txt | 4 +++- inc/client.h | 3 +++ inc/custom_shm.h | 2 +- inc/custom_sig.h | 2 +- src/F4Client.c | 25 +++---------------------- src/client.c | 6 ++++-- src/custom_msgq.c | 22 +++++++++++++++------- src/custom_shm.c | 9 +++++---- src/custom_sig.c | 2 +- 9 files changed, 36 insertions(+), 39 deletions(-) diff --git a/TODO.txt b/TODO.txt index 1e5070b..3d777e3 100644 --- a/TODO.txt +++ b/TODO.txt @@ -11,4 +11,6 @@ - abbandono player -- pulire f4client mettendo var con nomi normali \ No newline at end of file +- pulire f4client mettendo var con nomi normali + +- ho scoperto solo adesso che c'é set_rows ecc, forse non lho sempre usato facendo invece _ROWS = ... \ No newline at end of file diff --git a/inc/client.h b/inc/client.h index b717afb..35aaf7e 100644 --- a/inc/client.h +++ b/inc/client.h @@ -1,6 +1,9 @@ #ifndef CLIENT_H #define CLIENT_H + +msg_t msg; + void setupClient(int argc, char *argv[]); #endif \ No newline at end of file diff --git a/inc/custom_shm.h b/inc/custom_shm.h index 5b808cc..33a2b05 100644 --- a/inc/custom_shm.h +++ b/inc/custom_shm.h @@ -9,7 +9,7 @@ void setupServerShm(); int getShmid(); tile_t * shmServerAt(); -tile_t * shmClientAt(int shmid); +tile_t * shmClientAt(); void shmDt(void *shm_ptr); void shmServerRm(int shmid); diff --git a/inc/custom_sig.h b/inc/custom_sig.h index 2a4349d..138c0ce 100644 --- a/inc/custom_sig.h +++ b/inc/custom_sig.h @@ -8,6 +8,6 @@ extern pid_t _PIDS[2]; void sigHandlerServer(int sig); void setupServerSignalHandler(); -void setupServerSignalHandler(); +void setupClientSignalHandler(); #endif \ No newline at end of file diff --git a/src/F4Client.c b/src/F4Client.c index 1ef0c59..2ca1ea4 100644 --- a/src/F4Client.c +++ b/src/F4Client.c @@ -25,29 +25,14 @@ int main(int argc, char *argv[]){ printf("Il mio pid per killarmi: %d\n", getpid()); //TMP - setupClient(argc, argv); - - // - - // MSGQ - - - // msg - input_server_t input; msg_t msg; - - // msgrcv - printf(PCLIENT "Waiting for message...\n"); - rcvMsg(&msg); - - input = msg.server_in; - // + setupClient(argc, argv); //FIXME: msg é tmp // SHM printf(PCLIENT "Setting up shm\n"); - int shmBoardid = getShmid(); - _BOARD = shmClientAt(shmBoardid); + int _SHMID = getShmid(); + _BOARD = shmClientAt(); // // SEM @@ -59,7 +44,6 @@ int main(int argc, char *argv[]){ } // - // aspetto che il server mi dia il permesso di collegarmi e segnalo collegamento al server semOp(semid, msg.player_id, -1); @@ -82,9 +66,6 @@ int main(int argc, char *argv[]){ // PARTITA move_t move = {.mtype = 2}; - setRows(input.rows); //FIXME: da mettere in un setup - setCollums(input.collums); - setTokens(msg.server_in.player1Token, msg.server_in.player2Token); while (1) { // aspetto il mio turno diff --git a/src/client.c b/src/client.c index a443a87..139a9eb 100644 --- a/src/client.c +++ b/src/client.c @@ -22,10 +22,12 @@ void setupClient(int argc, char *argv[]) { // CHECK INPUT checkClientinput(argc, argv); + // SIGNAL + setupClientSignalHandler(); + + // MSGQ setupClientMsgq(); - - } void checkClientinput(int argc, char *argv[]){ diff --git a/src/custom_msgq.c b/src/custom_msgq.c index 92f125c..135f935 100644 --- a/src/custom_msgq.c +++ b/src/custom_msgq.c @@ -6,6 +6,8 @@ #include #include #include //FIXME: da togliere poi +#include +#include int _MSGQID; @@ -13,6 +15,7 @@ void msgRcv(void *msgp, size_t size, long mtype); void msgSnd(void *msgp, size_t size); void openMsgq(); void sndId(); +void rcvId(); void setupServerMsgq() { openMsgq(); @@ -20,19 +23,15 @@ void setupServerMsgq() { } void setupClientMsgq() { - printf(PCLIENT "Setting up msgq\n"); - key_t msgKey = ftok(KEYFILE, 'M'); - _MSGQID = msgget(msgKey, IPC_CREAT | S_IRUSR | S_IWUSR); - if (_MSGQID == -1) { - errExit("msgget", "F4Client"); - } + openMsgq(); + rcvId(); } void openMsgq() { key_t msgKey = ftok(KEYFILE, 'M'); _MSGQID = msgget(msgKey, IPC_CREAT | S_IRUSR | S_IWUSR); if (_MSGQID == -1){ - errExit("msgget", "F4Server"); + errExitMsg("msgget"); } } @@ -48,6 +47,15 @@ void sndId() { sndMsg(&msg); } +void rcvId() { + printf(PCLIENT "Waiting for message...\n"); + rcvMsg(&msg); + + setRows(msg.server_in.rows); + setCollums(msg.server_in.collums); + setTokens(msg.server_in.player1Token, msg.server_in.player2Token); +} + void msgRcv(void *msgp, size_t size, long mtype) { int res; diff --git a/src/custom_shm.c b/src/custom_shm.c index bfd807f..ce62eae 100644 --- a/src/custom_shm.c +++ b/src/custom_shm.c @@ -26,11 +26,10 @@ int getShmid() { errExit("shmget", "getShmid"); } - return shmid; } -tile_t *shmServerAt() { +tile_t *shmServerAt() { //FIXME: si possono fare void tile_t *board = (tile_t *)shmat(_SHMID, NULL, 0); if (board == SHMERR) { errExit("shmat", "shmServerAt"); @@ -39,8 +38,10 @@ tile_t *shmServerAt() { return board; } -tile_t * shmClientAt(int shmid) { - tile_t *board = (tile_t *)shmat(shmid, NULL, SHM_RDONLY); +tile_t * shmClientAt() { + tile_t *board = (tile_t *)shmat(_SHMID, NULL, SHM_RDONLY); + perror("perche"); + if (board == SHMERR) { errExit("shmat", "shmClientAt"); } diff --git a/src/custom_sig.c b/src/custom_sig.c index 8477f5b..4896b93 100644 --- a/src/custom_sig.c +++ b/src/custom_sig.c @@ -25,7 +25,7 @@ void sigIntHandler2(int sig) { void sigHandlerServer(int sig) { if (sig == SIGINT) { - printf("\n" PSERVER "Press again Ctrl^C to exit (whitin %d sec)\n", TIME_TO_RESET); + printf("\n" PSERVER "Press again Ctrl^C to exit (within %d sec)\n", TIME_TO_RESET); signal(SIGINT, sigIntHandler2); alarm(TIME_TO_RESET); }