Commit 2a0b2d19 authored by Roshan Rabinarayan's avatar Roshan Rabinarayan

added KVCLient libraary version1.1

parent 82957c3a
......@@ -58,9 +58,10 @@ int main(int argc, char const *argv[])
}
char key[256]="abc";
char value[256]="cde";
struct message *requestMessage= request('g',key,value);
struct message *replyMessage=malloc(sizeof(struct message));
func(sock,requestMessage);
char error[256];
printf("%d",(int)put(sock,key, value,error));
printf("%d",(int)del(sock,key,error));
printf("%d",(int)get(sock,key, value,error));
return 0;
}
\ No newline at end of file
......@@ -46,36 +46,96 @@ Else, it will malloc some memory into the error pointer and return it. And so on
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
struct message *request(char status,char* key,char* value)
// to print a message
void printMessage(struct message *requestMessage)
{
if(!status ||(key==NULL && value==NULL) )
printf("[Message Generated]\n[[Status:%d]\n[Key:%s]\n[Value:%s]]\n",requestMessage->status,requestMessage->key,requestMessage->value);
return ;
}
int get(int sockfd,char* key,char *value,char *error)
{
if(key==NULL)
{
printf("Invalid parameters in request()");
return NULL;
printf("Invalid PARAM to get\n");
return -1;
}
struct message *requestMessage= malloc(sizeof(struct message));
requestMessage->status=status;
memcpy(requestMessage->key,key,256); //copied the complete key
struct message *request=malloc(sizeof(struct message));
request->status=1;//status code for get
memcpy(request->key,key,256); //copied the complete key passed as parameter
explicit_bzero(request->value,256*sizeof(char));//null value to to the request->value field
struct message *reply=malloc(sizeof(struct message));
write(sockfd, request, sizeof(struct message)); //passing to server and waiting for response
read(sockfd,reply,sizeof(struct message));
int status=reply->status;
if(status==400)
{
memcpy(error,reply->key,256*sizeof(char)); //copying the error in the key field for an failed get
if(strlen(requestMessage->key)<256)
}
else
{
requestMessage->key[strlen(requestMessage->key)]='\0';
memcpy(value,reply->value,256*sizeof(char));
}
if(value!=NULL)
free(request);
free(reply);
return status; //return reply->key
}
int del(int sockfd,char *key,char *error)
{
if(key==NULL)
{
memcpy(requestMessage->value,value,256); //copied the complete value
printf("Invalid PARAM to del\n");
return -1;
}
if(strlen(requestMessage->value)<256) //to pad with \0
struct message *request=malloc(sizeof(struct message));
request->status=2;//status code for delete
memcpy(request->key,key,256); //copied the complete key passed as parameter
explicit_bzero(request->value,256*sizeof(char));//null value to to the request->value field
struct message *reply=malloc(sizeof(struct message));
write(sockfd, request, sizeof(struct message)); //passing to server and waiting for response to the del request
read(sockfd,reply,sizeof(struct message));
int status=(int)reply->status;
if(status==400)
{
requestMessage->value[strlen(requestMessage->value)]='\0';
memcpy(error,reply->key,256*sizeof(char)); //copying the error in the key field for an failed del
}
return requestMessage;
free(request);
free(reply);
return status; //return reply->key
}
void printMessage(struct message *requestMessage)
int put(int sockfd,char *key,char *value,char *error)
{
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
if(key==NULL)
{
printf("Invalid PARAM to del\n");
return -1;
}
struct message *request=malloc(sizeof(struct message));
request->status=3;//status code for delete
memcpy(request->key,key,256); //copied the complete key passed as parameter
memcpy(request->value,value,256); //copied the complete key passed as parameter
struct message *reply=malloc(sizeof(struct message));
write(sockfd, request, sizeof(struct message)); //passing to server and waiting for response to the del request
read(sockfd,reply,sizeof(struct message));
int status=reply->status;
if(status==400)
{
memcpy(error,reply->key,256*sizeof(char)); //copying the error in the key field for an failed del
}
else
{
printMessage(reply);
}
free(request);
free(reply);
return status; //return reply->key
}
......@@ -15,7 +15,7 @@ Reasons for error could be GET key not found, DEL key not found etc.
*/
struct message{
char status;
unsigned char status;
char key[256];
char value[256];
};
......@@ -140,7 +140,10 @@ void *worker(void *args) {
/* Parse the actual message from client */
struct message *requestMessage= malloc(sizeof(struct message));
int readlength=read(events[i].data.fd , requestMessage, sizeof(struct message));
printf("\n[Message Received from client]\n[[Status:%c][Key:%s][Value:%s]]",requestMessage->status,requestMessage->key,requestMessage->value);
printf("\n[Message Received from client]\n[[Status:%d][Key:%s][Value:%s]]",requestMessage->status,requestMessage->key,requestMessage->value);
requestMessage->status=200;
write(events[i].data.fd,requestMessage, sizeof(struct message));
fflush(stdout);
}
......
No preview for this file type
No preview for this file type
No preview for this file type
......@@ -97,6 +97,11 @@ void main()
message[i]='a';
}
message[sizeof(message)]='\0';
request('g',message,message);
request('2',message,message);
char c ='2';
int status=(int)c;
printf("%d",status);
//creating a message
}
\ No newline at end of file
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