Commit c0422e9f authored by Saikumar's avatar Saikumar

modified files on 09112020

parent 1f578b03
......@@ -4,7 +4,7 @@ all: clean
$(CC) $(CFLAGS) -o client_c client.c
# g++ server.c cache.cpp -o server_s -lpthread
# 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++ -o server_s server.o cache.o -lpthread
......
......@@ -6,6 +6,7 @@
#include <cstdlib>
// #include "cs_thread.h"
#include "cache.h"
#include "storage.h"
using namespace std;
/*
* The idea behind cache implementation :
......@@ -30,7 +31,7 @@ using namespace std;
*
*/
#define MAX_CACHE_SIZE 3
#define MAX_CACHE_SIZE 2
list<cacheNode> cache;
unordered_map<string, list <cacheNode> :: iterator> hashTable;
......@@ -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) {
// printf("GET function\n");
string ans = search(key);
/*need to add ravi code*/
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;
}
......@@ -116,13 +115,38 @@ string PUT(string key, string value) {
string ans = search(key);
if(ans == "") {
insert(key, value);
/*Need to add ravi code */
}
else {
unordered_map<string, list <cacheNode> :: iterator> :: iterator it = hashTable.find(key);
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;
}
/*
......
This diff is collapsed.
......@@ -10,6 +10,7 @@
#include "cache.h"
// int search(char *p);
pthread_mutex_t lock;
void initVals(char *ip, char *portNo, int *nThreads) {
FILE *ptr = fopen("config.txt", "r");
......@@ -55,7 +56,7 @@ void *respondToClient(void *args) {
key_string=key;
output=GET(key_string);
// cout << key << key1 << endl;
cout << " GET returns : " << output << endl;
// cout << " GET returns : " << output << endl;
strcpy(converted_output, output.c_str());
// write(clientFd, "Get returns\0", 12);
write(clientFd, converted_output, output.length());
......@@ -68,7 +69,7 @@ void *respondToClient(void *args) {
value_string=value;
output=PUT(key_string, value_string);
strcpy(converted_output, output.c_str());
cout << " PUT returns : " << output << endl;
// cout << " PUT returns : " << output << endl;
write(clientFd, converted_output, output.length());
break;
case '3':
......@@ -76,7 +77,7 @@ void *respondToClient(void *args) {
printf("Key: %s\n", key);
key_string=key;
output=DEL(key_string);
cout << " DEL returns : " << output << endl;
// cout << " DEL returns : " << output << endl;
strcpy(converted_output, output.c_str());
// write(clientFd, "Delete returns\0", 15);
write(clientFd, converted_output, output.length());
......@@ -130,16 +131,18 @@ int acceptConnections(char *addr, char *portNo, int nThreads) {
while(1) {
int clientFd = accept(sockfd, NULL, NULL);
printf("Connected to client with fd: %d\n", clientFd);
clientNo++; // shared variables hence we need conditional variable
int fid = fork();
if(fid==0) {
pthread_mutex_lock(&lock);
clientNo++;
pthread_mutex_unlock(&lock);
// int fid = fork();
// if(fid==0) {
// create thread
int *cfd = &clientFd;
pthread_create(&clientThreads[clientNo], NULL, respondToClient, (void *)cfd);
int *cfd = &clientFd;
pthread_create(&clientThreads[clientNo], NULL, respondToClient, (void *)cfd);
} else {
// } else {
// manage threads
}
// }
}
......@@ -150,6 +153,7 @@ int main(int argc, char **argv) {
printf("Server is running...\n");
char ip[20], portNo[10];
int nThreads;
pthread_mutex_init(&lock, NULL);
initVals(ip, portNo, &nThreads);
printf("Ip address: %s\n", ip);
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