Skip to content
Snippets Groups Projects
Commit 47a35182 authored by Winston Jodjana's avatar Winston Jodjana
Browse files

Struct and Initial Planning

parent b26e878e
No related branches found
No related tags found
No related merge requests found
......@@ -13,6 +13,10 @@
static const int num_threads = 16;
// This is the function that threads are dispatched into
// in order to process new client connections.
static void HttpServer_ThrFn(ThreadPool::Task *t);
// given a program name, it prints
// information about its usage.
void Usage(char *progname);
......@@ -70,7 +74,9 @@ int main(int argc, char **argv) {
unsigned char buf[BUFFER_SIZE];
while (1) {
// TODO: Spawn workers when you see a client connect
// Read file path, clean it up, pass it to worker thread
//
HttpServerTask *hst = new HttpServerTask(HttpServer_ThrFn);
// TODO: change to new fields
hst->base_dir = static_file_dir_path_;
......@@ -118,6 +124,16 @@ int main(int argc, char **argv) {
return EXIT_SUCCESS;
}
static void HttpServer_ThrFn(ThreadPool::Task *t) {
// TODO: Worker thread logic
// Input: Shared struct with cond. var., buffer, etc
// 1. A worker function that receives the name of a file (absolute file path),
// 2. reads the file content into the address space of the process,
// 3. and arranges to return (a pointer to) the file content to whoever dispatched the thread to the worker function.
// TODO: Tell main thread that task is done, main thread needs to close the connection
}
void Usage(char *progname) {
std::cerr << "usage: " << progname << " port" << std::endl;
exit(EXIT_FAILURE);
......
......@@ -67,4 +67,18 @@ class ThreadPool {
pthread_t *thread_array_;
};
class HttpServerTask : public ThreadPool::Task {
public:
explicit HttpServerTask(ThreadPool::thread_task_fn f)
: ThreadPool::Task(f) { }
Work* work;
};
struct Work {
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
};
#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