]> err.no Git - varnish/commitdiff
Server socket & thread creation
authorphk <phk@d4fa192b-c00b-0410-8231-f00ffab90ce4>
Sun, 15 Jun 2008 11:17:45 +0000 (11:17 +0000)
committerphk <phk@d4fa192b-c00b-0410-8231-f00ffab90ce4>
Sun, 15 Jun 2008 11:17:45 +0000 (11:17 +0000)
git-svn-id: svn+ssh://projects.linpro.no/svn/varnish/trunk@2671 d4fa192b-c00b-0410-8231-f00ffab90ce4

varnish-cache/bin/varnishtest/Makefile.am
varnish-cache/bin/varnishtest/vtc.h
varnish-cache/bin/varnishtest/vtc_server.c

index 2d1a5e1755ce73ab8b68f0f1062f532219058c8a..09e59dd89205c12b2e8f3b83398e38707a92ab75 100644 (file)
@@ -14,4 +14,5 @@ varnishtest_SOURCES = \
 
 varnishtest_LDADD = \
                $(top_builddir)/lib/libvarnish/libvarnish.la \
-               $(top_builddir)/lib/libvarnishcompat/libvarnishcompat.la 
+               $(top_builddir)/lib/libvarnishcompat/libvarnishcompat.la \
+               ${PTHREAD_LIBS}
index 1e40e9f541488e428d35f9dbaa1c36f5c4c87540..916888992d43d68d39b77a8ce244bf237153991d 100644 (file)
@@ -24,9 +24,6 @@
  * SUCH DAMAGE.
  */
 
-
-
-
 typedef void cmd_f(char **av, void *priv);
 
 struct cmds {
index 14d41cdea34d3c8ce294cf15dc8349b4ff1f49dc..46de8b39bb5573b01cc240b46c0c543868ec6c07 100644 (file)
 
 
 #include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+#include <pthread.h>
 
 #include "vtc.h"
 
+#include "vqueue.h"
+#include "miniobj.h"
+#include "vss.h"
+#include "libvarnish.h"
+
+struct server {
+       unsigned                magic;
+#define SERVER_MAGIC           0x55286619
+       char                    *name;
+       VTAILQ_ENTRY(server)    list;
+
+       unsigned                repeat;
+       char                    *spec;
+       
+       int                     depth;
+       int                     sock;
+       const char              *listen;
+       struct vss_addr         **vss_addr;
+       char                    *addr;
+       char                    *port;
+
+       pthread_t               tp;
+};
+
+static VTAILQ_HEAD(, server)   servers =
+    VTAILQ_HEAD_INITIALIZER(servers);
+
+/**********************************************************************
+ * Server thread
+ */
+
+static void *
+server_thread(void *priv)
+{
+       struct server *s;
+
+       CAST_OBJ_NOTNULL(s, priv, SERVER_MAGIC);
+
+       printf("### Server %s started\n", s->name);
+       sleep(3);
+       printf("### Server %s ending\n", s->name);
+
+       return (NULL);
+}
+
+/**********************************************************************
+ * Allocate and initialize a server
+ */
+
+static struct server *
+server_new(char *name)
+{
+       struct server *s;
+
+       ALLOC_OBJ(s, SERVER_MAGIC);
+       s->name = name;
+       s->listen = ":9080";
+       s->depth = 1;
+       s->sock = -1;
+       VTAILQ_INSERT_TAIL(&servers, s, list);
+       return (s);
+}
+
+/**********************************************************************
+ * Start the server thread
+ */
+
+static void
+server_start(struct server *s)
+{
+       int naddr;
+
+       CHECK_OBJ_NOTNULL(s, SERVER_MAGIC);
+       printf("Starting server %s\n", s->name);
+       if (s->sock < 0) {
+               AZ(VSS_parse(s->listen, &s->addr, &s->port));
+               naddr = VSS_resolve(s->addr, s->port, &s->vss_addr);
+               if (naddr != 1) {
+                       fprintf(stderr,
+                           "Server %s listen address not unique\n"
+                           "   \"%s\" resolves to (%d) sockets\n",
+                           s->name, s->listen, naddr);
+                       exit (1);
+               }
+               s->sock = VSS_listen(s->vss_addr[0], s->depth);
+               assert(s->sock >= 0);
+       }
+       printf("\tsocket fd is %d\n", s->sock);
+       AZ(pthread_create(&s->tp, NULL, server_thread, s));
+}
+
+/**********************************************************************
+ * Wait for server thread to stop
+ */
+
+static void
+server_wait(struct server *s)
+{
+       void *res;
+
+       CHECK_OBJ_NOTNULL(s, SERVER_MAGIC);
+       printf("Waiting for server %s\n", s->name);
+       AZ(pthread_join(s->tp, &res));
+       if (res != NULL) {
+               fprintf(stderr, "Server %s returned \"%s\"\n",
+                   s->name, (char *)res);
+               exit (1);
+       }
+       s->tp = NULL;
+}
+
+/**********************************************************************
+ * Server command dispatch
+ */
+
 void
 cmd_server(char **av, void *priv)
 {
+       struct server *s;
+
+       (void)priv;
+       assert(!strcmp(av[0], "server"));
+       av++;
+
+       VTAILQ_FOREACH(s, &servers, list)
+               if (!strcmp(s->name, av[0]))
+                       break;
+       if (s == NULL) 
+               s = server_new(av[0]);
+       av++;
 
-       cmd_dump(av, priv);
+       for (; *av != NULL; av++) {
+               if (!strcmp(*av, "-repeat")) {
+                       s->repeat = atoi(av[1]);
+                       av++;
+                       continue;
+               }
+               if (!strcmp(*av, "-start")) {
+                       server_start(s);
+                       continue;
+               }
+               if (!strcmp(*av, "-wait")) {
+                       server_wait(s);
+                       continue;
+               }
+               if (**av == '{') {
+                       s->spec = *av;
+                       continue;
+               }
+               fprintf(stderr, "Unknown server argument: %s\n", *av);
+               exit (1);
+       }
 }