Skip to content

Commit

Permalink
Merge branch 'master' into client-dev
Browse files Browse the repository at this point in the history
  • Loading branch information
amygara committed Apr 29, 2015
2 parents 43fc51a + 59037a7 commit b6b226b
Show file tree
Hide file tree
Showing 16 changed files with 592 additions and 97 deletions.
3 changes: 2 additions & 1 deletion lib/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
DEPS= linkedlist hashtable murmurhash marshal timer_uril
DEPS= linkedlist hashtable murmurhash marshal timer_util

TEST_DEPS= test_linkedlist test_hashtable test_murmurhash test_marshal test_timer_util

.PHONY: all test $(DEPS) $(TEST_DEPS)
Expand Down
1 change: 1 addition & 0 deletions lib/linkedlist/test/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test-linkedlist
Binary file removed lib/linkedlist/test/test-linkedlist
Binary file not shown.
134 changes: 67 additions & 67 deletions lib/marshal/marshal.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include <assert.h>
#include "marshal.h"

int
int
gwkv_marshal_server(struct operation* data, int status, char** ascii)
{
char* val = (char*)malloc(100000*sizeof(char));
Expand All @@ -23,7 +23,7 @@ gwkv_marshal_server(struct operation* data, int status, char** ascii)
char b1[] = "VALUE ";
char b2[] = " 0 ";
char b3[] = "\r\n";
char b4[] = "END\r\n";
char b4[] = "END\r\n";
switch(data->method_type) {
case SET:
strcat(val,comm_name);
Expand Down Expand Up @@ -64,68 +64,68 @@ gwkv_marshal_server(struct operation* data, int status, char** ascii)
#define COMMAND_LENGTH 3
#define FLAG_LENGTH 1 //right now 0
#define EXP_TIME_LENGTH 1 //unix time format. Right now 0.
#define SPACE_LENGTH 1
#define SPACE_LENGTH 1
#define NEWLINE_LENGTH 2

int
int
gwkv_marshal_client(struct operation* data, char** ascii)
{
{
char* final_marshed_value = NULL;
char space = ' ';
char zero = '0';
size_t size = 0;
char value_length[32] = {0};
char* marshaled_value = 0;

switch(data->method_type) {
case SET:
sprintf(value_length, "%d", data->value_length);
size = COMMAND_LENGTH + 5*SPACE_LENGTH +

size = COMMAND_LENGTH + 5*SPACE_LENGTH +
FLAG_LENGTH + EXP_TIME_LENGTH +
2* NEWLINE_LENGTH +
data->key_length +
strlen(value_length) + data->value_length;
2* NEWLINE_LENGTH +
data->key_length +
strlen(value_length) + data->value_length;

//convert data to this format:
//<command name> <key> <flags> ...
marshaled_value = calloc(1, size);
final_marshed_value = marshaled_value;

snprintf(marshaled_value, COMMAND_LENGTH + 1,"%s", "set");
marshaled_value += COMMAND_LENGTH;
marshaled_value += COMMAND_LENGTH;
snprintf(marshaled_value, SPACE_LENGTH + 1,"%c", space);
marshaled_value += SPACE_LENGTH;
marshaled_value += SPACE_LENGTH;
snprintf(marshaled_value, data->key_length +1 ,"%s", data->key);
marshaled_value += data->key_length;
marshaled_value += data->key_length;
snprintf(marshaled_value, SPACE_LENGTH + 1,"%c", space);
marshaled_value += SPACE_LENGTH;
marshaled_value += SPACE_LENGTH;
snprintf(marshaled_value, FLAG_LENGTH + 1 ,"%c", zero);
marshaled_value += FLAG_LENGTH;
marshaled_value += FLAG_LENGTH;
snprintf(marshaled_value, SPACE_LENGTH + 1, "%c", space);
marshaled_value += SPACE_LENGTH;
marshaled_value += SPACE_LENGTH;
snprintf(marshaled_value, EXP_TIME_LENGTH + 1 ,"%c", zero);
marshaled_value += EXP_TIME_LENGTH;
marshaled_value += EXP_TIME_LENGTH;
snprintf(marshaled_value, SPACE_LENGTH +1 ,"%c", space);
marshaled_value += SPACE_LENGTH;
marshaled_value += SPACE_LENGTH;


snprintf(marshaled_value, strlen(value_length) + 1 ,"%s", value_length);
marshaled_value += strlen(value_length);
marshaled_value += strlen(value_length);
//snprintf(marshaled_value, SPACE_LENGTH + 1,"%c", space);
//marshaled_value += SPACE_LENGTH;
//marshaled_value += SPACE_LENGTH;
snprintf(marshaled_value, NEWLINE_LENGTH + 1 ,"%s", "\r\n");
marshaled_value += NEWLINE_LENGTH;
marshaled_value += NEWLINE_LENGTH;
//snprintf(marshaled_value, SPACE_LENGTH ,"%s", space);
//marshaled_value += SPACE_LENGTH;
//marshaled_value += SPACE_LENGTH;
snprintf(marshaled_value, data->value_length + 1 ,"%s", data->value);
marshaled_value += data->value_length;
marshaled_value += data->value_length;
snprintf(marshaled_value, NEWLINE_LENGTH ,"%c", '\r');
marshaled_value += 1;
marshaled_value += 1;
snprintf(marshaled_value, NEWLINE_LENGTH ,"%c", '\n');

break;

case GET:
//convert data to this format
// get <key>* \r\n
Expand All @@ -135,19 +135,19 @@ gwkv_marshal_client(struct operation* data, char** ascii)
marshaled_value = calloc(1, size);
final_marshed_value = marshaled_value;
//fwrite("get", sizeof(char), COMMAND_LENGTH, marshaled_value);
//marshaled_value += COMMAND_LENGTH;
//marshaled_value += COMMAND_LENGTH;
//fwrite(marshaled_value, SPACE_LENGTH ,"%s", space);
snprintf(marshaled_value, COMMAND_LENGTH + 1,"%s", "get");
marshaled_value += COMMAND_LENGTH;
marshaled_value += COMMAND_LENGTH;
snprintf(marshaled_value, SPACE_LENGTH + 1,"%c", space);
marshaled_value += SPACE_LENGTH;
marshaled_value += SPACE_LENGTH;
snprintf(marshaled_value, data->key_length + 1,"%s", data->key);
marshaled_value += data->key_length;
marshaled_value += data->key_length;
snprintf(marshaled_value, NEWLINE_LENGTH ,"%c", '\r');
marshaled_value += 1;
marshaled_value += 1;
snprintf(marshaled_value, NEWLINE_LENGTH ,"%c", '\n');


break;
default:
assert(0);
Expand All @@ -156,12 +156,12 @@ gwkv_marshal_client(struct operation* data, char** ascii)
return 0;
}

int
int
gwkv_demarshal_server(char* ascii, struct operation** op)
{
/*switch(data->method_type) {
case SET:
//convert ascii to operation
//convert ascii to operation
case GET:
//convert ascii to operation
}*/
Expand All @@ -172,26 +172,26 @@ gwkv_demarshal_server(char* ascii, struct operation** op)
char s2[] = "get";

/*if (strcmp(ascii, s0) >= strlen(s0)) {
} else*/
} else*/
if ( 0 == strncmp(ascii, s1, strlen(s1))) {
data->method_type = SET;
traverse += strlen(s1) + 1;

//next charcater is space;
assert(traverse[-1] == ' ');


char* temp = strchr(traverse, ' ');
char* temp = strchr(traverse, ' ');
if ( NULL != temp) {
data->key_length = temp - traverse;
data->key = malloc(data->key_length + 1);
strncpy(data->key, traverse, data->key_length);
//data->key[data->key_length] = '\0';
//data->key[data->key_length] = '\0';
} else {
assert(0);
}

traverse = temp + 1;
assert(traverse[0] == '0');//flags
assert(traverse[1] == ' ');
Expand All @@ -206,9 +206,9 @@ gwkv_demarshal_server(char* ascii, struct operation** op)
} else {
assert(0);
}

traverse = temp;//pointing to \r\n.

/*temp = strchr(traverse, ' ');
if (NULL != temp) {
traverse = temp + 1;
Expand All @@ -218,7 +218,7 @@ gwkv_demarshal_server(char* ascii, struct operation** op)
assert(0);
}*/
assert(traverse[1] == '\n');

traverse += 2; // pointing to data

temp = strchr(traverse, '\r');
Expand All @@ -227,20 +227,20 @@ gwkv_demarshal_server(char* ascii, struct operation** op)
assert(data->value_length == temp - traverse);
data->value = malloc(data->value_length + 1);
strncpy(data->value, traverse, data->value_length);

} else {
assert(0);
}
traverse = temp;
assert(traverse[1] == '\n');



} else if (0 == strncmp(ascii, s2, strlen(s2))) {
data->method_type = GET;
traverse += strlen(s2) + 1;//pointing key
char* temp = strchr(traverse, '\r');

//Found the newline
if (NULL != temp) {
data->key_length = temp - traverse;
Expand All @@ -254,11 +254,11 @@ gwkv_demarshal_server(char* ascii, struct operation** op)
assert(0);
}

*op = data;
*op = data;
return 0;
}

int
int
gwkv_demarshal_client(char* ascii, struct operation** op, int* status)
{
struct operation *data = (struct operation*)malloc(sizeof(struct operation));
Expand Down Expand Up @@ -287,16 +287,16 @@ gwkv_demarshal_client(char* ascii, struct operation** op, int* status)
*status=3;
return 0;
}

char *value = "VALUE";
char* traverse = ascii;
if( 0 != strncmp(traverse, value, strlen(value))) {
assert(0);
}
}

data->method_type = GET;
traverse += strlen(value) + 1; // points to key now.

char* temp = strchr(traverse, ' ');
if ( NULL != temp) {
data->key_length = temp - traverse;
Expand All @@ -305,17 +305,17 @@ gwkv_demarshal_client(char* ascii, struct operation** op, int* status)
} else {
assert(0);
}

assert(temp[1] == '0'); //flags
assert(temp[2] == ' ');
traverse = temp + 3; //pointing to <bytes> now

temp = strchr(traverse, '\r');
if (NULL != temp) {
sscanf(traverse, "%d", &data->value_length);
data->value = malloc(data->value_length);
}

assert(temp[1] == '\n');

traverse = temp + 2;//pointing to value now.
Expand All @@ -336,44 +336,44 @@ gwkv_demarshal_client(char* ascii, struct operation** op, int* status)
while(a[i]!=' ' && a[i]!='\0'){
b[i]=a[i];
i++;
}
}
b[i]='\0';
offset += i+1;
char c1[]="VALUE";
if(strcmp(b,c1)!=0){
return -1;
}
data->method_type = GET;
//key
a = ascii + offset;
i=0;
while(a[i]!=' '&& a[i]!='\0'){
b[i]=a[i];
i++;
}
}
b[i]='\0';
offset += i+1;
data->key_length = i + 1;
data->key = malloc(data->key_length);
strcpy(data->key, b);
//flag
i=0;
while(b[i]!=' '&&b[i]!='\0'){
b[i]=a[i];
i++;
}
}
b[i]='\0';
offset += i+1;
//length
i=0;
while(b[i]!=' '&&b[i]!='\0'){
b[i]=a[i];
i++;
}
}
b[i]='\0';
offset += i+1;
data->value_length = atoi(b);
Expand All @@ -384,7 +384,7 @@ gwkv_demarshal_client(char* ascii, struct operation** op, int* status)
while(b[i]!='\r'&&b[i]!='\0'){
b[i]=a[i];
i++;
}
}
b[i]='\0';
offset += i+1;
char *b2 = (char*)malloc(50*sizeof(char));
Expand Down
2 changes: 2 additions & 0 deletions lib/marshal/marshal.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef __MARSHAL_H__
#define __MARSHAL_H__

#include <stdlib.h>

/* Defines for returned status */
#define STORED 0
#define NOT_STORED 1
Expand Down
1 change: 1 addition & 0 deletions server/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
server-kv
Loading

0 comments on commit b6b226b

Please sign in to comment.