-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.c
278 lines (218 loc) · 7.5 KB
/
server.c
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
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/time.h>
#include <time.h>
#include "util.h"
#define MAX_THREADS 100
#define MAX_QLEN 100
#define MAX_CE 100
// structs:
typedef struct request_queue {
int fd;
char *request;
} request_queue_t;
// globals:
FILE *logfile;
pthread_mutex_t log_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t req_queue_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t req_queue_notfull = PTHREAD_COND_INITIALIZER; // wait until queue isn't full (dispatch)
pthread_cond_t req_queue_notempty = PTHREAD_COND_INITIALIZER; // wait until queue isn't empty (worker)
request_queue_t requests[MAX_QLEN];
int req_remove_index = 0;
int req_insert_index = 0;
int req_num = 0;
int global_timestamp = 0;
int qlen;
void * dispatch(void *arg) {
int id = (int)arg;
int fd;
char mybuf[1024];
char *reqptr;
// printf("Dispatch %d started\n",id);
while (1) {
// printf("Thread %d waiting for request...\n",id);
fd = accept_connection();
if (fd < 0) {
return NULL;
}
if (get_request(fd,mybuf) != 0) {
// printf("Bad request, thread id=%d\n",id);
continue; // back to top of the loop!
}
// printf("Dispatch %d got request: %s\n",id,mybuf);
reqptr = (char *)malloc(strlen(mybuf)+1);
strcpy(reqptr,mybuf);
// put request in queue
pthread_mutex_lock(&req_queue_mutex);
while (req_num == qlen) // the queue is full!
pthread_cond_wait(&req_queue_notfull,&req_queue_mutex);
requests[req_insert_index].fd = fd;
requests[req_insert_index].request = reqptr;
req_num++;
req_insert_index = (req_insert_index + 1) % qlen; // ring buffer
// printf("disp[%d] inserted req into queue: %s\n",id,reqptr);
pthread_cond_signal(&req_queue_notempty);
pthread_mutex_unlock(&req_queue_mutex);
}
return NULL;
}
void * workerBad(void *arg) {
int id = (int)arg;
int fd, i, found, filesize, myreqnum;
void * memory;
char *hit_or_miss;
char mybuf[1024];
myreqnum = 0;
while (1) {
// get request from queue
pthread_mutex_lock(&req_queue_mutex);
while (req_num == 0) // the queue is empty!
pthread_cond_wait(&req_queue_notempty,&req_queue_mutex);
fd = requests[req_remove_index].fd;
if (fd == -9999) { // special meaning to this 'request': we must exit!!!
pthread_mutex_unlock(&req_queue_mutex); // dont forget to unlock first!
return NULL;
}
strcpy(mybuf,requests[req_remove_index].request);
free(requests[req_remove_index].request);
req_num--;
req_remove_index = (req_remove_index + 1) % qlen; // ring buffer
pthread_cond_signal(&req_queue_notfull);
pthread_mutex_unlock(&req_queue_mutex);
myreqnum++;
if (strcmp(mybuf,"/")==0)
strcpy(mybuf,"/index.html");
}
return NULL;
}
void * worker(void *arg) {
int id = (int)arg;
int fd, i, filesize, myreqnum;
void * memory;
char mybuf[1024];
myreqnum = 0;
while (1) {
// get request from queue
pthread_mutex_lock(&req_queue_mutex);
while (req_num == 0) // the queue is empty!
pthread_cond_wait(&req_queue_notempty,&req_queue_mutex);
fd = requests[req_remove_index].fd;
if (fd == -9999) { // special meaning to this 'request': we must exit!!!
pthread_mutex_unlock(&req_queue_mutex); // dont forget to unlock first!
return NULL;
}
strcpy(mybuf,requests[req_remove_index].request);
free(requests[req_remove_index].request);
req_num--;
req_remove_index = (req_remove_index + 1) % qlen; // ring buffer
pthread_cond_signal(&req_queue_notfull);
pthread_mutex_unlock(&req_queue_mutex);
myreqnum++;
if (strcmp(mybuf,"/")==0)
strcpy(mybuf,"/index.html");
// increment global timestamp
int filefd;
if ((filefd = open(mybuf+1,O_RDONLY)) == -1) {
return_error(fd,"File not found.");
pthread_mutex_lock(&log_mutex);
fprintf(logfile,"[%d][%d][%d][%s][%s]\n",id,myreqnum,fd,mybuf,"File not found.");
fflush(logfile);
pthread_mutex_unlock(&log_mutex);
continue; // next request => top of while loop
}
struct stat st;
fstat(filefd,&st);
filesize = st.st_size;
memory = malloc(filesize);
read(filefd,memory,filesize);
close(filefd);
pthread_mutex_lock(&log_mutex);
fprintf(logfile,"[%d][%d][%d][%s][%d]\n",id,myreqnum,fd,mybuf,filesize);
fflush(logfile);
pthread_mutex_unlock(&log_mutex);
int reqlen = strlen(mybuf);
char * contenttype;
if (strcmp(mybuf+reqlen-5,".html")==0) {
contenttype = "text/html";
} else if (strcmp(mybuf+reqlen-4,".jpg")==0) {
contenttype = "image/jpeg";
} else if (strcmp(mybuf+reqlen-4,".gif")==0) {
contenttype = "image/gif";
} else {
contenttype = "text/plain";
}
if (return_result(fd,contenttype,memory,filesize) != 0) {
printf("Couldn't return result, thread id=%d\n",id);
}
free(memory);
}
return NULL;
}
int main(int argc, char **argv) {
pthread_t dispatch_threads[MAX_THREADS];
pthread_t worker_threads[MAX_THREADS];
logfile = fopen("webserver_log","w");
if (argc != 6) {
printf("usage: %s port path num_dispatch num_workers qlen\n",argv[0]);
return -1;
}
if (chdir(argv[2])!=0) {
perror("couldn't change directory to server root");
return -1;
}
int port = atoi(argv[1]);
int num_dispatch = atoi(argv[3]);
int num_worker = atoi(argv[4]);
qlen = atoi(argv[5]);
init(port);
if (num_dispatch > MAX_THREADS || num_dispatch < 1) {
fprintf(stderr,"Invalid number of dispatch threads.\n");
return -2;
}
if (num_worker > MAX_THREADS || num_worker < 1) {
fprintf(stderr,"Invalid number of worker threads.\n");
return -2;
}
if (qlen > MAX_QLEN || qlen < 1) {
fprintf(stderr,"Invalid qlen size.\n");
return -2;
}
printf("Starting server on port %d: %d disp, %d work\n",port,num_dispatch,num_worker);
struct timeval time_start, time_end;
int j, i;
for (i=0; i < num_worker; i++) {
pthread_create(worker_threads+i,NULL,worker,(void *)i);
}
for (i=0; i < num_dispatch; i++) {
pthread_create(dispatch_threads+i,NULL,dispatch,(void *)i);
}
for (i=0; i < num_dispatch; i++) {
pthread_join(dispatch_threads[i],NULL);
}
// at this point, all the dispatch threads have managed to exit.
// the request queue could be empty, non-empty, and/or the workers
// could be busy doing stuff right now.
// => insert a 'dummy' item into the request queue, so that when workers
// receive it, it means that they should exit.
pthread_mutex_lock(&req_queue_mutex);
while (req_num == qlen) // the queue is full!
pthread_cond_wait(&req_queue_notfull,&req_queue_mutex);
requests[req_insert_index].fd = -9999;
requests[req_insert_index].request = NULL;
req_num++;
req_insert_index = (req_insert_index + 1) % qlen; // ring buffer
pthread_cond_broadcast(&req_queue_notempty); // wake EVERYONE UP!!
pthread_mutex_unlock(&req_queue_mutex);
// the workers should be able to exit now.
for (i=0; i < num_worker; i++) {
pthread_join(worker_threads[i],NULL);
}
fclose(logfile);
printf("all threads have exited, main thread exiting...\n");
return 0;
}