From 295e1cce2ce6622ce5fa7f03b4f8ca489ffa08b6 Mon Sep 17 00:00:00 2001 From: phk Date: Mon, 7 Jul 2008 18:02:06 +0000 Subject: [PATCH] Move random director to separate source file. git-svn-id: svn+ssh://projects.linpro.no/svn/varnish/trunk@2885 d4fa192b-c00b-0410-8231-f00ffab90ce4 --- varnish-cache/lib/libvcl/Makefile.am | 1 + varnish-cache/lib/libvcl/vcc_backend.c | 98 +--------------- varnish-cache/lib/libvcl/vcc_dir_random.c | 134 ++++++++++++++++++++++ varnish-cache/lib/libvcl/vcc_priv.h | 17 +++ 4 files changed, 157 insertions(+), 93 deletions(-) create mode 100644 varnish-cache/lib/libvcl/vcc_dir_random.c diff --git a/varnish-cache/lib/libvcl/Makefile.am b/varnish-cache/lib/libvcl/Makefile.am index 140ab27d..388ac77e 100644 --- a/varnish-cache/lib/libvcl/Makefile.am +++ b/varnish-cache/lib/libvcl/Makefile.am @@ -13,6 +13,7 @@ libvcl_la_SOURCES = \ vcc_action.c \ vcc_backend.c \ vcc_compile.c \ + vcc_dir_random.c \ vcc_parse.c \ vcc_fixed_token.c \ vcc_obj.c \ diff --git a/varnish-cache/lib/libvcl/vcc_backend.c b/varnish-cache/lib/libvcl/vcc_backend.c index e2909c54..15161c1e 100644 --- a/varnish-cache/lib/libvcl/vcc_backend.c +++ b/varnish-cache/lib/libvcl/vcc_backend.c @@ -100,7 +100,7 @@ struct fld_spec { struct token *found; }; -static void +void vcc_ResetFldSpec(struct fld_spec *f) { @@ -108,7 +108,7 @@ vcc_ResetFldSpec(struct fld_spec *f) f->found = NULL; } -static struct fld_spec * +struct fld_spec * vcc_FldSpec(struct tokenlist *tl, const char *first, ...) { struct fld_spec f[100], *r; @@ -136,7 +136,7 @@ vcc_FldSpec(struct tokenlist *tl, const char *first, ...) } -static void +void vcc_IsField(struct tokenlist *tl, struct token **t, struct fld_spec *fs) { struct token *t_field; @@ -172,7 +172,7 @@ vcc_IsField(struct tokenlist *tl, struct token **t, struct fld_spec *fs) return; } -static int +int vcc_FieldsOk(struct tokenlist *tl, const struct fld_spec *fs) { int ok = 1; @@ -322,7 +322,7 @@ vcc_ParseHostDef(struct tokenlist *tl, int *nbh, const struct token *name, const * The struct vrt_backend is emitted to Fh(). */ -static void +void vcc_ParseBackendHost(struct tokenlist *tl, int *nbh, const struct token *name, const char *qual, int serial) { struct host *h; @@ -401,94 +401,6 @@ vcc_ParseBackend(struct tokenlist *tl) tl->nbackend++; } -/*-------------------------------------------------------------------- - * Parse directors - */ - -static void -vcc_ParseRandomDirector(struct tokenlist *tl, struct token *t_dir) -{ - struct token *t_field, *t_be; - int nbh, nelem; - struct fld_spec *fs; - unsigned u; - - Fh(tl, 1, "\n#define VGC_backend_%.*s (VCL_conf.director[%d])\n", - PF(t_dir), tl->nbackend); - vcc_AddDef(tl, t_dir, R_BACKEND); - - fs = vcc_FldSpec(tl, "!backend", "!weight", NULL); - - vcc_NextToken(tl); /* ID: policy (= random) */ - - ExpectErr(tl, '{'); - vcc_NextToken(tl); - - Fc(tl, 0, - "\nstatic const struct vrt_dir_random_entry vdre_%.*s[] = {\n", - PF(t_dir)); - - for (nelem = 0; tl->t->tok != '}'; nelem++) { /* List of members */ - t_be = tl->t; - vcc_ResetFldSpec(fs); - nbh = -1; - - ExpectErr(tl, '{'); - vcc_NextToken(tl); - Fc(tl, 0, "\t{"); - - while (tl->t->tok != '}') { /* Member fields */ - vcc_IsField(tl, &t_field, fs); - ERRCHK(tl); - if (vcc_IdIs(t_field, "backend")) { - vcc_ParseBackendHost(tl, &nbh, - t_dir, "random", nelem); - Fc(tl, 0, " .host = &bh_%d,", nbh); - ERRCHK(tl); - } else if (vcc_IdIs(t_field, "weight")) { - ExpectErr(tl, CNUM); - u = vcc_UintVal(tl); - if (u == 0) { - vsb_printf(tl->sb, - "The .weight must be higher than zero."); - vcc_ErrToken(tl, tl->t); - vsb_printf(tl->sb, " at\n"); - vcc_ErrWhere(tl, tl->t); - return; - } - Fc(tl, 0, " .weight = %u", u); - vcc_NextToken(tl); - ExpectErr(tl, ';'); - vcc_NextToken(tl); - } else { - ErrInternal(tl); - } - } - if (!vcc_FieldsOk(tl, fs)) { - vsb_printf(tl->sb, - "\nIn member host specfication starting at:\n"); - vcc_ErrWhere(tl, t_be); - return; - } - Fc(tl, 0, " },\n"); - vcc_NextToken(tl); - } - Fc(tl, 0, "\t{ .host = 0 }\n"); - Fc(tl, 0, "};\n"); - Fc(tl, 0, - "\nstatic const struct vrt_dir_random vdr_%.*s = {\n", - PF(t_dir)); - Fc(tl, 0, "\t.name = \"%.*s\",\n", PF(t_dir)); - Fc(tl, 0, "\t.nmember = %d,\n", nelem); - Fc(tl, 0, "\t.members = vdre_%.*s,\n", PF(t_dir)); - Fc(tl, 0, "};\n"); - vcc_NextToken(tl); - Fi(tl, 0, - "\tVRT_init_dir_random(cli, &VGC_backend_%.*s , &vdr_%.*s);\n", - PF(t_dir), PF(t_dir)); - Ff(tl, 0, "\tVRT_fini_dir(cli, VGC_backend_%.*s);\n", PF(t_dir)); -} - /*-------------------------------------------------------------------- * Parse directors */ diff --git a/varnish-cache/lib/libvcl/vcc_dir_random.c b/varnish-cache/lib/libvcl/vcc_dir_random.c new file mode 100644 index 00000000..6cd9de1b --- /dev/null +++ b/varnish-cache/lib/libvcl/vcc_dir_random.c @@ -0,0 +1,134 @@ +/*- + * Copyright (c) 2006 Verdens Gang AS + * Copyright (c) 2006-2008 Linpro AS + * All rights reserved. + * + * Author: Poul-Henning Kamp + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $Id$ + */ + +#include "config.h" + +#include +#include + +#include +#include +#include +#include + +#include "vsb.h" + +#include "vcc_priv.h" +#include "vcc_compile.h" +#include "libvarnish.h" + +/*-------------------------------------------------------------------- + * Parse directors + */ + +void +vcc_ParseRandomDirector(struct tokenlist *tl, struct token *t_dir) +{ + struct token *t_field, *t_be; + int nbh, nelem; + struct fld_spec *fs; + unsigned u; + + Fh(tl, 1, "\n#define VGC_backend_%.*s (VCL_conf.director[%d])\n", + PF(t_dir), tl->nbackend); + vcc_AddDef(tl, t_dir, R_BACKEND); + + fs = vcc_FldSpec(tl, "!backend", "!weight", NULL); + + vcc_NextToken(tl); /* ID: policy (= random) */ + + ExpectErr(tl, '{'); + vcc_NextToken(tl); + + Fc(tl, 0, + "\nstatic const struct vrt_dir_random_entry vdre_%.*s[] = {\n", + PF(t_dir)); + + for (nelem = 0; tl->t->tok != '}'; nelem++) { /* List of members */ + t_be = tl->t; + vcc_ResetFldSpec(fs); + nbh = -1; + + ExpectErr(tl, '{'); + vcc_NextToken(tl); + Fc(tl, 0, "\t{"); + + while (tl->t->tok != '}') { /* Member fields */ + vcc_IsField(tl, &t_field, fs); + ERRCHK(tl); + if (vcc_IdIs(t_field, "backend")) { + vcc_ParseBackendHost(tl, &nbh, + t_dir, "random", nelem); + Fc(tl, 0, " .host = &bh_%d,", nbh); + ERRCHK(tl); + } else if (vcc_IdIs(t_field, "weight")) { + ExpectErr(tl, CNUM); + u = vcc_UintVal(tl); + if (u == 0) { + vsb_printf(tl->sb, + "The .weight must be higher than zero."); + vcc_ErrToken(tl, tl->t); + vsb_printf(tl->sb, " at\n"); + vcc_ErrWhere(tl, tl->t); + return; + } + Fc(tl, 0, " .weight = %u", u); + vcc_NextToken(tl); + ExpectErr(tl, ';'); + vcc_NextToken(tl); + } else { + ErrInternal(tl); + } + } + if (!vcc_FieldsOk(tl, fs)) { + vsb_printf(tl->sb, + "\nIn member host specfication starting at:\n"); + vcc_ErrWhere(tl, t_be); + return; + } + Fc(tl, 0, " },\n"); + vcc_NextToken(tl); + } + Fc(tl, 0, "\t{ .host = 0 }\n"); + Fc(tl, 0, "};\n"); + Fc(tl, 0, + "\nstatic const struct vrt_dir_random vdr_%.*s = {\n", + PF(t_dir)); + Fc(tl, 0, "\t.name = \"%.*s\",\n", PF(t_dir)); + Fc(tl, 0, "\t.nmember = %d,\n", nelem); + Fc(tl, 0, "\t.members = vdre_%.*s,\n", PF(t_dir)); + Fc(tl, 0, "};\n"); + vcc_NextToken(tl); + Fi(tl, 0, + "\tVRT_init_dir_random(cli, &VGC_backend_%.*s , &vdr_%.*s);\n", + PF(t_dir), PF(t_dir)); + Ff(tl, 0, "\tVRT_fini_dir(cli, VGC_backend_%.*s);\n", PF(t_dir)); +} diff --git a/varnish-cache/lib/libvcl/vcc_priv.h b/varnish-cache/lib/libvcl/vcc_priv.h index 91da4cfe..26d468c4 100644 --- a/varnish-cache/lib/libvcl/vcc_priv.h +++ b/varnish-cache/lib/libvcl/vcc_priv.h @@ -34,6 +34,8 @@ #include "vcc_token_defs.h" struct vsb; +struct tokenlist; +struct token; #define isident1(c) (isalpha(c)) #define isident(c) (isalpha(c) || isdigit(c) || (c) == '_' || (c) == '-') @@ -44,3 +46,18 @@ void vcl_init_tnames(void); void vcl_output_lang_h(struct vsb *sb); #define PF(t) (int)((t)->e - (t)->b), (t)->b + +/* vcc_backend.c */ +struct fld_spec; +struct fld_spec * vcc_FldSpec(struct tokenlist *tl, const char *first, ...); +void vcc_ResetFldSpec(struct fld_spec *f); +void vcc_IsField(struct tokenlist *tl, struct token **t, struct fld_spec *fs); +int vcc_FieldsOk(struct tokenlist *tl, const struct fld_spec *fs); +void vcc_ParseBackendHost(struct tokenlist *tl, int *nbh, const struct token *name, const char *qual, int serial); + + + + +/* vcc_dir_random.c */ +void vcc_ParseRandomDirector(struct tokenlist *tl, struct token *t_dir); + -- 2.39.5