From 1e7e9bec3f99f206938aba8eed67d10a417b42f5 Mon Sep 17 00:00:00 2001 From: Joel Date: Wed, 29 Apr 2015 11:06:17 -0700 Subject: [PATCH 01/15] created api for client --- client/Makefile | 6 ++- client/client_api.c | 117 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 122 insertions(+), 1 deletion(-) create mode 100644 client/client_api.c diff --git a/client/Makefile b/client/Makefile index 753e08f..fce068b 100644 --- a/client/Makefile +++ b/client/Makefile @@ -4,7 +4,7 @@ CFLAGS = -g DEPS = ../lib/marshal/marshal.o LDFLAGS = #Objects created by makefile -OBJS = client-kv +OBJS = client-kv client_api all: $(OBJS) @@ -14,6 +14,10 @@ client-kv: client-kv.c cd ../lib/marshal && make $(CC) $(CFLAGS) client-kv.c -o $(OBJS) $(DEPS) +client_api: client_api.c + cd ../lib/marshal && make + $(CC) $(CFLAGS) client-kv.c -o $(OBJS) $(DEPS + clean: @rm -f $(OBJS) diff --git a/client/client_api.c b/client/client_api.c new file mode 100644 index 0000000..cb862d0 --- /dev/null +++ b/client/client_api.c @@ -0,0 +1,117 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "../lib/marshal/marshal.h" +/**************************************** + Author: Joel Klein, Katie Stasaski, Lucas Chaufournier, Tim Wood + with a little help from + http://beej.us/guide/bgnet/ + ****************************************/ +//Returns the number of bytes to wait for + +int +set_mem(int sockfd, struct operation * msg) +{ + char * temp; + int num_bytes, rc; + int status = 0; + + gwkv_marshal_client(msg, &temp); + + rc = send(sockfd,temp,strlen(temp), 0); + if(rc < 0) { + status = -1; + } + + return status; +} + +int +get_mem(int sockfd, struct operation * msg) +{ + char * temp; + int num_bytes, rc; + int status = 0; + + gwkv_marshal_client(msg, &temp); + + rc = send(sockfd,temp,strlen(temp), 0); + if(rc < 0) { + status = -1; + return status; + } + + read_get_msg(sockfd, msg); + + return status; +} + +void +read_get_msg(int sockfd, struct operation * msg) +{ + int bytes_received; + + msg = demarshal_msg(sockfd); + bytes_received = recv(sockfd, msg->value, msg->value_length, 0); + +} + +struct operation* +demarshal_msg(int sockfd) +{ + char curr_char; + int count = 0; + int i; + char* msg = malloc(1024); + struct operation* marshal_msg; + int * status = malloc(sizeof(int)); + + recv(sockfd,&curr_char,1,0); + if(curr_char == 'E') + { + printf("END\n"); + exit(0); + } else { + msg[count] = curr_char; + count++; + + for(i=0; i<3; i++) { + while(1) { + recv(sockfd, &curr_char, 1, 0); + msg[count] = curr_char; + count++; + + if(curr_char == ' ') { + break; + } + } + } + + while(1) { + recv(sockfd, &curr_char, 1, 0); + if (curr_char == '\r') { + msg[count] = curr_char; + count++; + recv(sockfd, &curr_char, 1, 0); + if (curr_char == '\n') { + msg[count] = curr_char; + count++; + break; + } + } else { + msg[count] = curr_char; + } + count++; + } + } + gwkv_demarshal_client(msg, &marshal_msg, status); + free(status); + free(msg); + return marshal_msg; +} \ No newline at end of file From 50cace531a883a13dc1a045da54e07fcc00750a1 Mon Sep 17 00:00:00 2001 From: Joel Date: Wed, 29 Apr 2015 11:21:41 -0700 Subject: [PATCH 02/15] fixed api logic --- client/client_api.c | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/client/client_api.c b/client/client_api.c index cb862d0..d05acf7 100644 --- a/client/client_api.c +++ b/client/client_api.c @@ -47,36 +47,40 @@ get_mem(int sockfd, struct operation * msg) return status; } - read_get_msg(sockfd, msg); + status = read_get_msg(sockfd, msg); return status; } -void +int read_get_msg(int sockfd, struct operation * msg) { int bytes_received; - msg = demarshal_msg(sockfd); - bytes_received = recv(sockfd, msg->value, msg->value_length, 0); + status = demarshal_msg(sockfd, msg); + if (status < 0) { + return -1; + } else { + bytes_received = recv(sockfd, msg->value, msg->value_length, 0); + return 0; + } } -struct operation* -demarshal_msg(int sockfd) +int +demarshal_msg(int sockfd, struct operation * marshal_msg) { char curr_char; int count = 0; int i; char* msg = malloc(1024); - struct operation* marshal_msg; int * status = malloc(sizeof(int)); + int stat = 0; recv(sockfd,&curr_char,1,0); if(curr_char == 'E') { - printf("END\n"); - exit(0); + stat = -1; } else { msg[count] = curr_char; count++; @@ -109,9 +113,10 @@ demarshal_msg(int sockfd) } count++; } + + gwkv_demarshal_client(msg, &marshal_msg, status); } - gwkv_demarshal_client(msg, &marshal_msg, status); free(status); free(msg); - return marshal_msg; + return stat; } \ No newline at end of file From 79398494c375cc2b686b6810e475128a98a35dda Mon Sep 17 00:00:00 2001 From: Joel Date: Wed, 29 Apr 2015 11:42:53 -0700 Subject: [PATCH 03/15] fixed Makefile, but still broken --- client/Makefile | 4 ++-- client/client_api.c | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/client/Makefile b/client/Makefile index fce068b..5f0cac5 100644 --- a/client/Makefile +++ b/client/Makefile @@ -12,11 +12,11 @@ rebuild: clean all client-kv: client-kv.c cd ../lib/marshal && make - $(CC) $(CFLAGS) client-kv.c -o $(OBJS) $(DEPS) + $(CC) $(CFLAGS) client-kv.c -o client-kv $(DEPS) client_api: client_api.c cd ../lib/marshal && make - $(CC) $(CFLAGS) client-kv.c -o $(OBJS) $(DEPS + $(CC) $(CFLAGS) client_api.c -o client_api $(DEPS) clean: @rm -f $(OBJS) diff --git a/client/client_api.c b/client/client_api.c index d05acf7..6340880 100644 --- a/client/client_api.c +++ b/client/client_api.c @@ -55,7 +55,7 @@ get_mem(int sockfd, struct operation * msg) int read_get_msg(int sockfd, struct operation * msg) { - int bytes_received; + int bytes_received, status; status = demarshal_msg(sockfd, msg); if (status < 0) { From 372470ab56770bd93d01e73dddd0cdbcf6af2892 Mon Sep 17 00:00:00 2001 From: Katie Stasaski Date: Wed, 29 Apr 2015 14:43:13 -0400 Subject: [PATCH 04/15] basic test... --- client/client_api_test.c | 100 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 client/client_api_test.c diff --git a/client/client_api_test.c b/client/client_api_test.c new file mode 100644 index 0000000..ec1208f --- /dev/null +++ b/client/client_api_test.c @@ -0,0 +1,100 @@ +#include +#include +#include +#include +#include +#include +#include +#include "client_api.h" + + +int +main(int argc, char ** argv) +{ + char* server_port = "80"; + //ip address or host name of server http request will be sent to + char* server_ip = "google.com"; + //socket int used to connect + int sockfd, rc, o; + //used to store formatted http request + struct addrinfo hints, *server; + char * cmd; + char * key; + char * value; + char * temp; + while ((o = getopt (argc, argv, "p:h:c:k:v:")) != -1) { + switch(o){ + case 'p': + server_port = optarg; + break; + case 'h': + server_ip = optarg; + break; + case 'c': + cmd = optarg; + break; + case 'k': + key = optarg; + break; + case 'v': + value = optarg; + break; + case '?': + if(optopt == 'p' || optopt == 'h' ) { + fprintf (stderr, "Option %c requires an argument.\n", optopt); + } + else { + fprintf (stderr, "Unknown argument: %c.\n", optopt); + } + break; + } + } + /* The hints struct is used to specify what kind of server info we are looking for */ + memset(&hints, 0, sizeof hints); + hints.ai_family = AF_INET; + hints.ai_socktype = SOCK_STREAM; /* or SOCK_DGRAM */ + + /* getaddrinfo() gives us back a server address we can connect to. + It actually gives us a linked list of addresses, but we'll just use the first. + */ + if (rc = getaddrinfo(server_ip, server_port, &hints, &server) != 0) { + perror(gai_strerror(rc)); + exit(-1); + } + + sockfd = socket(server->ai_family, server->ai_socktype, server->ai_protocol); + if (sockfd == -1) { + perror("ERROR opening socket"); + exit(-1); + } + //printf("socket created\n"); + rc = connect(sockfd, server->ai_addr, server->ai_addrlen); + if (rc == -1) { + perror("ERROR on connect"); + close(sockfd); + exit(-1); + // TODO: could use goto here for error cleanup + } + + //read back get value + if (cmd[0] == 'g') { + struct * operation msg = malloc(sizeof (struct operation)); + msg -> method_type = GET; + msg -> key = key; + msg -> key_length = strlen(key); + msg -> value = value; + msg -> value_length = strlen(value); + get_mem(sockfd,msg); + } + else if(cmd[0] == 's'){ + set_mem(sockfd,msg); + } + free(temp); + out: + freeaddrinfo(server); + close(sockfd); + + //printf("Done.\n"); + return 0; + +} From 01a02623eb1af63094469c92d6eb4cc9b69878aa Mon Sep 17 00:00:00 2001 From: Lucas Chaufournier Date: Wed, 29 Apr 2015 14:44:03 -0400 Subject: [PATCH 05/15] added header file for api --- client/client_api.h | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 client/client_api.h diff --git a/client/client_api.h b/client/client_api.h new file mode 100644 index 0000000..5227b14 --- /dev/null +++ b/client/client_api.h @@ -0,0 +1,13 @@ +#ifndef __CLIENT_API__ +#define __CLIENT_API__ + +#include "../lib/marshal/marshal.h" + + +int +get_mem(int sockfd, struct operation *msg); + +int +send_mem(int sockfd, struct operation * msg); + +#endif//__CLIENT_API__ From 2997182eb233a7f5281357c508c81fccddb0debc Mon Sep 17 00:00:00 2001 From: Katie Stasaski Date: Wed, 29 Apr 2015 14:52:47 -0400 Subject: [PATCH 06/15] added set too... --- client/client_api_test.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/client/client_api_test.c b/client/client_api_test.c index ec1208f..7b5753b 100644 --- a/client/client_api_test.c +++ b/client/client_api_test.c @@ -82,11 +82,15 @@ main(int argc, char ** argv) msg -> method_type = GET; msg -> key = key; msg -> key_length = strlen(key); - msg -> value = value; - msg -> value_length = strlen(value); get_mem(sockfd,msg); } else if(cmd[0] == 's'){ + struct * operation msg = malloc(sizeof (struct operation)); + msg -> method_type = SET; + msg -> key = key; + msg -> key_length = strlen(key); + msg -> value = value; + msg -> value_length = strlen(value); set_mem(sockfd,msg); } free(temp); From 547f86c33b7bae1540da5761d7d49b5a26e9514e Mon Sep 17 00:00:00 2001 From: Katie Stasaski Date: Wed, 29 Apr 2015 14:55:32 -0400 Subject: [PATCH 07/15] fixed my silly imports --- client/client_api_test.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/client/client_api_test.c b/client/client_api_test.c index 7b5753b..5cd9b5c 100644 --- a/client/client_api_test.c +++ b/client/client_api_test.c @@ -3,6 +3,8 @@ #include #include #include +#include +#include #include #include #include "client_api.h" From 5655cd4fa42f60999d9a4a469700963d7ef81a0d Mon Sep 17 00:00:00 2001 From: Katie Stasaski Date: Wed, 29 Apr 2015 14:56:51 -0400 Subject: [PATCH 08/15] fixed my silly imports --- client/client_api_test.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/client/client_api_test.c b/client/client_api_test.c index 5cd9b5c..5c0fe79 100644 --- a/client/client_api_test.c +++ b/client/client_api_test.c @@ -80,14 +80,14 @@ main(int argc, char ** argv) //read back get value if (cmd[0] == 'g') { - struct * operation msg = malloc(sizeof (struct operation)); + struct operation * msg = malloc(sizeof (struct operation)); msg -> method_type = GET; msg -> key = key; msg -> key_length = strlen(key); get_mem(sockfd,msg); } else if(cmd[0] == 's'){ - struct * operation msg = malloc(sizeof (struct operation)); + struct operation * msg = malloc(sizeof (struct operation)); msg -> method_type = SET; msg -> key = key; msg -> key_length = strlen(key); From 1828badc60c955eeba76fa0c500404818db74a85 Mon Sep 17 00:00:00 2001 From: Katie Stasaski Date: Wed, 29 Apr 2015 15:02:07 -0400 Subject: [PATCH 09/15] removed random free --- client/client_api_test.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/client/client_api_test.c b/client/client_api_test.c index 5c0fe79..5d1db53 100644 --- a/client/client_api_test.c +++ b/client/client_api_test.c @@ -23,7 +23,6 @@ main(int argc, char ** argv) char * cmd; char * key; char * value; - char * temp; while ((o = getopt (argc, argv, "p:h:c:k:v:")) != -1) { switch(o){ case 'p': @@ -95,7 +94,6 @@ main(int argc, char ** argv) msg -> value_length = strlen(value); set_mem(sockfd,msg); } - free(temp); out: freeaddrinfo(server); close(sockfd); From b93b77fddb29a38880a0cb4212cd5b3ddbf96c5c Mon Sep 17 00:00:00 2001 From: Katie Stasaski Date: Wed, 29 Apr 2015 15:09:34 -0400 Subject: [PATCH 10/15] code cleanup and commenting --- client/client-kv.c | 60 ++++++++++----------------------------------- client/client_api.c | 20 +++++++++------ client/client_api.h | 12 +++++++++ 3 files changed, 37 insertions(+), 55 deletions(-) diff --git a/client/client-kv.c b/client/client-kv.c index d66d8e1..e7f4501 100644 --- a/client/client-kv.c +++ b/client/client-kv.c @@ -8,13 +8,18 @@ #include #include #include "../lib/marshal/marshal.h" -/**************************************** - Author: Joel Klein, Katie Stasaski, Lucas Chaufournier, Tim Wood - with a little help from - http://beej.us/guide/bgnet/ - ****************************************/ -//Returns the number of bytes to wait for - +/************************************************ + * GW KV + * https://github.com/gwAdvNet2015/gw-kv-store + * + * Copyright 2015 Lucas Chaufournier, Joel Klein, + * and Katie Stasaski + * + * This program is licensed under the MIT license. + * + * client-kv.c - client that gets and sets keys + * from a keystore. + *************************************************/ char* marshal_msg(char * cmd, char * key, char * value) { @@ -25,19 +30,14 @@ marshal_msg(char * cmd, char * key, char * value) if (cmd[0] == 's') { marshal_msg -> method_type = SET; num_bytes = strlen(value); - //marshal_msg -> value = malloc(sizeof(num_bytes)); marshal_msg -> value = value; marshal_msg -> value_length = num_bytes; } else { marshal_msg -> method_type = GET; } - //marshal_msg -> key = malloc(sizeof(key)); marshal_msg -> key = key; marshal_msg -> key_length = strlen(key); gwkv_marshal_client(marshal_msg, &temp); -// printf("This is temp. %s\n",temp); - //free(marshal_msg->value); - //free(marshal_msg->key); free(marshal_msg); return temp; } @@ -49,7 +49,7 @@ demarshal_msg(int sockfd) int count = 0; int i; char* msg = malloc(1024); - struct operation* marshal_msg;// = malloc(sizeof(struct operation)); + struct operation* marshal_msg; int * status = malloc(sizeof(int)); recv(sockfd,&curr_char,1,0); @@ -61,8 +61,6 @@ demarshal_msg(int sockfd) else{ msg[count] = curr_char; count++; - // printf("%c",curr_char); - //printf("reading lines\n"); i = 0; for(i=0; i<3; i++) { while(1) { @@ -71,29 +69,11 @@ demarshal_msg(int sockfd) count++; if(curr_char == ' ') { - // printf(" "); break; } - // printf("%c",curr_char); } } - /* while(1) { - recv(sockfd, &curr_char, 1, 0); - if (curr_char == '\r') { - msg[count] = curr_char; - count++; - recv(sockfd, &curr_char, 1, 0); - if (curr_char == '\n') { - msg[count] = curr_char; - count++; - break; - } - } else { - msg[count] = curr_char; - } - count++; - }*/ while(1) { recv(sockfd, &curr_char, 1, 0); if (curr_char == '\r') { @@ -110,18 +90,11 @@ demarshal_msg(int sockfd) } count++; } - //printf("\nEND\n"); - //printf("\nstring is %s\n",msg); } gwkv_demarshal_client(msg, &marshal_msg, status); free(status); free(msg); - // printf("Value is %s\n",*marshal_msg->value); - //if (*status == -1) { - // return 0; - //} else { return marshal_msg; - //} } send_msg(int sockfd, char * temp) @@ -139,8 +112,6 @@ send_msg(int sockfd, char * temp) read_get_msg(int sockfd) { int bytes_received; - //char * recv_data = (char *)malloc(sizeof(char*)*1000); - struct operation* demarshaled_msg = malloc(sizeof(struct operation)); demarshaled_msg = demarshal_msg(sockfd); bytes_received = recv(sockfd, demarshaled_msg->value, demarshaled_msg->value_length, 0); @@ -218,7 +189,6 @@ int main(int argc, char ** argv) perror("ERROR opening socket"); exit(-1); } - //printf("socket created\n"); rc = connect(sockfd, server->ai_addr, server->ai_addrlen); if (rc == -1) { perror("ERROR on connect"); @@ -229,11 +199,8 @@ int main(int argc, char ** argv) //create the marshalled message temp = marshal_msg(cmd, key, value); - // printf("This is temp return %s\n",temp); - //send the marshaled message to the server send_msg(sockfd, temp); - //read back get value if (cmd[0] == 'g') { read_get_msg(sockfd); } @@ -242,7 +209,6 @@ int main(int argc, char ** argv) freeaddrinfo(server); close(sockfd); - //printf("Done.\n"); return 0; } diff --git a/client/client_api.c b/client/client_api.c index 6340880..ba67a14 100644 --- a/client/client_api.c +++ b/client/client_api.c @@ -8,13 +8,17 @@ #include #include #include "../lib/marshal/marshal.h" -/**************************************** - Author: Joel Klein, Katie Stasaski, Lucas Chaufournier, Tim Wood - with a little help from - http://beej.us/guide/bgnet/ - ****************************************/ -//Returns the number of bytes to wait for - +/************************************************ + * GW KV + * https://github.com/gwAdvNet2015/gw-kv-store + * + * Copyright 2015 Lucas Chaufournier, Joel Klein, + * and Katie Stasaski + * + * This program is licensed under the MIT license. + * + * client_api.c- creates client api and modularizes + *************************************************/ int set_mem(int sockfd, struct operation * msg) { @@ -119,4 +123,4 @@ demarshal_msg(int sockfd, struct operation * marshal_msg) free(status); free(msg); return stat; -} \ No newline at end of file +} diff --git a/client/client_api.h b/client/client_api.h index 5227b14..0546e9f 100644 --- a/client/client_api.h +++ b/client/client_api.h @@ -3,6 +3,18 @@ #include "../lib/marshal/marshal.h" +/************************************************ + * GW KV + * https://github.com/gwAdvNet2015/gw-kv-store + * + * Copyright 2015 Lucas Chaufournier, Joel Klein, + * and Katie Stasaski + * + * This program is licensed under the MIT license. + * + * client_api.h - sets up functions for client's + * api + *************************************************/ int get_mem(int sockfd, struct operation *msg); From d7f30ed3a0d0a9fe01f3435746f039f9ea8b122c Mon Sep 17 00:00:00 2001 From: Lucas Chaufournier Date: Wed, 29 Apr 2015 15:10:46 -0400 Subject: [PATCH 11/15] updated testing for api --- client/client_api.c | 4 ++-- client/client_api_test.c | 4 +++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/client/client_api.c b/client/client_api.c index 6340880..d70719d 100644 --- a/client/client_api.c +++ b/client/client_api.c @@ -7,7 +7,7 @@ #include #include #include -#include "../lib/marshal/marshal.h" +#include "client_api.h" /**************************************** Author: Joel Klein, Katie Stasaski, Lucas Chaufournier, Tim Wood with a little help from @@ -119,4 +119,4 @@ demarshal_msg(int sockfd, struct operation * marshal_msg) free(status); free(msg); return stat; -} \ No newline at end of file +} diff --git a/client/client_api_test.c b/client/client_api_test.c index 5d1db53..fec0866 100644 --- a/client/client_api_test.c +++ b/client/client_api_test.c @@ -82,8 +82,9 @@ main(int argc, char ** argv) struct operation * msg = malloc(sizeof (struct operation)); msg -> method_type = GET; msg -> key = key; - msg -> key_length = strlen(key); + msg -> key_length = strlen(key); get_mem(sockfd,msg); + printf("Results: %s:%s\n",msg->key,msg->value); } else if(cmd[0] == 's'){ struct operation * msg = malloc(sizeof (struct operation)); @@ -93,6 +94,7 @@ main(int argc, char ** argv) msg -> value = value; msg -> value_length = strlen(value); set_mem(sockfd,msg); + printf("%s:%s\n",msg->key,msg->value); } out: freeaddrinfo(server); From 179b70c8799b545ff39b866008648be30627d5a4 Mon Sep 17 00:00:00 2001 From: Lucas Chaufournier Date: Wed, 29 Apr 2015 15:13:50 -0400 Subject: [PATCH 12/15] Fixed issue with compiling api --- client/Makefile | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/client/Makefile b/client/Makefile index 5f0cac5..4357824 100644 --- a/client/Makefile +++ b/client/Makefile @@ -1,10 +1,10 @@ #Use the gcc compiler CC = gcc CFLAGS = -g -DEPS = ../lib/marshal/marshal.o +DEPS = ../lib/marshal/marshal.o client_api.o LDFLAGS = #Objects created by makefile -OBJS = client-kv client_api +OBJS = client-kv client_api_test all: $(OBJS) @@ -15,8 +15,11 @@ client-kv: client-kv.c $(CC) $(CFLAGS) client-kv.c -o client-kv $(DEPS) client_api: client_api.c - cd ../lib/marshal && make - $(CC) $(CFLAGS) client_api.c -o client_api $(DEPS) + $(CC) -c client_api.c -o client_api.o + +client_api_test: client_api_test.c + make client_api && cd ../lib/marshal && make + $(CC) $(CFLAGS) client_api_test.c -o client_api_test $(DEPS) clean: @rm -f $(OBJS) From 8bb81c4c57bd84182b7797e08d09dc16612a0a12 Mon Sep 17 00:00:00 2001 From: Katie Stasaski Date: Wed, 29 Apr 2015 15:15:34 -0400 Subject: [PATCH 13/15] commented client api test --- client/client_api_test.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/client/client_api_test.c b/client/client_api_test.c index fec0866..367f0aa 100644 --- a/client/client_api_test.c +++ b/client/client_api_test.c @@ -8,7 +8,18 @@ #include #include #include "client_api.h" - +/************************************************ + * GW KV + * https://github.com/gwAdvNet2015/gw-kv-store + * + * Copyright 2015 Lucas Chaufournier, Joel Klein, + * and Katie Stasaski + * + * This program is licensed under the MIT license. + * + * client_api_test.c- test client to test client + * api + *************************************************/ int main(int argc, char ** argv) From b6c699a86a8500e6a9ce2f9a8ba2202df5a5c610 Mon Sep 17 00:00:00 2001 From: Lucas Chaufournier Date: Wed, 29 Apr 2015 15:32:39 -0400 Subject: [PATCH 14/15] fixed memory issue --- client/client_api.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/client/client_api.c b/client/client_api.c index 54ecc12..03c9045 100644 --- a/client/client_api.c +++ b/client/client_api.c @@ -65,6 +65,7 @@ read_get_msg(int sockfd, struct operation * msg) if (status < 0) { return -1; } else { + msg->value = malloc(msg->value_length); bytes_received = recv(sockfd, msg->value, msg->value_length, 0); return 0; } @@ -80,7 +81,7 @@ demarshal_msg(int sockfd, struct operation * marshal_msg) char* msg = malloc(1024); int * status = malloc(sizeof(int)); int stat = 0; - + struct operation* op; recv(sockfd,&curr_char,1,0); if(curr_char == 'E') { @@ -117,8 +118,9 @@ demarshal_msg(int sockfd, struct operation * marshal_msg) } count++; } - - gwkv_demarshal_client(msg, &marshal_msg, status); + gwkv_demarshal_client(msg, &op, status); + marshal_msg->value_length = op-> value_length; + free(op); } free(status); free(msg); From 6fbd714177851f7e76e7ca1448de3cd8c259c9d5 Mon Sep 17 00:00:00 2001 From: kstats Date: Tue, 5 May 2015 15:01:08 -0400 Subject: [PATCH 15/15] Fixed makefile and api, added test to folder for another group to finish --- client/Makefile | 14 ++--- client/client_api.c | 8 +-- client/tests/Makefile | 29 +++++++++ client/tests/client-tests.c | 117 ++++++++++++++++++++++++++++++++++++ 4 files changed, 155 insertions(+), 13 deletions(-) create mode 100644 client/tests/Makefile create mode 100644 client/tests/client-tests.c diff --git a/client/Makefile b/client/Makefile index 4357824..246fa74 100644 --- a/client/Makefile +++ b/client/Makefile @@ -1,25 +1,21 @@ #Use the gcc compiler CC = gcc CFLAGS = -g -DEPS = ../lib/marshal/marshal.o client_api.o +DEPS = ../lib/marshal/marshal.o LDFLAGS = #Objects created by makefile -OBJS = client-kv client_api_test +OBJS = client-kv client_api.o all: $(OBJS) rebuild: clean all -client-kv: client-kv.c - cd ../lib/marshal && make - $(CC) $(CFLAGS) client-kv.c -o client-kv $(DEPS) - client_api: client_api.c $(CC) -c client_api.c -o client_api.o -client_api_test: client_api_test.c - make client_api && cd ../lib/marshal && make - $(CC) $(CFLAGS) client_api_test.c -o client_api_test $(DEPS) +client-kv: client-kv.c + cd ../lib/marshal && make + $(CC) $(CFLAGS) client-kv.c -o client-kv $(DEPS) clean: @rm -f $(OBJS) diff --git a/client/client_api.c b/client/client_api.c index 03c9045..bcfa33b 100644 --- a/client/client_api.c +++ b/client/client_api.c @@ -51,17 +51,17 @@ get_mem(int sockfd, struct operation * msg) return status; } - status = read_get_msg(sockfd, msg); + status = __read_get_msg(sockfd, msg); return status; } int -read_get_msg(int sockfd, struct operation * msg) +__read_get_msg(int sockfd, struct operation * msg) { int bytes_received, status; - status = demarshal_msg(sockfd, msg); + status = __demarshal_msg(sockfd, msg); if (status < 0) { return -1; } else { @@ -73,7 +73,7 @@ read_get_msg(int sockfd, struct operation * msg) } int -demarshal_msg(int sockfd, struct operation * marshal_msg) +__demarshal_msg(int sockfd, struct operation * marshal_msg) { char curr_char; int count = 0; diff --git a/client/tests/Makefile b/client/tests/Makefile new file mode 100644 index 0000000..86c05c5 --- /dev/null +++ b/client/tests/Makefile @@ -0,0 +1,29 @@ +#Use the gcc compiler +CC = gcc +CFLAGS = -g +DEPS = ../lib/marshal/marshal.o client_api.o +LDFLAGS = +#Objects created by makefile +OBJS = client-kv client_api +.PHONY: client_api_test + +all: $(OBJS) + +rebuild: clean all + +client-kv: client-kv.c + cd ../lib/marshal && make + $(CC) $(CFLAGS) client-kv.c -o client-kv $(DEPS) + +client_api: client_api.c + $(CC) -c client_api.c -o client_api.o + +client_api_test: client_api_test.c + make client_api && cd ../lib/marshal && make + $(CC) $(CFLAGS) client_api_test.c -o client_api_test $(DEPS) + +clean: + @rm -f $(OBJS) + +test: + echo "No Tests Yet." diff --git a/client/tests/client-tests.c b/client/tests/client-tests.c new file mode 100644 index 0000000..367f0aa --- /dev/null +++ b/client/tests/client-tests.c @@ -0,0 +1,117 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "client_api.h" +/************************************************ + * GW KV + * https://github.com/gwAdvNet2015/gw-kv-store + * + * Copyright 2015 Lucas Chaufournier, Joel Klein, + * and Katie Stasaski + * + * This program is licensed under the MIT license. + * + * client_api_test.c- test client to test client + * api + *************************************************/ + +int +main(int argc, char ** argv) +{ + char* server_port = "80"; + //ip address or host name of server http request will be sent to + char* server_ip = "google.com"; + //socket int used to connect + int sockfd, rc, o; + //used to store formatted http request + struct addrinfo hints, *server; + char * cmd; + char * key; + char * value; + while ((o = getopt (argc, argv, "p:h:c:k:v:")) != -1) { + switch(o){ + case 'p': + server_port = optarg; + break; + case 'h': + server_ip = optarg; + break; + case 'c': + cmd = optarg; + break; + case 'k': + key = optarg; + break; + case 'v': + value = optarg; + break; + case '?': + if(optopt == 'p' || optopt == 'h' ) { + fprintf (stderr, "Option %c requires an argument.\n", optopt); + } + else { + fprintf (stderr, "Unknown argument: %c.\n", optopt); + } + break; + } + } + /* The hints struct is used to specify what kind of server info we are looking for */ + memset(&hints, 0, sizeof hints); + hints.ai_family = AF_INET; + hints.ai_socktype = SOCK_STREAM; /* or SOCK_DGRAM */ + + /* getaddrinfo() gives us back a server address we can connect to. + It actually gives us a linked list of addresses, but we'll just use the first. + */ + if (rc = getaddrinfo(server_ip, server_port, &hints, &server) != 0) { + perror(gai_strerror(rc)); + exit(-1); + } + + sockfd = socket(server->ai_family, server->ai_socktype, server->ai_protocol); + if (sockfd == -1) { + perror("ERROR opening socket"); + exit(-1); + } + //printf("socket created\n"); + rc = connect(sockfd, server->ai_addr, server->ai_addrlen); + if (rc == -1) { + perror("ERROR on connect"); + close(sockfd); + exit(-1); + // TODO: could use goto here for error cleanup + } + + //read back get value + if (cmd[0] == 'g') { + struct operation * msg = malloc(sizeof (struct operation)); + msg -> method_type = GET; + msg -> key = key; + msg -> key_length = strlen(key); + get_mem(sockfd,msg); + printf("Results: %s:%s\n",msg->key,msg->value); + } + else if(cmd[0] == 's'){ + struct operation * msg = malloc(sizeof (struct operation)); + msg -> method_type = SET; + msg -> key = key; + msg -> key_length = strlen(key); + msg -> value = value; + msg -> value_length = strlen(value); + set_mem(sockfd,msg); + printf("%s:%s\n",msg->key,msg->value); + } + out: + freeaddrinfo(server); + close(sockfd); + + //printf("Done.\n"); + return 0; + +}