Skip to content

Commit

Permalink
Merge pull request gwAdvNet2015#70 from nks5295/server-verbose
Browse files Browse the repository at this point in the history
Added verbose kvprintf functions. Still working on server/client runtime functionality.
  • Loading branch information
amygara committed Apr 29, 2015
2 parents 59263e3 + 0447b21 commit 52b7b34
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 2 deletions.
16 changes: 16 additions & 0 deletions common.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef __COMMON_H__
#define __COMMON_H__

extern volatile int verbose_print;

#define eprintf(format, ...) do { \
if (verbose_print) \
fprintf(stderr, format, ##__VA_ARGS__); \
} while(0)

#define kvprintf(format, ...) do { \
if (verbose_print) \
printf(format, ##__VA_ARGS__); \
} while(0)

#endif /* __COMMON_H__ */
3 changes: 3 additions & 0 deletions lib/marshal/marshal.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
#include <stdlib.h>
#include <assert.h>
#include "marshal.h"
#include "../../common.h"

volatile int verbose_print = 0;

int
gwkv_marshal_server(struct operation* data, int status, char** ascii)
Expand Down
21 changes: 20 additions & 1 deletion server/handle_operation.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* GW KV
* https://github.com/gwAdvNet2015/gw-kv-store
*
* Copyright 2015 Phil Lopreiato
* Copyright 2015 Phil Lopreiato Neel Shah
*
* This program is licensed under the MIT license.
*
Expand All @@ -12,6 +12,7 @@

#include "../lib/marshal/marshal.h"
#include "handle_operation.h"
#include "../common.h"

char*
gwkv_handle_operation(struct gwkv_server *ht, char *cmd)
Expand All @@ -28,18 +29,22 @@ gwkv_handle_operation(struct gwkv_server *ht, char *cmd)

switch(op->method_type) {
case GET:
kvprintf("[!] inside gwkv_handle_operation | entered switch | inside case GET\n");
ht_get = gwkv_handle_get(ht, op);
if (ht_get == NULL) {
perror("Something failed in gwkv_handle_get");
exit(-1);
}
kvprintf("[!] inside gwkv_handle_operation | entered switch | case GET done -- here's the message %s\n", ht_get);
return ht_get;
case SET:
kvprintf("[!] inside gwkv_handle_operation | entered switch | inside case SET\n");
ht_set = gwkv_handle_set(ht, op);
if (ht_set == NULL) {
perror("Something failed in gwkv_handle_set");
exit(-1);
}
kvprintf("[!] inside gwkv_handle_operation | entered switch | case SET done | here's the message -- %s\n", ht_set);
return ht_set;
default:
perror("Wrong command, switch dying");
Expand All @@ -57,12 +62,16 @@ gwkv_handle_get(struct gwkv_server *ht, struct operation *op)

ht_get = gwkv_server_get(ht, op->key, op->key_length, op->value_length);
if (ht_get == NULL) {
kvprintf("[!] inside gwkv_handle_get | ht_get == NULL | crafting message with NOT_FOUND\n");
gwkv_craft_message(op, NOT_FOUND, &msg);
} else {
kvprintf("[!] inside gwkv_handle_get | ht_get != NULL | crafting message with EXISTS\n");
op->value = ht_get;
gwkv_craft_message(op, EXISTS, &msg);
}

kvprintf("[!] inside gwkv_handle_get | here is the crafted GET message -- %s\n", msg);

return msg;
}

Expand All @@ -74,18 +83,24 @@ gwkv_handle_set(struct gwkv_server *ht, struct operation *op)

ht_set = gwkv_server_set(ht, op->key, op->key_length, op->value, op->value_length);

kvprintf("[!] inside gwkv_handle_set | result of gwkv_server_set = %d\n", ht_set);

switch(ht_set) {
case STORED:
kvprintf("[!] inside gwkv_handle_set | entered switch | case STORED | crafting message now\n");
gwkv_craft_message(op, STORED, &msg);
break;
case NOT_STORED:
kvprintf("[!] inside gwkv_handle_set | entered switch | case NOT_STORED | crafting message now\n");
gwkv_craft_message(op, NOT_STORED, &msg);
break;
default:
perror("gwkv_server_set failed, dying");
exit(-1);
}

kvprintf("[!] inside gwkv_handle_set | here\'s the crafted message for set command -- %s\n", msg);

return msg;
}

Expand All @@ -94,12 +109,16 @@ gwkv_craft_message(struct operation *op, int status, char **msg)
{
int res;

kvprintf("[!] inside gwkv_craft_message | about to call gwkv_marshal_server to craft message\n");

// Set lengths, because counting
op->key_length = strlen(op->key);
op->value_length = strlen(op->value);

res = gwkv_marshal_server(op, status, msg);

kvprintf("[!] inside gwkv_craft_message | here\'s the crafted message -- %s\n", res);

if (res != 0) {
return -1;
} else {
Expand Down
9 changes: 8 additions & 1 deletion server/server-kv-run.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
#include <string.h>
#include <arpa/inet.h>
#include <pthread.h>

#include "../common.h"
#include "../lib/socket_helper.h" /* Provides sh_* funcs */
#include "server-kv.h"
#include "gwkv_ht_wrapper.h"
Expand All @@ -42,15 +44,20 @@ main(int argc, char ** argv)
/* Command line args:
-p port
-n thread number
-d verbose output
*/
while ((o = getopt (argc, argv, "p:n:")) != -1) {
while ((o = getopt (argc, argv, "p:n:d")) != -1) {
switch(o){
case 'p':
server_port = optarg;
break;
case 'n':
thread_number = optarg;
break;
case 'd':
verbose_print = 1;
kvprintf("[!] verbose mode enabled\n");
break;
case '?':
if(optopt == 'p') {
fprintf (stderr, "Option %c requires an argument.\n", optopt);
Expand Down

0 comments on commit 52b7b34

Please sign in to comment.