-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver_main.cpp
More file actions
291 lines (246 loc) · 7.94 KB
/
server_main.cpp
File metadata and controls
291 lines (246 loc) · 7.94 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#include "myhttpd.h"
int main(int argc, char ** argv)
{
if (argc != 9)
{
cout<<"-! ERROR - Wrong number of parameters !-"<<endl;
exit(1);
}
int serving_sock = -1;
int serving_port = -1;
int command_sock = -1;
int command_port = -1;
int n_threads = -1;
char * root_dir = NULL;
char * serving_port_str = get_parameter(argv, argc, "-p");
if (serving_port_str == NULL)
{
cout<<"-! ERROR - Serving port parameter does not exist !-"<<endl;
exit(1);
}
else if (!is_unsigned_int_number(serving_port_str))
{
cout<<"-! ERROR - Service port's -p parameter must be an unsigned int !-"<<endl;
exit(1);
}
else
{
serving_port = atoi(serving_port_str);
}
char * command_port_str = get_parameter(argv, argc, "-c");
if (command_port_str == NULL)
{
cout<<"-! ERROR - Command port parameter does not exist !-"<<endl;
exit(1);
}
else if (!is_unsigned_int_number(command_port_str))
{
cout<<"-! ERROR - Command port's -c parameter must be an unsigned int !-"<<endl;
exit(1);
}
else
{
command_port = atoi(command_port_str);
}
char * n_threads_str = get_parameter(argv, argc, "-t");
if (n_threads_str == NULL)
{
cout<<"-! ERROR - Number of threads parameter does not exist !-"<<endl;
exit(1);
}
else if (!is_unsigned_int_number(n_threads_str))
{
cout<<"-! ERROR - Number of threads' -t parameter must be an unsigned int !-"<<endl;
exit(1);
}
else
{
n_threads = atoi(n_threads_str);
}
root_dir = get_parameter(argv, argc, "-d");
if (root_dir == NULL)
{
cout<<"-! ERROR - Root directory parameter does not exist !-"<<endl;
exit(1);
}
else if (!dir_exists(root_dir))
{
cout<<"-! ERROR - Root directory does not exist !-"<<endl;
exit(1);
}
if (command_port == serving_port)
{
cout<<"-! ERROR - Command and serving ports must be different !-"<<endl;
exit(1);
}
sep_term_line();
cout<<"--> Parameters given <--"<<endl;
cout<<"-> Serving port (-p): "<<serving_port<<endl;
cout<<"-> Command port (-c): "<<command_port<<endl;
cout<<"-> Number of threads (-t): "<<n_threads<<endl;
cout<<"-> Root directory (-d): '"<<root_dir<<"'"<<endl;
sep_term_line();
cout<<"--> SERVER IS ON ! <--"<<endl;
sep_term_line();
struct timeval t1, t2;
gettimeofday(&t1, NULL);
int enable = 1;
struct pollfd poll_tids[2];
//service --------------------------------------------------------------
if((serving_sock = socket(AF_INET, SOCK_STREAM , 0)) == -1)
{
perror("Socket creation failed!");
exit(1);
}
if (setsockopt(serving_sock, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int)) < 0)
{
perror("setsockopt(SO_REUSEADDR) failed");
exit(1);
}
poll_tids[0].fd = serving_sock;
poll_tids[0].events = POLLIN;
bind_on_port(serving_sock, serving_port);
if(listen(serving_sock, 128) < 0)
{
perror("listen");
exit(1);
}
//command ----------------------------------------------------------
if((command_sock = socket(AF_INET, SOCK_STREAM , 0)) == -1)
{
perror("Socket creation failed!");
exit(1);
}
if (setsockopt(command_sock, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int)) < 0)
{
perror("setsockopt(SO_REUSEADDR) failed");
exit(1);
}
poll_tids[1].fd = command_sock;
poll_tids[1].events = POLLIN;
bind_on_port(command_sock, command_port);
if(listen(command_sock , 128) < 0)
{
perror("listen");
exit(1);
}
//-----------------------------------------------------------
init_pool(&pool);
pthread_mutex_init(&mtx, 0);
pthread_mutex_init(&mtx2, 0); //for global variables used in stats
pthread_cond_init(&cond_nonempty, 0);
pthread_cond_init(&cond_nonfull, 0);
pthread_t * cons_tids;
if ((cons_tids = (pthread_t *) malloc(n_threads * sizeof(pthread_t))) == NULL)
{
perror("malloc");
exit(1);
}
for(int i=0; i < n_threads; i++)
{
int err = pthread_create(cons_tids + i, NULL, consumer, root_dir);
if(err < 0)
{
perror("pthread_create");
exit(1);
}
}
while(1)
{
if(poll(poll_tids, 2, -1) == -1)
{
perror("Eror in poll");
exit(1);
}
if(poll_tids[0].revents == POLLIN)
{//service sock
int client_sock = accept(serving_sock, NULL , NULL);
if (client_sock < 0)
{
perror("accept");
exit(1);
}
place(&pool, client_sock); //push back client's sockets' fds
pthread_cond_signal(&cond_nonempty);
}
else if (poll_tids[0].revents == POLLHUP)
{
perror("POLLHUP");
exit(1);
}
if(poll_tids[1].revents == POLLIN)
{//command sock
int cmd_sock = accept(command_sock, NULL , NULL);
if (cmd_sock < 0)
{
perror("accept");
exit(1);
}
if (!send_message(cmd_sock, "--> Type 'STATS' to get statistics or 'SHUTDOWN' to stop server.\r\n-> "))
{
cout<<"=> WARNING - Disconnection .."<<endl;
}
bool discon;
char * cmd = read_cmd(cmd_sock, discon);
if (discon == 1)
{
cout<<"=> WARNING - Connection to command socket lost. Server could not get a command .."<<endl;
}
else
{
if(strcmp(cmd, "SHUTDOWN") == 0)
{
free(cmd);
for(int i=0; i < n_threads; i++)
{
place(&pool, -1); //push back -1 to signal threads to finish (all existing clients' requests will be served first)
pthread_cond_signal(&cond_nonempty);
}
close(cmd_sock);
break;
}
else if(strcmp(cmd, "STATS") == 0)
{
gettimeofday(&t2, NULL);
int elapsedTime = (t2.tv_sec - t1.tv_sec) * 1000; // sec to ms
elapsedTime += (t2.tv_usec - t1.tv_usec) / 1000; // us to ms
char * elapsed_time_str = ms_to_time(elapsedTime);
pthread_mutex_lock(&mtx2); //global jointly accessed variables must be protected with mutexes
char * stats_str = get_stats_str(1, pages_served, total_bytes_sent, elapsed_time_str);
pthread_mutex_unlock(&mtx2);
free(elapsed_time_str);
if (!send_message(cmd_sock, stats_str))
{
cout<<"=> WARNING - Disconnection. Server could not send stats .."<<endl;
}
free(stats_str);
}
else
{
const char * msg = "-! ERROR - Wrong server command !-\r\n";
if (!send_message(cmd_sock, msg))
{
cout<<"=> WARNING - Disconnection. Server could not send response .."<<endl;
}
}
free(cmd);
close(cmd_sock);
}
}
else if (poll_tids[1].revents == POLLHUP)
{
perror("POLLHUP");
exit(1);
}
}
for(int i=0 ; i < n_threads; i++)
{ //wait for all threads to finish
pthread_join(*(cons_tids + i), 0);
}
free(cons_tids);
pthread_cond_destroy(&cond_nonempty);
pthread_cond_destroy(&cond_nonfull);
pthread_mutex_destroy(&mtx);
pthread_mutex_destroy(&mtx2);
return 0;
}