Commit 7395ad80 authored by RAVIKANT KUMAR YADAV's avatar RAVIKANT KUMAR YADAV

ravi

parent 264b5d6b
,ravi,ravi,23.11.2019 15:45,file:///home/ravi/.config/libreoffice/4;
\ No newline at end of file
PUT 110 value111_110 PUT 1 1
PUT 12 value12_12 GET 1
PUT 12 value12_12
PUT 12 value12_12
PUT 12 value12_12
PUT 12 value12_12
PUT 12 value12_12
PUT 12 value12_12
PUT 1 value11_11_11
PUT 12 value12_12
PUT 13 value13_13
PUT 14 value14_14
GET 11
GET 12
DEL 12
DEL 11
GET 11
GET 13
PUT 110 value111_110
PUT 12 value12_12
PUT 12 value12_12
PUT 12 value12_12
PUT 12 value12_12
PUT 12 value12_12
PUT 12 value12_12
PUT 12 value12_12
PUT 12 value12_12
PUT 1 value11_11_11
PUT 12 value12_12
PUT 13 value13_13
PUT 14 value14_14
GET 11
GET 12
DEL 12
DEL 11
GET 11
GET 13
PUT 110 value111_110
PUT 12 value12_12
PUT 12 value12_12
PUT 12 value12_12
PUT 12 value12_12
PUT 12 value12_12
PUT 12 value12_12
PUT 12 value12_12
Success
1 111111
Success
1 111111
Does not exist
Success
Success
2 222
Success
3 333
2 222
Success
Does not exist
Success
1 1
Success
Does not exist
Success
1 1
Success
1 1
Does not exist
Success
Success
2 222
Success
3 333
Does not exist
Success
Does not exist
Success
1 1
Success
1 1
Does not exist
Success
Success
2 222
Success
3 333
2 222
Success
3 333
Success
1 1
Success
1 1
Does not exist
Success
Success
2 222
Success
3 333
2 222
Success
3 333
Success
Success
1 1
Success
1 1
Success
1 1
Success
Does not exist
Success
1 1
Success
Does not exist
Success
1 1
Success
Does not exist
Success
1 1
Success
Does not exist
Success
1 1
Success
1 1
Success
1 1
Success
1 1
KVStore: KVStore:
Team Members:
1. 193050053 - Ravikant Yadav
2. 193050084 - Aman Jangde
Steps to execute: Steps to execute:
1. Run make command in parent directory. 1. Run make command in parent directory.
2. Go in build directory to run the executables. 2. Go in build directory to run the executables.
a. Run server as : ./server <port number> <number of sets> <n-way in set associative cache> a. Run server as: 1st way: "./server" ("It will take default values")
b. Run client as : ./client <server_ip> <port number> <Input Request fIle> <Output Response file> 2nd way: "./server" <port_number> <number_of_sets> <n-way_in_set_associative_cache> <num_of_threads>
b. Run client as: 1st way: Batch Mode: "./client" ("It will take default values")
2nd way: Interactive Mode: "./client" <server_ip> <port number>
3rd way: Batch Mode: "./client" <server_ip> <port number> <Input Request fIle> <Output Response file>
Note: For step 2 , above given Sequence of command line parameters must be followed. Note: For step 2 , above given Sequence of command line parameters must be followed.
Directory Structure: Directory Structure:
1. /build: Contains all the excecutable files. 1. /build: Contains all the excecutable files.
2. /ClientData: Contains default request file and generated response file. 2. /ClientData: Contains default request file and generated response file.
3. /include: Contains header files. 3. /include: Contains header files.
4. /ServerData: Contains the multiple generated csv file each for a unique set ID. 4. /ServerData: Contains the multiple generated csv file each for a unique set ID.
StoreXML file when generated by toXML method in KVCache and CacheXML file when generated by dumpToFile method in KVStore. StoreXML file when generated by toXML method in KVCache and CacheXML file when generated by dumpToFile method in KVStore.
5. /src: Contains all the source file. 5. /src: Contains all the source file(Runner files for debugging).
Steps to generate XML files: Steps to generate XML files:
1. StoreXML : At any point of time, after server serves request of client. 1. StoreXML.xml : About the file: This file collects all the data in xml format from different csv files (each for a unique set id).
Run the executable ./kvstore in parent directory
Steps: a. At any point of time, after server serves request of client.
b. Run the executable "./kvstore" in parent directory
2. CacheXML.xml : About the file: This file collects data of each memory cell in cache in xml format.
Steps: a. In "/src/server.c": define this macro "#define PRINTCACHE 1", since "toXML" function is defined in the
"#ifdef" body. By default it is generating CacheXML file (MACRO is already defined).
b. It will genrate the xml at termination of the server i.e cache values after all the clients are served.
Refrences:
1. https://www.geeksforgeeks.org/multithreading-in-cpp/
2. https://www.geeksforgeeks.org/socket-programming-cc/
3. https://www.geeksforgeeks.org/tcp-server-client-implementation-in-c/
https://aticleworld.com/parse-xml-response-in-c/
https://www.ibm.com/support/knowledgecenter/ssw_ibm_i_71/rzab6/poll.htm
...@@ -35,9 +35,17 @@ ...@@ -35,9 +35,17 @@
</CacheEntry> </CacheEntry>
</Set> </Set>
<Set Id="1"> <Set Id="1">
<CacheEntry isReferenced="​​false"isValid="​true"> <CacheEntry isReferenced="​​false"isValid="​false">
<Key>1</Key> <Key>1</Key>
<Value>value11_11_11</Value> <Value>1</Value>
</CacheEntry>
<CacheEntry isReferenced="​​false"isValid="​true">
<Key>2</Key>
<Value>2</Value>
</CacheEntry>
<CacheEntry isReferenced="​​false"isValid="​true">
<Key>3</Key>
<Value>3</Value>
</CacheEntry> </CacheEntry>
<CacheEntry isReferenced="​​false"isValid="​false"> <CacheEntry isReferenced="​​false"isValid="​false">
<Key></Key> <Key></Key>
...@@ -59,6 +67,8 @@ ...@@ -59,6 +67,8 @@
<Key></Key> <Key></Key>
<Value></Value> <Value></Value>
</CacheEntry> </CacheEntry>
</Set>
<Set Id="2">
<CacheEntry isReferenced="​​false"isValid="​false"> <CacheEntry isReferenced="​​false"isValid="​false">
<Key></Key> <Key></Key>
<Value></Value> <Value></Value>
...@@ -67,23 +77,13 @@ ...@@ -67,23 +77,13 @@
<Key></Key> <Key></Key>
<Value></Value> <Value></Value>
</CacheEntry> </CacheEntry>
</Set>
<Set Id="2">
<CacheEntry isReferenced="​​false"isValid="​false"> <CacheEntry isReferenced="​​false"isValid="​false">
<Key>12</Key> <Key></Key>
<Value>value12_12</Value> <Value></Value>
</CacheEntry>
<CacheEntry isReferenced="​​false"isValid="​true">
<Key>13</Key>
<Value>value13_13</Value>
</CacheEntry>
<CacheEntry isReferenced="​​false"isValid="​true">
<Key>14</Key>
<Value>value14_14</Value>
</CacheEntry> </CacheEntry>
<CacheEntry isReferenced="​​true"isValid="​true"> <CacheEntry isReferenced="​​false"isValid="​false">
<Key>12</Key> <Key></Key>
<Value>value12_12</Value> <Value></Value>
</CacheEntry> </CacheEntry>
<CacheEntry isReferenced="​​false"isValid="​false"> <CacheEntry isReferenced="​​false"isValid="​false">
<Key></Key> <Key></Key>
...@@ -103,9 +103,9 @@ ...@@ -103,9 +103,9 @@
</CacheEntry> </CacheEntry>
</Set> </Set>
<Set Id="3"> <Set Id="3">
<CacheEntry isReferenced="​​false"isValid="​true"> <CacheEntry isReferenced="​​false"isValid="​false">
<Key>110</Key> <Key></Key>
<Value>value111_110</Value> <Value></Value>
</CacheEntry> </CacheEntry>
<CacheEntry isReferenced="​​false"isValid="​false"> <CacheEntry isReferenced="​​false"isValid="​false">
<Key></Key> <Key></Key>
......
1,value11_11_11 2,2
3,3
4,(null)
5,5
6,6
7,7
8,8
9,9
12,value12_12
13,value13_13
14,value14_14
No preview for this file type
No preview for this file type
No preview for this file type
...@@ -160,6 +160,7 @@ char * getData( char key[]){ ...@@ -160,6 +160,7 @@ char * getData( char key[]){
kvc[set][i].chance = 1; kvc[set][i].chance = 1;
kvc[set][i].isReferenced = 1; kvc[set][i].isReferenced = 1;
pthread_mutex_unlock(&(setLock[set].work_mutex)); pthread_mutex_unlock(&(setLock[set].work_mutex));
// printf("GOT IT");
return kvc[set][i].value; return kvc[set][i].value;
...@@ -167,6 +168,7 @@ char * getData( char key[]){ ...@@ -167,6 +168,7 @@ char * getData( char key[]){
} }
// printf("\ncache miss"); // printf("\ncache miss");
char* value = restoreFromFile(key); char* value = restoreFromFile(key);
// printf("yes\n");
if(value) if(value)
{ {
findSlot(set , key , value); findSlot(set , key , value);
......
#include <iostream>
#include "headers.h"
#include "port.h"
/* generate a port number to run on */
void SocketAndPort::specifyPortServer(){
/* generating a port number between 1024 and 65535 */
srand(time(0));
portNoServer = rand() % 65536;
if(portNoServer < 1024)
portNoServer += 1024;
socklen_t len = sizeof(current);
sock = socket(AF_INET,SOCK_DGRAM,0);
current.sin_family = AF_INET;
current.sin_port = htons(portNoServer);
current.sin_addr.s_addr = inet_addr("127.0.0.1");
if( bind(sock,(struct sockaddr *)&current,len) < 0){
perror("error");
exit(-1);
}
}
/* change Port Number */
void SocketAndPort::changePortNumber(int newPortNumber){
if(newPortNumber < 1024 || newPortNumber > 65535){
cout<<"Please enter a valid port number\n";
}
else{
if( portInUse(newPortNumber) ){
cout<<"Sorry but port number is already in use\n";
}
else{
close(sock);
socklen_t len = sizeof(current);
sock = socket(AF_INET,SOCK_DGRAM,0);
current.sin_port = htons(newPortNumber);
if( bind(sock,(struct sockaddr *)&current,len) < 0){
perror("error");
current.sin_port = htons(portNoServer);
}
else{
portNoServer = newPortNumber;
cout<<"Port number changed to : "<<portNoServer<<endl;
}
}
}
}
/* check if a port number is already in use */
bool SocketAndPort::portInUse(int portNo){
int newSock = socket(AF_INET,SOCK_DGRAM,0);
struct sockaddr_in newCurr;
socklen_t len = sizeof(newCurr);
newCurr.sin_port = htons(portNo);
newCurr.sin_family = AF_INET;
newCurr.sin_addr.s_addr = inet_addr("127.0.0.1");
if( bind(newSock,(struct sockaddr *)&newCurr,len) < 0){
perror("error");
return true;
}
else{
close(newSock);
return false;
}
}
/* get IP Address */
string SocketAndPort::getIpAddress(){
string ip = inet_ntoa(current.sin_addr);
return ip;
}
/* get port number on which it is listening */
int SocketAndPort::getPortNumber(){
return portNoServer;
}
/* */
int SocketAndPort::getSocketFd(){
return sock;
}
/* close socket */
void SocketAndPort::closeSocket(){
close(sock);
}
\ No newline at end of file
...@@ -82,13 +82,19 @@ void serveRequest(char buffer[], int *rc, int *fd) ...@@ -82,13 +82,19 @@ void serveRequest(char buffer[], int *rc, int *fd)
} }
response = GenerateXML("", "", msg, msgType); response = GenerateXML("", "", msg, msgType);
} }
else
{
strcpy(msgType, "resp");
strcpy(msg, "XML Error: Received unparseable message");
response = GenerateXML("", "", msg, msgType);
}
} }
response[strlen(response)] = '\0'; response[strlen(response)] = '\0';
printf("ThreadID : %ld\n\n", pthread_self()); // printf("ThreadID : %ld\n\n", pthread_self());
*rc = send(*fd, response, strlen(response), 0); *rc = send(*fd, response, strlen(response), 0);
} }
\ No newline at end of file
...@@ -10,15 +10,18 @@ ...@@ -10,15 +10,18 @@
#include <sys/time.h> #include <sys/time.h>
#include <unistd.h> #include <unistd.h>
#include "xmlParser.h" #include "xmlParser.h"
#include <arpa/inet.h>
#define MAXKEYSIZE 260
#define MAXVALUESIZE 262150
#define MAXBUFSIZE 264000
#define MAXBUF 300000 char *toXML(char buffer[])
{
char *toXML(char buffer[]){
char delim[] = " "; char delim[] = " ";
char msgType[10] = {0}; char msgType[10] = {0};
char key[260] = {0}; char key[MAXKEYSIZE] = {0};
char value[256*1024 + 10] = {0}; char value[MAXVALUESIZE] = {0};
char *ptr = strtok(buffer, delim); char *ptr = strtok(buffer, delim);
...@@ -36,7 +39,7 @@ char *toXML(char buffer[]){ ...@@ -36,7 +39,7 @@ char *toXML(char buffer[]){
} }
else else
{ {
printf("error in parsing"); // printf("INvalid");
// TODO specify error types // TODO specify error types
} }
...@@ -59,8 +62,8 @@ char *toXML(char buffer[]){ ...@@ -59,8 +62,8 @@ char *toXML(char buffer[]){
//WRITING RESPONSE IN THE FILE //WRITING RESPONSE IN THE FILE
void bufferDump(char resp[], FILE *optr) void bufferDump(char resp[], FILE *optr)
{ {
char buffer[300000]=""; char buffer[MAXBUFSIZE]="";
char key[256]={0}, value[256*1024]={0}, msg[50]={0}, msgType[10]={0}; char key[MAXKEYSIZE]={0}, value[MAXVALUESIZE]={0}, msg[50]={0}, msgType[10]={0};
ParseXML(msg, msgType, key, value, resp); ParseXML(msg, msgType, key, value, resp);
// strcat(buffer, msgType); // strcat(buffer, msgType);
...@@ -77,9 +80,7 @@ void bufferDump(char resp[], FILE *optr) ...@@ -77,9 +80,7 @@ void bufferDump(char resp[], FILE *optr)
strcat(buffer, msg); strcat(buffer, msg);
} }
//strcat(buffer, "\0"); printf("RESPONSE:\t%s\n",buffer);
printf("%s\n",buffer);
fprintf(optr,"%s\n", buffer); fprintf(optr,"%s\n", buffer);
} }
...@@ -87,9 +88,10 @@ void bufferDump(char resp[], FILE *optr) ...@@ -87,9 +88,10 @@ void bufferDump(char resp[], FILE *optr)
void main(int argc, char *argv[]) void main(int argc, char *argv[])
{ {
char msg[MAXBUF]; char msg[50];
struct sockaddr_in addr = {0}; struct sockaddr_in addr = {0};
int n, sockfd,num=1; int n, sockfd,num=1;
char resp[MAXBUFSIZE];
// srandom(getpid()); // srandom(getpid());
/* Create socket and connect to server */ /* Create socket and connect to server */
sockfd = socket(AF_INET, SOCK_STREAM, 0); sockfd = socket(AF_INET, SOCK_STREAM, 0);
...@@ -104,18 +106,28 @@ void main(int argc, char *argv[]) ...@@ -104,18 +106,28 @@ void main(int argc, char *argv[])
char server_ip[20] = "127.0.0.1"; char server_ip[20] = "127.0.0.1";
char inputfile[100] = "ClientData/batchRequest.txt"; char inputfile[100] = "ClientData/batchRequest.txt";
char outputfile[100] = "ClientData/batchResponse.txt"; char outputfile[100] = "ClientData/batchResponse.txt";
char buffer[MAXBUFSIZE];
// printf("%d\n", argc);
if(argc > 1)
{
if(argc == 3)
{
if(argc >= 2)
strcpy(server_ip, argv[1]); strcpy(server_ip, argv[1]);
if(argc >= 3) // if(argc >= 3)
port = atoi(argv[2]); port = atoi(argv[2]);
if(argc >= 4) // printf("yess\n");
}
else if(argc == 5)
{
// if(argc >= 4)
strcpy(inputfile, argv[3]); strcpy(inputfile, argv[3]);
if(argc >= 5) // if(argc >= 5)
strcpy(outputfile, argv[4]); strcpy(outputfile, argv[4]);
}
}
// printf("server ip %s\n\n" , server_ip);
addr.sin_family = AF_INET; addr.sin_family = AF_INET;
addr.sin_port = htons(port); addr.sin_port = htons(port);
...@@ -128,11 +140,12 @@ void main(int argc, char *argv[]) ...@@ -128,11 +140,12 @@ void main(int argc, char *argv[])
} }
printf("child {%d} connected \n", getpid()); printf("child {%d} connected \n", getpid());
FILE *iptr;
FILE *optr;
if(argc != 3)
{
FILE *iptr;
FILE *optr;
if ((iptr = fopen(inputfile, "r")) == NULL) if ((iptr = fopen(inputfile, "r")) == NULL)
{ {
printf("Error! opening file"); printf("Error! opening file");
...@@ -146,31 +159,58 @@ void main(int argc, char *argv[]) ...@@ -146,31 +159,58 @@ void main(int argc, char *argv[])
// Program exits if file pointer returns NULL. // Program exits if file pointer returns NULL.
exit(1); exit(1);
} }
char buffer[300000];
while(fgets(buffer, 300000, iptr) != NULL){
while(fgets(buffer, MAXBUFSIZE, iptr) != NULL){
char *buf = strtok(buffer, "\n"); char *buf = strtok(buffer, "\n");
printf("SENDING: %ld bytes\n", strlen(buf)); printf("SENDING:\t%s \n", buf);
printf("===\n");
buf = toXML(buf); buf = toXML(buf);
// puts(buf); // puts(buf);
write(sockfd, buf, strlen(buf)); write(sockfd, buf, strlen(buf));
// sleep(2); // sleep(2);
char resp[300000];
int len = read(sockfd, resp, 300000);
resp[len] = '\0';
// puts(resp);
int len = read(sockfd, resp, MAXBUFSIZE);
resp[len] = '\0';
bufferDump(resp, optr); bufferDump(resp, optr);
printf("--------------------------------------\n");
// sleep(3); // sleep(3);
// fclose(optr);
} }
fclose(iptr); fclose(iptr);
fclose(optr); fclose(optr);
}
else
{
printf("Interactive mode: case sensitive command PUT, GET, DEL. Please enter \"exit\" to terminate\n");
while(1)
{
printf("IN:\t");
scanf("%[^\n]%*c", buffer);
if(strcmp(buffer , "exit"))
{
char *buf = buffer;
buf = toXML(buf);
write(sockfd, buf, strlen(buf));
int len = read(sockfd, resp, MAXBUFSIZE);
resp[len] = '\0';
char key[MAXKEYSIZE]={0}, value[MAXVALUESIZE]={0}, msg[50]={0}, msgType[10]={0};
ParseXML(msg, msgType, key, value, resp);
if(!strcmp(msg, ""))
{
printf("OUT:\tkey: %s\tvalue: %s\n",key,value);
}
else
{
printf("OUT:\t%s\n",msg);
}
}
else
{
exit(0);
}
}
}
} }
\ No newline at end of file
...@@ -17,12 +17,15 @@ ...@@ -17,12 +17,15 @@
#define MAXVALUESIZE 262150 #define MAXVALUESIZE 262150
#define MAXBUFSIZE 264000 #define MAXBUFSIZE 264000
//#ifndef
#define CACHEPRINT 1 /* Coment this macro to stop generating CacheXML file */
//ss#endif #define PRINTCACHE 1
#define TRUE 1 #define TRUE 1
#define FALSE 0 #define FALSE 0
size_t num_threads = 4; size_t num_threads = 4;
...@@ -108,11 +111,11 @@ void main (int argc, char *argv[]) ...@@ -108,11 +111,11 @@ void main (int argc, char *argv[])
fds[0].fd = listen_sd; fds[0].fd = listen_sd;
fds[0].events = POLLIN; fds[0].events = POLLIN;
timeout = (30 * 1000); timeout = (300 * 100);
do do
{ {
printf("Waiting on poll()...\n"); printf("---------------------------------------\n");
rc = poll(fds, nfds, timeout); rc = poll(fds, nfds, timeout);
if (rc < 0) if (rc < 0)
{ {
...@@ -243,7 +246,7 @@ void main (int argc, char *argv[]) ...@@ -243,7 +246,7 @@ void main (int argc, char *argv[])
close(fds[i].fd); close(fds[i].fd);
} }
#ifdef CACHEPRINT #ifdef PRINTCACHE
toXML("ServerData/CacheXML.xml"); toXML("ServerData/CacheXML.xml");
#endif #endif
} }
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
void main() void main()
{ {
char key[100], value[100], msg[100], msgType[100]; char key[260], value[263000], msg[100], msgType[100];
char *resp = GenerateXML("", "", "thisismsg", "thisismsgtype"); char *resp = GenerateXML("", "", "thisismsg", "thisismsgtype");
printf("%s\n", resp); printf("%s\n", resp);
......
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