-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
124 lines (98 loc) · 3.21 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// CSE421 Summer 2016 Team name: MikeJoelFrank
// University at Buffalo June 24, 2016
//
// Team Members:
//
// Name: Email: UB#
// Franklin Pinnock fgpinnoc@buffalo.edu 36398302
// Joel Little joellitt@buffalo.edu 36183498
// Michael Mastro mmastro@buffalo.edu 50035644
#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <iostream>
#include <string>
#include <vector>
#include <thread>
#include <algorithm>
#include <ctime>
#include <structs.h>
#include <functions.h>
#include <options.h>
#include <server_init.h>
#include <queue.h>
#include <scheduler.h>
#include <action.h>
#include <sync.h>
std::mutex data_mutex, work_mutex, log_mutex;
std::condition_variable_any work_cv;
server_options options;
std::vector<http_request_data> http_request_queue;
int main(int argc, char* argv[]){
//Use info flag to show useful output about program
options.info_flag = false;
//Set default options
set_default_server_options(options);
char option;
while((option = getopt(argc,argv,"hdl:p:r:t:n:s:"))!=-1){
switch(option){
case 'h':
show_help();
return 0;
break;
case 'd':
options.debug_flag = true;
break;
case 'l':
options.file = optarg;
options.logging_enabled = true;
break;
case 'p':
options.port = std::stoi(optarg, nullptr, 10);
break;
case 'r':
options.dir = optarg;
chdir(optarg);
break;
case 't':
options.time = std::stoi(optarg, nullptr, 10);
break;
case 'n':
options.threadnum = std::stoi(optarg, nullptr, 10);
break;
case 's':
options.sched = optarg;
break;
default:
break;
}
}
if (options.debug_flag == false){
daemon(1, 0);
}
//At this point options struct contains all options to be used in program.
if(options.info_flag == true) show_server_info(options);
//Initialize server
int server_socket_fd = initialize_server(options);
//Main thread creates queueing thread
std::thread queue_thread(http_request_handler, std::cref(server_socket_fd));
//Create the scheduling thread (the producer)
//if(options.info_flag == true) std::cout <<"(Main thread): Creating scheduling thread\n";
std::thread scheduler_thread(http_request_scheduler);
//Create worker threads (the consumers) from -n option
//if(options.info_flag == true) std::cout << "(Main thread): Creating worker threads\n";
std::vector<std::thread> threads;
threads.reserve(options.threadnum);
for(int thread_id = 1 ; thread_id <= options.threadnum; thread_id++ ) {
threads.emplace_back(process_queue_block, thread_id);
std::this_thread::sleep_for (std::chrono::seconds(1));
}
queue_thread.join();
scheduler_thread.join();
for(auto &thread : threads){
thread.detach();
}
return 0;
}