annotate src/c/driver.c @ 138:d6d78055f001

Change driver to use Pthreads
author Adam Chlipala <adamc@hcoop.net>
date Sat, 19 Jul 2008 18:56:57 -0400
parents 133fa2d51bb4
children adfa2c7a75da
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@138 89 char *cmd, *path;
adamc@138 90
adamc@138 91 *s = 0;
adamc@138 92
adamc@138 93 if (!(s = strstr(buf, "\r\n"))) {
adamc@138 94 fprintf(stderr, "No newline in buf\n");
adamc@138 95 break;
adamc@138 96 }
adamc@138 97
adamc@138 98 *s = 0;
adamc@138 99 cmd = s = buf;
adamc@138 100
adamc@138 101 if (!strsep(&s, " ")) {
adamc@138 102 fprintf(stderr, "No first space in HTTP command\n");
adamc@138 103 break;
adamc@138 104 }
adamc@138 105
adamc@138 106 if (strcmp(cmd, "GET")) {
adamc@138 107 fprintf(stderr, "Not ready for non-get command: %s\n", cmd);
adamc@138 108 break;
adamc@138 109 }
adamc@138 110
adamc@138 111 path = s;
adamc@138 112 if (!strsep(&s, " ")) {
adamc@138 113 fprintf(stderr, "No second space in HTTP command\n");
adamc@138 114 break;
adamc@138 115 }
adamc@138 116
adamc@138 117 printf("Serving URI %s....\n", path);
adamc@138 118
adamc@138 119 ctx = lw_init(1024, 1024);
adamc@138 120 lw_write (ctx, "HTTP/1.1 200 OK\r\n");
adamc@138 121 lw_write(ctx, "Content-type: text/html\r\n\r\n");
adamc@138 122 lw_write(ctx, "<html>");
adamc@138 123 lw_handle(ctx, path);
adamc@138 124 lw_write(ctx, "</html>");
adamc@138 125
adamc@138 126 lw_send(ctx, sock);
adamc@138 127
adamc@138 128 printf("Done with client.\n\n");
adamc@138 129 break;
adamc@138 130 }
adamc@116 131 }
adamc@116 132
adamc@138 133 close(sock);
adamc@138 134 lw_reset(ctx);
adamc@116 135 }
adamc@116 136 }
adamc@102 137
adamc@138 138 int main(int argc, char *argv[]) {
adamc@116 139 // The skeleton for this function comes from Beej's sockets tutorial.
adamc@138 140 int sockfd; // listen on sock_fd
adamc@116 141 struct sockaddr_in my_addr;
adamc@116 142 struct sockaddr_in their_addr; // connector's address information
adamc@116 143 int sin_size, yes = 1;
adamc@138 144 int nthreads, i, *names;
adamc@138 145
adamc@138 146 if (argc < 2) {
adamc@138 147 fprintf(stderr, "No thread count specified\n");
adamc@138 148 return 1;
adamc@138 149 }
adamc@138 150
adamc@138 151 nthreads = atoi(argv[1]);
adamc@138 152 if (nthreads <= 0) {
adamc@138 153 fprintf(stderr, "Invalid thread count\n");
adamc@138 154 return 1;
adamc@138 155 }
adamc@138 156 names = calloc(nthreads, sizeof(int));
adamc@116 157
adamc@116 158 sockfd = socket(PF_INET, SOCK_STREAM, 0); // do some error checking!
adamc@116 159
adamc@116 160 if (sockfd < 0) {
adamc@116 161 fprintf(stderr, "Listener socket creation failed\n");
adamc@116 162 return 1;
adamc@116 163 }
adamc@116 164
adamc@116 165 if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) < 0) {
adamc@116 166 fprintf(stderr, "Listener socket option setting failed\n");
adamc@116 167 return 1;
adamc@116 168 }
adamc@116 169
adamc@116 170 my_addr.sin_family = AF_INET; // host byte order
adamc@116 171 my_addr.sin_port = htons(lw_port); // short, network byte order
adamc@116 172 my_addr.sin_addr.s_addr = INADDR_ANY; // auto-fill with my IP
adamc@116 173 memset(my_addr.sin_zero, '\0', sizeof my_addr.sin_zero);
adamc@116 174
adamc@116 175 if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof my_addr) < 0) {
adamc@116 176 fprintf(stderr, "Listener socket bind failed\n");
adamc@116 177 return 1;
adamc@116 178 }
adamc@116 179
adamc@116 180 if (listen(sockfd, lw_backlog) < 0) {
adamc@116 181 fprintf(stderr, "Socket listen failed\n");
adamc@116 182 return 1;
adamc@116 183 }
adamc@116 184
adamc@116 185 sin_size = sizeof their_addr;
adamc@116 186
adamc@116 187 printf("Listening on port %d....\n", lw_port);
adamc@116 188
adamc@138 189 for (i = 0; i < nthreads; ++i) {
adamc@138 190 pthread_t thread;
adamc@138 191 names[i] = i;
adamc@138 192 if (pthread_create(&thread, NULL, worker, &names[i])) {
adamc@138 193 fprintf(stderr, "Error creating worker thread #%d\n", i);
adamc@138 194 return 1;
adamc@138 195 }
adamc@138 196 }
adamc@138 197
adamc@116 198 while (1) {
adamc@138 199 int new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size);
adamc@116 200
adamc@116 201 if (new_fd < 0) {
adamc@116 202 fprintf(stderr, "Socket accept failed\n");
adamc@116 203 return 1;
adamc@116 204 }
adamc@116 205
adamc@116 206 printf("Accepted connection.\n");
adamc@138 207
adamc@138 208 pthread_mutex_lock(&queue_mutex);
adamc@138 209 enqueue(new_fd);
adamc@138 210 pthread_mutex_unlock(&queue_mutex);
adamc@138 211 pthread_cond_broadcast(&queue_cond);
adamc@116 212 }
adamc@102 213 }