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
4b88969f
Commit
4b88969f
authored
Oct 26, 2020
by
Roshan Rabinarayan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added comments
parent
2a0b2d19
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
6 additions
and
94 deletions
+6
-94
KVServer.c
KVServer.c
+6
-94
server
server
+0
-0
No files found.
KVServer.c
View file @
4b88969f
...
...
@@ -71,9 +71,9 @@ error. (Assume each character to be 1 byte in size)
#include <string.h>
#include "KVMessageFormat.h"
#define MAX_EVENTS 10
static
int
thread
=
0
;
void
*
worker
(
void
*
args
)
{
thread
++
;
struct
epoll_event
ev
,
events
[
MAX_EVENTS
];
struct
epoll_event
socketEvent
;
socketEvent
.
events
=
EPOLLIN
;
...
...
@@ -111,7 +111,8 @@ void *worker(void *args) {
- add this newfd to epoll instance to monitor for client requests
*/
read
(
read_pipe
,
newfd
,
sizeof
(
newfd
));
printf
(
"
\n
read %d
\n
"
,
*
newfd
);
printf
(
"
\n
read %d
\n
"
,
read_pipe
);
ev
.
data
.
fd
=*
newfd
;
ev
.
events
=
EPOLLIN
|
EPOLLRDHUP
;
if
(
epoll_ctl
(
epollfd
,
EPOLL_CTL_ADD
,
*
newfd
,
&
ev
)
==
-
1
)
{
...
...
@@ -141,7 +142,7 @@ void *worker(void *args) {
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:%d][Key:%s][Value:%s]]"
,
requestMessage
->
status
,
requestMessage
->
key
,
requestMessage
->
value
);
/*Process the message and send appropriate response to client*/
requestMessage
->
status
=
200
;
write
(
events
[
i
].
data
.
fd
,
requestMessage
,
sizeof
(
struct
message
));
fflush
(
stdout
);
...
...
@@ -160,7 +161,7 @@ int main (int argc, int argv) {
int
i
;
int
next
=
0
;
// to decide which worker thread to assign client in round robin fashion
pthread_t
*
threads
;
// set of all worker threads
int
pool_thread_size
=
1
;
// TODO: get pool thread size from config file
int
pool_thread_size
=
3
;
// TODO: get pool thread size from config file
int
sockfd
,
newsockfd
,
portno
,
clilen
,
n
;
struct
sockaddr_in
serv_addr
,
cli_addr
;
...
...
@@ -211,92 +212,3 @@ int main (int argc, int argv) {
return
0
;
}
/*
#include <stdio.h>
#include <netdb.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include "KVMessageFormat.h"
#define MAX 80
#define PORT 6969
#define SA struct sockaddr
// Function designed for chat between client and server.
void func(int sockfd)
{
printf("receive functions:");
char buff[ sizeof(struct message)];
int n;
struct message* requestMessage=malloc(sizeof(struct message));
// infinite loop for chat
// read the message from client and copy it in buffer
read( sockfd , buff, sizeof(struct message));
// print buffer which contains the client contents
printf("From client: %c\t To client : ", buff[0]);
}
// Driver function
int main(int argc, char const *argv[])
{
int server_fd, new_socket, valread;
struct sockaddr_in address;
int opt = 1;
int addrlen = sizeof(address);
char buffer[1024] = {0};
char *hello = "Hello from server";
// Creating socket file descriptor
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0)
{
perror("socket failed");
exit(EXIT_FAILURE);
}
// Forcefully attaching socket to the port 8080
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT,
&opt, sizeof(opt)))
{
perror("setsockopt");
exit(EXIT_FAILURE);
}
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons( PORT );
// Forcefully attaching socket to the port 8080
if (bind(server_fd, (struct sockaddr *)&address,
sizeof(address))<0)
{
perror("bind failed");
exit(EXIT_FAILURE);
}
if (listen(server_fd, 3) < 0)
{
perror("listen");
exit(EXIT_FAILURE);
}
if ((new_socket = accept(server_fd, (struct sockaddr *)&address,
(socklen_t*)&addrlen))<0)
{
perror("accept");
exit(EXIT_FAILURE);
}
struct message *requestMessage= malloc(sizeof(struct message));
valread = read( new_socket , requestMessage, sizeof(struct message));
printf("[Message Received from client]\n[[Status:%c]\n[Key:%s]\n[Value:%s]]",requestMessage->status,requestMessage->key,requestMessage->value);
struct message *replyMessage=malloc(sizeof(struct message));
replyMessage->status='0';
char buff[256]="success";
memcpy(replyMessage->key,buff,256);
replyMessage->value[0]='\0';
send(new_socket , replyMessage , sizeof(struct message) , 0 );
printf("\n[Message sent to client]\n[[Status:%c]\n[Key:%s]\n[Value:%s]]",replyMessage->status,replyMessage->key,replyMessage->value);
return 0;
}
*/
\ No newline at end of file
server
View file @
4b88969f
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