annotate src/c/driver.c @ 668:b0c1a46b1f15

First message send delivered, but not interpreted
author Adam Chlipala <adamc@hcoop.net>
date Sun, 22 Mar 2009 15:05:07 -0400
parents a93d5324f400
children 729e65db2e2f
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@667 109 int sock, dont_close = 0;
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@668 171 uw_set_headers(ctx, headers);
adamc@668 172
adamc@668 173 if (!strcmp(path, "/.msgs")) {
adamc@668 174 char *id = uw_Basis_requestHeader(ctx, "UrWeb-Client");
adamc@668 175 char *pass = uw_Basis_requestHeader(ctx, "UrWeb-Pass");
adamc@668 176
adamc@668 177 if (id && pass) {
adamc@668 178 size_t idn = atoi(id);
adamc@668 179 uw_client_connect(idn, atoi(pass), sock);
adamc@668 180 dont_close = 1;
adamc@668 181 fprintf(stderr, "Processed request for messages by client %d\n\n", (int)idn);
adamc@668 182 }
adamc@667 183 break;
adamc@667 184 }
adamc@667 185
adamc@144 186 if (inputs = strchr(path, '?')) {
adamc@144 187 char *name, *value;
adamc@144 188 *inputs++ = 0;
adamc@144 189
adamc@144 190 while (*inputs) {
adamc@144 191 name = inputs;
adamc@145 192 if (inputs = strchr(inputs, '&'))
adamc@145 193 *inputs++ = 0;
adamc@145 194 else
adamc@145 195 inputs = strchr(name, 0);
adamc@145 196
adamc@145 197 if (value = strchr(name, '=')) {
adamc@144 198 *value++ = 0;
adamc@311 199 uw_set_input(ctx, name, value);
adamc@144 200 }
adamc@145 201 else
adamc@311 202 uw_set_input(ctx, name, "");
adamc@144 203 }
adamc@144 204 }
adamc@144 205
adamc@138 206 printf("Serving URI %s....\n", path);
adamc@138 207
adamc@167 208 while (1) {
adamc@424 209 if (uw_db_begin(ctx)) {
adamc@424 210 printf("Error running SQL BEGIN\n");
adamc@424 211 if (retries_left)
adamc@424 212 --retries_left;
adamc@424 213 else {
adamc@424 214 fk = FATAL;
adamc@424 215 uw_reset(ctx);
adamc@464 216 uw_write_header(ctx, "HTTP/1.1 500 Internal Server Error\n\r");
adamc@464 217 uw_write_header(ctx, "Content-type: text/plain\r\n\r\n");
adamc@424 218 uw_write(ctx, "Error running SQL BEGIN\n");
adamc@424 219
adamc@424 220 break;
adamc@424 221 }
adamc@424 222 }
adamc@167 223
adamc@462 224 uw_write_header(ctx, "HTTP/1.1 200 OK\r\n");
adamc@167 225
adamc@400 226 strcpy(path_copy, path);
adamc@458 227 fk = uw_begin(ctx, path_copy);
adamc@167 228 if (fk == SUCCESS) {
adamc@424 229 if (uw_db_commit(ctx)) {
adamc@424 230 fk = FATAL;
adamc@424 231
adamc@424 232 printf("Error running SQL COMMIT\n");
adamc@424 233 uw_reset(ctx);
adamc@464 234 uw_write_header(ctx, "HTTP/1.1 500 Internal Server Error\n\r");
adamc@464 235 uw_write_header(ctx, "Content-type: text/plain\r\n");
adamc@424 236 uw_write(ctx, "Error running SQL COMMIT\n");
adamc@424 237 }
adamc@424 238
adamc@167 239 break;
adamc@167 240 } else if (fk == BOUNDED_RETRY) {
adamc@167 241 if (retries_left) {
adamc@311 242 printf("Error triggers bounded retry: %s\n", uw_error_message(ctx));
adamc@167 243 --retries_left;
adamc@167 244 }
adamc@167 245 else {
adamc@311 246 printf("Fatal error (out of retries): %s\n", uw_error_message(ctx));
adamc@167 247
adamc@311 248 uw_reset_keep_error_message(ctx);
adamc@464 249 uw_write_header(ctx, "HTTP/1.1 500 Internal Server Error\n\r");
adamc@464 250 uw_write_header(ctx, "Content-type: text/plain\r\n");
adamc@311 251 uw_write(ctx, "Fatal error (out of retries): ");
adamc@311 252 uw_write(ctx, uw_error_message(ctx));
adamc@311 253 uw_write(ctx, "\n");
adamc@424 254
adamc@424 255 try_rollback(ctx);
adamc@424 256
adamc@424 257 break;
adamc@167 258 }
adamc@167 259 } else if (fk == UNLIMITED_RETRY)
adamc@311 260 printf("Error triggers unlimited retry: %s\n", uw_error_message(ctx));
adamc@167 261 else if (fk == FATAL) {
adamc@311 262 printf("Fatal error: %s\n", uw_error_message(ctx));
adamc@167 263
adamc@311 264 uw_reset_keep_error_message(ctx);
adamc@464 265 uw_write_header(ctx, "HTTP/1.1 500 Internal Server Error\r\n");
adamc@548 266 uw_write_header(ctx, "Content-type: text/html\r\n");
adamc@548 267 uw_write(ctx, "<html><head><title>Fatal Error</title></head><body>");
adamc@311 268 uw_write(ctx, "Fatal error: ");
adamc@311 269 uw_write(ctx, uw_error_message(ctx));
adamc@548 270 uw_write(ctx, "\n</body></html>");
adamc@167 271
adamc@424 272 try_rollback(ctx);
adamc@424 273
adamc@167 274 break;
adamc@167 275 } else {
adamc@311 276 printf("Unknown uw_handle return code!\n");
adamc@167 277
adamc@311 278 uw_reset_keep_request(ctx);
adamc@464 279 uw_write_header(ctx, "HTTP/1.1 500 Internal Server Error\n\r");
adamc@464 280 uw_write_header(ctx, "Content-type: text/plain\r\n");
adamc@311 281 uw_write(ctx, "Unknown uw_handle return code!\n");
adamc@167 282
adamc@424 283 try_rollback(ctx);
adamc@424 284
adamc@167 285 break;
adamc@167 286 }
adamc@167 287
adamc@311 288 uw_reset_keep_request(ctx);
adamc@424 289
adamc@424 290 if (try_rollback(ctx))
adamc@424 291 break;
adamc@167 292 }
adamc@138 293
adamc@311 294 uw_send(ctx, sock);
adamc@138 295
adamc@138 296 printf("Done with client.\n\n");
adamc@324 297 uw_memstats(ctx);
adamc@138 298 break;
adamc@138 299 }
adamc@116 300 }
adamc@116 301
adamc@667 302 if (!dont_close)
adamc@667 303 close(sock);
adamc@311 304 uw_reset(ctx);
adamc@116 305 }
adamc@116 306 }
adamc@102 307
adamc@667 308 static void *client_pruner(void *data) {
adamc@667 309 while (1) {
adamc@667 310 uw_prune_clients(5);
adamc@667 311 sleep(5);
adamc@667 312 }
adamc@667 313 }
adamc@667 314
adamc@477 315 static void help(char *cmd) {
adamc@477 316 printf("Usage: %s [-p <port>] [-t <thread-count>]\n", cmd);
adamc@477 317 }
adamc@477 318
adamc@502 319 static void sigint(int signum) {
adamc@502 320 printf("Exiting....\n");
adamc@502 321 exit(0);
adamc@502 322 }
adamc@502 323
adamc@138 324 int main(int argc, char *argv[]) {
adamc@116 325 // The skeleton for this function comes from Beej's sockets tutorial.
adamc@138 326 int sockfd; // listen on sock_fd
adamc@116 327 struct sockaddr_in my_addr;
adamc@116 328 struct sockaddr_in their_addr; // connector's address information
adamc@116 329 int sin_size, yes = 1;
adamc@472 330 int uw_port = 8080, nthreads = 1, i, *names, opt;
adamc@502 331
adamc@502 332 signal(SIGINT, sigint);
adamc@505 333 signal(SIGPIPE, SIG_IGN);
adamc@505 334
adamc@477 335 while ((opt = getopt(argc, argv, "hp:t:")) != -1) {
adamc@472 336 switch (opt) {
adamc@472 337 case '?':
adamc@472 338 fprintf(stderr, "Unknown command-line option");
adamc@477 339 help(argv[0]);
adamc@472 340 return 1;
adamc@138 341
adamc@477 342 case 'h':
adamc@477 343 help(argv[0]);
adamc@477 344 return 0;
adamc@477 345
adamc@472 346 case 'p':
adamc@472 347 uw_port = atoi(optarg);
adamc@472 348 if (uw_port <= 0) {
adamc@472 349 fprintf(stderr, "Invalid port number\n");
adamc@477 350 help(argv[0]);
adamc@472 351 return 1;
adamc@472 352 }
adamc@472 353 break;
adamc@472 354
adamc@472 355 case 't':
adamc@472 356 nthreads = atoi(optarg);
adamc@472 357 if (nthreads <= 0) {
adamc@472 358 fprintf(stderr, "Invalid thread count\n");
adamc@477 359 help(argv[0]);
adamc@472 360 return 1;
adamc@472 361 }
adamc@472 362 break;
adamc@472 363
adamc@472 364 default:
adamc@472 365 fprintf(stderr, "Unexpected getopt() behavior\n");
adamc@472 366 return 1;
adamc@472 367 }
adamc@138 368 }
adamc@138 369
adamc@138 370 names = calloc(nthreads, sizeof(int));
adamc@116 371
adamc@116 372 sockfd = socket(PF_INET, SOCK_STREAM, 0); // do some error checking!
adamc@116 373
adamc@116 374 if (sockfd < 0) {
adamc@116 375 fprintf(stderr, "Listener socket creation failed\n");
adamc@116 376 return 1;
adamc@116 377 }
adamc@116 378
adamc@116 379 if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) < 0) {
adamc@116 380 fprintf(stderr, "Listener socket option setting failed\n");
adamc@116 381 return 1;
adamc@116 382 }
adamc@116 383
adamc@116 384 my_addr.sin_family = AF_INET; // host byte order
adamc@311 385 my_addr.sin_port = htons(uw_port); // short, network byte order
adamc@116 386 my_addr.sin_addr.s_addr = INADDR_ANY; // auto-fill with my IP
adamc@116 387 memset(my_addr.sin_zero, '\0', sizeof my_addr.sin_zero);
adamc@116 388
adamc@116 389 if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof my_addr) < 0) {
adamc@116 390 fprintf(stderr, "Listener socket bind failed\n");
adamc@116 391 return 1;
adamc@116 392 }
adamc@116 393
adamc@311 394 if (listen(sockfd, uw_backlog) < 0) {
adamc@116 395 fprintf(stderr, "Socket listen failed\n");
adamc@116 396 return 1;
adamc@116 397 }
adamc@116 398
adamc@116 399 sin_size = sizeof their_addr;
adamc@116 400
adamc@667 401 uw_global_init();
adamc@667 402
adamc@311 403 printf("Listening on port %d....\n", uw_port);
adamc@116 404
adamc@667 405 {
adamc@667 406 pthread_t thread;
adamc@667 407 int name;
adamc@667 408
adamc@667 409 if (pthread_create(&thread, NULL, client_pruner, &name)) {
adamc@667 410 fprintf(stderr, "Error creating pruner thread\n");
adamc@667 411 return 1;
adamc@667 412 }
adamc@667 413 }
adamc@667 414
adamc@138 415 for (i = 0; i < nthreads; ++i) {
adamc@138 416 pthread_t thread;
adamc@138 417 names[i] = i;
adamc@138 418 if (pthread_create(&thread, NULL, worker, &names[i])) {
adamc@138 419 fprintf(stderr, "Error creating worker thread #%d\n", i);
adamc@138 420 return 1;
adamc@138 421 }
adamc@138 422 }
adamc@138 423
adamc@116 424 while (1) {
adamc@138 425 int new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size);
adamc@116 426
adamc@116 427 if (new_fd < 0) {
adamc@116 428 fprintf(stderr, "Socket accept failed\n");
adamc@116 429 return 1;
adamc@116 430 }
adamc@116 431
adamc@116 432 printf("Accepted connection.\n");
adamc@138 433
adamc@138 434 pthread_mutex_lock(&queue_mutex);
adamc@138 435 enqueue(new_fd);
adamc@139 436 pthread_cond_broadcast(&queue_cond);
adamc@138 437 pthread_mutex_unlock(&queue_mutex);
adamc@116 438 }
adamc@102 439 }