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

main thread initial layout

parent 2a33ec9d
No related branches found
No related tags found
No related merge requests found
......@@ -7,6 +7,7 @@
#include <cstring>
#include <sys/poll.h>
#include <fcntl.h>
#include <unordered_map>
#include "server_util.h"
#include "threadpool.h"
......@@ -37,6 +38,7 @@ int main(int argc, char **argv) {
int num_fds = 1;
int poll_timeout = -1; // -1 is same as infinite timeout
bool resize_events_arr = false;
unordered_map<int, Work*> worker_to_client; // read end of a worker -> work to do map
// Check for valid number of arguments
if (argc != 3)
......@@ -137,33 +139,57 @@ int main(int argc, char **argv) {
// TODO: clean up;
continue;
}
Work *thread_work = new Work;
thread_work->filepath = req_file_path;
// pipe_fds[1] is write end of pipe
thread_work->pipe_fd = pipe_fds[1];
thread_work->written = 0;
thread_work->client_fd = new_client_fd;
// add pollfd
events[num_fds].fd = new_client_fd;
events[num_fds].fd = pipe_fds[0];
events[num_fds].events = POLLIN;
num_fds++;
// dispatch thread
HttpServerTask *worker_task = new HttpServerTask(HttpServer_ThrFn);
worker_task-> work = thread_work;
worker_to_client[pipe_fds[0]] = thread_work;
tp.Dispatch(worker_task);
} else {
// this is probably a worker thread done with the work
Work *thread_work = worker_to_client[events[i].fd];
if (thread_work == nullptr) {
cerr << " worker_to_client mapping corrupted for fd: " << events[i].fd << "\n";
// TODO: need some action
}
// return data to client
int written_bytes = WrappedWrite(thread_work->client_fd, (unsigned char*)(thread_work->buffer), thread_work->written);
if (written_bytes != thread_work->written) {
cerr << " actual written: " << written_bytes << ", expected: " << thread_work->written << "\n";
}
// clean up resource: pipes, client socket, work struct
close(thread_work->client_fd);
close(events[i].fd);
close(thread_work->pipe_fd);
delete thread_work;
events[i].fd = -1;
resize_events_arr = true;
}
}
if (resize_events_arr)
{
// at least 1 event has been deleted, need to resize
if (resize_events_arr) {
resize_events_arr = false;
for (int i = 0; i < num_fds; i++)
{
if (events[i].fd == -1)
{
for(int j = i; j < num_fds; j++)
{
for (int i = 0; i < num_fds; i++) {
if (events[i].fd == -1) {
for(int j = i; j < num_fds; j++) {
events[j].fd = events[j+1].fd;
}
i--;
......@@ -171,35 +197,6 @@ int main(int argc, char **argv) {
}
}
}
// TODO: initialize shared data structure
// TODO: modify logic to add threapool
// TODO: use server_socket class
/*
1) accept client connection
2) initialize stuff for communication with threadpool
3) read from client and write to thread
4) get back info from thread
*/
// read from client fd and write to stdout
while (1) {
int rlen = WrappedRead(client_fd, buf, BUFFER_SIZE);
// check for reading failure
// on error, clean up and exit with failure
if (rlen <= 0) {
break;
}
int wlen = WrappedWrite(STDOUT_FILENO, buf, rlen);
// check that we wrote the same amount as we read in
// on error, clean up and exit with failure
if (wlen != rlen) {
break;
}
}
}
return EXIT_SUCCESS;
......
......@@ -79,7 +79,9 @@ class HttpServerTask : public ThreadPool::Task {
struct Work {
string filepath;
char buffer[FILE_BUFFER_SIZE]; // 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
int pipe_fd; // Write end for the pipe. Used to notify main thread that the worker is done writing the file contents.
int client_fd; // Client that this work is for
};
#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