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
2a0b2d19
Commit
2a0b2d19
authored
Oct 26, 2020
by
Roshan Rabinarayan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added KVCLient libraary version1.1
parent
82957c3a
Changes
8
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
96 additions
and
27 deletions
+96
-27
KVClient.c
KVClient.c
+4
-3
KVClientLibrary.c
KVClientLibrary.c
+81
-21
KVMessageFormat.h
KVMessageFormat.h
+1
-1
KVServer.c
KVServer.c
+4
-1
a.out
a.out
+0
-0
client
client
+0
-0
server
server
+0
-0
temp.c
temp.c
+6
-1
No files found.
KVClient.c
View file @
2a0b2d19
...
...
@@ -58,9 +58,10 @@ int main(int argc, char const *argv[])
}
char
key
[
256
]
=
"abc"
;
char
value
[
256
]
=
"cde"
;
struct
message
*
requestMessage
=
request
(
'g'
,
key
,
value
);
struct
message
*
replyMessage
=
malloc
(
sizeof
(
struct
message
));
func
(
sock
,
requestMessage
);
char
error
[
256
];
printf
(
"%d"
,(
int
)
put
(
sock
,
key
,
value
,
error
));
printf
(
"%d"
,(
int
)
del
(
sock
,
key
,
error
));
printf
(
"%d"
,(
int
)
get
(
sock
,
key
,
value
,
error
));
return
0
;
}
\ No newline at end of file
KVClientLibrary.c
View file @
2a0b2d19
...
...
@@ -46,36 +46,96 @@ Else, it will malloc some memory into the error pointer and return it. And so on
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
struct
message
*
request
(
char
status
,
char
*
key
,
char
*
value
)
// to print a message
void
printMessage
(
struct
message
*
requestMessage
)
{
if
(
!
status
||
(
key
==
NULL
&&
value
==
NULL
)
)
printf
(
"[Message Generated]
\n
[[Status:%d]
\n
[Key:%s]
\n
[Value:%s]]
\n
"
,
requestMessage
->
status
,
requestMessage
->
key
,
requestMessage
->
value
);
return
;
}
int
get
(
int
sockfd
,
char
*
key
,
char
*
value
,
char
*
error
)
{
if
(
key
==
NULL
)
{
printf
(
"Invalid
parameters in request()
"
);
return
NULL
;
printf
(
"Invalid
PARAM to get
\n
"
);
return
-
1
;
}
struct
message
*
requestMessage
=
malloc
(
sizeof
(
struct
message
));
requestMessage
->
status
=
status
;
memcpy
(
requestMessage
->
key
,
key
,
256
);
//copied the complete key
struct
message
*
request
=
malloc
(
sizeof
(
struct
message
));
request
->
status
=
1
;
//status code for get
memcpy
(
request
->
key
,
key
,
256
);
//copied the complete key passed as parameter
explicit_bzero
(
request
->
value
,
256
*
sizeof
(
char
));
//null value to to the request->value field
struct
message
*
reply
=
malloc
(
sizeof
(
struct
message
));
write
(
sockfd
,
request
,
sizeof
(
struct
message
));
//passing to server and waiting for response
read
(
sockfd
,
reply
,
sizeof
(
struct
message
));
int
status
=
reply
->
status
;
if
(
status
==
400
)
{
memcpy
(
error
,
reply
->
key
,
256
*
sizeof
(
char
));
//copying the error in the key field for an failed get
if
(
strlen
(
requestMessage
->
key
)
<
256
)
}
else
{
requestMessage
->
key
[
strlen
(
requestMessage
->
key
)]
=
'\0'
;
memcpy
(
value
,
reply
->
value
,
256
*
sizeof
(
char
))
;
}
if
(
value
!=
NULL
)
free
(
request
);
free
(
reply
);
return
status
;
//return reply->key
}
int
del
(
int
sockfd
,
char
*
key
,
char
*
error
)
{
if
(
key
==
NULL
)
{
memcpy
(
requestMessage
->
value
,
value
,
256
);
//copied the complete value
printf
(
"Invalid PARAM to del
\n
"
);
return
-
1
;
}
if
(
strlen
(
requestMessage
->
value
)
<
256
)
//to pad with \0
struct
message
*
request
=
malloc
(
sizeof
(
struct
message
));
request
->
status
=
2
;
//status code for delete
memcpy
(
request
->
key
,
key
,
256
);
//copied the complete key passed as parameter
explicit_bzero
(
request
->
value
,
256
*
sizeof
(
char
));
//null value to to the request->value field
struct
message
*
reply
=
malloc
(
sizeof
(
struct
message
));
write
(
sockfd
,
request
,
sizeof
(
struct
message
));
//passing to server and waiting for response to the del request
read
(
sockfd
,
reply
,
sizeof
(
struct
message
));
int
status
=
(
int
)
reply
->
status
;
if
(
status
==
400
)
{
requestMessage
->
value
[
strlen
(
requestMessage
->
value
)]
=
'\0'
;
memcpy
(
error
,
reply
->
key
,
256
*
sizeof
(
char
));
//copying the error in the key field for an failed del
}
return
requestMessage
;
free
(
request
);
free
(
reply
);
return
status
;
//return reply->key
}
void
printMessage
(
struct
message
*
requestMessage
)
int
put
(
int
sockfd
,
char
*
key
,
char
*
value
,
char
*
error
)
{
printf
(
"[Message Generated]
\n
[[Status:%c]
\n
[Key:%s]
\n
[Value:%s]]
\n
"
,
requestMessage
->
status
,
requestMessage
->
key
,
requestMessage
->
value
);
return
;
}
\ No newline at end of file
if
(
key
==
NULL
)
{
printf
(
"Invalid PARAM to del
\n
"
);
return
-
1
;
}
struct
message
*
request
=
malloc
(
sizeof
(
struct
message
));
request
->
status
=
3
;
//status code for delete
memcpy
(
request
->
key
,
key
,
256
);
//copied the complete key passed as parameter
memcpy
(
request
->
value
,
value
,
256
);
//copied the complete key passed as parameter
struct
message
*
reply
=
malloc
(
sizeof
(
struct
message
));
write
(
sockfd
,
request
,
sizeof
(
struct
message
));
//passing to server and waiting for response to the del request
read
(
sockfd
,
reply
,
sizeof
(
struct
message
));
int
status
=
reply
->
status
;
if
(
status
==
400
)
{
memcpy
(
error
,
reply
->
key
,
256
*
sizeof
(
char
));
//copying the error in the key field for an failed del
}
else
{
printMessage
(
reply
);
}
free
(
request
);
free
(
reply
);
return
status
;
//return reply->key
}
KVMessageFormat.h
View file @
2a0b2d19
...
...
@@ -15,7 +15,7 @@ Reasons for error could be GET key not found, DEL key not found etc.
*/
struct
message
{
char
status
;
unsigned
char
status
;
char
key
[
256
];
char
value
[
256
];
};
KVServer.c
View file @
2a0b2d19
...
...
@@ -140,7 +140,10 @@ void *worker(void *args) {
/* Parse the actual message from client */
struct
message
*
requestMessage
=
malloc
(
sizeof
(
struct
message
));
int
readlength
=
read
(
events
[
i
].
data
.
fd
,
requestMessage
,
sizeof
(
struct
message
));
printf
(
"
\n
[Message Received from client]
\n
[[Status:%c][Key:%s][Value:%s]]"
,
requestMessage
->
status
,
requestMessage
->
key
,
requestMessage
->
value
);
printf
(
"
\n
[Message Received from client]
\n
[[Status:%d][Key:%s][Value:%s]]"
,
requestMessage
->
status
,
requestMessage
->
key
,
requestMessage
->
value
);
requestMessage
->
status
=
200
;
write
(
events
[
i
].
data
.
fd
,
requestMessage
,
sizeof
(
struct
message
));
fflush
(
stdout
);
}
...
...
a.out
View file @
2a0b2d19
No preview for this file type
client
View file @
2a0b2d19
No preview for this file type
server
View file @
2a0b2d19
No preview for this file type
temp.c
View file @
2a0b2d19
...
...
@@ -97,6 +97,11 @@ void main()
message
[
i
]
=
'a'
;
}
message
[
sizeof
(
message
)]
=
'\0'
;
request
(
'g'
,
message
,
message
);
request
(
'2'
,
message
,
message
);
char
c
=
'2'
;
int
status
=
(
int
)
c
;
printf
(
"%d"
,
status
);
//creating a message
}
\ 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