Skip to content

Commit

Permalink
Separate client view from client api
Browse files Browse the repository at this point in the history
  • Loading branch information
uoo723 committed Jun 22, 2017
1 parent ec3562f commit 5c7e412
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 89 deletions.
1 change: 1 addition & 0 deletions include/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ void client();
void req_put(unsigned int, char *);
void req_get(unsigned int);
void req_remove(unsigned int);
void req_quit();

#endif
7 changes: 7 additions & 0 deletions include/client_view.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#ifndef CLIENT_VIEW_H
#define CLIENT_VIEW_H

void client_view();
void show_get_result(unsigned int, char *);

#endif
2 changes: 1 addition & 1 deletion include/msg.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#define TYPE_RES_GET 40
#define TYPE_QUIT 50

#define NODATA -1
#define NODATA ""

typedef struct msgbuf {
/* message queue type */
Expand Down
102 changes: 19 additions & 83 deletions src/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,97 +9,30 @@

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

void menu();
void menu_get();
void menu_put();
void menu_remove();
void menu_quit();
static void menu();
static void menu_get();
static void menu_put();
static void menu_remove();
static void menu_quit();

void snd_msg();
static void snd_msg();

key_t key_id;
msgbuf_t msg;
static key_t key_id;
static msgbuf_t msg;

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

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

while (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");

client_view();
}
}

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

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

req_put(key, value);
}

void menu_get() {
unsigned int key;

printf("key > ");
scanf("%d", &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_SERVER;
msg.type = TYPE_QUIT;
snd_msg();
exit(0);
}

void req_put(unsigned int key, char *value) {
msg.mtype = TYPE_SERVER;
msg.type = TYPE_REQ_PUT;
Expand All @@ -119,11 +52,7 @@ void req_get(unsigned int key) {
return;
}

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

void req_remove(unsigned int key) {
Expand All @@ -133,7 +62,14 @@ void req_remove(unsigned int key) {
snd_msg();
}

void snd_msg() {
void req_quit() {
msg.mtype = TYPE_SERVER;
msg.type = TYPE_QUIT;
snd_msg();
exit(0);
}

static void snd_msg() {
if (msgsnd(key_id, &msg, MSGSIZE, 0) < 0) {
perror("msgsnd error ");
return;
Expand Down
96 changes: 96 additions & 0 deletions src/client_view.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#include <stdio.h>
#include <stdbool.h>
#include <string.h>

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

static void menu();
static void menu_get();
static void menu_put();
static void menu_remove();
static void menu_quit();

static bool first_loop = true;

void client_view() {
if (first_loop) {
printf("Welcome to Key Value Store\n");
first_loop = false;
}

menu();
}

void show_get_result(unsigned int key, char *value) {
printf("%u\n", key);
if (strcmp(value, NODATA) == 0) {
printf("no data\n");
} else {
printf("value: %s\n", value);
}
}

static 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");

}
}

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

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

req_put(key, value);
}

static void menu_get() {
unsigned int key;

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

req_get(key);
}

static void menu_remove() {
unsigned int key;

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

req_remove(key);
}

static void menu_quit() {
printf("Bye\n");
req_quit();
}
10 changes: 5 additions & 5 deletions src/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
#include "hashtable.h"
#include "node.h"

key_t key_id;
msgbuf_t msg;
hashtable_t *hashtable;
static key_t key_id;
static msgbuf_t msg;
static hashtable_t *hashtable;

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

Expand Down Expand Up @@ -73,7 +73,7 @@ void rcv_get(unsigned int key) {
node_t *node = ht_get(hashtable, key);

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

0 comments on commit 5c7e412

Please sign in to comment.