annotate src/c/driver.c @ 682:5bbb542243e8

Redo channels, making them single-client
author Adam Chlipala <adamc@hcoop.net>
date Sun, 29 Mar 2009 11:37:29 -0400
parents 729e65db2e2f
children 9a2c18dab11d
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 static int try_rollback(uw_context ctx) {
adamc@671 57 int r = uw_rollback(ctx);
adamc@424 58
adamc@424 59 if (r) {
adamc@424 60 printf("Error running SQL ROLLBACK\n");
adamc@424 61 uw_reset(ctx);
adamc@424 62 uw_write(ctx, "HTTP/1.1 500 Internal Server Error\n\r");
adamc@424 63 uw_write(ctx, "Content-type: text/plain\r\n\r\n");
adamc@424 64 uw_write(ctx, "Error running SQL ROLLBACK\n");
adamc@424 65 }
adamc@424 66
adamc@424 67 return r;
adamc@424 68 }
adamc@424 69
adamc@138 70 static void *worker(void *data) {
adamc@324 71 int me = *(int *)data, retries_left = MAX_RETRIES;
adamc@562 72 uw_context ctx = uw_init(0, 0, 1024, 0);
adamc@272 73
adamc@272 74 while (1) {
adamc@311 75 failure_kind fk = uw_begin_init(ctx);
adamc@272 76
adamc@272 77 if (fk == SUCCESS) {
adamc@311 78 uw_db_init(ctx);
adamc@272 79 printf("Database connection initialized.\n");
adamc@272 80 break;
adamc@272 81 } else if (fk == BOUNDED_RETRY) {
adamc@272 82 if (retries_left) {
adamc@311 83 printf("Initialization error triggers bounded retry: %s\n", uw_error_message(ctx));
adamc@272 84 --retries_left;
adamc@272 85 } else {
adamc@311 86 printf("Fatal initialization error (out of retries): %s\n", uw_error_message(ctx));
adamc@311 87 uw_free(ctx);
adamc@272 88 return NULL;
adamc@272 89 }
adamc@272 90 } else if (fk == UNLIMITED_RETRY)
adamc@311 91 printf("Initialization error triggers unlimited retry: %s\n", uw_error_message(ctx));
adamc@272 92 else if (fk == FATAL) {
adamc@311 93 printf("Fatal initialization error: %s\n", uw_error_message(ctx));
adamc@311 94 uw_free(ctx);
adamc@272 95 return NULL;
adamc@272 96 } else {
adamc@311 97 printf("Unknown uw_handle return code!\n");
adamc@311 98 uw_free(ctx);
adamc@272 99 return NULL;
adamc@272 100 }
adamc@272 101 }
adamc@116 102
adamc@116 103 while (1) {
adamc@311 104 char buf[uw_bufsize+1], *back = buf, *s;
adamc@667 105 int sock, dont_close = 0;
adamc@116 106
adamc@138 107 pthread_mutex_lock(&queue_mutex);
adamc@138 108 while (empty())
adamc@138 109 pthread_cond_wait(&queue_cond, &queue_mutex);
adamc@138 110 sock = dequeue();
adamc@138 111 pthread_mutex_unlock(&queue_mutex);
adamc@138 112
adamc@138 113 printf("Handling connection with thread #%d.\n", me);
adamc@138 114
adamc@138 115 while (1) {
adamc@167 116 unsigned retries_left = MAX_RETRIES;
adamc@311 117 int r = recv(sock, back, uw_bufsize - (back - buf), 0);
adamc@138 118
adamc@138 119 if (r < 0) {
adamc@138 120 fprintf(stderr, "Recv failed\n");
adamc@138 121 break;
adamc@138 122 }
adamc@138 123
adamc@138 124 if (r == 0) {
adamc@138 125 printf("Connection closed.\n");
adamc@138 126 break;
adamc@138 127 }
adamc@138 128
adamc@138 129 printf("Received %d bytes.\n", r);
adamc@138 130
adamc@138 131 back += r;
adamc@138 132 *back = 0;
adamc@138 133
adamc@138 134 if (s = strstr(buf, "\r\n\r\n")) {
adamc@424 135 failure_kind fk;
adamc@457 136 char *cmd, *path, *headers, path_copy[uw_bufsize+1], *inputs;
adamc@138 137
adamc@475 138 s[2] = 0;
adamc@144 139
adamc@138 140 if (!(s = strstr(buf, "\r\n"))) {
adamc@138 141 fprintf(stderr, "No newline in buf\n");
adamc@138 142 break;
adamc@138 143 }
adamc@138 144
adamc@138 145 *s = 0;
adamc@457 146 headers = s + 2;
adamc@138 147 cmd = s = buf;
adamc@401 148
adamc@401 149 printf("Read: %s\n", buf);
adamc@138 150
adamc@138 151 if (!strsep(&s, " ")) {
adamc@138 152 fprintf(stderr, "No first space in HTTP command\n");
adamc@138 153 break;
adamc@138 154 }
adamc@138 155
adamc@138 156 if (strcmp(cmd, "GET")) {
adamc@138 157 fprintf(stderr, "Not ready for non-get command: %s\n", cmd);
adamc@138 158 break;
adamc@138 159 }
adamc@138 160
adamc@138 161 path = s;
adamc@138 162 if (!strsep(&s, " ")) {
adamc@138 163 fprintf(stderr, "No second space in HTTP command\n");
adamc@138 164 break;
adamc@138 165 }
adamc@138 166
adamc@668 167 uw_set_headers(ctx, headers);
adamc@668 168
adamc@668 169 if (!strcmp(path, "/.msgs")) {
adamc@668 170 char *id = uw_Basis_requestHeader(ctx, "UrWeb-Client");
adamc@668 171 char *pass = uw_Basis_requestHeader(ctx, "UrWeb-Pass");
adamc@668 172
adamc@668 173 if (id && pass) {
adamc@682 174 unsigned idn = atoi(id);
adamc@668 175 uw_client_connect(idn, atoi(pass), sock);
adamc@668 176 dont_close = 1;
adamc@682 177 fprintf(stderr, "Processed request for messages by client %u\n\n", idn);
adamc@668 178 }
adamc@667 179 break;
adamc@667 180 }
adamc@667 181
adamc@144 182 if (inputs = strchr(path, '?')) {
adamc@144 183 char *name, *value;
adamc@144 184 *inputs++ = 0;
adamc@144 185
adamc@144 186 while (*inputs) {
adamc@144 187 name = inputs;
adamc@145 188 if (inputs = strchr(inputs, '&'))
adamc@145 189 *inputs++ = 0;
adamc@145 190 else
adamc@145 191 inputs = strchr(name, 0);
adamc@145 192
adamc@145 193 if (value = strchr(name, '=')) {
adamc@144 194 *value++ = 0;
adamc@311 195 uw_set_input(ctx, name, value);
adamc@144 196 }
adamc@145 197 else
adamc@311 198 uw_set_input(ctx, name, "");
adamc@144 199 }
adamc@144 200 }
adamc@144 201
adamc@138 202 printf("Serving URI %s....\n", path);
adamc@138 203
adamc@167 204 while (1) {
adamc@462 205 uw_write_header(ctx, "HTTP/1.1 200 OK\r\n");
adamc@167 206
adamc@400 207 strcpy(path_copy, path);
adamc@458 208 fk = uw_begin(ctx, path_copy);
adamc@167 209 if (fk == SUCCESS) {
adamc@671 210 uw_commit(ctx);
adamc@167 211 break;
adamc@167 212 } else if (fk == BOUNDED_RETRY) {
adamc@167 213 if (retries_left) {
adamc@311 214 printf("Error triggers bounded retry: %s\n", uw_error_message(ctx));
adamc@167 215 --retries_left;
adamc@167 216 }
adamc@167 217 else {
adamc@311 218 printf("Fatal error (out of retries): %s\n", uw_error_message(ctx));
adamc@167 219
adamc@682 220 try_rollback(ctx);
adamc@682 221
adamc@311 222 uw_reset_keep_error_message(ctx);
adamc@464 223 uw_write_header(ctx, "HTTP/1.1 500 Internal Server Error\n\r");
adamc@464 224 uw_write_header(ctx, "Content-type: text/plain\r\n");
adamc@311 225 uw_write(ctx, "Fatal error (out of retries): ");
adamc@311 226 uw_write(ctx, uw_error_message(ctx));
adamc@311 227 uw_write(ctx, "\n");
adamc@424 228
adamc@424 229 break;
adamc@167 230 }
adamc@167 231 } else if (fk == UNLIMITED_RETRY)
adamc@311 232 printf("Error triggers unlimited retry: %s\n", uw_error_message(ctx));
adamc@167 233 else if (fk == FATAL) {
adamc@311 234 printf("Fatal error: %s\n", uw_error_message(ctx));
adamc@167 235
adamc@682 236 try_rollback(ctx);
adamc@682 237
adamc@311 238 uw_reset_keep_error_message(ctx);
adamc@464 239 uw_write_header(ctx, "HTTP/1.1 500 Internal Server Error\r\n");
adamc@548 240 uw_write_header(ctx, "Content-type: text/html\r\n");
adamc@548 241 uw_write(ctx, "<html><head><title>Fatal Error</title></head><body>");
adamc@311 242 uw_write(ctx, "Fatal error: ");
adamc@311 243 uw_write(ctx, uw_error_message(ctx));
adamc@548 244 uw_write(ctx, "\n</body></html>");
adamc@167 245
adamc@167 246 break;
adamc@167 247 } else {
adamc@311 248 printf("Unknown uw_handle return code!\n");
adamc@167 249
adamc@682 250 try_rollback(ctx);
adamc@682 251
adamc@311 252 uw_reset_keep_request(ctx);
adamc@464 253 uw_write_header(ctx, "HTTP/1.1 500 Internal Server Error\n\r");
adamc@464 254 uw_write_header(ctx, "Content-type: text/plain\r\n");
adamc@311 255 uw_write(ctx, "Unknown uw_handle return code!\n");
adamc@167 256
adamc@167 257 break;
adamc@167 258 }
adamc@167 259
adamc@424 260 if (try_rollback(ctx))
adamc@424 261 break;
adamc@682 262
adamc@682 263 uw_reset_keep_request(ctx);
adamc@167 264 }
adamc@138 265
adamc@311 266 uw_send(ctx, sock);
adamc@138 267
adamc@138 268 printf("Done with client.\n\n");
adamc@324 269 uw_memstats(ctx);
adamc@138 270 break;
adamc@138 271 }
adamc@116 272 }
adamc@116 273
adamc@667 274 if (!dont_close)
adamc@667 275 close(sock);
adamc@311 276 uw_reset(ctx);
adamc@116 277 }
adamc@116 278 }
adamc@102 279
adamc@667 280 static void *client_pruner(void *data) {
adamc@667 281 while (1) {
adamc@667 282 uw_prune_clients(5);
adamc@667 283 sleep(5);
adamc@667 284 }
adamc@667 285 }
adamc@667 286
adamc@477 287 static void help(char *cmd) {
adamc@477 288 printf("Usage: %s [-p <port>] [-t <thread-count>]\n", cmd);
adamc@477 289 }
adamc@477 290
adamc@502 291 static void sigint(int signum) {
adamc@502 292 printf("Exiting....\n");
adamc@502 293 exit(0);
adamc@502 294 }
adamc@502 295
adamc@138 296 int main(int argc, char *argv[]) {
adamc@116 297 // The skeleton for this function comes from Beej's sockets tutorial.
adamc@138 298 int sockfd; // listen on sock_fd
adamc@116 299 struct sockaddr_in my_addr;
adamc@116 300 struct sockaddr_in their_addr; // connector's address information
adamc@116 301 int sin_size, yes = 1;
adamc@472 302 int uw_port = 8080, nthreads = 1, i, *names, opt;
adamc@502 303
adamc@502 304 signal(SIGINT, sigint);
adamc@505 305 signal(SIGPIPE, SIG_IGN);
adamc@505 306
adamc@477 307 while ((opt = getopt(argc, argv, "hp:t:")) != -1) {
adamc@472 308 switch (opt) {
adamc@472 309 case '?':
adamc@472 310 fprintf(stderr, "Unknown command-line option");
adamc@477 311 help(argv[0]);
adamc@472 312 return 1;
adamc@138 313
adamc@477 314 case 'h':
adamc@477 315 help(argv[0]);
adamc@477 316 return 0;
adamc@477 317
adamc@472 318 case 'p':
adamc@472 319 uw_port = atoi(optarg);
adamc@472 320 if (uw_port <= 0) {
adamc@472 321 fprintf(stderr, "Invalid port number\n");
adamc@477 322 help(argv[0]);
adamc@472 323 return 1;
adamc@472 324 }
adamc@472 325 break;
adamc@472 326
adamc@472 327 case 't':
adamc@472 328 nthreads = atoi(optarg);
adamc@472 329 if (nthreads <= 0) {
adamc@472 330 fprintf(stderr, "Invalid thread count\n");
adamc@477 331 help(argv[0]);
adamc@472 332 return 1;
adamc@472 333 }
adamc@472 334 break;
adamc@472 335
adamc@472 336 default:
adamc@472 337 fprintf(stderr, "Unexpected getopt() behavior\n");
adamc@472 338 return 1;
adamc@472 339 }
adamc@138 340 }
adamc@138 341
adamc@138 342 names = calloc(nthreads, sizeof(int));
adamc@116 343
adamc@116 344 sockfd = socket(PF_INET, SOCK_STREAM, 0); // do some error checking!
adamc@116 345
adamc@116 346 if (sockfd < 0) {
adamc@116 347 fprintf(stderr, "Listener socket creation failed\n");
adamc@116 348 return 1;
adamc@116 349 }
adamc@116 350
adamc@116 351 if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) < 0) {
adamc@116 352 fprintf(stderr, "Listener socket option setting failed\n");
adamc@116 353 return 1;
adamc@116 354 }
adamc@116 355
adamc@116 356 my_addr.sin_family = AF_INET; // host byte order
adamc@311 357 my_addr.sin_port = htons(uw_port); // short, network byte order
adamc@116 358 my_addr.sin_addr.s_addr = INADDR_ANY; // auto-fill with my IP
adamc@116 359 memset(my_addr.sin_zero, '\0', sizeof my_addr.sin_zero);
adamc@116 360
adamc@116 361 if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof my_addr) < 0) {
adamc@116 362 fprintf(stderr, "Listener socket bind failed\n");
adamc@116 363 return 1;
adamc@116 364 }
adamc@116 365
adamc@311 366 if (listen(sockfd, uw_backlog) < 0) {
adamc@116 367 fprintf(stderr, "Socket listen failed\n");
adamc@116 368 return 1;
adamc@116 369 }
adamc@116 370
adamc@116 371 sin_size = sizeof their_addr;
adamc@116 372
adamc@667 373 uw_global_init();
adamc@667 374
adamc@311 375 printf("Listening on port %d....\n", uw_port);
adamc@116 376
adamc@667 377 {
adamc@667 378 pthread_t thread;
adamc@667 379 int name;
adamc@667 380
adamc@667 381 if (pthread_create(&thread, NULL, client_pruner, &name)) {
adamc@667 382 fprintf(stderr, "Error creating pruner thread\n");
adamc@667 383 return 1;
adamc@667 384 }
adamc@667 385 }
adamc@667 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 }