r61 - trunk/varnish-cache/bin/varnishd

phk at projects.linpro.no phk at projects.linpro.no
Thu Mar 23 13:20:31 CET 2006


Author: phk
Date: 2006-03-23 13:20:30 +0100 (Thu, 23 Mar 2006)
New Revision: 61

Added:
   trunk/varnish-cache/bin/varnishd/cache.h
   trunk/varnish-cache/bin/varnishd/cache_acceptor.c
Modified:
   trunk/varnish-cache/bin/varnishd/Makefile.am
   trunk/varnish-cache/bin/varnishd/cache_main.c
   trunk/varnish-cache/bin/varnishd/mgt_child.c
   trunk/varnish-cache/bin/varnishd/tcp.c
   trunk/varnish-cache/bin/varnishd/varnishd.c
Log:
Now we're starting to get somewhere:  Accept connections and assemble
a HTTP request.



Modified: trunk/varnish-cache/bin/varnishd/Makefile.am
===================================================================
--- trunk/varnish-cache/bin/varnishd/Makefile.am	2006-03-23 08:45:44 UTC (rev 60)
+++ trunk/varnish-cache/bin/varnishd/Makefile.am	2006-03-23 12:20:30 UTC (rev 61)
@@ -5,6 +5,7 @@
 bin_PROGRAMS = varnishd
 
 varnishd_SOURCES = \
+	cache_acceptor.c \
 	cache_main.c \
 	cli_event.c \
 	mgt_child.c \
@@ -14,4 +15,5 @@
 varnishd_LDADD = \
 	$(top_builddir)/lib/libvarnish/libvarnish.la \
 	$(top_builddir)/lib/libsbuf/libsbuf.la \
-	$(top_builddir)/contrib/libevent/libevent.la
+	$(top_builddir)/contrib/libevent/libevent.la \
+	-lpthread

Added: trunk/varnish-cache/bin/varnishd/cache.h
===================================================================
--- trunk/varnish-cache/bin/varnishd/cache.h	2006-03-23 08:45:44 UTC (rev 60)
+++ trunk/varnish-cache/bin/varnishd/cache.h	2006-03-23 12:20:30 UTC (rev 61)
@@ -0,0 +1,14 @@
+/*
+ * $Id$
+ */
+
+#define VCA_RXBUFSIZE		1024
+struct sess {
+	int		fd;
+	char		rcv[VCA_RXBUFSIZE + 1];
+	unsigned	rcv_len;
+	struct event	rd_e;
+};
+
+/* cache_acceptor.c */
+void *vca_main(void *arg);

Added: trunk/varnish-cache/bin/varnishd/cache_acceptor.c
===================================================================
--- trunk/varnish-cache/bin/varnishd/cache_acceptor.c	2006-03-23 08:45:44 UTC (rev 60)
+++ trunk/varnish-cache/bin/varnishd/cache_acceptor.c	2006-03-23 12:20:30 UTC (rev 61)
@@ -0,0 +1,111 @@
+/*
+ * $Id$
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include <assert.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include <sys/types.h>
+#include <sys/socket.h>
+
+#include <sbuf.h>
+#include <event.h>
+
+#include "heritage.h"
+#include "cache.h"
+
+static struct event_base *evb;
+
+static struct event accept_e[2 * HERITAGE_NSOCKS];
+
+static void
+http_read_f(int fd, short event, void *arg)
+{
+	struct sess *sp = arg;
+	const char *p;
+	int i;
+
+	printf("%s(%d, %d, ...)\n", __func__, fd, event);
+	assert(VCA_RXBUFSIZE - sp->rcv_len > 0);
+	i = read(fd, sp->rcv + sp->rcv_len, VCA_RXBUFSIZE - sp->rcv_len);
+	assert(i > 0);
+	sp->rcv_len += i;
+	sp->rcv[sp->rcv_len] = '\0';
+
+	p = sp->rcv;
+	while (1) {
+		/* XXX: we could save location of all linebreaks for later */
+		p = strchr(p, '\n');
+		if (p == NULL)
+			return;
+		p++;
+		if (*p == '\r')
+			p++;
+		if (*p != '\n')
+			continue;
+		break;
+	}
+	printf("full <%s>\n", sp->rcv);
+	event_del(&sp->rd_e);
+}
+
+static void
+accept_f(int fd, short event, void *arg __unused)
+{
+	socklen_t l;
+	struct sockaddr addr;
+	struct sess *sp;
+
+	sp = calloc(sizeof *sp, 1);
+	assert(sp != NULL);
+
+	printf("%s(%d, %d, ...)\n", __func__, fd, event);
+
+	l = sizeof addr;
+	sp->fd = accept(fd, &addr, &l);
+	if (sp->fd < 0) {
+		free(sp);
+		return;
+	}
+
+	event_set(&sp->rd_e, sp->fd, EV_READ | EV_PERSIST,
+	    http_read_f, sp);
+	event_base_set(evb, &sp->rd_e);
+	event_add(&sp->rd_e, NULL);	/* XXX: timeout */
+}
+
+void *
+vca_main(void *arg)
+{
+	unsigned u;
+	struct event *ep;
+
+	evb = event_init();
+
+	ep = accept_e;
+	for (u = 0; u < HERITAGE_NSOCKS; u++) {
+		if (heritage.sock_local[u] >= 0) {
+			event_set(ep, heritage.sock_local[u],
+			    EV_READ | EV_PERSIST,
+			    accept_f, NULL);
+			event_base_set(evb, ep);
+			event_add(ep, NULL);
+			ep++;
+		}
+		if (heritage.sock_remote[u] >= 0) {
+			event_set(ep, heritage.sock_remote[u],
+			    EV_READ | EV_PERSIST,
+			    accept_f, NULL);
+			event_base_set(evb, ep);
+			event_add(ep, NULL);
+			ep++;
+		}
+	}
+
+	event_base_loop(evb, 0);
+
+	return ("FOOBAR");
+}

Modified: trunk/varnish-cache/bin/varnishd/cache_main.c
===================================================================
--- trunk/varnish-cache/bin/varnishd/cache_main.c	2006-03-23 08:45:44 UTC (rev 60)
+++ trunk/varnish-cache/bin/varnishd/cache_main.c	2006-03-23 12:20:30 UTC (rev 61)
@@ -8,12 +8,16 @@
 #include <stdlib.h>
 #include <sys/time.h>
 
+#include <pthread.h>
+
 #include <event.h>
 
 #include <cli.h>
 #include <cli_priv.h>
 
+#include "libvarnish.h"
 #include "heritage.h"
+#include "cache.h"
 #include "cli_event.h"
 
 static struct event ev_keepalive;
@@ -48,7 +52,6 @@
 {
 
 	cli_out(cli, "url <%s>", av[2]);
-	sleep(1);
 	cli_result(cli, CLIS_UNIMPL);
 }
 
@@ -76,6 +79,8 @@
 	{ NULL }
 };
 
+static pthread_t vca_thread;
+
 void
 child_main(void)
 {
@@ -87,6 +92,8 @@
 	setbuf(stderr, NULL);
 	printf("Child starts\n");
 
+	AZ(pthread_create(&vca_thread, NULL, vca_main, NULL));
+
 	eb = event_init();
 	assert(eb != NULL);
 

Modified: trunk/varnish-cache/bin/varnishd/mgt_child.c
===================================================================
--- trunk/varnish-cache/bin/varnishd/mgt_child.c	2006-03-23 08:45:44 UTC (rev 60)
+++ trunk/varnish-cache/bin/varnishd/mgt_child.c	2006-03-23 12:20:30 UTC (rev 61)
@@ -62,10 +62,12 @@
 {
 	const char *p;
 
-	p = evbuffer_readline(bev->input);
-	if (p == NULL)
-		return;
-	printf("Child said <%s>\n", p);
+	while (1) {
+		p = evbuffer_readline(bev->input);
+		if (p == NULL)
+			return;
+		printf("Child said <%s>\n", p);
+	}
 }
 
 static void

Modified: trunk/varnish-cache/bin/varnishd/tcp.c
===================================================================
--- trunk/varnish-cache/bin/varnishd/tcp.c	2006-03-23 08:45:44 UTC (rev 60)
+++ trunk/varnish-cache/bin/varnishd/tcp.c	2006-03-23 12:20:30 UTC (rev 61)
@@ -10,6 +10,7 @@
 #include <netdb.h>
 
 #include "heritage.h"
+#include "libvarnish.h"
 
 static void
 create_listen_socket(const char *addr, const char *port, int *sp, int nsp)
@@ -24,7 +25,7 @@
 	i = getaddrinfo(addr, port, &ai, &r0);
 
 	if (i) {
-		fprintf("getaddrinfo failed: %s\n", gai_strerror(i));
+		fprintf(stderr, "getaddrinfo failed: %s\n", gai_strerror(i));
 		return;
 	}
 
@@ -62,5 +63,11 @@
 	create_listen_socket(NULL, port,
 	    &heritage.sock_remote[0], HERITAGE_NSOCKS);
 
+	for (u = 0; u < HERITAGE_NSOCKS; u++) {
+		if (heritage.sock_local[u] >= 0)
+			AZ(listen(heritage.sock_local[u], 16));
+		if (heritage.sock_remote[u] >= 0)
+			AZ(listen(heritage.sock_remote[u], 16));
+	}
 	return (0);
 }

Modified: trunk/varnish-cache/bin/varnishd/varnishd.c
===================================================================
--- trunk/varnish-cache/bin/varnishd/varnishd.c	2006-03-23 08:45:44 UTC (rev 60)
+++ trunk/varnish-cache/bin/varnishd/varnishd.c	2006-03-23 12:20:30 UTC (rev 61)
@@ -176,20 +176,18 @@
 main(int argc, char *argv[])
 {
 	int o;
-	unsigned portnumber = 8080;
+	const char *portnumber = "8080";
 	unsigned dflag = 1;	/* XXX: debug=on for now */
 
 	register_printf_render_std((const unsigned char *)"HVQ");
 
-	open_tcp("8080");
-
 	while ((o = getopt(argc, argv, "dp:")) != -1)
 		switch (o) {
 		case 'd':
 			dflag++;
 			break;
 		case 'p':
-			portnumber = strtoul(optarg, NULL, 0);
+			portnumber = optarg;
 			break;
 		default:
 			usage();
@@ -201,6 +199,15 @@
 	if (argc != 0)
 		usage();
 
+	/*
+	 * XXX: Lacking the suspend/resume facility (due to the socket API
+	 * missing an unlisten(2) facility) we may want to push this into
+	 * the child to limit the amount of time where the socket(s) exists
+	 * but do not answer.  That, on the other hand, would eliminate the
+	 * possibility of doing a "no-glitch" restart of the child process.
+	 */
+	open_tcp(portnumber);
+
 	testme();
 
 




More information about the varnish-commit mailing list