annotate src/c/driver.c @ 420:9119a5920106

Prose for Ref and Metaform
author Adam Chlipala <adamc@hcoop.net>
date Thu, 23 Oct 2008 17:52:04 -0400
parents cc71fb7e5e54
children b10132434adc
rev   line source
adamc@116 1 #include <stdio.h>
adamc@116 2
adamc@116 3 #include <string.h>
adamc@116 4 #include <sys/types.h>
adamc@116 5 #include <sys/socket.h>
adamc@116 6 #include <netinet/in.h>
adamc@116 7
adamc@138 8 #include <pthread.h>
adamc@138 9
adamc@244 10 #include "urweb.h"
adamc@117 11
adamc@311 12 int uw_port = 8080;
adamc@311 13 int uw_backlog = 10;
adamc@311 14 int uw_bufsize = 1024;
adamc@116 15
adamc@138 16 typedef struct node {
adamc@138 17 int fd;
adamc@138 18 struct node *next;
adamc@138 19 } *node;
adamc@138 20
adamc@138 21 static node front = NULL, back = NULL;
adamc@138 22
adamc@138 23 static int empty() {
adamc@138 24 return front == NULL;
adamc@138 25 }
adamc@138 26
adamc@138 27 static void enqueue(int fd) {
adamc@138 28 node n = malloc(sizeof(struct node));
adamc@138 29
adamc@138 30 n->fd = fd;
adamc@138 31 n->next = NULL;
adamc@138 32 if (back)
adamc@138 33 back->next = n;
adamc@138 34 else
adamc@138 35 front = n;
adamc@138 36 back = n;
adamc@138 37 }
adamc@138 38
adamc@138 39 static int dequeue() {
adamc@138 40 int ret = front->fd;
adamc@138 41
adamc@138 42 front = front->next;
adamc@138 43 if (!front)
adamc@138 44 back = NULL;
adamc@138 45
adamc@138 46 return ret;
adamc@138 47 }
adamc@138 48
adamc@138 49 static pthread_mutex_t queue_mutex = PTHREAD_MUTEX_INITIALIZER;
adamc@138 50 static pthread_cond_t queue_cond = PTHREAD_COND_INITIALIZER;
adamc@138 51
adamc@167 52 #define MAX_RETRIES 5
adamc@167 53
adamc@138 54 static void *worker(void *data) {
adamc@324 55 int me = *(int *)data, retries_left = MAX_RETRIES;
adamc@324 56 uw_context ctx = uw_init(1024, 0);
adamc@272 57
adamc@272 58 while (1) {
adamc@311 59 failure_kind fk = uw_begin_init(ctx);
adamc@272 60
adamc@272 61 if (fk == SUCCESS) {
adamc@311 62 uw_db_init(ctx);
adamc@272 63 printf("Database connection initialized.\n");
adamc@272 64 break;
adamc@272 65 } else if (fk == BOUNDED_RETRY) {
adamc@272 66 if (retries_left) {
adamc@311 67 printf("Initialization error triggers bounded retry: %s\n", uw_error_message(ctx));
adamc@272 68 --retries_left;
adamc@272 69 } else {
adamc@311 70 printf("Fatal initialization error (out of retries): %s\n", uw_error_message(ctx));
adamc@311 71 uw_free(ctx);
adamc@272 72 return NULL;
adamc@272 73 }
adamc@272 74 } else if (fk == UNLIMITED_RETRY)
adamc@311 75 printf("Initialization error triggers unlimited retry: %s\n", uw_error_message(ctx));
adamc@272 76 else if (fk == FATAL) {
adamc@311 77 printf("Fatal initialization error: %s\n", uw_error_message(ctx));
adamc@311 78 uw_free(ctx);
adamc@272 79 return NULL;
adamc@272 80 } else {
adamc@311 81 printf("Unknown uw_handle return code!\n");
adamc@311 82 uw_free(ctx);
adamc@272 83 return NULL;
adamc@272 84 }
adamc@272 85 }
adamc@116 86
adamc@116 87 while (1) {
adamc@311 88 char buf[uw_bufsize+1], *back = buf, *s;
adamc@138 89 int sock;
adamc@116 90
adamc@138 91 pthread_mutex_lock(&queue_mutex);
adamc@138 92 while (empty())
adamc@138 93 pthread_cond_wait(&queue_cond, &queue_mutex);
adamc@138 94 sock = dequeue();
adamc@138 95 pthread_mutex_unlock(&queue_mutex);
adamc@138 96
adamc@138 97 printf("Handling connection with thread #%d.\n", me);
adamc@138 98
adamc@138 99 while (1) {
adamc@167 100 unsigned retries_left = MAX_RETRIES;
adamc@311 101 int r = recv(sock, back, uw_bufsize - (back - buf), 0);
adamc@138 102
adamc@138 103 if (r < 0) {
adamc@138 104 fprintf(stderr, "Recv failed\n");
adamc@138 105 break;
adamc@138 106 }
adamc@138 107
adamc@138 108 if (r == 0) {
adamc@138 109 printf("Connection closed.\n");
adamc@138 110 break;
adamc@138 111 }
adamc@138 112
adamc@138 113 printf("Received %d bytes.\n", r);
adamc@138 114
adamc@138 115 back += r;
adamc@138 116 *back = 0;
adamc@138 117
adamc@138 118 if (s = strstr(buf, "\r\n\r\n")) {
adamc@400 119 char *cmd, *path, path_copy[uw_bufsize+1], *inputs;
adamc@138 120
adamc@138 121 *s = 0;
adamc@144 122
adamc@138 123 if (!(s = strstr(buf, "\r\n"))) {
adamc@138 124 fprintf(stderr, "No newline in buf\n");
adamc@138 125 break;
adamc@138 126 }
adamc@138 127
adamc@138 128 *s = 0;
adamc@138 129 cmd = s = buf;
adamc@401 130
adamc@401 131 printf("Read: %s\n", buf);
adamc@138 132
adamc@138 133 if (!strsep(&s, " ")) {
adamc@138 134 fprintf(stderr, "No first space in HTTP command\n");
adamc@138 135 break;
adamc@138 136 }
adamc@138 137
adamc@138 138 if (strcmp(cmd, "GET")) {
adamc@138 139 fprintf(stderr, "Not ready for non-get command: %s\n", cmd);
adamc@138 140 break;
adamc@138 141 }
adamc@138 142
adamc@138 143 path = s;
adamc@138 144 if (!strsep(&s, " ")) {
adamc@138 145 fprintf(stderr, "No second space in HTTP command\n");
adamc@138 146 break;
adamc@138 147 }
adamc@138 148
adamc@144 149 if (inputs = strchr(path, '?')) {
adamc@144 150 char *name, *value;
adamc@144 151 *inputs++ = 0;
adamc@144 152
adamc@144 153 while (*inputs) {
adamc@144 154 name = inputs;
adamc@145 155 if (inputs = strchr(inputs, '&'))
adamc@145 156 *inputs++ = 0;
adamc@145 157 else
adamc@145 158 inputs = strchr(name, 0);
adamc@145 159
adamc@145 160 if (value = strchr(name, '=')) {
adamc@144 161 *value++ = 0;
adamc@311 162 uw_set_input(ctx, name, value);
adamc@144 163 }
adamc@145 164 else
adamc@311 165 uw_set_input(ctx, name, "");
adamc@144 166 }
adamc@144 167 }
adamc@144 168
adamc@138 169 printf("Serving URI %s....\n", path);
adamc@138 170
adamc@167 171 while (1) {
adamc@167 172 failure_kind fk;
adamc@167 173
adamc@311 174 uw_write(ctx, "HTTP/1.1 200 OK\r\n");
adamc@311 175 uw_write(ctx, "Content-type: text/html\r\n\r\n");
adamc@311 176 uw_write(ctx, "<html>");
adamc@167 177
adamc@400 178 strcpy(path_copy, path);
adamc@400 179 fk = uw_begin(ctx, path_copy);
adamc@167 180 if (fk == SUCCESS) {
adamc@311 181 uw_write(ctx, "</html>");
adamc@167 182 break;
adamc@167 183 } else if (fk == BOUNDED_RETRY) {
adamc@167 184 if (retries_left) {
adamc@311 185 printf("Error triggers bounded retry: %s\n", uw_error_message(ctx));
adamc@167 186 --retries_left;
adamc@167 187 }
adamc@167 188 else {
adamc@311 189 printf("Fatal error (out of retries): %s\n", uw_error_message(ctx));
adamc@167 190
adamc@311 191 uw_reset_keep_error_message(ctx);
adamc@311 192 uw_write(ctx, "HTTP/1.1 500 Internal Server Error\n\r");
adamc@311 193 uw_write(ctx, "Content-type: text/plain\r\n\r\n");
adamc@311 194 uw_write(ctx, "Fatal error (out of retries): ");
adamc@311 195 uw_write(ctx, uw_error_message(ctx));
adamc@311 196 uw_write(ctx, "\n");
adamc@167 197 }
adamc@167 198 } else if (fk == UNLIMITED_RETRY)
adamc@311 199 printf("Error triggers unlimited retry: %s\n", uw_error_message(ctx));
adamc@167 200 else if (fk == FATAL) {
adamc@311 201 printf("Fatal error: %s\n", uw_error_message(ctx));
adamc@167 202
adamc@311 203 uw_reset_keep_error_message(ctx);
adamc@311 204 uw_write(ctx, "HTTP/1.1 500 Internal Server Error\n\r");
adamc@311 205 uw_write(ctx, "Content-type: text/plain\r\n\r\n");
adamc@311 206 uw_write(ctx, "Fatal error: ");
adamc@311 207 uw_write(ctx, uw_error_message(ctx));
adamc@311 208 uw_write(ctx, "\n");
adamc@167 209
adamc@167 210 break;
adamc@167 211 } else {
adamc@311 212 printf("Unknown uw_handle return code!\n");
adamc@167 213
adamc@311 214 uw_reset_keep_request(ctx);
adamc@311 215 uw_write(ctx, "HTTP/1.1 500 Internal Server Error\n\r");
adamc@311 216 uw_write(ctx, "Content-type: text/plain\r\n\r\n");
adamc@311 217 uw_write(ctx, "Unknown uw_handle return code!\n");
adamc@167 218
adamc@167 219 break;
adamc@167 220 }
adamc@167 221
adamc@311 222 uw_reset_keep_request(ctx);
adamc@167 223 }
adamc@138 224
adamc@311 225 uw_send(ctx, sock);
adamc@138 226
adamc@138 227 printf("Done with client.\n\n");
adamc@324 228 uw_memstats(ctx);
adamc@138 229 break;
adamc@138 230 }
adamc@116 231 }
adamc@116 232
adamc@138 233 close(sock);
adamc@311 234 uw_reset(ctx);
adamc@116 235 }
adamc@116 236 }
adamc@102 237
adamc@138 238 int main(int argc, char *argv[]) {
adamc@116 239 // The skeleton for this function comes from Beej's sockets tutorial.
adamc@138 240 int sockfd; // listen on sock_fd
adamc@116 241 struct sockaddr_in my_addr;
adamc@116 242 struct sockaddr_in their_addr; // connector's address information
adamc@116 243 int sin_size, yes = 1;
adamc@138 244 int nthreads, i, *names;
adamc@138 245
adamc@138 246 if (argc < 2) {
adamc@138 247 fprintf(stderr, "No thread count specified\n");
adamc@138 248 return 1;
adamc@138 249 }
adamc@138 250
adamc@138 251 nthreads = atoi(argv[1]);
adamc@138 252 if (nthreads <= 0) {
adamc@138 253 fprintf(stderr, "Invalid thread count\n");
adamc@138 254 return 1;
adamc@138 255 }
adamc@138 256 names = calloc(nthreads, sizeof(int));
adamc@116 257
adamc@116 258 sockfd = socket(PF_INET, SOCK_STREAM, 0); // do some error checking!
adamc@116 259
adamc@116 260 if (sockfd < 0) {
adamc@116 261 fprintf(stderr, "Listener socket creation failed\n");
adamc@116 262 return 1;
adamc@116 263 }
adamc@116 264
adamc@116 265 if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) < 0) {
adamc@116 266 fprintf(stderr, "Listener socket option setting failed\n");
adamc@116 267 return 1;
adamc@116 268 }
adamc@116 269
adamc@116 270 my_addr.sin_family = AF_INET; // host byte order
adamc@311 271 my_addr.sin_port = htons(uw_port); // short, network byte order
adamc@116 272 my_addr.sin_addr.s_addr = INADDR_ANY; // auto-fill with my IP
adamc@116 273 memset(my_addr.sin_zero, '\0', sizeof my_addr.sin_zero);
adamc@116 274
adamc@116 275 if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof my_addr) < 0) {
adamc@116 276 fprintf(stderr, "Listener socket bind failed\n");
adamc@116 277 return 1;
adamc@116 278 }
adamc@116 279
adamc@311 280 if (listen(sockfd, uw_backlog) < 0) {
adamc@116 281 fprintf(stderr, "Socket listen failed\n");
adamc@116 282 return 1;
adamc@116 283 }
adamc@116 284
adamc@116 285 sin_size = sizeof their_addr;
adamc@116 286
adamc@311 287 printf("Listening on port %d....\n", uw_port);
adamc@116 288
adamc@138 289 for (i = 0; i < nthreads; ++i) {
adamc@138 290 pthread_t thread;
adamc@138 291 names[i] = i;
adamc@138 292 if (pthread_create(&thread, NULL, worker, &names[i])) {
adamc@138 293 fprintf(stderr, "Error creating worker thread #%d\n", i);
adamc@138 294 return 1;
adamc@138 295 }
adamc@138 296 }
adamc@138 297
adamc@116 298 while (1) {
adamc@138 299 int new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size);
adamc@116 300
adamc@116 301 if (new_fd < 0) {
adamc@116 302 fprintf(stderr, "Socket accept failed\n");
adamc@116 303 return 1;
adamc@116 304 }
adamc@116 305
adamc@116 306 printf("Accepted connection.\n");
adamc@138 307
adamc@138 308 pthread_mutex_lock(&queue_mutex);
adamc@138 309 enqueue(new_fd);
adamc@139 310 pthread_cond_broadcast(&queue_cond);
adamc@138 311 pthread_mutex_unlock(&queue_mutex);
adamc@116 312 }
adamc@102 313 }