Commit 3a427d2d authored by Samarth Joshi's avatar Samarth Joshi

Adding interactive client

parent b4d1e7ac
No preview for this file type
...@@ -2,13 +2,67 @@ ...@@ -2,13 +2,67 @@
#define PORT 8000 #define PORT 8000
#define SA struct sockaddr #define SA struct sockaddr
#include <stdio.h> #include <stdio.h>
#include <unistd.h>
#include <sys/types.h> #include <sys/types.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <netinet/in.h> #include <netinet/in.h>
#include "KVClientLibrary.h" #include "KVClientLibrary.h"
#include <string.h>
struct message* requestMessage; struct message* requestMessage;
void interactive (int sock) {
char command[515];
int readlen, i, j;
int status_code;
char key[256];
char value[256];
char error[256];
do {
memset(key, 0, sizeof(key));
memset(value, 0, sizeof(value));
memset(error, 0, sizeof(error));
printf("interactive> ");
fflush(stdout);
readlen = read(0, command, sizeof(command));
i=5;
while(command[i] && command[i]!=' ' && command[i]!='\n') {
i++;
}
memcpy(key, command+4, i-4);
if(command[i]==' ') {
j=i+1;
while(command[j] && command[j]!=':' && command[j]!='\n') {
j++;
}
memcpy(value, command+i+1, j-i-1);
}
// printf("key: %s\n", key);
// printf("value: %s\n", value);
// printf("error: %s\n", error);
switch(command[0]) {
case 'g': status_code = get(sock, key, value, error);
printf("[%d]", status_code);
printf(" Value recieved: %s\n", value);
break;
case 'p':status_code = put(sock, key, value, error);
printf("[%d]\n", status_code);
break;
case 'd':status_code = del(sock, key, error);
printf("[%d]\n", status_code);
break;
default: return;
}
} while(command[0] != 'q');
}
int main(int argc, char const *argv[]) int main(int argc, char const *argv[])
{ {
int sock = 0, valread; int sock = 0, valread;
...@@ -48,9 +102,10 @@ int main(int argc, char const *argv[]) ...@@ -48,9 +102,10 @@ int main(int argc, char const *argv[])
//printf("%d",(int)del(sock,key,error)); //printf("%d",(int)del(sock,key,error));
//printf("%d",(int)get(sock,key, value,error)); //printf("%d",(int)get(sock,key, value,error));
//while (1) { //while (1) {
printf("%d",(int)put(sock, key1, value1, error1)); //printf("%d",(int)put(sock, key1, value1, error1));
printf("%d",(int)put(sock, key2, value2, error2)); //printf("%d",(int)put(sock, key2, value2, error2));
printf("%d",(int)get(sock, key1, value3, error2)); //printf("%d\n",(int)get(sock, key1, value3, error2));
//printf("%s", value3);
interactive(sock);
return 0; return 0;
} }
\ No newline at end of file
...@@ -6,7 +6,6 @@ ...@@ -6,7 +6,6 @@
#include <unistd.h> #include <unistd.h>
#include "KVClientLibrary.h" #include "KVClientLibrary.h"
// to print a message // to print a message
void printMessage(struct message *requestMessage) void printMessage(struct message *requestMessage)
{ {
...@@ -90,7 +89,7 @@ int put(int sockfd,char *key,char *value,char *error) ...@@ -90,7 +89,7 @@ int put(int sockfd,char *key,char *value,char *error)
} }
else else
{ {
printMessage(reply); //printMessage(reply);
} }
free(request); free(request);
......
...@@ -32,6 +32,7 @@ void *worker(void *args) { ...@@ -32,6 +32,7 @@ void *worker(void *args) {
int newfd; int newfd;
int status; int status;
// Generate name for each thread for debugging // Generate name for each thread for debugging
char *name = (char *) malloc(5*sizeof(char)); char *name = (char *) malloc(5*sizeof(char));
gen_random(name, 5); gen_random(name, 5);
...@@ -97,11 +98,11 @@ void *worker(void *args) { ...@@ -97,11 +98,11 @@ void *worker(void *args) {
switch(requestMessage->status) { switch(requestMessage->status) {
case STATUS_GET: case STATUS_GET:
if DEBUG printf("[%s] GET \n", name); if DEBUG printf("[%s] GET \n", name);
memcpy(requestMessage->value, cache_get(requestMessage->key), 256); status = cache_get(requestMessage->key, requestMessage->value);
if(requestMessage->value == 0) { if(status) {
requestMessage->status = 240;
} else {
requestMessage->status = 200; requestMessage->status = 200;
} else {
requestMessage->status = 240;
} }
break; break;
case STATUS_PUT: case STATUS_PUT:
...@@ -121,8 +122,6 @@ void *worker(void *args) { ...@@ -121,8 +122,6 @@ void *worker(void *args) {
} }
requestMessage->status=200;
write(events[i].data.fd, requestMessage, sizeof(struct message)); write(events[i].data.fd, requestMessage, sizeof(struct message));
free(requestMessage); free(requestMessage);
} }
......
...@@ -79,14 +79,14 @@ int cache_del(char *key) ...@@ -79,14 +79,14 @@ int cache_del(char *key)
return 1; return 1;
} }
} }
printf("Cache after DEL: \n");
print_cache();
return 0; return 0;
//TODO remove key from file also //TODO remove key from file also
} }
void cache_put(char *key, char *value) void cache_put(char *key, char *value)
{ {
printf("Cache before PUT: \n");
print_cache();
int indx=-1; int indx=-1;
for(int i=0;i<MAX_SIZE;i++) for(int i=0;i<MAX_SIZE;i++)
{ {
...@@ -119,17 +119,18 @@ void cache_put(char *key, char *value) ...@@ -119,17 +119,18 @@ void cache_put(char *key, char *value)
print_cache(); print_cache();
} }
char* cache_get(char *key) int cache_get(char *key, char *value)
{ {
for(int i=0;i<MAX_SIZE;i++) for(int i=0;i<MAX_SIZE;i++)
{ {
if(strcmp(array[i]->key , key) == 0) if(strcmp(array[i]->key , key) == 0 && array[i]->valid)
{ {
while(ATOMIC_TEST_AND_SET(&(array[i]->lock),1) == 1); while(ATOMIC_TEST_AND_SET(&(array[i]->lock),1) == 1);
remove_element_from_deque(key); remove_element_from_deque(key);
insert_into_queue(key); insert_into_queue(key);
CLEAR(&(array[i]->lock),0); CLEAR(&(array[i]->lock),0);
return array[i]->value; memcpy(value, array[i]->value, VAL_SIZE);
return 1;
} }
} }
return 0; return 0;
......
...@@ -40,7 +40,7 @@ int cache_del(char *key); ...@@ -40,7 +40,7 @@ int cache_del(char *key);
void cache_put(char *key, char *value); void cache_put(char *key, char *value);
char* cache_get(char *key); int cache_get(char *key, char *value);
void init_cache(); void init_cache();
......
No preview for this file type
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment