Commit 82957c3a authored by Roshan Rabinarayan's avatar Roshan Rabinarayan

added KVCLientLibrary.c

parent b6242314
...@@ -9,55 +9,17 @@ send GET , PUT , and DELETE requests to the server process. It will have a main ...@@ -9,55 +9,17 @@ send GET , PUT , and DELETE requests to the server process. It will have a main
client library interfaces to do the actual operations. client library interfaces to do the actual operations.
*/ */
#include <stdio.h>
#include <netdb.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include "KVMessageFormat.h"
#include <arpa/inet.h>
#define MAX 80 #define MAX 80
#define PORT 8000 #define PORT 8000
#define SA struct sockaddr #define SA struct sockaddr
struct message *request(char status,char* key,char* value) #include "KVClientLibrary.c"
{ struct message* requestMessage;
if(!status ||(key==NULL && value==NULL) )
{
printf("Invalid parameters in request()");
return NULL;
}
struct message *requestMessage= malloc(sizeof(struct message));
requestMessage->status=status;
memcpy(requestMessage->key,key,256); //copied the complete key
if(strlen(requestMessage->key)<256)
{
requestMessage->key[strlen(requestMessage->key)]='\0';
}
if(value!=NULL)
{
memcpy(requestMessage->value,value,256); //copied the complete value
}
if(strlen(requestMessage->value)<256) //to pad with \0
{
requestMessage->value[strlen(requestMessage->value)]='\0';
}
printf("\n[Message Generated at Client]\n[[Status:%c]\n[Key:%s]\n[Value:%s]]",requestMessage->status,requestMessage->key,requestMessage->value);
return requestMessage;
}
struct message* requestMessage;
void func(int sockfd,struct message* requestMessage) void func(int sockfd,struct message* requestMessage)
{ {
char buff[MAX]; char buff[MAX];
int n=0; int n=0;
for (;;) { for (;;) {
bzero(buff, sizeof(buff)); bzero(buff, sizeof(buff));
if(1) if(1)
{ {
n++; n++;
...@@ -65,9 +27,6 @@ void func(int sockfd,struct message* requestMessage) ...@@ -65,9 +27,6 @@ void func(int sockfd,struct message* requestMessage)
write(sockfd, requestMessage, sizeof(struct message)); write(sockfd, requestMessage, sizeof(struct message));
sleep(2); sleep(2);
} }
//printf("[Message sent to server]\n[[Status:%c]\n[Key:%s]\n[Value:%s]]",requestMessage->status,requestMessage->key,requestMessage->value);
} }
} }
int main(int argc, char const *argv[]) int main(int argc, char const *argv[])
...@@ -101,11 +60,7 @@ int main(int argc, char const *argv[]) ...@@ -101,11 +60,7 @@ int main(int argc, char const *argv[])
char value[256]="cde"; char value[256]="cde";
struct message *requestMessage= request('g',key,value); struct message *requestMessage= request('g',key,value);
struct message *replyMessage=malloc(sizeof(struct message)); struct message *replyMessage=malloc(sizeof(struct message));
///send(sock , requestMessage ,sizeof(struct message) , 0 );
///printf("Hello message sent\n");
func(sock,requestMessage); func(sock,requestMessage);
// valread = read( sock , replyMessage, sizeof(struct message));
//printf("[Message Received from client]\n[[Status:%c]\n[Key:%s]\n[Value:%s]]",replyMessage->status,replyMessage->key,replyMessage->value);
return 0; return 0;
} }
\ No newline at end of file
...@@ -33,3 +33,49 @@ If the code is 200, then get will fill in the value with malloced memory. ...@@ -33,3 +33,49 @@ If the code is 200, then get will fill in the value with malloced memory.
Else, it will malloc some memory into the error pointer and return it. And so on.... Else, it will malloc some memory into the error pointer and return it. And so on....
*/ */
#include "KVMessageFormat.h"
#include<stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <netdb.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
struct message *request(char status,char* key,char* value)
{
if(!status ||(key==NULL && value==NULL) )
{
printf("Invalid parameters in request()");
return NULL;
}
struct message *requestMessage= malloc(sizeof(struct message));
requestMessage->status=status;
memcpy(requestMessage->key,key,256); //copied the complete key
if(strlen(requestMessage->key)<256)
{
requestMessage->key[strlen(requestMessage->key)]='\0';
}
if(value!=NULL)
{
memcpy(requestMessage->value,value,256); //copied the complete value
}
if(strlen(requestMessage->value)<256) //to pad with \0
{
requestMessage->value[strlen(requestMessage->value)]='\0';
}
return requestMessage;
}
void printMessage(struct message *requestMessage)
{
printf("[Message Generated]\n[[Status:%c]\n[Key:%s]\n[Value:%s]]\n",requestMessage->status,requestMessage->key,requestMessage->value);
return ;
}
\ No newline at end of file
...@@ -7,10 +7,6 @@ status code, and this status code will uniquely identify different requests and ...@@ -7,10 +7,6 @@ status code, and this status code will uniquely identify different requests and
the message will be value, again you can use padding if value is not 256B. In case of GET and the message will be value, again you can use padding if value is not 256B. In case of GET and
DEL requests where no value is required you can only consider the first 257B of the request DEL requests where no value is required you can only consider the first 257B of the request
message. message.
Status Codes for request message
GET: 1
PUT: 2
DEL: 3 DEL: 3
Status Codes for response message Status Codes for response message
Success: 200 Success: 200
......
all: KVClient.c KVServer.c KVMessageFormat.h all: KVClient.c KVServer.c KVMessageFormat.h KVClientLibrary.c
gcc KVServer.c -o server gcc -pthread KVServer.c -o server
gcc KVClient.c -o client gcc KVClient.c -o client
\ No newline at end of file
No preview for this file type
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