diff --git a/include/msg.h b/include/msg.h index 2783393..e7f9a2f 100644 --- a/include/msg.h +++ b/include/msg.h @@ -4,6 +4,9 @@ #define VALUESIZE 100 #define KEYID 2817 +#define TYPE_SERVER 1 +#define TYPE_CLIENT 2 + #define TYPE_REQ_PUT 1 #define TYPE_REQ_GET 2 #define TYPE_REQ_REMOVE 3 @@ -14,6 +17,7 @@ typedef struct msgbuf { long mtype; + long type; unsigned int key; char value[VALUESIZE]; } msgbuf_t; diff --git a/src/client.c b/src/client.c index 727c359..a2f7445 100644 --- a/src/client.c +++ b/src/client.c @@ -94,24 +94,27 @@ void menu_remove() { void menu_quit() { printf("Bye\n"); - msg.mtype = TYPE_QUIT; + msg.mtype = TYPE_SERVER; + msg.type = TYPE_QUIT; snd_msg(); exit(0); } void req_put(unsigned int key, char *value) { - msg.mtype = TYPE_REQ_PUT; + msg.mtype = TYPE_SERVER; + msg.type = TYPE_REQ_PUT; msg.key = key; strcpy(msg.value, value); snd_msg(); } void req_get(unsigned int key) { - msg.mtype = TYPE_REQ_GET; + msg.mtype = TYPE_SERVER; + msg.type = TYPE_REQ_GET; msg.key = key; snd_msg(); - if (msgrcv(key_id, &msg, MSGSIZE, TYPE_RES_GET, 0) < 0) { + if (msgrcv(key_id, &msg, MSGSIZE, TYPE_CLIENT, 0) < 0) { perror("msgrcv error "); return; } @@ -124,7 +127,8 @@ void req_get(unsigned int key) { } void req_remove(unsigned int key) { - msg.mtype = TYPE_REQ_REMOVE; + msg.mtype = TYPE_SERVER; + msg.type = TYPE_REQ_REMOVE; msg.key = key; snd_msg(); } diff --git a/src/server.c b/src/server.c index 104d587..495b568 100644 --- a/src/server.c +++ b/src/server.c @@ -36,40 +36,12 @@ void server() { } while (1) { - msg.mtype = 0; - - if (msgrcv(key_id, &msg, MSGSIZE, TYPE_REQ_PUT, IPC_NOWAIT) < 0) { - if (errno != ENOMSG) { - perror("msgrcv error "); - return; - } - } - - if (msgrcv(key_id, &msg, MSGSIZE, TYPE_REQ_GET, IPC_NOWAIT) < 0) { - if (errno != ENOMSG) { - perror("msgrcv error "); - return; - } - } - - if (msgrcv(key_id, &msg, MSGSIZE, TYPE_REQ_REMOVE, IPC_NOWAIT) < 0) { - if (errno != ENOMSG) { - perror("msgrcv error "); - return; - } + if (msgrcv(key_id, &msg, MSGSIZE, TYPE_SERVER, 0) < 0) { + perror("msgrcv error "); + return; } - if (msgrcv(key_id, &msg, MSGSIZE, TYPE_QUIT, IPC_NOWAIT) < 0) { - if (errno != ENOMSG) { - perror("msgrcv error "); - return; - } - } - - switch (msg.mtype) { - case 0: - break; - + switch (msg.type) { case TYPE_REQ_PUT: rcv_put(msg.key, msg.value); break; @@ -106,7 +78,8 @@ void rcv_get(unsigned int key) { strcpy(msg.value, node->value); } - msg.mtype = TYPE_RES_GET; + msg.mtype = TYPE_CLIENT; + msg.type = TYPE_RES_GET; if (msgsnd(key_id, &msg, MSGSIZE, 0) < 0) { perror("msgsnd error ");