Commit b6242314 authored by Samarth Joshi's avatar Samarth Joshi

Adding Comments

parent 70aaa378
No preview for this file type
......@@ -82,13 +82,14 @@ void *worker(void *args) {
int epollfd;
int *newfd=malloc(sizeof(int));
read_pipe = ((int *) args)[0]; //assigned the read end of the pipe
epollfd = epoll_create1(0);
if (epollfd == -1) {
perror("epoll_create1");
exit(EXIT_FAILURE);
}
/* Add the pipe which we get from main thread */
read_pipe = ((int *) args)[0];
ev.events = EPOLLIN;
ev.data.fd = read_pipe;
if (epoll_ctl(epollfd, EPOLL_CTL_ADD, read_pipe, &ev) == -1) {
......@@ -104,7 +105,11 @@ void *worker(void *args) {
}
for ( i= 0; i < nfds; ++i ) {
if (events[i].data.fd == read_pipe) {
fflush(stdout);
/* if we get a request from main thread to add new client
ie. events[i].data.fd is equal to the pipe which we use to talk with main thread
- read this newfd from pipe
- add this newfd to epoll instance to monitor for client requests
*/
read(read_pipe, newfd, sizeof(newfd));
printf("\nread %d\n", *newfd);
ev.data.fd=*newfd;
......@@ -114,24 +119,25 @@ void *worker(void *args) {
exit(EXIT_FAILURE);
}
fflush(stdout);
}
else{
} else {
/* if we get a request from client
ie. events[i].data.fd is equal to the client socket fd
*/
int flag = events[i].events;
if (flag & EPOLLRDHUP) {
/* Connection was closed.
Remove socket from epoll instance
Close the socket and dont process more info from this socket
- Remove socket from epoll instance
- Close the socket and dont process more info from this socket
Note: This if block should be before EPOLLIN
*/
char eof;
read(events[i].data.fd , &eof, sizeof(char));
epoll_ctl( epollfd, EPOLL_CTL_DEL, events[i].data.fd , NULL );
close(events[i].data.fd);
continue;
}
if (flag & EPOLLIN) {
/* 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);
......@@ -156,7 +162,7 @@ int main (int argc, int argv) {
struct sockaddr_in serv_addr, cli_addr;
//int *pipes = (int*) malloc(pool_thread_size * 2 * sizeof(pipes));
int pipes[5][2];
int pipes[5][2]; // TODO: initialize pipes dynamically
// initialize thread pool with initial pool size
threads = malloc(pool_thread_size * sizeof(pthread_t));
......@@ -194,7 +200,7 @@ int main (int argc, int argv) {
perror("ERROR on accept");
write(pipes[next][1], &newsockfd, sizeof(newsockfd));
printf("main thread ka fd ka value: %d\n",newsockfd);
printf("main thread value: %d\n",newsockfd);
fflush(stdout);
next = (next+1) % pool_thread_size;
......
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