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
Hide 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
./server_s
client
:
./client_c 127.1.1.
2
20000
./client_c 127.1.1.
3
20000
cache.cpp
View file @
acf37be8
...
...
@@ -4,7 +4,7 @@
#include <unordered_map>
#include <string>
#include <cstdlib>
//
#include "cs_thread.h"
#include "cs_thread.h"
#include "cache.h"
#include "storage.h"
using
namespace
std
;
...
...
@@ -31,10 +31,11 @@ using namespace std;
*
*/
#define MAX_CACHE_SIZE
2
#define MAX_CACHE_SIZE
8
list
<
cacheNode
>
cache
;
unordered_map
<
string
,
list
<
cacheNode
>
::
iterator
>
hashTable
;
pthread_mutex_t
cLock
;
/*
* Returns 1 if key-value pair is present in cache and 0 otherwise
...
...
@@ -91,27 +92,35 @@ void insert(string key, string value) {
}
}
string
GET
(
string
key
)
{
// printf("GET function\n");
pthread_mutex_lock
(
&
cLock
);
string
ans
=
search
(
key
);
pthread_mutex_unlock
(
&
cLock
);
/*need to add ravi code*/
if
(
ans
==
""
)
{
// 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
;
// 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[0] != (char)240) {
// // cLock
// pthread_mutex_lock(&cLock);
// insert(key, value);
// pthread_mutex_unlock(&cLock);
// //uncLock
// }
// return value;
}
return
ans
;
}
string
PUT
(
string
key
,
string
value
)
{
cout
<<
"PUT started"
<<
endl
;
pthread_mutex_lock
(
&
cLock
);
string
ans
=
search
(
key
);
if
(
ans
==
""
)
{
insert
(
key
,
value
);
...
...
@@ -120,14 +129,18 @@ string PUT(string key, string value) {
unordered_map
<
string
,
list
<
cacheNode
>
::
iterator
>
::
iterator
it
=
hashTable
.
find
(
key
);
it
->
second
->
value
=
value
;
}
pthread_mutex_unlock
(
&
cLock
);
// uncLock
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"
;
// PUTT(new_key, new_value);
string
s
(
1
,
(
char
)
200
);
string
response
=
s
+
"Put Completed Successfully"
;
return
response
;
}
/*
...
...
@@ -135,17 +148,24 @@ string PUT(string key, string value) {
*/
string
DEL
(
string
key
)
{
/*Need to add ravi code*/
//cLock
pthread_mutex_lock
(
&
cLock
);
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
);
pthread_mutex_unlock
(
&
cLock
);
return
"SUCCESS"
;
}
//uncLock
pthread_mutex_unlock
(
&
cLock
);
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
);
// string response=DELL(new_key);
string
response
=
"ERROR"
;
return
response
;
}
...
...
client.c
View file @
acf37be8
...
...
@@ -34,7 +34,7 @@ void sendToServer(int sockfd) {
int
choice
;
char
*
buffer
=
(
char
*
)
malloc
(
513
*
sizeof
(
char
));
while
(
1
)
{
printf
(
"Enter choice:
:
"
);
printf
(
"Enter choice: "
);
scanf
(
"%d"
,
&
choice
);
char
key
[
100
],
value
[
100
];
switch
(
choice
)
{
...
...
@@ -61,7 +61,6 @@ void sendToServer(int sockfd) {
// return NULL;
break
;
}
// printf("buffer : %s\n", buffer);
write
(
sockfd
,
buffer
,
strlen
(
buffer
));
char
recvBuffer
[
1000
];
...
...
persistentStorage.cpp
View file @
acf37be8
...
...
@@ -564,7 +564,8 @@ string GETT(char * key){
{
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
"
);
// record[0]=240;
// record[1]='\0';
...
...
@@ -689,15 +690,15 @@ struct n2 * checkfile(struct n2 *h2,char *s){
}
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"
;
string
s
(
1
,
(
char
)
240
);
string
response
=
s
+
"Delete key not found"
;
return
response
;
}
// write(STDOUT_FILENO,"the finding is not null \n",22);
char
filename2
[
10
];
...
...
@@ -719,7 +720,9 @@ string DELL(char *key){
// write(STDOUT_FILENO,"\n",1);
if
(
tp
==
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);
tp
->
nooffreeline
=
tp
->
nooffreeline
-
1
;
...
...
@@ -727,15 +730,11 @@ string DELL(char *key){
free
(
finding
);
finding
=
NULL
;
// printf("del function completed\n");
return
"SUCCESS"
;
string
s
(
1
,
(
char
)
200
);
string
response
=
s
+
"Delete Successful"
;
return
response
;
}
void
pp
(
struct
n1
*
r
){
if
(
r
==
NULL
){
return
;
...
...
server.cpp
View file @
acf37be8
...
...
@@ -8,8 +8,10 @@
#include <pthread.h>
#include <stdlib.h>
#include "cache.h"
#include "cs_thread.h"
using
namespace
std
;
//
int search(char *p)
;
//
struct lock cLock
;
pthread_mutex_t
lock
;
void
initVals
(
char
*
ip
,
char
*
portNo
,
int
*
nThreads
)
{
...
...
@@ -20,6 +22,7 @@ void initVals(char *ip, char *portNo, int *nThreads) {
void
*
respondToClient
(
void
*
args
)
{
int
clientFd
=
*
((
int
*
)
args
);
cout
<<
"Client fd : "
<<
clientFd
<<
endl
;
while
(
1
)
{
// printf("reading for client %d\n", clientFd);
// char buffer[513];
...
...
@@ -27,34 +30,36 @@ void *respondToClient(void *args) {
int
len
=
read
(
clientFd
,
buffer
,
513
);
buffer
[
len
]
=
'\0'
;
char
key
[
100
],
value
[
100
],
converted_output
[
100
];
char
key
[
257
],
value
[
257
],
converted_output
[
257
];
string
output
;
memset
(
key
,
'\0'
,
100
);
memset
(
value
,
'\0'
,
100
);
memset
(
key
,
'\0'
,
257
);
memset
(
value
,
'\0'
,
257
);
char
todo
=
buffer
[
0
];
// key: first non-zero till byte 257
int
i
,
j
;
if
(
todo
!=
'4'
)
{
for
(
i
=
1
;
buffer
[
i
]
==
'0'
;
i
++
);
if
(
todo
!=
'4'
)
{
for
(
i
=
1
;
buffer
[
i
]
==
'0'
;
i
++
);
// printf("I: %d %c\n", i, buffer[i]);
printf
(
"key length: %d
\n
"
,
257
-
i
);
memcpy
(
key
,
buffer
+
i
,
257
-
i
);
// printf("key length: %d\n", 257 -
i);
memcpy
(
key
,
buffer
+
i
,
257
-
i
);
// value: first non-zero till byte 513
for
(
i
=
257
;
buffer
[
i
]
==
'0'
;
i
++
);
printf
(
"value length: %d
\n
"
,
513
-
i
);
memcpy
(
value
,
buffer
+
i
,
513
-
i
);
// printf("value length: %d\n", 513 -
i);
memcpy
(
value
,
buffer
+
i
,
513
-
i
);
}
string
key_string
,
value_string
;
switch
(
todo
)
{
case
'1'
:
printf
(
"GET recvd
\n
"
);
printf
(
"Key: %s
\n
"
,
key
);
//
printf("Key: %s\n", key);
// j = search(key);
// printf("j=%d\n", j);
key_string
=
key
;
output
=
GET
(
key_string
);
key_string
=
key
;
// pthread_mutex_lock(&cLock);
output
=
GET
(
key_string
);
// pthread_mutex_unlock(&cLock);
// cout << key << key1 << endl;
// cout << " GET returns : " << output << endl;
strcpy
(
converted_output
,
output
.
c_str
());
...
...
@@ -63,20 +68,24 @@ void *respondToClient(void *args) {
break
;
case
'2'
:
printf
(
"PUT recvd
\n
"
);
printf
(
"Key: %s
\n
"
,
key
);
printf
(
"Value: %s
\n
"
,
value
);
key_string
=
key
;
value_string
=
value
;
output
=
PUT
(
key_string
,
value_string
);
strcpy
(
converted_output
,
output
.
c_str
());
// printf("Key: %s\n", key);
// printf("Value: %s\n", value);
key_string
=
key
;
value_string
=
value
;
// pthread_mutex_lock(&cLock);
output
=
PUT
(
key_string
,
value_string
);
// pthread_mutex_unlock(&cLock);
strcpy
(
converted_output
,
output
.
c_str
());
// cout << " PUT returns : " << output << endl;
write
(
clientFd
,
converted_output
,
output
.
length
());
break
;
case
'3'
:
printf
(
"DELETE recvd
\n
"
);
printf
(
"Key: %s
\n
"
,
key
);
key_string
=
key
;
output
=
DEL
(
key_string
);
// printf("Key: %s\n", key);
key_string
=
key
;
// pthread_mutex_lock(&cLock);
output
=
DEL
(
key_string
);
// pthread_mutex_unlock(&cLock);
// cout << " DEL returns : " << output << endl;
strcpy
(
converted_output
,
output
.
c_str
());
// write(clientFd, "Delete returns\0", 15);
...
...
@@ -101,6 +110,7 @@ void *respondToClient(void *args) {
int
acceptConnections
(
char
*
addr
,
char
*
portNo
,
int
nThreads
)
{
int
sockfd
=
socket
(
AF_INET
,
SOCK_STREAM
,
0
);
pthread_t
clientThreads
[
nThreads
];
int
clientNo
=
0
;
struct
addrinfo
hints
,
*
result
;
...
...
@@ -129,16 +139,18 @@ int acceptConnections(char *addr, char *portNo, int nThreads) {
printf
(
"Waiting for connection...
\n
"
);
while
(
1
)
{
pthread_mutex_lock
(
&
lock
);
int
clientFd
=
accept
(
sockfd
,
NULL
,
NULL
);
printf
(
"Connected to client with fd: %d
\n
"
,
clientFd
);
pthread_mutex_lock
(
&
lock
);
//
pthread_mutex_lock(&lock);
clientNo
++
;
pthread_mutex_unlock
(
&
lock
);
//
pthread_mutex_unlock(&lock);
// int fid = fork();
// if(fid==0) {
// create thread
int
*
cfd
=
&
clientFd
;
pthread_create
(
&
clientThreads
[
clientNo
],
NULL
,
respondToClient
,
(
void
*
)
cfd
);
pthread_mutex_unlock
(
&
lock
);
// } else {
// manage threads
...
...
@@ -159,6 +171,6 @@ int main(int argc, char **argv) {
printf
(
"portno: %s
\n
"
,
portNo
);
printf
(
"no of threads: %d
\n
"
,
nThreads
);
acceptConnections
(
ip
,
portNo
,
nThreads
);
// lock_init(&cLock);
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