annotate src/c/driver.c @ 660:9abeb533f6a7

Describe AJAX RPC structure
author Adam Chlipala <adamc@hcoop.net>
date Thu, 12 Mar 2009 12:18:11 -0400
parents 162d5308e34f
children a93d5324f400
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@424 196 if (uw_db_begin(ctx)) {
adamc@424 197 printf("Error running SQL BEGIN\n");
adamc@424 198 if (retries_left)
adamc@424 199 --retries_left;
adamc@424 200 else {
adamc@424 201 fk = FATAL;
adamc@424 202 uw_reset(ctx);
adamc@464 203 uw_write_header(ctx, "HTTP/1.1 500 Internal Server Error\n\r");
adamc@464 204 uw_write_header(ctx, "Content-type: text/plain\r\n\r\n");
adamc@424 205 uw_write(ctx, "Error running SQL BEGIN\n");
adamc@424 206
adamc@424 207 break;
adamc@424 208 }
adamc@424 209 }
adamc@167 210
adamc@462 211 uw_write_header(ctx, "HTTP/1.1 200 OK\r\n");
adamc@167 212
adamc@400 213 strcpy(path_copy, path);
adamc@458 214 fk = uw_begin(ctx, path_copy);
adamc@167 215 if (fk == SUCCESS) {
adamc@424 216 if (uw_db_commit(ctx)) {
adamc@424 217 fk = FATAL;
adamc@424 218
adamc@424 219 printf("Error running SQL COMMIT\n");
adamc@424 220 uw_reset(ctx);
adamc@464 221 uw_write_header(ctx, "HTTP/1.1 500 Internal Server Error\n\r");
adamc@464 222 uw_write_header(ctx, "Content-type: text/plain\r\n");
adamc@424 223 uw_write(ctx, "Error running SQL COMMIT\n");
adamc@424 224 }
adamc@424 225
adamc@167 226 break;
adamc@167 227 } else if (fk == BOUNDED_RETRY) {
adamc@167 228 if (retries_left) {
adamc@311 229 printf("Error triggers bounded retry: %s\n", uw_error_message(ctx));
adamc@167 230 --retries_left;
adamc@167 231 }
adamc@167 232 else {
adamc@311 233 printf("Fatal error (out of retries): %s\n", uw_error_message(ctx));
adamc@167 234
adamc@311 235 uw_reset_keep_error_message(ctx);
adamc@464 236 uw_write_header(ctx, "HTTP/1.1 500 Internal Server Error\n\r");
adamc@464 237 uw_write_header(ctx, "Content-type: text/plain\r\n");
adamc@311 238 uw_write(ctx, "Fatal error (out of retries): ");
adamc@311 239 uw_write(ctx, uw_error_message(ctx));
adamc@311 240 uw_write(ctx, "\n");
adamc@424 241
adamc@424 242 try_rollback(ctx);
adamc@424 243
adamc@424 244 break;
adamc@167 245 }
adamc@167 246 } else if (fk == UNLIMITED_RETRY)
adamc@311 247 printf("Error triggers unlimited retry: %s\n", uw_error_message(ctx));
adamc@167 248 else if (fk == FATAL) {
adamc@311 249 printf("Fatal error: %s\n", uw_error_message(ctx));
adamc@167 250
adamc@311 251 uw_reset_keep_error_message(ctx);
adamc@464 252 uw_write_header(ctx, "HTTP/1.1 500 Internal Server Error\r\n");
adamc@548 253 uw_write_header(ctx, "Content-type: text/html\r\n");
adamc@548 254 uw_write(ctx, "<html><head><title>Fatal Error</title></head><body>");
adamc@311 255 uw_write(ctx, "Fatal error: ");
adamc@311 256 uw_write(ctx, uw_error_message(ctx));
adamc@548 257 uw_write(ctx, "\n</body></html>");
adamc@167 258
adamc@424 259 try_rollback(ctx);
adamc@424 260
adamc@167 261 break;
adamc@167 262 } else {
adamc@311 263 printf("Unknown uw_handle return code!\n");
adamc@167 264
adamc@311 265 uw_reset_keep_request(ctx);
adamc@464 266 uw_write_header(ctx, "HTTP/1.1 500 Internal Server Error\n\r");
adamc@464 267 uw_write_header(ctx, "Content-type: text/plain\r\n");
adamc@311 268 uw_write(ctx, "Unknown uw_handle return code!\n");
adamc@167 269
adamc@424 270 try_rollback(ctx);
adamc@424 271
adamc@167 272 break;
adamc@167 273 }
adamc@167 274
adamc@311 275 uw_reset_keep_request(ctx);
adamc@424 276
adamc@424 277 if (try_rollback(ctx))
adamc@424 278 break;
adamc@167 279 }
adamc@138 280
adamc@311 281 uw_send(ctx, sock);
adamc@138 282
adamc@138 283 printf("Done with client.\n\n");
adamc@324 284 uw_memstats(ctx);
adamc@138 285 break;
adamc@138 286 }
adamc@116 287 }
adamc@116 288
adamc@138 289 close(sock);
adamc@311 290 uw_reset(ctx);
adamc@116 291 }
adamc@116 292 }
adamc@102 293
adamc@477 294 static void help(char *cmd) {
adamc@477 295 printf("Usage: %s [-p <port>] [-t <thread-count>]\n", cmd);
adamc@477 296 }
adamc@477 297
adamc@502 298 static void sigint(int signum) {
adamc@502 299 printf("Exiting....\n");
adamc@502 300 exit(0);
adamc@502 301 }
adamc@502 302
adamc@138 303 int main(int argc, char *argv[]) {
adamc@116 304 // The skeleton for this function comes from Beej's sockets tutorial.
adamc@138 305 int sockfd; // listen on sock_fd
adamc@116 306 struct sockaddr_in my_addr;
adamc@116 307 struct sockaddr_in their_addr; // connector's address information
adamc@116 308 int sin_size, yes = 1;
adamc@472 309 int uw_port = 8080, nthreads = 1, i, *names, opt;
adamc@502 310
adamc@502 311 signal(SIGINT, sigint);
adamc@505 312 signal(SIGPIPE, SIG_IGN);
adamc@505 313
adamc@477 314 while ((opt = getopt(argc, argv, "hp:t:")) != -1) {
adamc@472 315 switch (opt) {
adamc@472 316 case '?':
adamc@472 317 fprintf(stderr, "Unknown command-line option");
adamc@477 318 help(argv[0]);
adamc@472 319 return 1;
adamc@138 320
adamc@477 321 case 'h':
adamc@477 322 help(argv[0]);
adamc@477 323 return 0;
adamc@477 324
adamc@472 325 case 'p':
adamc@472 326 uw_port = atoi(optarg);
adamc@472 327 if (uw_port <= 0) {
adamc@472 328 fprintf(stderr, "Invalid port number\n");
adamc@477 329 help(argv[0]);
adamc@472 330 return 1;
adamc@472 331 }
adamc@472 332 break;
adamc@472 333
adamc@472 334 case 't':
adamc@472 335 nthreads = atoi(optarg);
adamc@472 336 if (nthreads <= 0) {
adamc@472 337 fprintf(stderr, "Invalid thread count\n");
adamc@477 338 help(argv[0]);
adamc@472 339 return 1;
adamc@472 340 }
adamc@472 341 break;
adamc@472 342
adamc@472 343 default:
adamc@472 344 fprintf(stderr, "Unexpected getopt() behavior\n");
adamc@472 345 return 1;
adamc@472 346 }
adamc@138 347 }
adamc@138 348
adamc@138 349 names = calloc(nthreads, sizeof(int));
adamc@116 350
adamc@116 351 sockfd = socket(PF_INET, SOCK_STREAM, 0); // do some error checking!
adamc@116 352
adamc@116 353 if (sockfd < 0) {
adamc@116 354 fprintf(stderr, "Listener socket creation failed\n");
adamc@116 355 return 1;
adamc@116 356 }
adamc@116 357
adamc@116 358 if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) < 0) {
adamc@116 359 fprintf(stderr, "Listener socket option setting failed\n");
adamc@116 360 return 1;
adamc@116 361 }
adamc@116 362
adamc@116 363 my_addr.sin_family = AF_INET; // host byte order
adamc@311 364 my_addr.sin_port = htons(uw_port); // short, network byte order
adamc@116 365 my_addr.sin_addr.s_addr = INADDR_ANY; // auto-fill with my IP
adamc@116 366 memset(my_addr.sin_zero, '\0', sizeof my_addr.sin_zero);
adamc@116 367
adamc@116 368 if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof my_addr) < 0) {
adamc@116 369 fprintf(stderr, "Listener socket bind failed\n");
adamc@116 370 return 1;
adamc@116 371 }
adamc@116 372
adamc@311 373 if (listen(sockfd, uw_backlog) < 0) {
adamc@116 374 fprintf(stderr, "Socket listen failed\n");
adamc@116 375 return 1;
adamc@116 376 }
adamc@116 377
adamc@116 378 sin_size = sizeof their_addr;
adamc@116 379
adamc@311 380 printf("Listening on port %d....\n", uw_port);
adamc@116 381
adamc@138 382 for (i = 0; i < nthreads; ++i) {
adamc@138 383 pthread_t thread;
adamc@138 384 names[i] = i;
adamc@138 385 if (pthread_create(&thread, NULL, worker, &names[i])) {
adamc@138 386 fprintf(stderr, "Error creating worker thread #%d\n", i);
adamc@138 387 return 1;
adamc@138 388 }
adamc@138 389 }
adamc@138 390
adamc@116 391 while (1) {
adamc@138 392 int new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size);
adamc@116 393
adamc@116 394 if (new_fd < 0) {
adamc@116 395 fprintf(stderr, "Socket accept failed\n");
adamc@116 396 return 1;
adamc@116 397 }
adamc@116 398
adamc@116 399 printf("Accepted connection.\n");
adamc@138 400
adamc@138 401 pthread_mutex_lock(&queue_mutex);
adamc@138 402 enqueue(new_fd);
adamc@139 403 pthread_cond_broadcast(&queue_cond);
adamc@138 404 pthread_mutex_unlock(&queue_mutex);
adamc@116 405 }
adamc@102 406 }