Commit 4b88969f authored by Roshan Rabinarayan's avatar Roshan Rabinarayan

added comments

parent 2a0b2d19
...@@ -71,9 +71,9 @@ error. (Assume each character to be 1 byte in size) ...@@ -71,9 +71,9 @@ error. (Assume each character to be 1 byte in size)
#include <string.h> #include <string.h>
#include "KVMessageFormat.h" #include "KVMessageFormat.h"
#define MAX_EVENTS 10 #define MAX_EVENTS 10
static int thread=0;
void *worker(void *args) { void *worker(void *args) {
thread++;
struct epoll_event ev,events[MAX_EVENTS]; struct epoll_event ev,events[MAX_EVENTS];
struct epoll_event socketEvent; struct epoll_event socketEvent;
socketEvent.events=EPOLLIN; socketEvent.events=EPOLLIN;
...@@ -111,7 +111,8 @@ void *worker(void *args) { ...@@ -111,7 +111,8 @@ void *worker(void *args) {
- add this newfd to epoll instance to monitor for client requests - add this newfd to epoll instance to monitor for client requests
*/ */
read(read_pipe, newfd, sizeof(newfd)); read(read_pipe, newfd, sizeof(newfd));
printf("\nread %d\n", *newfd);
printf("\nread %d\n", read_pipe);
ev.data.fd=*newfd; ev.data.fd=*newfd;
ev.events = EPOLLIN | EPOLLRDHUP; ev.events = EPOLLIN | EPOLLRDHUP;
if (epoll_ctl(epollfd, EPOLL_CTL_ADD, *newfd, &ev) == -1) { if (epoll_ctl(epollfd, EPOLL_CTL_ADD, *newfd, &ev) == -1) {
...@@ -141,7 +142,7 @@ void *worker(void *args) { ...@@ -141,7 +142,7 @@ void *worker(void *args) {
struct message *requestMessage= malloc(sizeof(struct message)); struct message *requestMessage= malloc(sizeof(struct message));
int readlength=read(events[i].data.fd , requestMessage, 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); 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; requestMessage->status=200;
write(events[i].data.fd,requestMessage, sizeof(struct message)); write(events[i].data.fd,requestMessage, sizeof(struct message));
fflush(stdout); fflush(stdout);
...@@ -160,7 +161,7 @@ int main (int argc, int argv) { ...@@ -160,7 +161,7 @@ int main (int argc, int argv) {
int i; int i;
int next = 0; // to decide which worker thread to assign client in round robin fashion int next = 0; // to decide which worker thread to assign client in round robin fashion
pthread_t *threads; // set of all worker threads 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; int sockfd, newsockfd, portno, clilen, n;
struct sockaddr_in serv_addr, cli_addr; struct sockaddr_in serv_addr, cli_addr;
...@@ -211,92 +212,3 @@ int main (int argc, int argv) { ...@@ -211,92 +212,3 @@ int main (int argc, int argv) {
return 0; 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
No preview for this file type
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment