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
c0422e9f
Commit
c0422e9f
authored
Nov 09, 2020
by
Saikumar
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
modified files on 09112020
parent
1f578b03
Changes
5
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
836 additions
and
27 deletions
+836
-27
Makefile
Makefile
+1
-1
cache.cpp
cache.cpp
+40
-16
persistentStorage.cpp
persistentStorage.cpp
+775
-0
server.cpp
server.cpp
+14
-10
storage.h
storage.h
+6
-0
No files found.
Makefile
View file @
c0422e9f
...
...
@@ -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
...
...
cache.cpp
View file @
c0422e9f
...
...
@@ -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
;
}
/*
...
...
persistentStorage.cpp
0 → 100644
View file @
c0422e9f
This diff is collapsed.
Click to expand it.
server.cpp
View file @
c0422e9f
...
...
@@ -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
);
...
...
storage.h
0 → 100644
View file @
c0422e9f
#include <iostream>
using
namespace
std
;
string
GETT
(
char
*
);
void
PUTT
(
char
*
,
char
*
);
string
DELL
(
char
*
);
\ No newline at end of file
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