Skip to content

Commit

Permalink
Implement put get remove quit
Browse files Browse the repository at this point in the history
  • Loading branch information
uoo723 committed Jun 22, 2017
1 parent 242bcc6 commit 34cf150
Show file tree
Hide file tree
Showing 7 changed files with 226 additions and 46 deletions.
4 changes: 4 additions & 0 deletions include/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@

void client();

void req_put(unsigned int, char *);
void req_get(unsigned int);
void req_remove(unsigned int);

#endif
8 changes: 8 additions & 0 deletions include/msg.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@
#define VALUESIZE 100
#define KEYID 2817

#define TYPE_REQ_PUT 1
#define TYPE_REQ_GET 2
#define TYPE_REQ_REMOVE 3
#define TYPE_RES_GET 4
#define TYPE_QUIT 5

#define NODATA -1

typedef struct msgbuf {
long mtype;
unsigned int key;
Expand Down
4 changes: 4 additions & 0 deletions include/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@

void server();

void rcv_put(unsigned int, char *);
void rcv_get(unsigned int);
void rcv_remove(unsigned int);

#endif
130 changes: 112 additions & 18 deletions src/client.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>

Expand All @@ -9,35 +8,130 @@
#include <sys/stat.h>

#include "msg.h"
#include "client.h"

void client() {
key_t key_id;
msgbuf_t msg;
char *text;
void menu();
void menu_get();
void menu_put();
void menu_remove();
void menu_quit();

void snd_msg();

key_id = msgget(KEYID, IPC_CREAT|0666);
key_t key_id;
msgbuf_t msg;

if (key_id < 0) {
void client() {
if ((key_id = msgget(KEYID, IPC_CREAT|0666)) < 0) {
perror("msgget error ");
exit(0);
}

printf("Start client\n");
printf("Welcome to Key Value Store\n");

while (1) {
printf("input > ");
scanf("%s", text);
msg.mtype = 1;
menu();
}
}

void menu() {
printf("1. put, 2. get, 3. delete, 4. quit: ");
int num;
scanf("%d", &num);

switch (num) {
case 1:
menu_put();
break;

case 2:
menu_get();
break;

case 3:
menu_remove();
break;

case 4:
menu_quit();
break;

default:
printf("invalid num\n");

}
}

void menu_put() {
unsigned int key;
char value[VALUESIZE];

printf("key > ");
scanf("%d", &key);
printf("value > ");
scanf("%s", value);

req_put(key, value);
}

// strcpy(msg.mtext, text);
void menu_get() {
unsigned int key;

if (msgsnd(key_id, &msg, MSGSIZE, 0) < 0) {
perror("msgsnd error ");
exit(0);
}
printf("key > ");
scanf("%d", &key);

// printf("client: send %s\n", msg.key);
req_get(key);
}

void menu_remove() {
unsigned int key;

printf("key > ");
scanf("%d", &key);

req_remove(key);
}

void menu_quit() {
printf("Bye\n");
msg.mtype = TYPE_QUIT;
snd_msg();
exit(0);
}

void req_put(unsigned int key, char *value) {
msg.mtype = TYPE_REQ_PUT;
msg.key = key;
strcpy(msg.value, value);
snd_msg();
}

void req_get(unsigned int key) {
msg.mtype = TYPE_REQ_GET;
msg.key = key;
snd_msg();

if (msgrcv(key_id, &msg, MSGSIZE, TYPE_RES_GET, 0) < 0) {
perror("msgrcv error ");
return;
}

if (msg.key == NODATA) {
printf("no data\n");
} else {
printf("value: %s\n", msg.value);
}
}

void req_remove(unsigned int key) {
msg.mtype = TYPE_REQ_REMOVE;
msg.key = key;
snd_msg();
}

sleep(1);
void snd_msg() {
if (msgsnd(key_id, &msg, MSGSIZE, 0) < 0) {
perror("msgsnd error ");
return;
}
}
3 changes: 0 additions & 3 deletions src/hashtable.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,6 @@ node_t *ht_get(hashtable_t *hashtable, unsigned int key) {
return node;
}

#include <stdio.h>

int ht_remove(hashtable_t *hashtable, unsigned int key) {
int index;
node_t *head;
Expand All @@ -92,7 +90,6 @@ int ht_remove(hashtable_t *hashtable, unsigned int key) {
}

if (ll_remove(head, key) < 0) {
printf("key: %d", key);
return -1;
}

Expand Down
16 changes: 0 additions & 16 deletions src/main.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <signal.h>

#include <sys/types.h>
#include <sys/ipc.h>
Expand All @@ -12,22 +11,7 @@

#include "msg.h"

void int_handler(int signo) {
key_t key_id;

key_id = msgget(KEYID, IPC_CREAT|0666);

if (key_id < 0) {
perror("msgget error ");
exit(0);
}

msgctl(key_id, IPC_RMID, 0);
}

int main() {
signal(SIGINT, int_handler);

if (fork() == 0) {
client();
} else {
Expand Down
107 changes: 98 additions & 9 deletions src/server.c
Original file line number Diff line number Diff line change
@@ -1,32 +1,121 @@
#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/stat.h>
#include <errno.h>

#include "msg.h"
#include "server.h"
#include "hashtable.h"
#include "node.h"

void server() {
key_t key_id;
msgbuf_t msg;
key_t key_id;
msgbuf_t msg;
hashtable_t *hashtable;

void int_handler(int signo) {
msgctl(key_id, IPC_RMID, 0);
}

key_id = msgget(KEYID, IPC_CREAT|0666);
void server() {
signal(SIGINT, int_handler);

if (key_id < 0) {
if ((key_id = msgget(KEYID, IPC_CREAT|0666)) < 0) {
perror("msgget error ");
exit(0);
}

printf("Start server\n");
if ((hashtable = ht_create(1000000)) == NULL) {
perror("hashtable is null ");
exit(0);
}

while (1) {
if (msgrcv(key_id, &msg, MSGSIZE, 0, 0) < 0) {
perror("msgrcv error ");
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_QUIT, IPC_NOWAIT) < 0) {
if (errno != ENOMSG) {
perror("msgrcv error ");
return;
}
}

switch (msg.mtype) {
case 0:
break;

case TYPE_REQ_PUT:
rcv_put(msg.key, msg.value);
break;

case TYPE_REQ_GET:
rcv_get(msg.key);
break;

case TYPE_REQ_REMOVE:
rcv_remove(msg.key);
break;

case TYPE_QUIT:
int_handler(2);
exit(0);
break;

default:
printf("type is invalid\n");
}
}
}

void rcv_put(unsigned int key, char *value) {
ht_set(hashtable, key, value);
}

// printf("server: recevied %s\n", msg.mtext);
void rcv_get(unsigned int key) {
node_t *node = ht_get(hashtable, key);

if (node == NULL) {
msg.key = NODATA;
} else {
strcpy(msg.value, node->value);
}

msg.mtype = TYPE_RES_GET;

if (msgsnd(key_id, &msg, MSGSIZE, 0) < 0) {
perror("msgsnd error ");
return;
}

msg.mtype = 0;
}

void rcv_remove(unsigned int key) {
ht_remove(hashtable, key);
}

0 comments on commit 34cf150

Please sign in to comment.