From e873bb6090290c8615c9649f92c8c13259bafca1 Mon Sep 17 00:00:00 2001 From: Adam Dickmeiss Date: Thu, 25 Oct 2012 11:06:20 +0200 Subject: [PATCH] New utility yaz_decode_init_diag By adding this, we move code from both ZOOM C and Metaproxy to YAZ. --- include/yaz/proto.h | 7 ++++++ src/Makefile.am | 2 +- src/init_diag.c | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/zoom-z3950.c | 40 ++++---------------------------- win/makefile | 1 + 5 files changed, 78 insertions(+), 36 deletions(-) create mode 100644 src/init_diag.c diff --git a/include/yaz/proto.h b/include/yaz/proto.h index e63f2e9..c998104 100644 --- a/include/yaz/proto.h +++ b/include/yaz/proto.h @@ -185,6 +185,13 @@ const char *yaz_get_esn(Z_RecordComposition *comp); YAZ_EXPORT void yaz_set_esn(Z_RecordComposition **comp_p, const char *esn, NMEM nmem); +/** \brief returns diagnostics from an init response + \param no number of diagnotic entry to get. 0=first, 1=second, .. + \param initrs InitResponse structure + \retuns diag entry or 0 (NULL) if none could be found +*/ +YAZ_EXPORT +Z_DefaultDiagFormat *yaz_decode_init_diag(int no, Z_InitResponse *initrs); YAZ_END_CDECL diff --git a/src/Makefile.am b/src/Makefile.am index 88acf09..3a80f93 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -87,7 +87,7 @@ libyaz_la_SOURCES=base64.c version.c options.c log.c \ ill-get.c \ zget.c yaz-ccl.c diag-entry.c diag-entry.h \ logrpn.c \ - otherinfo.c pquery.c sortspec.c charneg.c initopt.c \ + otherinfo.c pquery.c sortspec.c charneg.c initopt.c init_diag.c \ zoom-c.c zoom-z3950.c zoom-sru.c zoom-query.c zoom-record-cache.c \ zoom-event.c \ record_render.c zoom-socket.c zoom-opt.c zoom-p.h sru-p.h \ diff --git a/src/init_diag.c b/src/init_diag.c new file mode 100644 index 0000000..fbcc059 --- /dev/null +++ b/src/init_diag.c @@ -0,0 +1,64 @@ +/* This file is part of the YAZ toolkit. + * Copyright (C) 1995-2012 Index Data + * See the file LICENSE for details. + */ +/** + * \file init_diag.c + * \brief Decoding of diagnostics embedded in init response + */ +#if HAVE_CONFIG_H +#include +#endif + +#include + +static Z_DefaultDiagFormat *interpret_init_diag2(int *no, + Z_DiagnosticFormat *diag) +{ + int i; + for (i = 0; i < diag->num; i++) + { + Z_DiagnosticFormat_s *ds = diag->elements[i]; + if (ds->which == Z_DiagnosticFormat_s_defaultDiagRec) + { + if (*no == 0) + return ds->u.defaultDiagRec; + (*no)--; + } + } + return 0; +} + +Z_DefaultDiagFormat *yaz_decode_init_diag(int no, Z_InitResponse *initrs) +{ + Z_External *uif = initrs->userInformationField; + if (uif && uif->which == Z_External_userInfo1) + { + int i; + Z_OtherInformation *ui = uif->u.userInfo1; + for (i = 0; i < ui->num_elements; i++) + { + Z_OtherInformationUnit *unit = ui->list[i]; + if (unit->which == Z_OtherInfo_externallyDefinedInfo && + unit->information.externallyDefinedInfo && + unit->information.externallyDefinedInfo->which == + Z_External_diag1) + { + Z_DefaultDiagFormat *r = + interpret_init_diag2 + (&no, unit->information.externallyDefinedInfo->u.diag1); + if (r) + return r; + } + } + } + return 0; +} +/* + * Local variables: + * c-basic-offset: 4 + * c-file-style: "Stroustrup" + * indent-tabs-mode: nil + * End: + * vim: shiftwidth=4 tabstop=8 expandtab + */ diff --git a/src/zoom-z3950.c b/src/zoom-z3950.c index cf1b12f..27a4d4e 100644 --- a/src/zoom-z3950.c +++ b/src/zoom-z3950.c @@ -1085,35 +1085,6 @@ static int handle_Z3950_es_response(ZOOM_connection c, return 1; } -static void interpret_init_diag(ZOOM_connection c, - Z_DiagnosticFormat *diag) -{ - if (diag->num > 0) - { - Z_DiagnosticFormat_s *ds = diag->elements[0]; - if (ds->which == Z_DiagnosticFormat_s_defaultDiagRec) - response_default_diag(c, ds->u.defaultDiagRec); - } -} - - -static void interpret_otherinformation_field(ZOOM_connection c, - Z_OtherInformation *ui) -{ - int i; - for (i = 0; i < ui->num_elements; i++) - { - Z_OtherInformationUnit *unit = ui->list[i]; - if (unit->which == Z_OtherInfo_externallyDefinedInfo && - unit->information.externallyDefinedInfo && - unit->information.externallyDefinedInfo->which == - Z_External_diag1) - { - interpret_init_diag(c, unit->information.externallyDefinedInfo->u.diag1); - } - } -} - static char *get_term_cstr(ODR odr, Z_Term *term) { switch (term->which) { @@ -1680,12 +1651,11 @@ void ZOOM_handle_Z3950_apdu(ZOOM_connection c, Z_APDU *apdu) if (!*initrs->result) { - Z_External *uif = initrs->userInformationField; - - ZOOM_set_error(c, ZOOM_ERROR_INIT, 0); /* default error */ - - if (uif && uif->which == Z_External_userInfo1) - interpret_otherinformation_field(c, uif->u.userInfo1); + Z_DefaultDiagFormat *df = yaz_decode_init_diag(0, initrs); + if (df) + response_default_diag(c, df); + else + ZOOM_set_error(c, ZOOM_ERROR_INIT, 0); /* default error */ } else { diff --git a/win/makefile b/win/makefile index bc667d3..24f764a 100644 --- a/win/makefile +++ b/win/makefile @@ -507,6 +507,7 @@ MISC_OBJS= \ $(OBJDIR)\zoom-opt.obj \ $(OBJDIR)\zoom-socket.obj \ $(OBJDIR)\initopt.obj \ + $(OBJDIR)\init_diag.obj \ $(OBJDIR)\xmlquery.obj \ $(OBJDIR)\xmlerror.obj \ $(OBJDIR)\mime.obj \ -- 1.7.10.4