From: phk Date: Sun, 15 Jun 2008 11:47:01 +0000 (+0000) Subject: Hash out the client, up to and including connection to the server. X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=13b8125df396d9e723639fd77a2e36a62ca0cb91;p=varnish Hash out the client, up to and including connection to the server. git-svn-id: svn+ssh://projects.linpro.no/svn/varnish/trunk@2674 d4fa192b-c00b-0410-8231-f00ffab90ce4 --- diff --git a/varnish-cache/bin/varnishtest/vtc_client.c b/varnish-cache/bin/varnishtest/vtc_client.c index d269cbcb..22c1845c 100644 --- a/varnish-cache/bin/varnishtest/vtc_client.c +++ b/varnish-cache/bin/varnishtest/vtc_client.c @@ -25,12 +25,172 @@ */ #include +#include +#include +#include +#include + +#include +#include #include "vtc.h" +#include "vqueue.h" +#include "miniobj.h" +#include "vss.h" +#include "libvarnish.h" + +struct client { + unsigned magic; +#define CLIENT_MAGIC 0x6242397c + char *name; + VTAILQ_ENTRY(client) list; + + char *spec; + + const char *connect; + int naddr; + struct vss_addr **vss_addr; + char *addr; + char *port; + + pthread_t tp; +}; + +static VTAILQ_HEAD(, client) clients = + VTAILQ_HEAD_INITIALIZER(clients); + +/********************************************************************** + * Server thread + */ + +static void * +client_thread(void *priv) +{ + struct client *c; + int i; + int fd; + + CAST_OBJ_NOTNULL(c, priv, CLIENT_MAGIC); + assert(c->naddr > 0); + + printf("### Client %s started\n", c->name); + printf("#### Client %s connect to %s\n", c->name, c->connect); + for (i = 0; i < c->naddr; i++) { + fd = VSS_connect(c->vss_addr[i]); + if (fd >= 0) + break; + } + assert(fd >= 0); + printf("#### Client %s connected to %s fd is %d\n", + c->name, c->connect, fd); + sleep (1); + close(fd); + printf("### Client %s ending\n", c->name); + + return (NULL); +} + +/********************************************************************** + * Allocate and initialize a client + */ + +static struct client * +client_new(char *name) +{ + struct client *c; + + ALLOC_OBJ(c, CLIENT_MAGIC); + c->name = name; + c->connect = ":8080"; + VTAILQ_INSERT_TAIL(&clients, c, list); + return (c); +} + +/********************************************************************** + * Start the client thread + */ + +static void +client_start(struct client *c) +{ + + CHECK_OBJ_NOTNULL(c, CLIENT_MAGIC); + printf("Starting client %s\n", c->name); + AZ(pthread_create(&c->tp, NULL, client_thread, c)); +} + +/********************************************************************** + * Wait for client thread to stop + */ + +static void +client_wait(struct client *c) +{ + void *res; + + CHECK_OBJ_NOTNULL(c, CLIENT_MAGIC); + printf("Waiting for client %s\n", c->name); + AZ(pthread_join(c->tp, &res)); + if (res != NULL) { + fprintf(stderr, "Server %s returned \"%s\"\n", + c->name, (char *)res); + exit (1); + } + c->tp = NULL; +} + +/********************************************************************** + * Run the client thread + */ + +static void +client_run(struct client *c) +{ + + client_start(c); + client_wait(c); +} + + +/********************************************************************** + * Server command dispatch + */ + void cmd_client(char **av, void *priv) { + struct client *c; + + (void)priv; + assert(!strcmp(av[0], "client")); + av++; + + VTAILQ_FOREACH(c, &clients, list) + if (!strcmp(c->name, av[0])) + break; + if (c == NULL) + c = client_new(av[0]); + av++; - cmd_dump(av, priv); + for (; *av != NULL; av++) { + if (!strcmp(*av, "-connect")) { + c->connect = av[1]; + av++; + AZ(VSS_parse(c->connect, &c->addr, &c->port)); + c->naddr = VSS_resolve(c->addr, c->port, &c->vss_addr); + assert(c->naddr > 0); + continue; + } + if (!strcmp(*av, "-run")) { + client_run(c); + continue; + } + if (**av == '{') { + c->spec = *av; + continue; + } + fprintf(stderr, "Unknown client argument: %s\n", *av); + exit (1); + } } diff --git a/varnish-cache/bin/varnishtest/vtc_server.c b/varnish-cache/bin/varnishtest/vtc_server.c index 0a98f5e7..7d2ec5ae 100644 --- a/varnish-cache/bin/varnishtest/vtc_server.c +++ b/varnish-cache/bin/varnishtest/vtc_server.c @@ -138,7 +138,8 @@ server_start(struct server *s) s->sock = VSS_listen(s->vss_addr[0], s->depth); assert(s->sock >= 0); } - printf("\tsocket fd is %d\n", s->sock); + printf("#### Server %s listen on %s (fd %d)\n", + s->name, s->listen, s->sock); AZ(pthread_create(&s->tp, NULL, server_thread, s)); }