comparison src/c/driver.c @ 311:9ad92047a499

Rename 'lw' prefixes to 'uw'
author Adam Chlipala <adamc@hcoop.net>
date Sun, 07 Sep 2008 15:40:42 -0400
parents 4d80d6122df1
children b91480c9a729
comparison
equal deleted inserted replaced
310:0aee86b8a6d6 311:9ad92047a499
7 7
8 #include <pthread.h> 8 #include <pthread.h>
9 9
10 #include "urweb.h" 10 #include "urweb.h"
11 11
12 int lw_port = 8080; 12 int uw_port = 8080;
13 int lw_backlog = 10; 13 int uw_backlog = 10;
14 int lw_bufsize = 1024; 14 int uw_bufsize = 1024;
15 15
16 typedef struct node { 16 typedef struct node {
17 int fd; 17 int fd;
18 struct node *next; 18 struct node *next;
19 } *node; 19 } *node;
51 51
52 #define MAX_RETRIES 5 52 #define MAX_RETRIES 5
53 53
54 static void *worker(void *data) { 54 static void *worker(void *data) {
55 int me = *(int *)data, retries_left = MAX_RETRIES;; 55 int me = *(int *)data, retries_left = MAX_RETRIES;;
56 lw_context ctx = lw_init(1024, 1024); 56 uw_context ctx = uw_init(1024, 1024);
57 57
58 while (1) { 58 while (1) {
59 failure_kind fk = lw_begin_init(ctx); 59 failure_kind fk = uw_begin_init(ctx);
60 60
61 if (fk == SUCCESS) { 61 if (fk == SUCCESS) {
62 lw_db_init(ctx); 62 uw_db_init(ctx);
63 printf("Database connection initialized.\n"); 63 printf("Database connection initialized.\n");
64 break; 64 break;
65 } else if (fk == BOUNDED_RETRY) { 65 } else if (fk == BOUNDED_RETRY) {
66 if (retries_left) { 66 if (retries_left) {
67 printf("Initialization error triggers bounded retry: %s\n", lw_error_message(ctx)); 67 printf("Initialization error triggers bounded retry: %s\n", uw_error_message(ctx));
68 --retries_left; 68 --retries_left;
69 } else { 69 } else {
70 printf("Fatal initialization error (out of retries): %s\n", lw_error_message(ctx)); 70 printf("Fatal initialization error (out of retries): %s\n", uw_error_message(ctx));
71 lw_free(ctx); 71 uw_free(ctx);
72 return NULL; 72 return NULL;
73 } 73 }
74 } else if (fk == UNLIMITED_RETRY) 74 } else if (fk == UNLIMITED_RETRY)
75 printf("Initialization error triggers unlimited retry: %s\n", lw_error_message(ctx)); 75 printf("Initialization error triggers unlimited retry: %s\n", uw_error_message(ctx));
76 else if (fk == FATAL) { 76 else if (fk == FATAL) {
77 printf("Fatal initialization error: %s\n", lw_error_message(ctx)); 77 printf("Fatal initialization error: %s\n", uw_error_message(ctx));
78 lw_free(ctx); 78 uw_free(ctx);
79 return NULL; 79 return NULL;
80 } else { 80 } else {
81 printf("Unknown lw_handle return code!\n"); 81 printf("Unknown uw_handle return code!\n");
82 lw_free(ctx); 82 uw_free(ctx);
83 return NULL; 83 return NULL;
84 } 84 }
85 } 85 }
86 86
87 while (1) { 87 while (1) {
88 char buf[lw_bufsize+1], *back = buf, *s; 88 char buf[uw_bufsize+1], *back = buf, *s;
89 int sock; 89 int sock;
90 90
91 pthread_mutex_lock(&queue_mutex); 91 pthread_mutex_lock(&queue_mutex);
92 while (empty()) 92 while (empty())
93 pthread_cond_wait(&queue_cond, &queue_mutex); 93 pthread_cond_wait(&queue_cond, &queue_mutex);
96 96
97 printf("Handling connection with thread #%d.\n", me); 97 printf("Handling connection with thread #%d.\n", me);
98 98
99 while (1) { 99 while (1) {
100 unsigned retries_left = MAX_RETRIES; 100 unsigned retries_left = MAX_RETRIES;
101 int r = recv(sock, back, lw_bufsize - (back - buf), 0); 101 int r = recv(sock, back, uw_bufsize - (back - buf), 0);
102 102
103 if (r < 0) { 103 if (r < 0) {
104 fprintf(stderr, "Recv failed\n"); 104 fprintf(stderr, "Recv failed\n");
105 break; 105 break;
106 } 106 }
157 else 157 else
158 inputs = strchr(name, 0); 158 inputs = strchr(name, 0);
159 159
160 if (value = strchr(name, '=')) { 160 if (value = strchr(name, '=')) {
161 *value++ = 0; 161 *value++ = 0;
162 lw_set_input(ctx, name, value); 162 uw_set_input(ctx, name, value);
163 } 163 }
164 else 164 else
165 lw_set_input(ctx, name, ""); 165 uw_set_input(ctx, name, "");
166 } 166 }
167 } 167 }
168 168
169 printf("Serving URI %s....\n", path); 169 printf("Serving URI %s....\n", path);
170 170
171 while (1) { 171 while (1) {
172 failure_kind fk; 172 failure_kind fk;
173 173
174 lw_write(ctx, "HTTP/1.1 200 OK\r\n"); 174 uw_write(ctx, "HTTP/1.1 200 OK\r\n");
175 lw_write(ctx, "Content-type: text/html\r\n\r\n"); 175 uw_write(ctx, "Content-type: text/html\r\n\r\n");
176 lw_write(ctx, "<html>"); 176 uw_write(ctx, "<html>");
177 177
178 fk = lw_begin(ctx, path); 178 fk = uw_begin(ctx, path);
179 if (fk == SUCCESS) { 179 if (fk == SUCCESS) {
180 lw_write(ctx, "</html>"); 180 uw_write(ctx, "</html>");
181 break; 181 break;
182 } else if (fk == BOUNDED_RETRY) { 182 } else if (fk == BOUNDED_RETRY) {
183 if (retries_left) { 183 if (retries_left) {
184 printf("Error triggers bounded retry: %s\n", lw_error_message(ctx)); 184 printf("Error triggers bounded retry: %s\n", uw_error_message(ctx));
185 --retries_left; 185 --retries_left;
186 } 186 }
187 else { 187 else {
188 printf("Fatal error (out of retries): %s\n", lw_error_message(ctx)); 188 printf("Fatal error (out of retries): %s\n", uw_error_message(ctx));
189 189
190 lw_reset_keep_error_message(ctx); 190 uw_reset_keep_error_message(ctx);
191 lw_write(ctx, "HTTP/1.1 500 Internal Server Error\n\r"); 191 uw_write(ctx, "HTTP/1.1 500 Internal Server Error\n\r");
192 lw_write(ctx, "Content-type: text/plain\r\n\r\n"); 192 uw_write(ctx, "Content-type: text/plain\r\n\r\n");
193 lw_write(ctx, "Fatal error (out of retries): "); 193 uw_write(ctx, "Fatal error (out of retries): ");
194 lw_write(ctx, lw_error_message(ctx)); 194 uw_write(ctx, uw_error_message(ctx));
195 lw_write(ctx, "\n"); 195 uw_write(ctx, "\n");
196 } 196 }
197 } else if (fk == UNLIMITED_RETRY) 197 } else if (fk == UNLIMITED_RETRY)
198 printf("Error triggers unlimited retry: %s\n", lw_error_message(ctx)); 198 printf("Error triggers unlimited retry: %s\n", uw_error_message(ctx));
199 else if (fk == FATAL) { 199 else if (fk == FATAL) {
200 printf("Fatal error: %s\n", lw_error_message(ctx)); 200 printf("Fatal error: %s\n", uw_error_message(ctx));
201 201
202 lw_reset_keep_error_message(ctx); 202 uw_reset_keep_error_message(ctx);
203 lw_write(ctx, "HTTP/1.1 500 Internal Server Error\n\r"); 203 uw_write(ctx, "HTTP/1.1 500 Internal Server Error\n\r");
204 lw_write(ctx, "Content-type: text/plain\r\n\r\n"); 204 uw_write(ctx, "Content-type: text/plain\r\n\r\n");
205 lw_write(ctx, "Fatal error: "); 205 uw_write(ctx, "Fatal error: ");
206 lw_write(ctx, lw_error_message(ctx)); 206 uw_write(ctx, uw_error_message(ctx));
207 lw_write(ctx, "\n"); 207 uw_write(ctx, "\n");
208 208
209 break; 209 break;
210 } else { 210 } else {
211 printf("Unknown lw_handle return code!\n"); 211 printf("Unknown uw_handle return code!\n");
212 212
213 lw_reset_keep_request(ctx); 213 uw_reset_keep_request(ctx);
214 lw_write(ctx, "HTTP/1.1 500 Internal Server Error\n\r"); 214 uw_write(ctx, "HTTP/1.1 500 Internal Server Error\n\r");
215 lw_write(ctx, "Content-type: text/plain\r\n\r\n"); 215 uw_write(ctx, "Content-type: text/plain\r\n\r\n");
216 lw_write(ctx, "Unknown lw_handle return code!\n"); 216 uw_write(ctx, "Unknown uw_handle return code!\n");
217 217
218 break; 218 break;
219 } 219 }
220 220
221 lw_reset_keep_request(ctx); 221 uw_reset_keep_request(ctx);
222 } 222 }
223 223
224 lw_send(ctx, sock); 224 uw_send(ctx, sock);
225 225
226 printf("Done with client.\n\n"); 226 printf("Done with client.\n\n");
227 break; 227 break;
228 } 228 }
229 } 229 }
230 230
231 close(sock); 231 close(sock);
232 lw_reset(ctx); 232 uw_reset(ctx);
233 } 233 }
234 } 234 }
235 235
236 int main(int argc, char *argv[]) { 236 int main(int argc, char *argv[]) {
237 // The skeleton for this function comes from Beej's sockets tutorial. 237 // The skeleton for this function comes from Beej's sockets tutorial.
264 fprintf(stderr, "Listener socket option setting failed\n"); 264 fprintf(stderr, "Listener socket option setting failed\n");
265 return 1; 265 return 1;
266 } 266 }
267 267
268 my_addr.sin_family = AF_INET; // host byte order 268 my_addr.sin_family = AF_INET; // host byte order
269 my_addr.sin_port = htons(lw_port); // short, network byte order 269 my_addr.sin_port = htons(uw_port); // short, network byte order
270 my_addr.sin_addr.s_addr = INADDR_ANY; // auto-fill with my IP 270 my_addr.sin_addr.s_addr = INADDR_ANY; // auto-fill with my IP
271 memset(my_addr.sin_zero, '\0', sizeof my_addr.sin_zero); 271 memset(my_addr.sin_zero, '\0', sizeof my_addr.sin_zero);
272 272
273 if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof my_addr) < 0) { 273 if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof my_addr) < 0) {
274 fprintf(stderr, "Listener socket bind failed\n"); 274 fprintf(stderr, "Listener socket bind failed\n");
275 return 1; 275 return 1;
276 } 276 }
277 277
278 if (listen(sockfd, lw_backlog) < 0) { 278 if (listen(sockfd, uw_backlog) < 0) {
279 fprintf(stderr, "Socket listen failed\n"); 279 fprintf(stderr, "Socket listen failed\n");
280 return 1; 280 return 1;
281 } 281 }
282 282
283 sin_size = sizeof their_addr; 283 sin_size = sizeof their_addr;
284 284
285 printf("Listening on port %d....\n", lw_port); 285 printf("Listening on port %d....\n", uw_port);
286 286
287 for (i = 0; i < nthreads; ++i) { 287 for (i = 0; i < nthreads; ++i) {
288 pthread_t thread; 288 pthread_t thread;
289 names[i] = i; 289 names[i] = i;
290 if (pthread_create(&thread, NULL, worker, &names[i])) { 290 if (pthread_create(&thread, NULL, worker, &names[i])) {