annotate src/c/driver.c @ 563:44958d74c43f

Initial conversion to arbitrary-kind classes
author Adam Chlipala <adamc@hcoop.net>
date Fri, 19 Dec 2008 10:03:31 -0500
parents 6daa59a55c43
children 162d5308e34f
rev   line source
adamc@116 1 #include <stdio.h>
adamc@116 2
adamc@116 3 #include <string.h>
adamc@502 4 #include <stdlib.h>
adamc@116 5 #include <sys/types.h>
adamc@116 6 #include <sys/socket.h>
adamc@116 7 #include <netinet/in.h>
adamc@472 8 #include <unistd.h>
adamc@502 9 #include <signal.h>
adamc@116 10
adamc@138 11 #include <pthread.h>
adamc@138 12
adamc@244 13 #include "urweb.h"
adamc@117 14
adamc@311 15 int uw_backlog = 10;
adamc@311 16 int uw_bufsize = 1024;
adamc@116 17
adamc@138 18 typedef struct node {
adamc@138 19 int fd;
adamc@138 20 struct node *next;
adamc@138 21 } *node;
adamc@138 22
adamc@138 23 static node front = NULL, back = NULL;
adamc@138 24
adamc@138 25 static int empty() {
adamc@138 26 return front == NULL;
adamc@138 27 }
adamc@138 28
adamc@138 29 static void enqueue(int fd) {
adamc@138 30 node n = malloc(sizeof(struct node));
adamc@138 31
adamc@138 32 n->fd = fd;
adamc@138 33 n->next = NULL;
adamc@138 34 if (back)
adamc@138 35 back->next = n;
adamc@138 36 else
adamc@138 37 front = n;
adamc@138 38 back = n;
adamc@138 39 }
adamc@138 40
adamc@138 41 static int dequeue() {
adamc@138 42 int ret = front->fd;
adamc@138 43
adamc@138 44 front = front->next;
adamc@138 45 if (!front)
adamc@138 46 back = NULL;
adamc@138 47
adamc@138 48 return ret;
adamc@138 49 }
adamc@138 50
adamc@138 51 static pthread_mutex_t queue_mutex = PTHREAD_MUTEX_INITIALIZER;
adamc@138 52 static pthread_cond_t queue_cond = PTHREAD_COND_INITIALIZER;
adamc@138 53
adamc@167 54 #define MAX_RETRIES 5
adamc@167 55
adamc@424 56 int uw_db_begin(uw_context);
adamc@424 57 int uw_db_commit(uw_context);
adamc@424 58 int uw_db_rollback(uw_context);
adamc@424 59
adamc@424 60 static int try_rollback(uw_context ctx) {
adamc@424 61 int r = uw_db_rollback(ctx);
adamc@424 62
adamc@424 63 if (r) {
adamc@424 64 printf("Error running SQL ROLLBACK\n");
adamc@424 65 uw_reset(ctx);
adamc@424 66 uw_write(ctx, "HTTP/1.1 500 Internal Server Error\n\r");
adamc@424 67 uw_write(ctx, "Content-type: text/plain\r\n\r\n");
adamc@424 68 uw_write(ctx, "Error running SQL ROLLBACK\n");
adamc@424 69 }
adamc@424 70
adamc@424 71 return r;
adamc@424 72 }
adamc@424 73
adamc@138 74 static void *worker(void *data) {
adamc@324 75 int me = *(int *)data, retries_left = MAX_RETRIES;
adamc@562 76 uw_context ctx = uw_init(0, 0, 1024, 0);
adamc@272 77
adamc@272 78 while (1) {
adamc@311 79 failure_kind fk = uw_begin_init(ctx);
adamc@272 80
adamc@272 81 if (fk == SUCCESS) {
adamc@311 82 uw_db_init(ctx);
adamc@272 83 printf("Database connection initialized.\n");
adamc@272 84 break;
adamc@272 85 } else if (fk == BOUNDED_RETRY) {
adamc@272 86 if (retries_left) {
adamc@311 87 printf("Initialization error triggers bounded retry: %s\n", uw_error_message(ctx));
adamc@272 88 --retries_left;
adamc@272 89 } else {
adamc@311 90 printf("Fatal initialization error (out of retries): %s\n", uw_error_message(ctx));
adamc@311 91 uw_free(ctx);
adamc@272 92 return NULL;
adamc@272 93 }
adamc@272 94 } else if (fk == UNLIMITED_RETRY)
adamc@311 95 printf("Initialization error triggers unlimited retry: %s\n", uw_error_message(ctx));
adamc@272 96 else if (fk == FATAL) {
adamc@311 97 printf("Fatal initialization error: %s\n", uw_error_message(ctx));
adamc@311 98 uw_free(ctx);
adamc@272 99 return NULL;
adamc@272 100 } else {
adamc@311 101 printf("Unknown uw_handle return code!\n");
adamc@311 102 uw_free(ctx);
adamc@272 103 return NULL;
adamc@272 104 }
adamc@272 105 }
adamc@116 106
adamc@116 107 while (1) {
adamc@311 108 char buf[uw_bufsize+1], *back = buf, *s;
adamc@138 109 int sock;
adamc@116 110
adamc@138 111 pthread_mutex_lock(&queue_mutex);
adamc@138 112 while (empty())
adamc@138 113 pthread_cond_wait(&queue_cond, &queue_mutex);
adamc@138 114 sock = dequeue();
adamc@138 115 pthread_mutex_unlock(&queue_mutex);
adamc@138 116
adamc@138 117 printf("Handling connection with thread #%d.\n", me);
adamc@138 118
adamc@138 119 while (1) {
adamc@167 120 unsigned retries_left = MAX_RETRIES;
adamc@311 121 int r = recv(sock, back, uw_bufsize - (back - buf), 0);
adamc@138 122
adamc@138 123 if (r < 0) {
adamc@138 124 fprintf(stderr, "Recv failed\n");
adamc@138 125 break;
adamc@138 126 }
adamc@138 127
adamc@138 128 if (r == 0) {
adamc@138 129 printf("Connection closed.\n");
adamc@138 130 break;
adamc@138 131 }
adamc@138 132
adamc@138 133 printf("Received %d bytes.\n", r);
adamc@138 134
adamc@138 135 back += r;
adamc@138 136 *back = 0;
adamc@138 137
adamc@138 138 if (s = strstr(buf, "\r\n\r\n")) {
adamc@424 139 failure_kind fk;
adamc@457 140 char *cmd, *path, *headers, path_copy[uw_bufsize+1], *inputs;
adamc@138 141
adamc@475 142 s[2] = 0;
adamc@144 143
adamc@138 144 if (!(s = strstr(buf, "\r\n"))) {
adamc@138 145 fprintf(stderr, "No newline in buf\n");
adamc@138 146 break;
adamc@138 147 }
adamc@138 148
adamc@138 149 *s = 0;
adamc@457 150 headers = s + 2;
adamc@138 151 cmd = s = buf;
adamc@401 152
adamc@401 153 printf("Read: %s\n", buf);
adamc@138 154
adamc@138 155 if (!strsep(&s, " ")) {
adamc@138 156 fprintf(stderr, "No first space in HTTP command\n");
adamc@138 157 break;
adamc@138 158 }
adamc@138 159
adamc@138 160 if (strcmp(cmd, "GET")) {
adamc@138 161 fprintf(stderr, "Not ready for non-get command: %s\n", cmd);
adamc@138 162 break;
adamc@138 163 }
adamc@138 164
adamc@138 165 path = s;
adamc@138 166 if (!strsep(&s, " ")) {
adamc@138 167 fprintf(stderr, "No second space in HTTP command\n");
adamc@138 168 break;
adamc@138 169 }
adamc@138 170
adamc@144 171 if (inputs = strchr(path, '?')) {
adamc@144 172 char *name, *value;
adamc@144 173 *inputs++ = 0;
adamc@144 174
adamc@144 175 while (*inputs) {
adamc@144 176 name = inputs;
adamc@145 177 if (inputs = strchr(inputs, '&'))
adamc@145 178 *inputs++ = 0;
adamc@145 179 else
adamc@145 180 inputs = strchr(name, 0);
adamc@145 181
adamc@145 182 if (value = strchr(name, '=')) {
adamc@144 183 *value++ = 0;
adamc@311 184 uw_set_input(ctx, name, value);
adamc@144 185 }
adamc@145 186 else
adamc@311 187 uw_set_input(ctx, name, "");
adamc@144 188 }
adamc@144 189 }
adamc@144 190
adamc@138 191 printf("Serving URI %s....\n", path);
adamc@138 192
adamc@458 193 uw_set_headers(ctx, headers);
adamc@458 194
adamc@167 195 while (1) {
adamc@500 196 uw_write(ctx, "<html>");
adamc@500 197
adamc@424 198 if (uw_db_begin(ctx)) {
adamc@424 199 printf("Error running SQL BEGIN\n");
adamc@424 200 if (retries_left)
adamc@424 201 --retries_left;
adamc@424 202 else {
adamc@424 203 fk = FATAL;
adamc@424 204 uw_reset(ctx);
adamc@464 205 uw_write_header(ctx, "HTTP/1.1 500 Internal Server Error\n\r");
adamc@464 206 uw_write_header(ctx, "Content-type: text/plain\r\n\r\n");
adamc@424 207 uw_write(ctx, "Error running SQL BEGIN\n");
adamc@424 208
adamc@424 209 break;
adamc@424 210 }
adamc@424 211 }
adamc@167 212
adamc@462 213 uw_write_header(ctx, "HTTP/1.1 200 OK\r\n");
adamc@462 214 uw_write_header(ctx, "Content-type: text/html\r\n");
adamc@167 215
adamc@400 216 strcpy(path_copy, path);
adamc@458 217 fk = uw_begin(ctx, path_copy);
adamc@167 218 if (fk == SUCCESS) {
adamc@464 219 uw_write(ctx, "</html>");
adamc@464 220
adamc@424 221 if (uw_db_commit(ctx)) {
adamc@424 222 fk = FATAL;
adamc@424 223
adamc@424 224 printf("Error running SQL COMMIT\n");
adamc@424 225 uw_reset(ctx);
adamc@464 226 uw_write_header(ctx, "HTTP/1.1 500 Internal Server Error\n\r");
adamc@464 227 uw_write_header(ctx, "Content-type: text/plain\r\n");
adamc@424 228 uw_write(ctx, "Error running SQL COMMIT\n");
adamc@424 229 }
adamc@424 230
adamc@167 231 break;
adamc@167 232 } else if (fk == BOUNDED_RETRY) {
adamc@167 233 if (retries_left) {
adamc@311 234 printf("Error triggers bounded retry: %s\n", uw_error_message(ctx));
adamc@167 235 --retries_left;
adamc@167 236 }
adamc@167 237 else {
adamc@311 238 printf("Fatal error (out of retries): %s\n", uw_error_message(ctx));
adamc@167 239
adamc@311 240 uw_reset_keep_error_message(ctx);
adamc@464 241 uw_write_header(ctx, "HTTP/1.1 500 Internal Server Error\n\r");
adamc@464 242 uw_write_header(ctx, "Content-type: text/plain\r\n");
adamc@311 243 uw_write(ctx, "Fatal error (out of retries): ");
adamc@311 244 uw_write(ctx, uw_error_message(ctx));
adamc@311 245 uw_write(ctx, "\n");
adamc@424 246
adamc@424 247 try_rollback(ctx);
adamc@424 248
adamc@424 249 break;
adamc@167 250 }
adamc@167 251 } else if (fk == UNLIMITED_RETRY)
adamc@311 252 printf("Error triggers unlimited retry: %s\n", uw_error_message(ctx));
adamc@167 253 else if (fk == FATAL) {
adamc@311 254 printf("Fatal error: %s\n", uw_error_message(ctx));
adamc@167 255
adamc@311 256 uw_reset_keep_error_message(ctx);
adamc@464 257 uw_write_header(ctx, "HTTP/1.1 500 Internal Server Error\r\n");
adamc@548 258 uw_write_header(ctx, "Content-type: text/html\r\n");
adamc@548 259 uw_write(ctx, "<html><head><title>Fatal Error</title></head><body>");
adamc@311 260 uw_write(ctx, "Fatal error: ");
adamc@311 261 uw_write(ctx, uw_error_message(ctx));
adamc@548 262 uw_write(ctx, "\n</body></html>");
adamc@167 263
adamc@424 264 try_rollback(ctx);
adamc@424 265
adamc@167 266 break;
adamc@167 267 } else {
adamc@311 268 printf("Unknown uw_handle return code!\n");
adamc@167 269
adamc@311 270 uw_reset_keep_request(ctx);
adamc@464 271 uw_write_header(ctx, "HTTP/1.1 500 Internal Server Error\n\r");
adamc@464 272 uw_write_header(ctx, "Content-type: text/plain\r\n");
adamc@311 273 uw_write(ctx, "Unknown uw_handle return code!\n");
adamc@167 274
adamc@424 275 try_rollback(ctx);
adamc@424 276
adamc@167 277 break;
adamc@167 278 }
adamc@167 279
adamc@311 280 uw_reset_keep_request(ctx);
adamc@424 281
adamc@424 282 if (try_rollback(ctx))
adamc@424 283 break;
adamc@167 284 }
adamc@138 285
adamc@311 286 uw_send(ctx, sock);
adamc@138 287
adamc@138 288 printf("Done with client.\n\n");
adamc@324 289 uw_memstats(ctx);
adamc@138 290 break;
adamc@138 291 }
adamc@116 292 }
adamc@116 293
adamc@138 294 close(sock);
adamc@311 295 uw_reset(ctx);
adamc@116 296 }
adamc@116 297 }
adamc@102 298
adamc@477 299 static void help(char *cmd) {
adamc@477 300 printf("Usage: %s [-p <port>] [-t <thread-count>]\n", cmd);
adamc@477 301 }
adamc@477 302
adamc@502 303 static void sigint(int signum) {
adamc@502 304 printf("Exiting....\n");
adamc@502 305 exit(0);
adamc@502 306 }
adamc@502 307
adamc@138 308 int main(int argc, char *argv[]) {
adamc@116 309 // The skeleton for this function comes from Beej's sockets tutorial.
adamc@138 310 int sockfd; // listen on sock_fd
adamc@116 311 struct sockaddr_in my_addr;
adamc@116 312 struct sockaddr_in their_addr; // connector's address information
adamc@116 313 int sin_size, yes = 1;
adamc@472 314 int uw_port = 8080, nthreads = 1, i, *names, opt;
adamc@502 315
adamc@502 316 signal(SIGINT, sigint);
adamc@505 317 signal(SIGPIPE, SIG_IGN);
adamc@505 318
adamc@477 319 while ((opt = getopt(argc, argv, "hp:t:")) != -1) {
adamc@472 320 switch (opt) {
adamc@472 321 case '?':
adamc@472 322 fprintf(stderr, "Unknown command-line option");
adamc@477 323 help(argv[0]);
adamc@472 324 return 1;
adamc@138 325
adamc@477 326 case 'h':
adamc@477 327 help(argv[0]);
adamc@477 328 return 0;
adamc@477 329
adamc@472 330 case 'p':
adamc@472 331 uw_port = atoi(optarg);
adamc@472 332 if (uw_port <= 0) {
adamc@472 333 fprintf(stderr, "Invalid port number\n");
adamc@477 334 help(argv[0]);
adamc@472 335 return 1;
adamc@472 336 }
adamc@472 337 break;
adamc@472 338
adamc@472 339 case 't':
adamc@472 340 nthreads = atoi(optarg);
adamc@472 341 if (nthreads <= 0) {
adamc@472 342 fprintf(stderr, "Invalid thread count\n");
adamc@477 343 help(argv[0]);
adamc@472 344 return 1;
adamc@472 345 }
adamc@472 346 break;
adamc@472 347
adamc@472 348 default:
adamc@472 349 fprintf(stderr, "Unexpected getopt() behavior\n");
adamc@472 350 return 1;
adamc@472 351 }
adamc@138 352 }
adamc@138 353
adamc@138 354 names = calloc(nthreads, sizeof(int));
adamc@116 355
adamc@116 356 sockfd = socket(PF_INET, SOCK_STREAM, 0); // do some error checking!
adamc@116 357
adamc@116 358 if (sockfd < 0) {
adamc@116 359 fprintf(stderr, "Listener socket creation failed\n");
adamc@116 360 return 1;
adamc@116 361 }
adamc@116 362
adamc@116 363 if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) < 0) {
adamc@116 364 fprintf(stderr, "Listener socket option setting failed\n");
adamc@116 365 return 1;
adamc@116 366 }
adamc@116 367
adamc@116 368 my_addr.sin_family = AF_INET; // host byte order
adamc@311 369 my_addr.sin_port = htons(uw_port); // short, network byte order
adamc@116 370 my_addr.sin_addr.s_addr = INADDR_ANY; // auto-fill with my IP
adamc@116 371 memset(my_addr.sin_zero, '\0', sizeof my_addr.sin_zero);
adamc@116 372
adamc@116 373 if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof my_addr) < 0) {
adamc@116 374 fprintf(stderr, "Listener socket bind failed\n");
adamc@116 375 return 1;
adamc@116 376 }
adamc@116 377
adamc@311 378 if (listen(sockfd, uw_backlog) < 0) {
adamc@116 379 fprintf(stderr, "Socket listen failed\n");
adamc@116 380 return 1;
adamc@116 381 }
adamc@116 382
adamc@116 383 sin_size = sizeof their_addr;
adamc@116 384
adamc@311 385 printf("Listening on port %d....\n", uw_port);
adamc@116 386
adamc@138 387 for (i = 0; i < nthreads; ++i) {
adamc@138 388 pthread_t thread;
adamc@138 389 names[i] = i;
adamc@138 390 if (pthread_create(&thread, NULL, worker, &names[i])) {
adamc@138 391 fprintf(stderr, "Error creating worker thread #%d\n", i);
adamc@138 392 return 1;
adamc@138 393 }
adamc@138 394 }
adamc@138 395
adamc@116 396 while (1) {
adamc@138 397 int new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size);
adamc@116 398
adamc@116 399 if (new_fd < 0) {
adamc@116 400 fprintf(stderr, "Socket accept failed\n");
adamc@116 401 return 1;
adamc@116 402 }
adamc@116 403
adamc@116 404 printf("Accepted connection.\n");
adamc@138 405
adamc@138 406 pthread_mutex_lock(&queue_mutex);
adamc@138 407 enqueue(new_fd);
adamc@139 408 pthread_cond_broadcast(&queue_cond);
adamc@138 409 pthread_mutex_unlock(&queue_mutex);
adamc@116 410 }
adamc@102 411 }