Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
K
key-value-store
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Analytics
Analytics
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
Samarth Joshi
key-value-store
Commits
3a427d2d
Commit
3a427d2d
authored
Nov 08, 2020
by
Samarth Joshi
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Adding interactive client
parent
b4d1e7ac
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
72 additions
and
18 deletions
+72
-18
Client
Client
+0
-0
KVClient.c
KVClient.c
+59
-4
KVClientLibrary.c
KVClientLibrary.c
+1
-2
KVServer.c
KVServer.c
+5
-6
LRU.c
LRU.c
+6
-5
LRU.h
LRU.h
+1
-1
Server
Server
+0
-0
No files found.
Client
View file @
3a427d2d
No preview for this file type
KVClient.c
View file @
3a427d2d
...
...
@@ -2,13 +2,67 @@
#define PORT 8000
#define SA struct sockaddr
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include "KVClientLibrary.h"
#include <string.h>
struct
message
*
requestMessage
;
void
interactive
(
int
sock
)
{
char
command
[
515
];
int
readlen
,
i
,
j
;
int
status_code
;
char
key
[
256
];
char
value
[
256
];
char
error
[
256
];
do
{
memset
(
key
,
0
,
sizeof
(
key
));
memset
(
value
,
0
,
sizeof
(
value
));
memset
(
error
,
0
,
sizeof
(
error
));
printf
(
"interactive> "
);
fflush
(
stdout
);
readlen
=
read
(
0
,
command
,
sizeof
(
command
));
i
=
5
;
while
(
command
[
i
]
&&
command
[
i
]
!=
' '
&&
command
[
i
]
!=
'\n'
)
{
i
++
;
}
memcpy
(
key
,
command
+
4
,
i
-
4
);
if
(
command
[
i
]
==
' '
)
{
j
=
i
+
1
;
while
(
command
[
j
]
&&
command
[
j
]
!=
':'
&&
command
[
j
]
!=
'\n'
)
{
j
++
;
}
memcpy
(
value
,
command
+
i
+
1
,
j
-
i
-
1
);
}
// printf("key: %s\n", key);
// printf("value: %s\n", value);
// printf("error: %s\n", error);
switch
(
command
[
0
])
{
case
'g'
:
status_code
=
get
(
sock
,
key
,
value
,
error
);
printf
(
"[%d]"
,
status_code
);
printf
(
" Value recieved: %s
\n
"
,
value
);
break
;
case
'p'
:
status_code
=
put
(
sock
,
key
,
value
,
error
);
printf
(
"[%d]
\n
"
,
status_code
);
break
;
case
'd'
:
status_code
=
del
(
sock
,
key
,
error
);
printf
(
"[%d]
\n
"
,
status_code
);
break
;
default:
return
;
}
}
while
(
command
[
0
]
!=
'q'
);
}
int
main
(
int
argc
,
char
const
*
argv
[])
{
int
sock
=
0
,
valread
;
...
...
@@ -48,9 +102,10 @@ int main(int argc, char const *argv[])
//printf("%d",(int)del(sock,key,error));
//printf("%d",(int)get(sock,key, value,error));
//while (1) {
printf
(
"%d"
,(
int
)
put
(
sock
,
key1
,
value1
,
error1
));
printf
(
"%d"
,(
int
)
put
(
sock
,
key2
,
value2
,
error2
));
printf
(
"%d"
,(
int
)
get
(
sock
,
key1
,
value3
,
error2
));
//printf("%d",(int)put(sock, key1, value1, error1));
//printf("%d",(int)put(sock, key2, value2, error2));
//printf("%d\n",(int)get(sock, key1, value3, error2));
//printf("%s", value3);
interactive
(
sock
);
return
0
;
}
\ No newline at end of file
KVClientLibrary.c
View file @
3a427d2d
...
...
@@ -6,7 +6,6 @@
#include <unistd.h>
#include "KVClientLibrary.h"
// to print a message
void
printMessage
(
struct
message
*
requestMessage
)
{
...
...
@@ -90,7 +89,7 @@ int put(int sockfd,char *key,char *value,char *error)
}
else
{
printMessage
(
reply
);
//
printMessage(reply);
}
free
(
request
);
...
...
KVServer.c
View file @
3a427d2d
...
...
@@ -32,6 +32,7 @@ void *worker(void *args) {
int
newfd
;
int
status
;
// Generate name for each thread for debugging
char
*
name
=
(
char
*
)
malloc
(
5
*
sizeof
(
char
));
gen_random
(
name
,
5
);
...
...
@@ -97,11 +98,11 @@ void *worker(void *args) {
switch
(
requestMessage
->
status
)
{
case
STATUS_GET
:
if
DEBUG
printf
(
"[%s] GET
\n
"
,
name
);
memcpy
(
requestMessage
->
value
,
cache_get
(
requestMessage
->
key
),
256
);
if
(
requestMessage
->
value
==
0
)
{
requestMessage
->
status
=
240
;
}
else
{
status
=
cache_get
(
requestMessage
->
key
,
requestMessage
->
value
);
if
(
status
)
{
requestMessage
->
status
=
200
;
}
else
{
requestMessage
->
status
=
240
;
}
break
;
case
STATUS_PUT
:
...
...
@@ -121,8 +122,6 @@ void *worker(void *args) {
}
requestMessage
->
status
=
200
;
write
(
events
[
i
].
data
.
fd
,
requestMessage
,
sizeof
(
struct
message
));
free
(
requestMessage
);
}
...
...
LRU.c
View file @
3a427d2d
...
...
@@ -79,14 +79,14 @@ int cache_del(char *key)
return
1
;
}
}
printf
(
"Cache after DEL:
\n
"
);
print_cache
();
return
0
;
//TODO remove key from file also
}
void
cache_put
(
char
*
key
,
char
*
value
)
{
printf
(
"Cache before PUT:
\n
"
);
print_cache
();
int
indx
=-
1
;
for
(
int
i
=
0
;
i
<
MAX_SIZE
;
i
++
)
{
...
...
@@ -119,17 +119,18 @@ void cache_put(char *key, char *value)
print_cache
();
}
char
*
cache_get
(
char
*
key
)
int
cache_get
(
char
*
key
,
char
*
value
)
{
for
(
int
i
=
0
;
i
<
MAX_SIZE
;
i
++
)
{
if
(
strcmp
(
array
[
i
]
->
key
,
key
)
==
0
)
if
(
strcmp
(
array
[
i
]
->
key
,
key
)
==
0
&&
array
[
i
]
->
valid
)
{
while
(
ATOMIC_TEST_AND_SET
(
&
(
array
[
i
]
->
lock
),
1
)
==
1
);
remove_element_from_deque
(
key
);
insert_into_queue
(
key
);
CLEAR
(
&
(
array
[
i
]
->
lock
),
0
);
return
array
[
i
]
->
value
;
memcpy
(
value
,
array
[
i
]
->
value
,
VAL_SIZE
);
return
1
;
}
}
return
0
;
...
...
LRU.h
View file @
3a427d2d
...
...
@@ -40,7 +40,7 @@ int cache_del(char *key);
void
cache_put
(
char
*
key
,
char
*
value
);
char
*
cache_get
(
char
*
key
);
int
cache_get
(
char
*
key
,
char
*
value
);
void
init_cache
();
...
...
Server
View file @
3a427d2d
No preview for this file type
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