diff --git a/common.h b/common.h new file mode 100644 index 0000000..71af579 --- /dev/null +++ b/common.h @@ -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__ */ diff --git a/lib/marshal/marshal.c b/lib/marshal/marshal.c index 1adb346..54b04ca 100644 --- a/lib/marshal/marshal.c +++ b/lib/marshal/marshal.c @@ -10,6 +10,9 @@ #include #include #include "marshal.h" +#include "../../common.h" + +volatile int verbose_print = 0; int gwkv_marshal_server(struct operation* data, int status, char** ascii) diff --git a/server/handle_operation.c b/server/handle_operation.c index d006155..20bc38f 100644 --- a/server/handle_operation.c +++ b/server/handle_operation.c @@ -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. * @@ -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) @@ -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"); @@ -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; } @@ -74,11 +83,15 @@ 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: @@ -86,6 +99,8 @@ gwkv_handle_set(struct gwkv_server *ht, struct operation *op) exit(-1); } + kvprintf("[!] inside gwkv_handle_set | here\'s the crafted message for set command -- %s\n", msg); + return msg; } @@ -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 { diff --git a/server/server-kv-run.c b/server/server-kv-run.c index 6e5e220..48bd5e7 100644 --- a/server/server-kv-run.c +++ b/server/server-kv-run.c @@ -20,6 +20,8 @@ #include #include #include + +#include "../common.h" #include "../lib/socket_helper.h" /* Provides sh_* funcs */ #include "server-kv.h" #include "gwkv_ht_wrapper.h" @@ -42,8 +44,9 @@ 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; @@ -51,6 +54,10 @@ main(int argc, char ** argv) 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);