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
251ecd4f
Commit
251ecd4f
authored
Nov 09, 2020
by
Saikumar
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
modified cpp files
parent
158e764c
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
265 additions
and
61 deletions
+265
-61
Makefile
Makefile
+24
-9
cache.cpp
cache.cpp
+60
-50
cache.h
cache.h
+3
-0
client.c
client.c
+18
-2
server.cpp
server.cpp
+160
-0
No files found.
Makefile
View file @
251ecd4f
all
:
$(CC)
-o
server server.c
-lpthread
$(CC)
-o
client client.c
CFLAGS
=
-w
all
:
clean
# $(CC) $(CFLAGS) -o server_s server.c -lpthread
$(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++ -c -o cache.o cache.cpp
# g++ -o server_s server.o cache.o -lpthread
server
:
$(CC)
-o
server server.c
-lpthread
# server:
# $(CC) -o server server.c -lpthread
# g++ -std=c++11 -pthread server.cpp -o server
client
:
$(CC)
-o
client client.c
# client:
# $(CC) -o client client.c
clean
:
$(RM)
client
$(RM)
server
\ No newline at end of file
$(RM)
client_c
$(RM)
server_s
$(RM)
*
.o
server
:
all
./server_s
client
:
./client_c 127.1.1.2 20000
cache.cpp
View file @
251ecd4f
...
...
@@ -3,7 +3,8 @@
#include <iterator>
#include <unordered_map>
#include <string>
#include "cs_thread.h"
#include <cstdlib>
// #include "cs_thread.h"
#include "cache.h"
using
namespace
std
;
/*
...
...
@@ -92,15 +93,22 @@ void insert(string key, string value) {
/*
* Delete from cache
*/
void
DEL
(
string
key
)
{
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"
;
return
ans
;
}
...
...
@@ -108,61 +116,63 @@ 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"
;
}
/*
* Testing code
*/
int
main
()
{
string
key1
=
"AB"
;
string
key2
=
"BC"
;
string
key3
=
"CD"
;
string
key4
=
"DE"
;
string
key5
=
"EF"
;
string
value
=
"1"
;
insert
(
key1
,
value
);
insert
(
key2
,
value
);
for
(
auto
p
:
hashTable
)
{
cout
<<
p
.
first
<<
" : "
;
cout
<<
p
.
second
->
key
<<
" "
<<
p
.
second
->
value
<<
" "
<<
p
.
second
->
dirtyBit
<<
endl
;
}
printf
(
"-----------------------------
\n
"
);
insert
(
key3
,
value
);
for
(
auto
p
:
hashTable
)
{
cout
<<
p
.
first
<<
" : "
;
cout
<<
p
.
second
->
key
<<
" "
<<
p
.
second
->
value
<<
" "
<<
p
.
second
->
dirtyBit
<<
endl
;
}
printf
(
"-----------------------------
\n
"
);
insert
(
key4
,
value
);
for
(
auto
p
:
hashTable
)
{
cout
<<
p
.
first
<<
" : "
;
cout
<<
p
.
second
->
key
<<
" "
<<
p
.
second
->
value
<<
" "
<<
p
.
second
->
dirtyBit
<<
endl
;
}
printf
(
"-----------------------------
\n
"
);
cout
<<
key1
<<
" "
<<
search
(
key1
)
<<
endl
;
cout
<<
key2
<<
" "
<<
search
(
key2
)
<<
endl
;
cout
<<
key3
<<
" "
<<
search
(
key3
)
<<
endl
;
cout
<<
key4
<<
" "
<<
search
(
key4
)
<<
endl
;
printf
(
"-----------------------------
\n
"
);
for
(
auto
p
:
hashTable
)
{
cout
<<
p
.
first
<<
" : "
;
cout
<<
p
.
second
->
key
<<
" "
<<
p
.
second
->
value
<<
" "
<<
p
.
second
->
dirtyBit
<<
endl
;
}
printf
(
"-----------------------------
\n
"
);
insert
(
key5
,
value
);
for
(
auto
p
:
hashTable
)
{
cout
<<
p
.
first
<<
" : "
;
cout
<<
p
.
second
->
key
<<
" "
<<
p
.
second
->
value
<<
" "
<<
p
.
second
->
dirtyBit
<<
endl
;
}
printf
(
"-----------------------------
\n
"
);
DEL
(
key5
);
for
(
auto
p
:
hashTable
)
{
cout
<<
p
.
first
<<
" : "
;
cout
<<
p
.
second
->
key
<<
" "
<<
p
.
second
->
value
<<
" "
<<
p
.
second
->
dirtyBit
<<
endl
;
}
}
//
int main() {
//
string key1 = "AB";
//
string key2 = "BC";
//
string key3 = "CD";
//
string key4 = "DE";
//
string key5 = "EF";
//
string value = "1";
//
insert(key1, value);
//
insert(key2, value);
//
for (auto p : hashTable) {
//
cout << p.first << " : ";
//
cout << p.second->key << " " << p.second->value << " " << p.second->dirtyBit << endl;
//
}
//
printf("-----------------------------\n");
//
insert(key3, value);
//
for (auto p : hashTable) {
//
cout << p.first << " : ";
//
cout << p.second->key << " " << p.second->value << " " << p.second->dirtyBit << endl;
//
}
//
printf("-----------------------------\n");
//
insert(key4, value);
//
for (auto p : hashTable) {
//
cout << p.first << " : ";
//
cout << p.second->key << " " << p.second->value << " " << p.second->dirtyBit << endl;
//
}
//
printf("-----------------------------\n");
//
cout << key1 << " " << search(key1) << endl;
//
cout << key2 << " " << search(key2) << endl;
//
cout << key3 << " " << search(key3) << endl;
//
cout << key4 << " " << search(key4) << endl;
//
printf("-----------------------------\n");
//
for (auto p : hashTable) {
//
cout << p.first << " : ";
//
cout << p.second->key << " " << p.second->value << " " << p.second->dirtyBit << endl;
//
}
//
printf("-----------------------------\n");
//
insert(key5, value);
//
for (auto p : hashTable) {
//
cout << p.first << " : ";
//
cout << p.second->key << " " << p.second->value << " " << p.second->dirtyBit << endl;
//
}
//
printf("-----------------------------\n");
//
DEL(key5);
//
for (auto p : hashTable) {
//
cout << p.first << " : ";
//
cout << p.second->key << " " << p.second->value << " " << p.second->dirtyBit << endl;
//
}
//
}
cache.h
View file @
251ecd4f
...
...
@@ -20,3 +20,6 @@ typedef struct cacheNode {
char
dirtyBit
;
}
cacheNode
;
string
GET
(
string
);
string
PUT
(
string
,
string
);
string
DEL
(
string
);
\ No newline at end of file
client.c
View file @
251ecd4f
...
...
@@ -33,10 +33,10 @@ void sendToServer(int sockfd) {
printf
(
"------OPTIONS------
\n
1. GET
\n
2. PUT
\n
3. DELETE
\n
4. EXIT
\n
"
);
int
choice
;
char
*
buffer
=
(
char
*
)
malloc
(
513
*
sizeof
(
char
));
char
key
[
100
],
value
[
100
];
while
(
1
)
{
printf
(
"Enter choice: :"
);
scanf
(
"%d"
,
&
choice
);
char
key
[
100
],
value
[
100
];
switch
(
choice
)
{
case
1
:
printf
(
"Key: "
);
...
...
@@ -56,14 +56,22 @@ void sendToServer(int sockfd) {
DELETE
(
key
,
buffer
);
break
;
case
4
:
printf
(
"Requested for connection termination
\n
"
);
terminateConnection
(
buffer
);
// return NULL;
break
;
}
// printf("buffer : %s\n", buffer);
write
(
sockfd
,
buffer
,
strlen
(
buffer
));
char
recvBuffer
[
1000
];
int
len
=
read
(
sockfd
,
recvBuffer
,
999
);
recvBuffer
[
len
]
=
'\0'
;
printf
(
"%s
\n
"
,
recvBuffer
);
if
(
choice
==
4
)
{
return
NULL
;
}
}
// char buffer[100];
// while(fgets(buffer, 100, stdin) != NULL) {
...
...
@@ -82,7 +90,7 @@ int GET(char* key, char *request) {
request
[
0
]
=
'1'
;
// memcpy(request+1, key, strlen(key));
memcpy
(
request
+
257
-
strlen
(
key
),
key
,
strlen
(
key
));
// printf("%s\n", request);
// printf("
request :
%s\n", request);
}
int
PUT
(
char
*
key
,
char
*
value
,
char
*
request
)
{
...
...
@@ -106,6 +114,14 @@ int DELETE(char* key, char *request) {
// printf("%s\n", request);
}
void
terminateConnection
(
char
*
request
)
{
//char request[513];
// printf("Termination function called\n");
memset
(
request
,
'0'
,
513
);
request
[
0
]
=
'4'
;
request
[
512
]
=
'\0'
;
}
int
main
(
int
argc
,
char
**
argv
)
{
printf
(
"IP = %s, Port = %s
\n
"
,
argv
[
1
],
argv
[
2
]);
// PUT("100", "200");
...
...
server.cpp
0 → 100644
View file @
251ecd4f
#include <stdio.h>
#include "server.h"
#include <sys/socket.h>
#include <netdb.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <pthread.h>
#include <stdlib.h>
#include "cache.h"
// int search(char *p);
void
initVals
(
char
*
ip
,
char
*
portNo
,
int
*
nThreads
)
{
FILE
*
ptr
=
fopen
(
"config.txt"
,
"r"
);
fscanf
(
ptr
,
"%s
\n
%s
\n
%d
\n
"
,
ip
,
portNo
,
nThreads
);
fclose
(
ptr
);
}
void
*
respondToClient
(
void
*
args
)
{
int
clientFd
=
*
((
int
*
)
args
);
while
(
1
)
{
// printf("reading for client %d\n", clientFd);
// char buffer[513];
char
*
buffer
=
(
char
*
)
malloc
(
513
*
sizeof
(
char
));
int
len
=
read
(
clientFd
,
buffer
,
513
);
buffer
[
len
]
=
'\0'
;
char
key
[
100
],
value
[
100
],
converted_output
[
100
];
string
output
;
memset
(
key
,
'\0'
,
100
);
memset
(
value
,
'\0'
,
100
);
char
todo
=
buffer
[
0
];
// key: first non-zero till byte 257
int
i
,
j
;
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
);
// 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
);
}
string
key_string
,
value_string
;
switch
(
todo
)
{
case
'1'
:
printf
(
"GET recvd
\n
"
);
printf
(
"Key: %s
\n
"
,
key
);
// j = search(key);
// printf("j=%d\n", j);
key_string
=
key
;
output
=
GET
(
key_string
);
// cout << key << key1 << endl;
cout
<<
" GET returns : "
<<
output
<<
endl
;
strcpy
(
converted_output
,
output
.
c_str
());
// write(clientFd, "Get returns\0", 12);
write
(
clientFd
,
converted_output
,
output
.
length
());
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
());
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
);
cout
<<
" DEL returns : "
<<
output
<<
endl
;
strcpy
(
converted_output
,
output
.
c_str
());
// write(clientFd, "Delete returns\0", 15);
write
(
clientFd
,
converted_output
,
output
.
length
());
break
;
case
'4'
:
printf
(
"Connection terminated
\n
"
);
write
(
clientFd
,
"Connection terminated
\0
"
,
22
);
return
NULL
;
// break;
}
// printf("%s\n", buffer);
// write(clientFd, "Client response\0", 16);
// if (todo=='4')
// {
// return NULL;
// }
}
}
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
;
memset
(
&
hints
,
0
,
sizeof
(
struct
addrinfo
));
hints
.
ai_family
=
AF_INET
;
hints
.
ai_socktype
=
SOCK_STREAM
;
hints
.
ai_flags
=
AI_PASSIVE
;
int
s
=
getaddrinfo
(
addr
,
portNo
,
&
hints
,
&
result
);
if
(
s
!=
0
)
{
fprintf
(
stderr
,
"getaddrinfo: %s
\n
"
,
gai_strerror
(
s
));
return
-
1
;
}
int
b
=
bind
(
sockfd
,
result
->
ai_addr
,
result
->
ai_addrlen
);
if
(
b
!=
0
)
{
printf
(
"Binding error
\n
"
);
return
-
1
;
}
int
l
=
listen
(
sockfd
,
nThreads
);
if
(
l
!=
0
)
{
printf
(
"Error while listening...
\n
"
);
return
-
1
;
}
printf
(
"Waiting for connection...
\n
"
);
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
)
{
// create thread
int
*
cfd
=
&
clientFd
;
pthread_create
(
&
clientThreads
[
clientNo
],
NULL
,
respondToClient
,
(
void
*
)
cfd
);
}
else
{
// manage threads
}
}
return
0
;
}
int
main
(
int
argc
,
char
**
argv
)
{
printf
(
"Server is running...
\n
"
);
char
ip
[
20
],
portNo
[
10
];
int
nThreads
;
initVals
(
ip
,
portNo
,
&
nThreads
);
printf
(
"Ip address: %s
\n
"
,
ip
);
printf
(
"portno: %s
\n
"
,
portNo
);
printf
(
"no of threads: %d
\n
"
,
nThreads
);
acceptConnections
(
ip
,
portNo
,
nThreads
);
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