annotate src/c/driver.c @ 164:6847741e1f5f

Datatypes through monoize
author Adam Chlipala <adamc@hcoop.net>
date Tue, 29 Jul 2008 13:32:07 -0400
parents b1b33f7cf555
children 2be573fec9a6
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@117 16 void lw_handle(lw_context, char*);
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@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@138 71 int r = recv(sock, back, lw_bufsize - (back - buf), 0);
adamc@138 72
adamc@138 73 if (r < 0) {
adamc@138 74 fprintf(stderr, "Recv failed\n");
adamc@138 75 break;
adamc@138 76 }
adamc@138 77
adamc@138 78 if (r == 0) {
adamc@138 79 printf("Connection closed.\n");
adamc@138 80 break;
adamc@138 81 }
adamc@138 82
adamc@138 83 printf("Received %d bytes.\n", r);
adamc@138 84
adamc@138 85 back += r;
adamc@138 86 *back = 0;
adamc@138 87
adamc@138 88 if (s = strstr(buf, "\r\n\r\n")) {
adamc@144 89 char *cmd, *path, *inputs;
adamc@138 90
adamc@138 91 *s = 0;
adamc@144 92
adamc@144 93 printf("Read: %s\n", buf);
adamc@138 94
adamc@138 95 if (!(s = strstr(buf, "\r\n"))) {
adamc@138 96 fprintf(stderr, "No newline in buf\n");
adamc@138 97 break;
adamc@138 98 }
adamc@138 99
adamc@138 100 *s = 0;
adamc@138 101 cmd = s = buf;
adamc@138 102
adamc@138 103 if (!strsep(&s, " ")) {
adamc@138 104 fprintf(stderr, "No first space in HTTP command\n");
adamc@138 105 break;
adamc@138 106 }
adamc@138 107
adamc@138 108 if (strcmp(cmd, "GET")) {
adamc@138 109 fprintf(stderr, "Not ready for non-get command: %s\n", cmd);
adamc@138 110 break;
adamc@138 111 }
adamc@138 112
adamc@138 113 path = s;
adamc@138 114 if (!strsep(&s, " ")) {
adamc@138 115 fprintf(stderr, "No second space in HTTP command\n");
adamc@138 116 break;
adamc@138 117 }
adamc@138 118
adamc@144 119 if (inputs = strchr(path, '?')) {
adamc@144 120 char *name, *value;
adamc@144 121 *inputs++ = 0;
adamc@144 122
adamc@144 123 while (*inputs) {
adamc@144 124 name = inputs;
adamc@145 125 if (inputs = strchr(inputs, '&'))
adamc@145 126 *inputs++ = 0;
adamc@145 127 else
adamc@145 128 inputs = strchr(name, 0);
adamc@145 129
adamc@145 130 if (value = strchr(name, '=')) {
adamc@144 131 *value++ = 0;
adamc@144 132 lw_set_input(ctx, name, value);
adamc@144 133 }
adamc@145 134 else
adamc@144 135 lw_set_input(ctx, name, "");
adamc@144 136 }
adamc@144 137 }
adamc@144 138
adamc@138 139 printf("Serving URI %s....\n", path);
adamc@138 140
adamc@138 141 lw_write (ctx, "HTTP/1.1 200 OK\r\n");
adamc@138 142 lw_write(ctx, "Content-type: text/html\r\n\r\n");
adamc@138 143 lw_write(ctx, "<html>");
adamc@138 144 lw_handle(ctx, path);
adamc@138 145 lw_write(ctx, "</html>");
adamc@138 146
adamc@138 147 lw_send(ctx, sock);
adamc@138 148
adamc@138 149 printf("Done with client.\n\n");
adamc@138 150 break;
adamc@138 151 }
adamc@116 152 }
adamc@116 153
adamc@138 154 close(sock);
adamc@138 155 lw_reset(ctx);
adamc@116 156 }
adamc@116 157 }
adamc@102 158
adamc@138 159 int main(int argc, char *argv[]) {
adamc@116 160 // The skeleton for this function comes from Beej's sockets tutorial.
adamc@138 161 int sockfd; // listen on sock_fd
adamc@116 162 struct sockaddr_in my_addr;
adamc@116 163 struct sockaddr_in their_addr; // connector's address information
adamc@116 164 int sin_size, yes = 1;
adamc@138 165 int nthreads, i, *names;
adamc@138 166
adamc@138 167 if (argc < 2) {
adamc@138 168 fprintf(stderr, "No thread count specified\n");
adamc@138 169 return 1;
adamc@138 170 }
adamc@138 171
adamc@138 172 nthreads = atoi(argv[1]);
adamc@138 173 if (nthreads <= 0) {
adamc@138 174 fprintf(stderr, "Invalid thread count\n");
adamc@138 175 return 1;
adamc@138 176 }
adamc@138 177 names = calloc(nthreads, sizeof(int));
adamc@116 178
adamc@116 179 sockfd = socket(PF_INET, SOCK_STREAM, 0); // do some error checking!
adamc@116 180
adamc@116 181 if (sockfd < 0) {
adamc@116 182 fprintf(stderr, "Listener socket creation failed\n");
adamc@116 183 return 1;
adamc@116 184 }
adamc@116 185
adamc@116 186 if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) < 0) {
adamc@116 187 fprintf(stderr, "Listener socket option setting failed\n");
adamc@116 188 return 1;
adamc@116 189 }
adamc@116 190
adamc@116 191 my_addr.sin_family = AF_INET; // host byte order
adamc@116 192 my_addr.sin_port = htons(lw_port); // short, network byte order
adamc@116 193 my_addr.sin_addr.s_addr = INADDR_ANY; // auto-fill with my IP
adamc@116 194 memset(my_addr.sin_zero, '\0', sizeof my_addr.sin_zero);
adamc@116 195
adamc@116 196 if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof my_addr) < 0) {
adamc@116 197 fprintf(stderr, "Listener socket bind failed\n");
adamc@116 198 return 1;
adamc@116 199 }
adamc@116 200
adamc@116 201 if (listen(sockfd, lw_backlog) < 0) {
adamc@116 202 fprintf(stderr, "Socket listen failed\n");
adamc@116 203 return 1;
adamc@116 204 }
adamc@116 205
adamc@116 206 sin_size = sizeof their_addr;
adamc@116 207
adamc@116 208 printf("Listening on port %d....\n", lw_port);
adamc@116 209
adamc@138 210 for (i = 0; i < nthreads; ++i) {
adamc@138 211 pthread_t thread;
adamc@138 212 names[i] = i;
adamc@138 213 if (pthread_create(&thread, NULL, worker, &names[i])) {
adamc@138 214 fprintf(stderr, "Error creating worker thread #%d\n", i);
adamc@138 215 return 1;
adamc@138 216 }
adamc@138 217 }
adamc@138 218
adamc@116 219 while (1) {
adamc@138 220 int new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size);
adamc@116 221
adamc@116 222 if (new_fd < 0) {
adamc@116 223 fprintf(stderr, "Socket accept failed\n");
adamc@116 224 return 1;
adamc@116 225 }
adamc@116 226
adamc@116 227 printf("Accepted connection.\n");
adamc@138 228
adamc@138 229 pthread_mutex_lock(&queue_mutex);
adamc@138 230 enqueue(new_fd);
adamc@139 231 pthread_cond_broadcast(&queue_cond);
adamc@138 232 pthread_mutex_unlock(&queue_mutex);
adamc@116 233 }
adamc@102 234 }