custom_msg rcv and snd

This commit is contained in:
Lorenzo Bianchi 2023-05-19 15:21:15 +02:00
parent 1de8a88b01
commit 26ae8d5cc7
7 changed files with 32 additions and 42 deletions

View File

@ -78,6 +78,6 @@ test: comp
comp: server_c client_c
server_c:
gcc src/F4Server.c src/custom_sem.c src/custom_shm.c src/custom_signal.c src/errExit.c src/forza4.c src/server.c src/nonsodovemetterle.c -o bin/F4Server -I inc/
gcc src/F4Server.c src/custom_sem.c src/custom_shm.c src/custom_signal.c src/custom_msgq.c src/errExit.c src/forza4.c src/server.c src/nonsodovemetterle.c -o bin/F4Server -I inc/
client_c:
gcc src/F4Client.c src/custom_sem.c src/custom_shm.c src/custom_signal.c src/errExit.c src/forza4.c src/nonsodovemetterle.c -o bin/F4Client -I inc/
gcc src/F4Client.c src/custom_sem.c src/custom_shm.c src/custom_signal.c src/custom_msgq.c src/errExit.c src/forza4.c src/nonsodovemetterle.c -o bin/F4Client -I inc/

View File

@ -7,3 +7,5 @@
- invece di fare tutti if == -1 si potrebbe fare come semOp e creare una funzione identica
che fa gia il controllo (ha come svantaggio che non puoi specifare dove va in errore con l'errexit)
(anche se forse basta passare la/le stringe ma vedi riga 3) EDI APPROVA
- testare chiusura figli perche i miei terminali si chiudono da soli

View File

@ -5,7 +5,7 @@
#define MAX_NAME 16
static int _MSGQID;
int _MSGQID; //FIXME: static?
#define MSG 1
#define MOVE 2

View File

@ -23,9 +23,9 @@ int getIndex(int i, int j);
int checkWin(tile_t *board, int pos);
int checkWinAll(tile_t *board);
int checkMove(tile_t *board, int collums);
void printBoard(tile_t *board);
void printTile(tile_t t);
int getHeight(tile_t *board, int col);
#endif

View File

@ -15,7 +15,8 @@
#include <custom_shm.h>
#include <errExit.h>
#include <nonsodovemetterle.h>
#include <custom_msg.h>
#include <custom_msgq.h>
#include <forza4.h>
int game_state;
@ -50,9 +51,7 @@ void sigHandlerClient(int sig) {
// Fine partita
if (sig == SIGUSR1) {
game_end_t winner;
if (msgrcv(_MSGQID, &winner, sizeof(game_end_t) - sizeof(long), 4, 0) == -1) {
errExit("msgrcv", "fine partita");
}
rcvGame_end(&winner);
game_state = winner.winner;
}
}
@ -101,9 +100,7 @@ int main(int argc, char *argv[]){
// msgrcv
printfClient("Waiting for message...\n");
if (msgrcv(_MSGQID, &msg, sizeof(msg_t) - sizeof(long), 1, 0) == -1) {
errExit("msgrcv", "F4Client");
}
rcvMsg(&msg);
input = msg.server_in;
//
@ -135,9 +132,7 @@ int main(int argc, char *argv[]){
// mando i miei dati al server
player_ds player = {.mtype = 3, .id = msg.player_id, .pid = getpid()};
strcpy(player.name_player, argv[1]);
if (msgsnd(_MSGQID, &player, sizeof(player_ds) - sizeof(long), 0) == -1) {
errExit("msgsnd", "mandare nome");
}
sndPlayer(&player);
printfClient("Searching for oponent...\n");
@ -160,14 +155,12 @@ int main(int argc, char *argv[]){
do {
scanf("%d", &move.move);
move.move--;
current_move = checkMove(move.move);
//FIXME: current_move = checkMove(move.move);
} while(current_move < 0);
// mando mossa al server
move.move = current_move;
if (msgsnd(_MSGQID, &move, sizeof(move_t) - sizeof(long), 0) == -1) {
errExit("msgsnd", "partita_client");
}
sndMove(&move);
}
return 0;

View File

@ -16,7 +16,7 @@
#include <errExit.h>
#include <forza4.h>
#include <nonsodovemetterle.h>
#include <custom_msg.h>
#include <custom_msgq.h>
int semid = 0;
int _MSGQID = 0;
@ -36,12 +36,14 @@ void sigHandlerServer(int sig) {
}
if (sig == SIGTERM || sig == SIGHUP) {
printf("\n");
printfServer("Terminating players");
if (pids[0] > 0) {
printf("\n");
printfServer("Terminating player one");
kill(pids[0], SIGTERM);
}
if (pids[1] > 0){
printf("\n");
printfServer("Terminating player two");
kill(pids[1], SIGTERM);
}
@ -49,7 +51,7 @@ void sigHandlerServer(int sig) {
if (_MSGQID) {
printf("\n");
printfServer("Deleting msg");
if (msgctl(_MSGQID, IPC_RMID, NULL) == -1) {
if (msgctl(_MSGQID, IPC_RMID, NULL) == -1) { //TODO: funzione
errExit("msgctl", "sigHandlerServer");
}
}
@ -126,15 +128,9 @@ int main(int argc, char *argv[]){
};
// msgsnd
if (msgsnd(_MSGQID, &msg, sizeof(msg_t) - sizeof(long), 0) == -1) {
errExit("msgsnd1", "F4Server");
}
sndMsg(&msg);
msg.player_id = 1;
if (msgsnd(_MSGQID, &msg, sizeof(msg_t) - sizeof(long), 0) == -1) {
errExit("msgsnd2", "F4Server");
}
sndMsg(&msg);
//
// SHM
@ -170,9 +166,7 @@ int main(int argc, char *argv[]){
pids[2];
// ricevuta nome primo client
if (msgrcv(_MSGQID, &player, sizeof(player_ds) - sizeof(long), 3, 0) == -1) {
errExit("msgrcv", "ricevuta primo nome");
}
rcvPlayer(&player);
strcpy(name[player.id], player.name_player);
pids[player.id] = player.pid;
@ -183,9 +177,8 @@ int main(int argc, char *argv[]){
// aspetto secondo client
semOp(semid, 2, -1);
// ricevuta nome secondo client
if (msgrcv(_MSGQID, &player, sizeof(player_ds) - sizeof(long), 3, 0) == -1) {
errExit("msgrcv", "ricevuta secondo nome");
}
rcvPlayer(&player);
strcpy(name[player.id], player.name_player);
pids[player.id] = player.pid;
@ -213,9 +206,7 @@ int main(int argc, char *argv[]){
printfServer("");
printf("%s's turn\n", name[turn]);
if (msgrcv(_MSGQID, &move, sizeof(move_t) - sizeof(long), 2, 0) == -1) {
errExit("msgrcv", "F4Client");
}
rcvMove(&move);
//TMP
printf("move: %d\n", move.move);
@ -231,8 +222,10 @@ int main(int argc, char *argv[]){
} else {
printfServer("Game ended\n");
}
game_end_t status = {.mtype = 4, .winner = result};
snd
game_end_t game_end = {.mtype = 4, .winner = result};
sndGame_end(&game_end);
sndGame_end(&game_end);
kill(pids[0], SIGUSR1);
kill(pids[1], SIGUSR1);

View File

@ -1,6 +1,8 @@
#include <custom_msgq.h>
#include <sys/msg.h>
#include <custom_msgq.h>
#include <errExit.h>
void msgRcv(void *msgp, size_t size, long mtype) {
if (msgrcv(_MSGQID, msgp, size, mtype, 0) == -1) {
errExitMsg("msgrcv");