Skip to content
Snippets Groups Projects
Commit 3813d331 authored by Mengqi Chen's avatar Mengqi Chen
Browse files

wip

parent 7897bcf4
No related branches found
No related tags found
No related merge requests found
......@@ -5,6 +5,7 @@
#include <stdlib.h>
#include <iostream>
#include <cstring>
#include <sys/poll.h>
#include "server_util.h"
#include "threadpool.h"
......@@ -25,11 +26,20 @@ static void HttpServer_ThrFn(ThreadPool::Task *t);
void Usage(char *progname);
int main(int argc, char **argv) {
// We need 1 pollfd for the listening socket, num_threads pollfd for the workers,
// and num_threads pollfd for the client connections.
// We are using a fixed size number of clients == 16
// events[0].fd will always be the listening_socket fd
// then for each i > 0, events[i].fd = client_fd, and events[i + 1].fd = corresponding worker_fd
struct pollfd events[1 + num_threads];
int client_fds[num_threads];
int num_fds = 1;
int poll_timeout = -1; // -1 is same as infinite timeout
// Check for valid number of arguments
if (argc != 3)
Usage(argv[0]);
// Parse the port number, and check
// for failure
unsigned short port = 0;
......@@ -46,9 +56,58 @@ int main(int argc, char **argv) {
// threadpool of workers
ThreadPool tp(num_threads);
// initialize pollfd structs for event polling
memset(events, 0, sizeof(events));
events[0].fd = listen_socket.GetSocketFd();
events[0].events = POLLIN;
// loop and wait for client connections
while (1) {
// poll for events
int poll_res = poll(events, num_fds, poll_timeout);
if (poll_res < 0) {
perror(" poll failed");
return EXIT_FAILURE;
} else if (poll_res == 0) {
// happens if we set timeout other than -1
perror(" poll timed out");
return EXIT_FAILURE;
}
// we have at least one event
int curr_num_fs = num_fds;
for (int i = 0; i < curr_num_fs; ++i) {
/*********************************************************/
/* Loop through to find the descriptors that returned */
/* POLLIN and determine whether it's the listening */
/* or the active connection. */
/*********************************************************/
if(events[i].revents == 0)
continue;
/*********************************************************/
/* If revents is not POLLIN, it's an unexpected result, */
/* log and end the server. */
/*********************************************************/
if(events[i].revents != POLLIN)
{
printf(" Error! revents = %d\n", events[i].revents);
break;
}
if (events[i].fd == listen_socket.GetSocketFd()) {
// we have a new client connection
} else {
}
}
struct sockaddr_storage caddr;
socklen_t caddr_len = sizeof(caddr);
int client_fd = accept4(listen_fd,
......
......@@ -219,3 +219,7 @@ bool ServerSocket::Accept(int *accepted_fd,
return true;
}
int ServerSocket::GetSocketFd() {
return listen_sock_fd_;
}
......@@ -70,6 +70,8 @@ class ServerSocket {
std::string *client_dnsname, std::string *server_addr,
std::string *server_dnsname);
int GetSocketFd();
private:
uint16_t port_;
int listen_sock_fd_;
......
......@@ -10,6 +10,8 @@ extern "C" {
#include "./verify550.h" // For asserts
static const int FILE_BUFFER_SIZE = 2000000;
class ThreadPool {
public:
// Construct a new ThreadPool with a certain number of worker
......@@ -76,10 +78,8 @@ class HttpServerTask : public ThreadPool::Task {
struct Work {
string filepath;
char *buffer; // Buffer for main thread to read file contents from worker thread
int written; // Number of bytes (char) that the worker thread has written to the buffer
pthread_cond_t buffer_filled; // Cond. var. for when the main thread should read from the buffer and write back the file contents to the client
pthread_cond_t task_finished; // Cond. var. for when the worker thread has finished reading all the file contents
char buffer[FILE_BUFFER_SIZE]; // Buffer for main thread to read file contents from worker thread
int pipe_fd; // Write end for the pipe
};
#endif // THREADPOOL_H_
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment