Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
pa4
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Nilesh Jagdish
pa4
Commits
acf37be8
Commit
acf37be8
authored
Nov 10, 2020
by
Nilesh Jagdish
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Modified with locks
parent
c0422e9f
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
85 additions
and
55 deletions
+85
-55
Makefile
Makefile
+1
-1
cache.cpp
cache.cpp
+35
-15
client.c
client.c
+1
-2
persistentStorage.cpp
persistentStorage.cpp
+11
-12
server.cpp
server.cpp
+37
-25
No files found.
Makefile
View file @
acf37be8
...
@@ -25,4 +25,4 @@ server:all
...
@@ -25,4 +25,4 @@ server:all
./server_s
./server_s
client
:
client
:
./client_c 127.1.1.
2
20000
./client_c 127.1.1.
3
20000
cache.cpp
View file @
acf37be8
...
@@ -4,7 +4,7 @@
...
@@ -4,7 +4,7 @@
#include <unordered_map>
#include <unordered_map>
#include <string>
#include <string>
#include <cstdlib>
#include <cstdlib>
//
#include "cs_thread.h"
#include "cs_thread.h"
#include "cache.h"
#include "cache.h"
#include "storage.h"
#include "storage.h"
using
namespace
std
;
using
namespace
std
;
...
@@ -31,10 +31,11 @@ using namespace std;
...
@@ -31,10 +31,11 @@ using namespace std;
*
*
*/
*/
#define MAX_CACHE_SIZE
2
#define MAX_CACHE_SIZE
8
list
<
cacheNode
>
cache
;
list
<
cacheNode
>
cache
;
unordered_map
<
string
,
list
<
cacheNode
>
::
iterator
>
hashTable
;
unordered_map
<
string
,
list
<
cacheNode
>
::
iterator
>
hashTable
;
pthread_mutex_t
cLock
;
/*
/*
* Returns 1 if key-value pair is present in cache and 0 otherwise
* Returns 1 if key-value pair is present in cache and 0 otherwise
...
@@ -91,27 +92,35 @@ void insert(string key, string value) {
...
@@ -91,27 +92,35 @@ void insert(string key, string value) {
}
}
}
}
string
GET
(
string
key
)
{
string
GET
(
string
key
)
{
// printf("GET function\n");
// printf("GET function\n");
pthread_mutex_lock
(
&
cLock
);
string
ans
=
search
(
key
);
string
ans
=
search
(
key
);
pthread_mutex_unlock
(
&
cLock
);
/*need to add ravi code*/
/*need to add ravi code*/
if
(
ans
==
""
)
if
(
ans
==
""
)
{
{
// char *new_key=key.c_str();
// char * new_key = new char[key.size() + 1];
char
*
new_key
=
new
char
[
key
.
size
()
+
1
];
// copy(key.begin(), key.end(), new_key);
copy
(
key
.
begin
(),
key
.
end
(),
new_key
);
// new_key[key.size()] = '\0';
new_key
[
key
.
size
()]
=
'\0'
;
// string value = GETT(new_key);
string
value
=
GETT
(
new_key
);
// if(value[0] != (char)240) {
if
(
value
!=
"ERROR"
)
// // cLock
insert
(
key
,
value
);
// pthread_mutex_lock(&cLock);
return
value
;
// insert(key, value);
// pthread_mutex_unlock(&cLock);
// //uncLock
// }
// return value;
}
}
return
ans
;
return
ans
;
}
}
string
PUT
(
string
key
,
string
value
)
{
string
PUT
(
string
key
,
string
value
)
{
cout
<<
"PUT started"
<<
endl
;
pthread_mutex_lock
(
&
cLock
);
string
ans
=
search
(
key
);
string
ans
=
search
(
key
);
if
(
ans
==
""
)
{
if
(
ans
==
""
)
{
insert
(
key
,
value
);
insert
(
key
,
value
);
...
@@ -120,14 +129,18 @@ string PUT(string key, string value) {
...
@@ -120,14 +129,18 @@ string PUT(string key, string value) {
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
;
}
}
pthread_mutex_unlock
(
&
cLock
);
// uncLock
char
*
new_key
=
new
char
[
key
.
size
()
+
1
];
char
*
new_key
=
new
char
[
key
.
size
()
+
1
];
copy
(
key
.
begin
(),
key
.
end
(),
new_key
);
copy
(
key
.
begin
(),
key
.
end
(),
new_key
);
new_key
[
key
.
size
()]
=
'\0'
;
new_key
[
key
.
size
()]
=
'\0'
;
char
*
new_value
=
new
char
[
value
.
size
()
+
1
];
char
*
new_value
=
new
char
[
value
.
size
()
+
1
];
copy
(
value
.
begin
(),
value
.
end
(),
new_value
);
copy
(
value
.
begin
(),
value
.
end
(),
new_value
);
new_value
[
value
.
size
()]
=
'\0'
;
new_value
[
value
.
size
()]
=
'\0'
;
PUTT
(
new_key
,
new_value
);
// PUTT(new_key, new_value);
return
"SUCCESS"
;
string
s
(
1
,
(
char
)
200
);
string
response
=
s
+
"Put Completed Successfully"
;
return
response
;
}
}
/*
/*
...
@@ -135,17 +148,24 @@ string PUT(string key, string value) {
...
@@ -135,17 +148,24 @@ string PUT(string key, string value) {
*/
*/
string
DEL
(
string
key
)
{
string
DEL
(
string
key
)
{
/*Need to add ravi code*/
/*Need to add ravi code*/
//cLock
pthread_mutex_lock
(
&
cLock
);
string
ans
=
search
(
key
);
string
ans
=
search
(
key
);
if
(
ans
!=
""
)
{
if
(
ans
!=
""
)
{
unordered_map
<
string
,
list
<
cacheNode
>
::
iterator
>
::
iterator
it
=
hashTable
.
find
(
key
);
unordered_map
<
string
,
list
<
cacheNode
>
::
iterator
>
::
iterator
it
=
hashTable
.
find
(
key
);
list
<
cacheNode
>
::
iterator
cnode_it
=
it
->
second
;
list
<
cacheNode
>
::
iterator
cnode_it
=
it
->
second
;
cache
.
erase
(
cnode_it
);
cache
.
erase
(
cnode_it
);
hashTable
.
erase
(
key
);
hashTable
.
erase
(
key
);
pthread_mutex_unlock
(
&
cLock
);
return
"SUCCESS"
;
}
}
//uncLock
pthread_mutex_unlock
(
&
cLock
);
char
*
new_key
=
new
char
[
key
.
size
()
+
1
];
char
*
new_key
=
new
char
[
key
.
size
()
+
1
];
copy
(
key
.
begin
(),
key
.
end
(),
new_key
);
copy
(
key
.
begin
(),
key
.
end
(),
new_key
);
new_key
[
key
.
size
()]
=
'\0'
;
new_key
[
key
.
size
()]
=
'\0'
;
string
response
=
DELL
(
new_key
);
// string response=DELL(new_key);
string
response
=
"ERROR"
;
return
response
;
return
response
;
}
}
...
...
client.c
View file @
acf37be8
...
@@ -34,7 +34,7 @@ void sendToServer(int sockfd) {
...
@@ -34,7 +34,7 @@ void sendToServer(int sockfd) {
int
choice
;
int
choice
;
char
*
buffer
=
(
char
*
)
malloc
(
513
*
sizeof
(
char
));
char
*
buffer
=
(
char
*
)
malloc
(
513
*
sizeof
(
char
));
while
(
1
)
{
while
(
1
)
{
printf
(
"Enter choice:
:
"
);
printf
(
"Enter choice: "
);
scanf
(
"%d"
,
&
choice
);
scanf
(
"%d"
,
&
choice
);
char
key
[
100
],
value
[
100
];
char
key
[
100
],
value
[
100
];
switch
(
choice
)
{
switch
(
choice
)
{
...
@@ -61,7 +61,6 @@ void sendToServer(int sockfd) {
...
@@ -61,7 +61,6 @@ void sendToServer(int sockfd) {
// return NULL;
// return NULL;
break
;
break
;
}
}
// printf("buffer : %s\n", buffer);
// printf("buffer : %s\n", buffer);
write
(
sockfd
,
buffer
,
strlen
(
buffer
));
write
(
sockfd
,
buffer
,
strlen
(
buffer
));
char
recvBuffer
[
1000
];
char
recvBuffer
[
1000
];
...
...
persistentStorage.cpp
View file @
acf37be8
...
@@ -564,7 +564,8 @@ string GETT(char * key){
...
@@ -564,7 +564,8 @@ string GETT(char * key){
{
{
if
(
dh
==
NULL
)
if
(
dh
==
NULL
)
{
{
string
result
=
"ERROR"
;
string
s
(
1
,
(
char
)
240
);
string
result
=
s
+
"Get key not found"
;
printf
(
"The key is not present
\n
"
);
printf
(
"The key is not present
\n
"
);
// record[0]=240;
// record[0]=240;
// record[1]='\0';
// record[1]='\0';
...
@@ -689,15 +690,15 @@ struct n2 * checkfile(struct n2 *h2,char *s){
...
@@ -689,15 +690,15 @@ struct n2 * checkfile(struct n2 *h2,char *s){
}
}
string
DELL
(
char
*
key
){
string
DELL
(
char
*
key
){
h1
=
del
(
h1
,
key
);
h1
=
del
(
h1
,
key
);
// write(STDOUT_FILENO,"came out of del \n",17);
// write(STDOUT_FILENO,"came out of del \n",17);
// printf("key = %s\n", key);
// printf("key = %s\n", key);
if
(
finding
==
NULL
){
if
(
finding
==
NULL
){
printf
(
"could not delete as key does not exist
\n
"
);
printf
(
"could not delete as key does not exist
\n
"
);
return
"ERROR"
;
string
s
(
1
,
(
char
)
240
);
string
response
=
s
+
"Delete key not found"
;
return
response
;
}
}
// write(STDOUT_FILENO,"the finding is not null \n",22);
// write(STDOUT_FILENO,"the finding is not null \n",22);
char
filename2
[
10
];
char
filename2
[
10
];
...
@@ -719,7 +720,9 @@ string DELL(char *key){
...
@@ -719,7 +720,9 @@ string DELL(char *key){
// write(STDOUT_FILENO,"\n",1);
// write(STDOUT_FILENO,"\n",1);
if
(
tp
==
NULL
){
if
(
tp
==
NULL
){
printf
(
"the checkfile has given NULL"
);
printf
(
"the checkfile has given NULL"
);
return
"ERROR"
;
string
s
(
1
,
(
char
)
240
);
string
response
=
s
+
"Delete Failed"
;
return
response
;
}
}
// write(STDOUT_FILENO,"test \n",6);
// write(STDOUT_FILENO,"test \n",6);
tp
->
nooffreeline
=
tp
->
nooffreeline
-
1
;
tp
->
nooffreeline
=
tp
->
nooffreeline
-
1
;
...
@@ -727,15 +730,11 @@ string DELL(char *key){
...
@@ -727,15 +730,11 @@ string DELL(char *key){
free
(
finding
);
free
(
finding
);
finding
=
NULL
;
finding
=
NULL
;
// printf("del function completed\n");
// printf("del function completed\n");
return
"SUCCESS"
;
string
s
(
1
,
(
char
)
200
);
string
response
=
s
+
"Delete Successful"
;
return
response
;
}
}
void
pp
(
struct
n1
*
r
){
void
pp
(
struct
n1
*
r
){
if
(
r
==
NULL
){
if
(
r
==
NULL
){
return
;
return
;
...
...
server.cpp
View file @
acf37be8
...
@@ -8,8 +8,10 @@
...
@@ -8,8 +8,10 @@
#include <pthread.h>
#include <pthread.h>
#include <stdlib.h>
#include <stdlib.h>
#include "cache.h"
#include "cache.h"
#include "cs_thread.h"
using
namespace
std
;
//
int search(char *p)
;
//
struct lock cLock
;
pthread_mutex_t
lock
;
pthread_mutex_t
lock
;
void
initVals
(
char
*
ip
,
char
*
portNo
,
int
*
nThreads
)
{
void
initVals
(
char
*
ip
,
char
*
portNo
,
int
*
nThreads
)
{
...
@@ -20,6 +22,7 @@ void initVals(char *ip, char *portNo, int *nThreads) {
...
@@ -20,6 +22,7 @@ void initVals(char *ip, char *portNo, int *nThreads) {
void
*
respondToClient
(
void
*
args
)
{
void
*
respondToClient
(
void
*
args
)
{
int
clientFd
=
*
((
int
*
)
args
);
int
clientFd
=
*
((
int
*
)
args
);
cout
<<
"Client fd : "
<<
clientFd
<<
endl
;
while
(
1
)
{
while
(
1
)
{
// printf("reading for client %d\n", clientFd);
// printf("reading for client %d\n", clientFd);
// char buffer[513];
// char buffer[513];
...
@@ -27,34 +30,36 @@ void *respondToClient(void *args) {
...
@@ -27,34 +30,36 @@ void *respondToClient(void *args) {
int
len
=
read
(
clientFd
,
buffer
,
513
);
int
len
=
read
(
clientFd
,
buffer
,
513
);
buffer
[
len
]
=
'\0'
;
buffer
[
len
]
=
'\0'
;
char
key
[
100
],
value
[
100
],
converted_output
[
100
];
char
key
[
257
],
value
[
257
],
converted_output
[
257
];
string
output
;
string
output
;
memset
(
key
,
'\0'
,
100
);
memset
(
key
,
'\0'
,
257
);
memset
(
value
,
'\0'
,
100
);
memset
(
value
,
'\0'
,
257
);
char
todo
=
buffer
[
0
];
char
todo
=
buffer
[
0
];
// key: first non-zero till byte 257
// key: first non-zero till byte 257
int
i
,
j
;
int
i
,
j
;
if
(
todo
!=
'4'
)
{
if
(
todo
!=
'4'
)
{
for
(
i
=
1
;
buffer
[
i
]
==
'0'
;
i
++
);
for
(
i
=
1
;
buffer
[
i
]
==
'0'
;
i
++
);
// printf("I: %d %c\n", i, buffer[i]);
// printf("I: %d %c\n", i, buffer[i]);
printf
(
"key length: %d
\n
"
,
257
-
i
);
// printf("key length: %d\n", 257 -
i);
memcpy
(
key
,
buffer
+
i
,
257
-
i
);
memcpy
(
key
,
buffer
+
i
,
257
-
i
);
// value: first non-zero till byte 513
// value: first non-zero till byte 513
for
(
i
=
257
;
buffer
[
i
]
==
'0'
;
i
++
);
for
(
i
=
257
;
buffer
[
i
]
==
'0'
;
i
++
);
printf
(
"value length: %d
\n
"
,
513
-
i
);
// printf("value length: %d\n", 513 -
i);
memcpy
(
value
,
buffer
+
i
,
513
-
i
);
memcpy
(
value
,
buffer
+
i
,
513
-
i
);
}
}
string
key_string
,
value_string
;
string
key_string
,
value_string
;
switch
(
todo
)
{
switch
(
todo
)
{
case
'1'
:
case
'1'
:
printf
(
"GET recvd
\n
"
);
printf
(
"GET recvd
\n
"
);
printf
(
"Key: %s
\n
"
,
key
);
//
printf("Key: %s\n", key);
// j = search(key);
// j = search(key);
// printf("j=%d\n", j);
// printf("j=%d\n", j);
key_string
=
key
;
key_string
=
key
;
output
=
GET
(
key_string
);
// pthread_mutex_lock(&cLock);
output
=
GET
(
key_string
);
// pthread_mutex_unlock(&cLock);
// 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
());
...
@@ -63,20 +68,24 @@ void *respondToClient(void *args) {
...
@@ -63,20 +68,24 @@ void *respondToClient(void *args) {
break
;
break
;
case
'2'
:
case
'2'
:
printf
(
"PUT recvd
\n
"
);
printf
(
"PUT recvd
\n
"
);
printf
(
"Key: %s
\n
"
,
key
);
// printf("Key: %s\n", key);
printf
(
"Value: %s
\n
"
,
value
);
// printf("Value: %s\n", value);
key_string
=
key
;
key_string
=
key
;
value_string
=
value
;
value_string
=
value
;
output
=
PUT
(
key_string
,
value_string
);
// pthread_mutex_lock(&cLock);
output
=
PUT
(
key_string
,
value_string
);
// pthread_mutex_unlock(&cLock);
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'
:
printf
(
"DELETE recvd
\n
"
);
printf
(
"DELETE recvd
\n
"
);
printf
(
"Key: %s
\n
"
,
key
);
// printf("Key: %s\n", key);
key_string
=
key
;
key_string
=
key
;
output
=
DEL
(
key_string
);
// pthread_mutex_lock(&cLock);
output
=
DEL
(
key_string
);
// pthread_mutex_unlock(&cLock);
// 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);
...
@@ -101,6 +110,7 @@ void *respondToClient(void *args) {
...
@@ -101,6 +110,7 @@ void *respondToClient(void *args) {
int
acceptConnections
(
char
*
addr
,
char
*
portNo
,
int
nThreads
)
{
int
acceptConnections
(
char
*
addr
,
char
*
portNo
,
int
nThreads
)
{
int
sockfd
=
socket
(
AF_INET
,
SOCK_STREAM
,
0
);
int
sockfd
=
socket
(
AF_INET
,
SOCK_STREAM
,
0
);
pthread_t
clientThreads
[
nThreads
];
pthread_t
clientThreads
[
nThreads
];
int
clientNo
=
0
;
int
clientNo
=
0
;
struct
addrinfo
hints
,
*
result
;
struct
addrinfo
hints
,
*
result
;
...
@@ -129,16 +139,18 @@ int acceptConnections(char *addr, char *portNo, int nThreads) {
...
@@ -129,16 +139,18 @@ int acceptConnections(char *addr, char *portNo, int nThreads) {
printf
(
"Waiting for connection...
\n
"
);
printf
(
"Waiting for connection...
\n
"
);
while
(
1
)
{
while
(
1
)
{
pthread_mutex_lock
(
&
lock
);
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
);
pthread_mutex_lock
(
&
lock
);
//
pthread_mutex_lock(&lock);
clientNo
++
;
clientNo
++
;
pthread_mutex_unlock
(
&
lock
);
//
pthread_mutex_unlock(&lock);
// int fid = fork();
// int fid = fork();
// if(fid==0) {
// 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
);
pthread_mutex_unlock
(
&
lock
);
// } else {
// } else {
// manage threads
// manage threads
...
@@ -159,6 +171,6 @@ int main(int argc, char **argv) {
...
@@ -159,6 +171,6 @@ int main(int argc, char **argv) {
printf
(
"portno: %s
\n
"
,
portNo
);
printf
(
"portno: %s
\n
"
,
portNo
);
printf
(
"no of threads: %d
\n
"
,
nThreads
);
printf
(
"no of threads: %d
\n
"
,
nThreads
);
acceptConnections
(
ip
,
portNo
,
nThreads
);
acceptConnections
(
ip
,
portNo
,
nThreads
);
// lock_init(&cLock);
return
0
;
return
0
;
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment