annotate src/c/driver.c @ 177:5d030ee143e2

Case through corify
author Adam Chlipala <adamc@hcoop.net>
date Sat, 02 Aug 2008 11:15:32 -0400
parents 2be573fec9a6
children 71bafe66dbe1
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@117 10 #include "lacweb.h"
adamc@117 11
adamc@116 12 int lw_port = 8080;
adamc@116 13 int lw_backlog = 10;
adamc@116 14 int lw_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@138 55 int me = *(int *)data;
adamc@138 56 lw_context ctx = lw_init(1024, 1024);
adamc@116 57
adamc@116 58 while (1) {
adamc@138 59 char buf[lw_bufsize+1], *back = buf, *s;
adamc@138 60 int sock;
adamc@116 61
adamc@138 62 pthread_mutex_lock(&queue_mutex);
adamc@138 63 while (empty())
adamc@138 64 pthread_cond_wait(&queue_cond, &queue_mutex);
adamc@138 65 sock = dequeue();
adamc@138 66 pthread_mutex_unlock(&queue_mutex);
adamc@138 67
adamc@138 68 printf("Handling connection with thread #%d.\n", me);
adamc@138 69
adamc@138 70 while (1) {
adamc@167 71 unsigned retries_left = MAX_RETRIES;
adamc@138 72 int r = recv(sock, back, lw_bufsize - (back - buf), 0);
adamc@138 73
adamc@138 74 if (r < 0) {
adamc@138 75 fprintf(stderr, "Recv failed\n");
adamc@138 76 break;
adamc@138 77 }
adamc@138 78
adamc@138 79 if (r == 0) {
adamc@138 80 printf("Connection closed.\n");
adamc@138 81 break;
adamc@138 82 }
adamc@138 83
adamc@138 84 printf("Received %d bytes.\n", r);
adamc@138 85
adamc@138 86 back += r;
adamc@138 87 *back = 0;
adamc@138 88
adamc@138 89 if (s = strstr(buf, "\r\n\r\n")) {
adamc@144 90 char *cmd, *path, *inputs;
adamc@138 91
adamc@138 92 *s = 0;
adamc@144 93
adamc@144 94 printf("Read: %s\n", buf);
adamc@138 95
adamc@138 96 if (!(s = strstr(buf, "\r\n"))) {
adamc@138 97 fprintf(stderr, "No newline in buf\n");
adamc@138 98 break;
adamc@138 99 }
adamc@138 100
adamc@138 101 *s = 0;
adamc@138 102 cmd = s = buf;
adamc@138 103
adamc@138 104 if (!strsep(&s, " ")) {
adamc@138 105 fprintf(stderr, "No first space in HTTP command\n");
adamc@138 106 break;
adamc@138 107 }
adamc@138 108
adamc@138 109 if (strcmp(cmd, "GET")) {
adamc@138 110 fprintf(stderr, "Not ready for non-get command: %s\n", cmd);
adamc@138 111 break;
adamc@138 112 }
adamc@138 113
adamc@138 114 path = s;
adamc@138 115 if (!strsep(&s, " ")) {
adamc@138 116 fprintf(stderr, "No second space in HTTP command\n");
adamc@138 117 break;
adamc@138 118 }
adamc@138 119
adamc@144 120 if (inputs = strchr(path, '?')) {
adamc@144 121 char *name, *value;
adamc@144 122 *inputs++ = 0;
adamc@144 123
adamc@144 124 while (*inputs) {
adamc@144 125 name = inputs;
adamc@145 126 if (inputs = strchr(inputs, '&'))
adamc@145 127 *inputs++ = 0;
adamc@145 128 else
adamc@145 129 inputs = strchr(name, 0);
adamc@145 130
adamc@145 131 if (value = strchr(name, '=')) {
adamc@144 132 *value++ = 0;
adamc@144 133 lw_set_input(ctx, name, value);
adamc@144 134 }
adamc@145 135 else
adamc@144 136 lw_set_input(ctx, name, "");
adamc@144 137 }
adamc@144 138 }
adamc@144 139
adamc@138 140 printf("Serving URI %s....\n", path);
adamc@138 141
adamc@167 142 while (1) {
adamc@167 143 failure_kind fk;
adamc@167 144
adamc@167 145 lw_write(ctx, "HTTP/1.1 200 OK\r\n");
adamc@167 146 lw_write(ctx, "Content-type: text/html\r\n\r\n");
adamc@167 147 lw_write(ctx, "<html>");
adamc@167 148
adamc@167 149 fk = lw_begin(ctx, path);
adamc@167 150 if (fk == SUCCESS) {
adamc@167 151 lw_write(ctx, "</html>");
adamc@167 152 break;
adamc@167 153 } else if (fk == BOUNDED_RETRY) {
adamc@167 154 if (retries_left) {
adamc@167 155 printf("Error triggers bounded retry: %s\n", lw_error_message(ctx));
adamc@167 156 --retries_left;
adamc@167 157 }
adamc@167 158 else {
adamc@167 159 printf("Fatal error (out of retries): %s\n", lw_error_message(ctx));
adamc@167 160
adamc@167 161 lw_reset_keep_error_message(ctx);
adamc@167 162 lw_write(ctx, "HTTP/1.1 500 Internal Server Error\n\r");
adamc@167 163 lw_write(ctx, "Content-type: text/plain\r\n\r\n");
adamc@167 164 lw_write(ctx, "Fatal error (out of retries): ");
adamc@167 165 lw_write(ctx, lw_error_message(ctx));
adamc@167 166 lw_write(ctx, "\n");
adamc@167 167 }
adamc@167 168 } else if (fk == UNLIMITED_RETRY)
adamc@167 169 printf("Error triggers unlimited retry: %s\n", lw_error_message(ctx));
adamc@167 170 else if (fk == FATAL) {
adamc@167 171 printf("Fatal error: %s\n", lw_error_message(ctx));
adamc@167 172
adamc@167 173 lw_reset_keep_error_message(ctx);
adamc@167 174 lw_write(ctx, "HTTP/1.1 500 Internal Server Error\n\r");
adamc@167 175 lw_write(ctx, "Content-type: text/plain\r\n\r\n");
adamc@167 176 lw_write(ctx, "Fatal error: ");
adamc@167 177 lw_write(ctx, lw_error_message(ctx));
adamc@167 178 lw_write(ctx, "\n");
adamc@167 179
adamc@167 180 break;
adamc@167 181 } else {
adamc@167 182 printf("Unknown lw_handle return code!\n");
adamc@167 183
adamc@167 184 lw_reset_keep_request(ctx);
adamc@167 185 lw_write(ctx, "HTTP/1.1 500 Internal Server Error\n\r");
adamc@167 186 lw_write(ctx, "Content-type: text/plain\r\n\r\n");
adamc@167 187 lw_write(ctx, "Unknown lw_handle return code!\n");
adamc@167 188
adamc@167 189 break;
adamc@167 190 }
adamc@167 191
adamc@167 192 lw_reset_keep_request(ctx);
adamc@167 193 }
adamc@138 194
adamc@138 195 lw_send(ctx, sock);
adamc@138 196
adamc@138 197 printf("Done with client.\n\n");
adamc@138 198 break;
adamc@138 199 }
adamc@116 200 }
adamc@116 201
adamc@138 202 close(sock);
adamc@138 203 lw_reset(ctx);
adamc@116 204 }
adamc@116 205 }
adamc@102 206
adamc@138 207 int main(int argc, char *argv[]) {
adamc@116 208 // The skeleton for this function comes from Beej's sockets tutorial.
adamc@138 209 int sockfd; // listen on sock_fd
adamc@116 210 struct sockaddr_in my_addr;
adamc@116 211 struct sockaddr_in their_addr; // connector's address information
adamc@116 212 int sin_size, yes = 1;
adamc@138 213 int nthreads, i, *names;
adamc@138 214
adamc@138 215 if (argc < 2) {
adamc@138 216 fprintf(stderr, "No thread count specified\n");
adamc@138 217 return 1;
adamc@138 218 }
adamc@138 219
adamc@138 220 nthreads = atoi(argv[1]);
adamc@138 221 if (nthreads <= 0) {
adamc@138 222 fprintf(stderr, "Invalid thread count\n");
adamc@138 223 return 1;
adamc@138 224 }
adamc@138 225 names = calloc(nthreads, sizeof(int));
adamc@116 226
adamc@116 227 sockfd = socket(PF_INET, SOCK_STREAM, 0); // do some error checking!
adamc@116 228
adamc@116 229 if (sockfd < 0) {
adamc@116 230 fprintf(stderr, "Listener socket creation failed\n");
adamc@116 231 return 1;
adamc@116 232 }
adamc@116 233
adamc@116 234 if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) < 0) {
adamc@116 235 fprintf(stderr, "Listener socket option setting failed\n");
adamc@116 236 return 1;
adamc@116 237 }
adamc@116 238
adamc@116 239 my_addr.sin_family = AF_INET; // host byte order
adamc@116 240 my_addr.sin_port = htons(lw_port); // short, network byte order
adamc@116 241 my_addr.sin_addr.s_addr = INADDR_ANY; // auto-fill with my IP
adamc@116 242 memset(my_addr.sin_zero, '\0', sizeof my_addr.sin_zero);
adamc@116 243
adamc@116 244 if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof my_addr) < 0) {
adamc@116 245 fprintf(stderr, "Listener socket bind failed\n");
adamc@116 246 return 1;
adamc@116 247 }
adamc@116 248
adamc@116 249 if (listen(sockfd, lw_backlog) < 0) {
adamc@116 250 fprintf(stderr, "Socket listen failed\n");
adamc@116 251 return 1;
adamc@116 252 }
adamc@116 253
adamc@116 254 sin_size = sizeof their_addr;
adamc@116 255
adamc@116 256 printf("Listening on port %d....\n", lw_port);
adamc@116 257
adamc@138 258 for (i = 0; i < nthreads; ++i) {
adamc@138 259 pthread_t thread;
adamc@138 260 names[i] = i;
adamc@138 261 if (pthread_create(&thread, NULL, worker, &names[i])) {
adamc@138 262 fprintf(stderr, "Error creating worker thread #%d\n", i);
adamc@138 263 return 1;
adamc@138 264 }
adamc@138 265 }
adamc@138 266
adamc@116 267 while (1) {
adamc@138 268 int new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size);
adamc@116 269
adamc@116 270 if (new_fd < 0) {
adamc@116 271 fprintf(stderr, "Socket accept failed\n");
adamc@116 272 return 1;
adamc@116 273 }
adamc@116 274
adamc@116 275 printf("Accepted connection.\n");
adamc@138 276
adamc@138 277 pthread_mutex_lock(&queue_mutex);
adamc@138 278 enqueue(new_fd);
adamc@139 279 pthread_cond_broadcast(&queue_cond);
adamc@138 280 pthread_mutex_unlock(&queue_mutex);
adamc@116 281 }
adamc@102 282 }