Commit c0422e9f authored by Saikumar's avatar Saikumar

modified files on 09112020

parent 1f578b03
...@@ -4,7 +4,7 @@ all: clean ...@@ -4,7 +4,7 @@ all: clean
$(CC) $(CFLAGS) -o client_c client.c $(CC) $(CFLAGS) -o client_c client.c
# g++ server.c cache.cpp -o server_s -lpthread # g++ server.c cache.cpp -o server_s -lpthread
# g++ -o cache cache.cpp # g++ -o cache cache.cpp
g++ -w -o server_s server.cpp cache.cpp -lpthread g++ -w -o server_s server.cpp cache.cpp persistentStorage.cpp -lpthread
# g++ -c -o cache.o cache.cpp # g++ -c -o cache.o cache.cpp
# g++ -o server_s server.o cache.o -lpthread # g++ -o server_s server.o cache.o -lpthread
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#include <cstdlib> #include <cstdlib>
// #include "cs_thread.h" // #include "cs_thread.h"
#include "cache.h" #include "cache.h"
#include "storage.h"
using namespace std; using namespace std;
/* /*
* The idea behind cache implementation : * The idea behind cache implementation :
...@@ -30,7 +31,7 @@ using namespace std; ...@@ -30,7 +31,7 @@ using namespace std;
* *
*/ */
#define MAX_CACHE_SIZE 3 #define MAX_CACHE_SIZE 2
list<cacheNode> cache; list<cacheNode> cache;
unordered_map<string, list <cacheNode> :: iterator> hashTable; unordered_map<string, list <cacheNode> :: iterator> hashTable;
...@@ -90,25 +91,23 @@ void insert(string key, string value) { ...@@ -90,25 +91,23 @@ void insert(string key, string value) {
} }
} }
/*
* Delete from cache
*/
string DEL(string key) {
// printf("DEL function\n");
unordered_map<string, list <cacheNode> :: iterator> :: iterator it = hashTable.find(key);
list<cacheNode> :: iterator cnode_it = it->second;
cache.erase(cnode_it);
hashTable.erase(key);
/*Need to add ravi code*/
return "200";
}
string GET(string key) { string GET(string key) {
// printf("GET function\n"); // printf("GET function\n");
string ans = search(key); string ans = search(key);
/*need to add ravi code*/ /*need to add ravi code*/
if(ans=="") if(ans=="")
return "200"; {
// char *new_key=key.c_str();
char * new_key = new char[key.size() + 1];
copy(key.begin(), key.end(), new_key);
new_key[key.size()] = '\0';
string value=GETT(new_key);
if(value!="ERROR")
insert(key, value);
return value;
}
return ans; return ans;
} }
...@@ -116,13 +115,38 @@ string PUT(string key, string value) { ...@@ -116,13 +115,38 @@ string PUT(string key, string value) {
string ans = search(key); string ans = search(key);
if(ans == "") { if(ans == "") {
insert(key, value); insert(key, value);
/*Need to add ravi code */
} }
else { else {
unordered_map<string, list <cacheNode> :: iterator> :: iterator it = hashTable.find(key); unordered_map<string, list <cacheNode> :: iterator> :: iterator it = hashTable.find(key);
it->second->value = value; it->second->value = value;
} }
return "200"; char * new_key = new char[key.size() + 1];
copy(key.begin(), key.end(), new_key);
new_key[key.size()] = '\0';
char * new_value = new char[value.size() + 1];
copy(value.begin(), value.end(), new_value);
new_value[value.size()] = '\0';
PUTT(new_key, new_value);
return "SUCCESS";
}
/*
* Delete from cache
*/
string DEL(string key) {
/*Need to add ravi code*/
string ans = search(key);
if(ans != "") {
unordered_map<string, list <cacheNode> :: iterator> :: iterator it = hashTable.find(key);
list<cacheNode> :: iterator cnode_it = it->second;
cache.erase(cnode_it);
hashTable.erase(key);
}
char * new_key = new char[key.size() + 1];
copy(key.begin(), key.end(), new_key);
new_key[key.size()] = '\0';
string response=DELL(new_key);
return response;
} }
/* /*
......
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <unistd.h>
#include <string.h>
// #include <cstdlib>
#include<fcntl.h>
#include "storage.h"
#include <iostream>
using namespace std;
struct n1
{
char key[257];
int lineno;
char filename[5];
struct n1 *p1;
struct n1 *p2;
int h;
};
struct n2
{
char filename[5];
int nooffreeline;
struct n2 *pointer;
};
struct n1 *h1=NULL;
struct n2 *h2=NULL;
unsigned int nooffiles=0;
struct n2 * check(struct n2 *h2){
while (1)
{
if(h2==NULL){
return NULL;
}
else{
if((h2->nooffreeline)>0){
return h2;}
else{
h2=h2->pointer;
}
}
}
}
int max(int a,int b){
if(a>b){
return a;
}
return b;
}
int hei(struct n1*r){
if(r==NULL){
return 0;
}
return (1+max(hei(r->p1),hei(r->p2)));
}
struct n1* ll(struct n1*r){
struct n1*a=r->p1;
r->p1=r->p1->p2;
a->p2=r;
r->h=max(hei(r->p1),hei(r->p2));
a->h=max(hei(a->p1),hei(a->p2));
return a;
}
struct n1* rr(struct n1*r ){
struct n1*a=r->p2;
r->p2=a->p1;
a->p1=r;
a->h=max(hei(a->p1),hei(a->p2));
r->h=max(hei(r->p1),hei(r->p2));
return a;
}
struct n1 * add;
char lineno=100;
struct n1 * insert(struct n1 *r,char * key){
int tt=0;
if(r==NULL){
r=(struct n1 *)malloc(sizeof(struct n1));
strcpy(r->key,key);
r->p1=NULL;
r->p2=NULL;
r->h=0;
add=r;
struct n2 *tt2=check(h2);
if(tt2==NULL){
tt2=(struct n2 *)malloc(sizeof(struct n2));
char filename2[10];
sprintf(filename2,"%u",(nooffiles=nooffiles+1));
strcpy(tt2->filename,filename2);
// printf(" for the first time storing the file name in n2 is %s\n",tt2->filename);
strcpy(r->filename,filename2);
// printf(" for the first time storing the file name in n1 is %s\n",r->filename);
tt2->nooffreeline=19;
tt2->pointer=h2;
h2=tt2;
FILE *fptr=fopen(strcat(filename2,".txt"),"w");
fprintf(fptr,"aaaaaaaaaaaaaaaaaaaa############################################################################################################################################################################################################################################\n");
fclose(fptr);
// printf(" for the first time storing the file name in n2 is %s\n",tt2->filename);
// printf(" for the first time storing the file name in n1 is %s\n",r->filename);
}
else{
tt2->nooffreeline-=1;
char dum[38];
// sprintf(dum,"the no of free lines left in file %02d",tt2->nooffreeline);
// write(STDOUT_FILENO,dum,36);
// write(STDOUT_FILENO,"\n",1);
strcpy(r->filename,tt2->filename);
}
return r;
}
else{
if((tt=strcmp(key,r->key))>0){//the key i want to add is alphabeticaly greter than the one that is added strcmp(myelement,pre element)
r->p2=insert(r->p2,key);
r->h=max(hei(r->p1),hei(r->p2));
int r2;
if(r->p1==NULL){
r2=1+r->p2->h;
}else{
r2=r->p2->h-r->p1->h;
}
if(r2==1||r2==-1||r2==0){
return r;
}
else{
if(r2<0){
int r3;
if(r->p1->p1==NULL){
r3=1+r->p1->p2->h;
}
else if(r->p1->p2==NULL){
r3=-1-r->p1->p1->h;
}else{
r3=r->p1->p2->h-r->p1->p1->h;
}
if(r3<0){
//left -left
return ll(r);
}
else{
//left-right
r->p1=rr(r->p1);
return ll(r);
}
}
else{
int r3;
if(r->p2->p1==NULL){
r3=1+r->p2->p2->h;
}
else if(r->p2->p2==NULL){
r3=-1-r->p2->p1->h;
}else{
r3=r->p2->p2->h-r->p2->p1->h;
}
if(r3<0){
//right-left
r->p2=ll(r->p2);
return rr(r);
}
else{
//right-right
return rr(r);
}
}
}
}
else if(tt<0){
r->p1=insert(r->p1,key);
r->h=max(hei(r->p1),hei(r->p2));
int r2;
if(r->p2==NULL){
r2=-1-(r->p1->h);
}
else{
r2=r->p2->h-(r->p1->h);
}
if(r2==1||r2==-1||r2==0){
return r;
}
else{
if(r2<0){
int r3;
if(r->p1->p1==NULL){
r3=1+r->p1->p2->h;
}
else if(r->p1->p2==NULL){
r3=-1-r->p1->p1->h;
}else{
r3=r->p1->p2->h-r->p1->p1->h;
}
if(r3<0){
//left -left
return ll(r);
}
else{
//left-right
r->p1=rr(r->p1);
return ll(r);
}
}
else{
int r3;
if(r->p2->p1==NULL){
r3=1+r->p2->p2->h;
}
else if(r->p2->p2==NULL){
r3=-1-r->p2->p1->h;
}else{
r3=r->p2->p2->h-r->p2->p1->h;
}
if(r3<0){
//right-left
r->p2=ll(r->p2);
return rr(r);
}
else{
//right-right
return rr(r);
}
}
}
}
else{
add=r;
lineno=r->lineno;
return r;
}
}
}
struct n1* finding=NULL;
struct n1* pre;
struct n1* fpre(struct n1 *r){
if(r->p2==NULL){
pre=r;
return r->p1;
}
r->p2=fpre(r->p2);
r->h=max(hei(r->p1),hei(r->p2));
int r2;
if(r->p1==NULL){
if(r->p2==NULL){
return r;
}
else{
r2=1+r->p2->h;
}
}
else{
if(r->p2==NULL){
r2=-1-r->p1->h;
}
else{
r2=r->p2->h-r->p1->h;
}
}
if(r2==1||r2==-1||r2==0){
return r;
}
else{
if(r2<0){
int r3;
if(r->p1->p1==NULL){
r3=1+r->p1->p2->h;
}
else if(r->p1->p2==NULL){
r3=-1-r->p1->p1->h;
}else{
r3=r->p1->p2->h-r->p1->p1->h;
}
if(r3<0){
//left -left
return ll(r);
}
else{
//left-right
r->p1=rr(r->p1);
return ll(r);
}
}
else{
int r3;
if(r->p2->p1==NULL){
r3=1+r->p2->p2->h;
}
else if(r->p2->p2==NULL){
r3=-1-r->p2->p1->h;
}else{
r3=r->p2->p2->h-r->p2->p1->h;
}
if(r3<0){
//right-left
r->p2=ll(r->p2);
return rr(r);
}
else{
//right-right
return rr(r);
}
}
}
return r;
}
struct n1* del(struct n1*r,char *key){
int tt;
if(r==NULL){
return NULL;
}
if((tt=strcmp(key,r->key))==0){//both are equal
if(r->p1==NULL&&r->p2==NULL){
finding=r;
return NULL;
}
if(r->p1==NULL){
finding=r;
return r->p2;
}
if(r->p2==NULL){
finding=r;
return r->p1;
}
finding=r;
fpre(r->p1);
pre->p1=r->p1;
pre->p2=r->p2;
return pre;
}
if(tt>0){//the key i want to add is alphabeticaly greter than the one that is added strcmp(myelement,pre element)
r->p2=del(r->p2,key);
r->h=max(hei(r->p1),hei(r->p2));
int r2;
if(r->p1==NULL){
if(r->p2==NULL){
return r;
}
else{
r2=1+r->p2->h;
}
}
else{
if(r->p2==NULL){
r2=-1-r->p1->h;
}
else{
r2=r->p2->h-r->p1->h;
}
}
if(r2==1||r2==-1||r2==0){
return r;
}
else{
if(r2<0){
int r3;
if(r->p1->p1==NULL){
r3=1+r->p1->p2->h;
}
else if(r->p1->p2==NULL){
r3=-1-r->p1->p1->h;
}else{
r3=r->p1->p2->h-r->p1->p1->h;
}
if(r3<0){
//left -left
return ll(r);
}
else{
//left-right
r->p1=rr(r->p1);
return ll(r);
}
}
else{
int r3;
if(r->p2->p1==NULL){
r3=1+r->p2->p2->h;
}
else if(r->p2->p2==NULL){
r3=-1-r->p2->p1->h;
}else{
r3=r->p2->p2->h-r->p2->p1->h;
}
if(r3<0){
//right-left
r->p2=ll(r->p2);
return rr(r);
}
else{
//right-right
return rr(r);
}
}
}
}
else{
r->p1=del(r->p1,key);
r->h=max(hei(r->p1),hei(r->p2));
int r2;
if(r->p1==NULL){
if(r->p2==NULL){
return r;
}
else{
r2=1+r->p2->h;
}
}
else{
if(r->p2==NULL){
r2=-1-r->p1->h;
}
else{
r2=r->p2->h-r->p1->h;
}
}
if(r2==1||r2==-1||r2==0){
return r;
}
else{
if(r2<0){
int r3;
if(r->p1->p1==NULL){
r3=1+r->p1->p2->h;
}
else if(r->p1->p2==NULL){
r3=-1-r->p1->p1->h;
}else{
r3=r->p1->p2->h-r->p1->p1->h;
}
if(r3<0){
//left -left
return ll(r);
}
else{
//left-right
r->p1=rr(r->p1);
return ll(r);
}
}
else{
int r3;
if(r->p2->p1==NULL){
r3=1+r->p2->p2->h;
}
else if(r->p2->p2==NULL){
r3=-1-r->p2->p1->h;
}else{
r3=r->p2->p2->h-r->p2->p1->h;
}
if(r3<0){
//right-left
r->p2=ll(r->p2);
return rr(r);
}
else{
//right-right
return rr(r);
}
}
}
}
}
string GETT(char * key){
int tt;
struct n1 *dh=h1;
while(1)
{
if(dh==NULL)
{
string result="ERROR";
printf("The key is not present\n");
// record[0]=240;
// record[1]='\0';
// char *pointer=record;
return result;
}
else
{
printf("%s = %s \n",key ,dh->key);
if((tt=strcmp(key,dh->key))==0)
{
char filename2[10];
strcpy(filename2,dh->filename);
FILE *f=fopen(strcat(filename2,".txt"),"r");
char record[257];
fseek(f,dh->lineno*257,SEEK_SET);
fgets(record,256,f);
fclose(f);
int sp=0;
while (record[sp]!='#')
{
sp=sp+1;
}
record[sp]='\0';
string result=record;
// char *recor=record;
// return recor;
return result;
}
else if(tt>0)
{
dh=dh->p2;
}
else{
dh=dh->p1;
}
}
}
}
void PUTT(char *key,char *value){
printf("key = %s\nvalue=%s",key, value);
h1=insert(h1,key);
char filename2[10];
strcpy(filename2,add->filename);
FILE *f=fopen(strcat(filename2,".txt"),"r");
char s[21];
int n=0;
fgets(s,21,f);
fclose(f);
// printf("%s\n",s);
if(lineno==100){
while(s[n]!='a'){
n=n+1;
}
int mm=n;
add->lineno=n+1;
char dum[24];
// sprintf(dum,"the free line no is %02d",mm+1);
// write(STDOUT_FILENO,dum,22);
// write(STDOUT_FILENO,"\n",1);
n=strlen(value);
char ts[257];
strcpy(ts,value);
while(n!=256){
ts[n]='#';
n+=1;
}
ts[256]='\n';
n=open(filename2,O_WRONLY);
lseek(n,mm,SEEK_SET);
write(n,"n",1);
lseek(n,(add->lineno)*257,SEEK_SET);
write(n,ts,257);
close(n);
}
else{
n=strlen(value);
char ts[257];
strcpy(ts,value);
while(n!=256){
ts[n]='#';
n+=1;
}
ts[256]='\n';
n=open(strcat(add->filename,".txt"),O_WRONLY);
lseek(n,(add->lineno)*257,SEEK_SET);
lineno=100;
write(n,ts,257);
close(n);
}
printf("PUTT completed\n");
}
struct n2 * checkfile(struct n2 *h2,char *s){
while (1)
{
if(h2==NULL){
// printf("return \n");
return NULL;
}
else{
// printf("%s = %s \n",h2->filename,s);
if(strcmp(h2->filename,s)==0){
// printf("if \n");
return h2;}
else{
h2=h2->pointer;
// printf("else \n");
}
}
}
}
string DELL(char *key){
h1=del(h1,key);
// write(STDOUT_FILENO,"came out of del \n",17);
// printf("key = %s\n", key);
if(finding==NULL){
printf("could not delete as key does not exist \n");
return "ERROR";
}
// write(STDOUT_FILENO,"the finding is not null \n",22);
char filename2[10];
strcpy(filename2,finding->filename);
int n=open(strcat(filename2,".txt"),O_WRONLY);
// write(STDOUT_FILENO,"test \n",6);
lseek(n,finding->lineno-1,SEEK_SET);
// write(STDOUT_FILENO,"test \n",6);
write(n,"a",1);
// write(STDOUT_FILENO,"test \n",6);
close(n);
// write(STDOUT_FILENO,"test \n",6);
// write(STDOUT_FILENO,"the file name i shoud be delted",31);
// write(STDOUT_FILENO,finding->filename,5);
// write(STDOUT_FILENO,"\n",1);
struct n2 *tp=checkfile(h2,finding->filename);
// write(STDOUT_FILENO,"the file name i shoud be delted",31);
// write(STDOUT_FILENO,finding->filename,1);
// write(STDOUT_FILENO,"\n",1);
if(tp==NULL){
printf("the checkfile has given NULL");
return "ERROR";
}
// write(STDOUT_FILENO,"test \n",6);
tp->nooffreeline=tp->nooffreeline-1;
// write(STDOUT_FILENO,"test \n",6);
free(finding);
finding=NULL;
// printf("del function completed\n");
return "SUCCESS";
}
void pp(struct n1*r){
if(r==NULL){
return;
}
pp(r->p1);
printf("%s\n",r->key);
pp(r->p2);
}
void pp2(struct n1*r){
if(r==NULL){
return;
}
printf("%s\n",r->key);
pp2(r->p1);
pp2(r->p2);
}
// int main()
// {
// printf("Saikumar\n");
// return 0;
// }
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#include "cache.h" #include "cache.h"
// int search(char *p); // int search(char *p);
pthread_mutex_t lock;
void initVals(char *ip, char *portNo, int *nThreads) { void initVals(char *ip, char *portNo, int *nThreads) {
FILE *ptr = fopen("config.txt", "r"); FILE *ptr = fopen("config.txt", "r");
...@@ -55,7 +56,7 @@ void *respondToClient(void *args) { ...@@ -55,7 +56,7 @@ void *respondToClient(void *args) {
key_string=key; key_string=key;
output=GET(key_string); output=GET(key_string);
// cout << key << key1 << endl; // cout << key << key1 << endl;
cout << " GET returns : " << output << endl; // cout << " GET returns : " << output << endl;
strcpy(converted_output, output.c_str()); strcpy(converted_output, output.c_str());
// write(clientFd, "Get returns\0", 12); // write(clientFd, "Get returns\0", 12);
write(clientFd, converted_output, output.length()); write(clientFd, converted_output, output.length());
...@@ -68,7 +69,7 @@ void *respondToClient(void *args) { ...@@ -68,7 +69,7 @@ void *respondToClient(void *args) {
value_string=value; value_string=value;
output=PUT(key_string, value_string); output=PUT(key_string, value_string);
strcpy(converted_output, output.c_str()); strcpy(converted_output, output.c_str());
cout << " PUT returns : " << output << endl; // cout << " PUT returns : " << output << endl;
write(clientFd, converted_output, output.length()); write(clientFd, converted_output, output.length());
break; break;
case '3': case '3':
...@@ -76,7 +77,7 @@ void *respondToClient(void *args) { ...@@ -76,7 +77,7 @@ void *respondToClient(void *args) {
printf("Key: %s\n", key); printf("Key: %s\n", key);
key_string=key; key_string=key;
output=DEL(key_string); output=DEL(key_string);
cout << " DEL returns : " << output << endl; // cout << " DEL returns : " << output << endl;
strcpy(converted_output, output.c_str()); strcpy(converted_output, output.c_str());
// write(clientFd, "Delete returns\0", 15); // write(clientFd, "Delete returns\0", 15);
write(clientFd, converted_output, output.length()); write(clientFd, converted_output, output.length());
...@@ -130,16 +131,18 @@ int acceptConnections(char *addr, char *portNo, int nThreads) { ...@@ -130,16 +131,18 @@ int acceptConnections(char *addr, char *portNo, int nThreads) {
while(1) { while(1) {
int clientFd = accept(sockfd, NULL, NULL); int clientFd = accept(sockfd, NULL, NULL);
printf("Connected to client with fd: %d\n", clientFd); printf("Connected to client with fd: %d\n", clientFd);
clientNo++; // shared variables hence we need conditional variable pthread_mutex_lock(&lock);
int fid = fork(); clientNo++;
if(fid==0) { pthread_mutex_unlock(&lock);
// int fid = fork();
// if(fid==0) {
// create thread // create thread
int *cfd = &clientFd; int *cfd = &clientFd;
pthread_create(&clientThreads[clientNo], NULL, respondToClient, (void *)cfd); pthread_create(&clientThreads[clientNo], NULL, respondToClient, (void *)cfd);
} else { // } else {
// manage threads // manage threads
} // }
} }
...@@ -150,6 +153,7 @@ int main(int argc, char **argv) { ...@@ -150,6 +153,7 @@ int main(int argc, char **argv) {
printf("Server is running...\n"); printf("Server is running...\n");
char ip[20], portNo[10]; char ip[20], portNo[10];
int nThreads; int nThreads;
pthread_mutex_init(&lock, NULL);
initVals(ip, portNo, &nThreads); initVals(ip, portNo, &nThreads);
printf("Ip address: %s\n", ip); printf("Ip address: %s\n", ip);
printf("portno: %s\n", portNo); printf("portno: %s\n", portNo);
......
#include <iostream>
using namespace std;
string GETT(char *);
void PUTT(char *, char *);
string DELL(char *);
\ 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