Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added API for client and created test client. Also cleaned up code. #73

Open
wants to merge 24 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
2072ea4
Merge branch 'client-dev' of https://github.com/gwAdvNet2015/gw-kv-st…
kstats Apr 20, 2015
b3b3986
Merge branch 'apiclientversion' of https://github.com/sacul29/gw-kv-s…
kstats Apr 27, 2015
682e258
Merge branch 'cleanup' of https://github.com/sacul29/gw-kv-store into…
kstats Apr 27, 2015
53e3934
Merge branch 'cleanup' of https://github.com/sacul29/gw-kv-store into…
kstats Apr 27, 2015
1e7e9be
created api for client
jdk514 Apr 29, 2015
50cace5
fixed api logic
jdk514 Apr 29, 2015
7939849
fixed Makefile, but still broken
jdk514 Apr 29, 2015
372470a
basic test...
kstats Apr 29, 2015
2d758c7
Merge branch 'client_api' of https://github.com/sacul29/gw-kv-store i…
kstats Apr 29, 2015
01a0262
added header file for api
Apr 29, 2015
5b824fe
Merge branch 'client_api' of github.com:sacul29/gw-kv-store into clie…
Apr 29, 2015
4c79e51
Merge branch 'client_api' of github.com:sacul29/gw-kv-store into clie…
Apr 29, 2015
2997182
added set too...
kstats Apr 29, 2015
547f86c
fixed my silly imports
kstats Apr 29, 2015
5655cd4
fixed my silly imports
kstats Apr 29, 2015
1828bad
removed random free
kstats Apr 29, 2015
b93b77f
code cleanup and commenting
kstats Apr 29, 2015
d7f30ed
updated testing for api
Apr 29, 2015
e56e344
fixed problem with merge
Apr 29, 2015
179b70c
Fixed issue with compiling api
Apr 29, 2015
8bb81c4
commented client api test
kstats Apr 29, 2015
b6c699a
fixed memory issue
Apr 29, 2015
89b76ae
Merge branch 'client_api' of https://github.com/sacul29/gw-kv-store i…
kstats May 5, 2015
6fbd714
Fixed makefile and api, added test to folder for another group to finish
kstats May 5, 2015
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions client/Makefile
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
#Use the gcc compiler
CC = gcc
CFLAGS = -g
DEPS = ../lib/marshal/marshal.o
DEPS = ../lib/marshal/marshal.o
LDFLAGS =
#Objects created by makefile
OBJS = client-kv
OBJS = client-kv client_api.o

all: $(OBJS)

rebuild: clean all

client_api: client_api.c
$(CC) -c client_api.c -o client_api.o
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-o flag is unnecessary (you're only compiling one field and it defaults to the name), but you can leave it


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)

clean:
@rm -f $(OBJS)
Expand Down
60 changes: 13 additions & 47 deletions client/client-kv.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,18 @@
#include <inttypes.h>
#include <string.h>
#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)
{
Expand All @@ -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;
}
Expand All @@ -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);
Expand All @@ -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) {
Expand All @@ -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') {
Expand All @@ -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)
Expand All @@ -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);
Expand Down Expand Up @@ -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");
Expand All @@ -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);
}
Expand All @@ -242,7 +209,6 @@ int main(int argc, char ** argv)
freeaddrinfo(server);
close(sockfd);

//printf("Done.\n");
return 0;

}
128 changes: 128 additions & 0 deletions client/client_api.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <unistd.h>
#include <inttypes.h>
#include <string.h>
#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.c- creates client api and modularizes
*************************************************/
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;
}

status = __read_get_msg(sockfd, msg);

return status;
}

int
__read_get_msg(int sockfd, struct operation * msg)
{
int bytes_received, status;

status = __demarshal_msg(sockfd, 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;
}

}

int
__demarshal_msg(int sockfd, struct operation * marshal_msg)
{
char curr_char;
int count = 0;
int i;
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')
{
stat = -1;
} 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, &op, status);
marshal_msg->value_length = op-> value_length;
free(op);
}
free(status);
free(msg);
return stat;
}
25 changes: 25 additions & 0 deletions client/client_api.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#ifndef __CLIENT_API__
#define __CLIENT_API__

#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);

int
send_mem(int sockfd, struct operation * msg);

#endif//__CLIENT_API__
Loading