From a4d692f24164e5e8271599c2fcfd9cc5acc25c4b Mon Sep 17 00:00:00 2001 From: Adam Dickmeiss Date: Mon, 7 Sep 2009 11:01:29 +0200 Subject: [PATCH] Multiple services. The 'service' element may be repeated inside 'server'. A service may be identified by an attribute 'id'. There can be exactly one or no unnamed service (no attribute). For command init a service may be selected by the use of parameter 'service'. No 'service' parameter selects the unnamed service (backwards compatible). --- src/http_command.c | 14 ++++++++------ src/pazpar2_config.c | 51 ++++++++++++++++++++++++++++++++++++++++++-------- src/pazpar2_config.h | 7 ++++++- src/test_config.c | 2 +- src/test_record.c | 2 +- 5 files changed, 59 insertions(+), 17 deletions(-) diff --git a/src/http_command.c b/src/http_command.c index f88e296..762a0fd 100644 --- a/src/http_command.c +++ b/src/http_command.c @@ -240,20 +240,22 @@ static void cmd_exit(struct http_channel *c) http_close_server(); } -static struct conf_service *locate_service(const char *service_name) -{ - return global_parameters.server->service; -} - static void cmd_init(struct http_channel *c) { unsigned int sesid; char buf[1024]; const char *clear = http_argbyname(c->request, "clear"); const char *service_name = http_argbyname(c->request, "service"); - struct http_session *s = http_session_create(locate_service(service_name)); + struct conf_service *service = locate_service(service_name); + struct http_session *s = http_session_create(service); struct http_response *rs = c->response; + if (!service) + { + error(rs, PAZPAR2_MALFORMED_PARAMETER_VALUE, "service"); + return; + } + yaz_log(YLOG_DEBUG, "HTTP Session init"); if (!clear || *clear == '0') session_init_databases(s->psession); diff --git a/src/pazpar2_config.c b/src/pazpar2_config.c index 6f14cff..e796e00 100644 --- a/src/pazpar2_config.c +++ b/src/pazpar2_config.c @@ -95,7 +95,8 @@ struct conf_sortkey * conf_sortkey_assign(NMEM nmem, } -struct conf_service * conf_service_create(int num_metadata, int num_sortkeys) +struct conf_service * conf_service_create(int num_metadata, int num_sortkeys, + const char *service_id) { struct conf_service * service = 0; NMEM nmem = nmem_create(); @@ -103,7 +104,10 @@ struct conf_service * conf_service_create(int num_metadata, int num_sortkeys) //assert(nmem); service = nmem_malloc(nmem, sizeof(struct conf_service)); + service->nmem = nmem; + service->next = 0; + service->id = service_id ? nmem_strdup(nmem, service_id) : 0; service->num_metadata = num_metadata; service->metadata = 0; if (service->num_metadata) @@ -116,8 +120,6 @@ struct conf_service * conf_service_create(int num_metadata, int num_sortkeys) service->sortkeys = nmem_malloc(nmem, sizeof(struct conf_sortkey) * service->num_sortkeys); - - service->nmem = nmem; service->dictionary = 0; return service; } @@ -206,7 +208,7 @@ int conf_service_sortkey_field_id(struct conf_service *service, /* Code to parse configuration file */ /* ==================================================== */ -static struct conf_service *parse_service(xmlNode *node) +static struct conf_service *parse_service(xmlNode *node, const char *service_id) { xmlNode *n; int md_node = 0; @@ -228,7 +230,7 @@ static struct conf_service *parse_service(xmlNode *node) xmlFree(sortkey); } - service = conf_service_create(num_metadata, num_sortkeys); + service = conf_service_create(num_metadata, num_sortkeys, service_id); for (n = node->children; n; n = n->next) { @@ -514,10 +516,31 @@ static struct conf_server *parse_server(NMEM nmem, xmlNode *node) } else if (!strcmp((const char *) n->name, "service")) { - struct conf_service *s = parse_service(n); - if (!s) + const char *service_id = (const char *) + xmlGetProp(n, (xmlChar *) "id"); + + struct conf_service **sp = &server->service; + for (; *sp; sp = &(*sp)->next) + if ((*sp)->id && service_id && + 0 == strcmp((*sp)->id, service_id)) + { + yaz_log(YLOG_FATAL, "Duplicate service: %s", service_id); + break; + } + else if (!(*sp)->id && !service_id) + { + yaz_log(YLOG_FATAL, "Duplicate unnamed service"); + break; + } + + if (*sp) /* service already exist */ return 0; - server->service = s; + else + { + struct conf_service *s = parse_service(n, service_id); + if (s) + *sp = s; + } } else { @@ -581,6 +604,18 @@ static struct conf_targetprofiles *parse_targetprofiles(NMEM nmem, return r; } +struct conf_service *locate_service(const char *service_id) +{ + struct conf_service *s = config->servers->service; + for (; s; s = s->next) + if (s->id && service_id && 0 == strcmp(s->id, service_id)) + return s; + else if (!s->id && !service_id) + return s; + return 0; +} + + static struct conf_config *parse_config(xmlNode *root) { NMEM nmem = nmem_create(); diff --git a/src/pazpar2_config.h b/src/pazpar2_config.h index a26aeae..fbfe5c6 100644 --- a/src/pazpar2_config.h +++ b/src/pazpar2_config.h @@ -101,10 +101,13 @@ struct conf_service int num_sortkeys; struct conf_sortkey *sortkeys; struct setting_dictionary *dictionary; + struct conf_service *next; + char *id; NMEM nmem; }; -struct conf_service * conf_service_create(int num_metadata, int num_sortkeys); +struct conf_service * conf_service_create(int num_metadata, int num_sortkeys, + const char *service_id); struct conf_metadata* conf_service_add_metadata(struct conf_service *service, int field_id, @@ -129,6 +132,8 @@ int conf_service_metadata_field_id(struct conf_service *service, const char * na int conf_service_sortkey_field_id(struct conf_service *service, const char * name); +struct conf_service *locate_service(const char *service_id); + struct conf_server { char *host; diff --git a/src/test_config.c b/src/test_config.c index 8ecfa24..52d524a 100644 --- a/src/test_config.c +++ b/src/test_config.c @@ -35,7 +35,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA void test_conf_service(int argc, char **argv) { struct conf_service *service = 0; - service = conf_service_create(4, 3); + service = conf_service_create(4, 3, 0); YAZ_CHECK(service); diff --git a/src/test_record.c b/src/test_record.c index 7ff8996..0b62f0f 100644 --- a/src/test_record.c +++ b/src/test_record.c @@ -51,7 +51,7 @@ void test_record(int argc, char **argv) data_num.number.max = 5; - service = conf_service_create(4, 3); + service = conf_service_create(4, 3, 0); YAZ_CHECK(service); YAZ_CHECK(conf_service_add_metadata( -- 1.7.10.4