1 /* This file is part of Pazpar2.
2 Copyright (C) 2006-2009 Index Data
4 Pazpar2 is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
9 Pazpar2 is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
40 #include <yaz/marcdisp.h>
41 #include <yaz/comstack.h>
42 #include <yaz/tcpip.h>
43 #include <yaz/proto.h>
44 #include <yaz/readconf.h>
45 #include <yaz/pquery.h>
46 #include <yaz/otherinfo.h>
47 #include <yaz/yaz-util.h>
49 #include <yaz/query-charset.h>
50 #include <yaz/querytowrbuf.h>
51 #include <yaz/oid_db.h>
52 #include <yaz/diagbib1.h>
53 #include <yaz/snprintf.h>
54 #include <yaz/rpn2cql.h>
58 #include <yaz/timing.h>
64 #include "connection.h"
67 /** \brief Represents client state for a connection to one search target */
69 struct session_database *database;
70 struct conf_server *server;
71 struct connection *connection;
72 struct session *session;
73 char *pquery; // Current search
74 char *cqlquery; // used for SRU targets only
78 enum client_state state;
79 struct show_raw *show_raw;
80 struct client *next; // next client in session or next in free list
81 ZOOM_resultset resultset;
85 int active; // whether this request has been sent to the server
90 void (*error_handler)(void *data, const char *addinfo);
91 void (*record_handler)(void *data, const char *buf, size_t sz);
93 struct show_raw *next;
96 static const char *client_states[] = {
102 "Client_Disconnected"
105 static struct client *client_freelist = 0;
107 const char *client_get_state_str(struct client *cl)
109 return client_states[cl->state];
112 enum client_state client_get_state(struct client *cl)
117 void client_set_state(struct client *cl, enum client_state st)
122 int no_active = session_active_clients(cl->session);
124 session_alert_watch(cl->session, SESSION_WATCH_SHOW);
128 static void client_show_raw_error(struct client *cl, const char *addinfo);
130 // Close connection and set state to error
131 void client_fatal(struct client *cl)
133 yaz_log(YLOG_WARN, "Fatal error from %s", client_get_url(cl));
134 connection_destroy(cl->connection);
135 client_set_state(cl, Client_Error);
138 struct connection *client_get_connection(struct client *cl)
140 return cl->connection;
143 struct session_database *client_get_database(struct client *cl)
148 struct session *client_get_session(struct client *cl)
153 const char *client_get_pquery(struct client *cl)
158 static void client_send_raw_present(struct client *cl);
159 static int nativesyntax_to_type(struct session_database *sdb, char *type,
162 static void client_show_immediate(
163 ZOOM_resultset resultset, struct session_database *sdb, int position,
165 void (*error_handler)(void *data, const char *addinfo),
166 void (*record_handler)(void *data, const char *buf, size_t sz),
176 error_handler(data, "no resultset");
179 rec = ZOOM_resultset_record(resultset, position-1);
182 error_handler(data, "no record");
188 nativesyntax_to_type(sdb, type, rec);
189 buf = ZOOM_record_get(rec, type, &len);
192 error_handler(data, "no record");
195 record_handler(data, buf, len);
199 int client_show_raw_begin(struct client *cl, int position,
200 const char *syntax, const char *esn,
202 void (*error_handler)(void *data, const char *addinfo),
203 void (*record_handler)(void *data, const char *buf,
207 if (syntax == 0 && esn == 0)
208 client_show_immediate(cl->resultset, client_get_database(cl),
210 error_handler, record_handler,
214 struct show_raw *rr, **rrp;
220 rr = xmalloc(sizeof(*rr));
221 rr->position = position;
224 rr->error_handler = error_handler;
225 rr->record_handler = record_handler;
228 rr->syntax = xstrdup(syntax);
232 rr->esn = xstrdup(esn);
237 for (rrp = &cl->show_raw; *rrp; rrp = &(*rrp)->next)
241 if (cl->state == Client_Failed)
243 client_show_raw_error(cl, "client failed");
245 else if (cl->state == Client_Disconnected)
247 client_show_raw_error(cl, "client disconnected");
251 client_send_raw_present(cl);
257 void client_show_raw_remove(struct client *cl, void *data)
259 struct show_raw *rr = data;
260 struct show_raw **rrp = &cl->show_raw;
270 void client_show_raw_dequeue(struct client *cl)
272 struct show_raw *rr = cl->show_raw;
274 cl->show_raw = rr->next;
278 static void client_show_raw_error(struct client *cl, const char *addinfo)
282 cl->show_raw->error_handler(cl->show_raw->data, addinfo);
283 client_show_raw_dequeue(cl);
287 static void client_send_raw_present(struct client *cl)
289 struct session_database *sdb = client_get_database(cl);
290 struct connection *co = client_get_connection(cl);
291 ZOOM_resultset set = cl->resultset;
293 int offset = cl->show_raw->position;
294 const char *syntax = 0;
295 const char *elements = 0;
297 assert(cl->show_raw);
300 yaz_log(YLOG_DEBUG, "%s: trying to present %d record(s) from %d",
301 client_get_url(cl), 1, offset);
303 if (cl->show_raw->syntax)
304 syntax = cl->show_raw->syntax;
306 syntax = session_setting_oneval(sdb, PZ_REQUESTSYNTAX);
307 ZOOM_resultset_option_set(set, "preferredRecordSyntax", syntax);
309 if (cl->show_raw->esn)
310 elements = cl->show_raw->esn;
312 elements = session_setting_oneval(sdb, PZ_ELEMENTS);
313 if (elements && *elements)
314 ZOOM_resultset_option_set(set, "elementSetName", elements);
316 ZOOM_resultset_records(set, 0, offset-1, 1);
317 cl->show_raw->active = 1;
319 connection_continue(co);
322 static int nativesyntax_to_type(struct session_database *sdb, char *type,
325 const char *s = session_setting_oneval(sdb, PZ_NATIVESYNTAX);
329 if (!strncmp(s, "iso2709", 7))
331 const char *cp = strchr(s, ';');
332 yaz_snprintf(type, 80, "xml; charset=%s", cp ? cp+1 : "marc-8s");
334 else if (!strncmp(s, "xml", 3))
342 else /* attempt to deduce structure */
344 const char *syntax = ZOOM_record_get(rec, "syntax", NULL);
347 if (!strcmp(syntax, "XML"))
352 else if (!strcmp(syntax, "USmarc") || !strcmp(syntax, "MARC21"))
354 strcpy(type, "xml; charset=marc8-s");
363 static void ingest_raw_record(struct client *cl, ZOOM_record rec)
369 if (cl->show_raw->binary)
373 struct session_database *sdb = client_get_database(cl);
374 nativesyntax_to_type(sdb, type, rec);
377 buf = ZOOM_record_get(rec, type, &len);
378 cl->show_raw->record_handler(cl->show_raw->data, buf, len);
379 client_show_raw_dequeue(cl);
382 void client_search_response(struct client *cl)
384 struct connection *co = cl->connection;
385 struct session *se = cl->session;
386 ZOOM_connection link = connection_get_link(co);
387 ZOOM_resultset resultset = cl->resultset;
388 const char *error, *addinfo;
390 if (ZOOM_connection_error(link, &error, &addinfo))
393 client_set_state(cl, Client_Error);
394 yaz_log(YLOG_WARN, "Search error %s (%s): %s",
395 error, addinfo, client_get_url(cl));
399 cl->record_offset = 0;
400 cl->hits = ZOOM_resultset_size(resultset);
401 se->total_hits += cl->hits;
406 void client_record_response(struct client *cl)
408 struct connection *co = cl->connection;
409 ZOOM_connection link = connection_get_link(co);
410 ZOOM_resultset resultset = cl->resultset;
411 const char *error, *addinfo;
413 if (ZOOM_connection_error(link, &error, &addinfo))
415 client_set_state(cl, Client_Error);
416 yaz_log(YLOG_WARN, "Search error %s (%s): %s",
417 error, addinfo, client_get_url(cl));
422 const char *msg, *addinfo;
424 if (cl->show_raw && cl->show_raw->active)
426 if ((rec = ZOOM_resultset_record(resultset,
427 cl->show_raw->position-1)))
429 cl->show_raw->active = 0;
430 ingest_raw_record(cl, rec);
434 yaz_log(YLOG_WARN, "Expected record, but got NULL, offset=%d",
435 cl->show_raw->position-1);
440 int offset = cl->record_offset;
441 if ((rec = ZOOM_resultset_record(resultset, offset)))
444 if (ZOOM_record_error(rec, &msg, &addinfo, 0))
445 yaz_log(YLOG_WARN, "Record error %s (%s): %s (rec #%d)",
446 error, addinfo, client_get_url(cl),
450 struct session_database *sdb = client_get_database(cl);
453 if (nativesyntax_to_type(sdb, type, rec))
454 yaz_log(YLOG_WARN, "Failed to determine record type");
455 if ((xmlrec = ZOOM_record_get(rec, type, NULL)))
457 if (ingest_record(cl, xmlrec, cl->record_offset))
459 session_alert_watch(cl->session, SESSION_WATCH_SHOW);
460 session_alert_watch(cl->session, SESSION_WATCH_RECORD);
463 yaz_log(YLOG_WARN, "Failed to ingest");
466 yaz_log(YLOG_WARN, "Failed to extract ZOOM record");
472 yaz_log(YLOG_WARN, "Expected record, but got NULL, offset=%d",
479 void client_start_search(struct client *cl)
481 struct session_database *sdb = client_get_database(cl);
482 struct connection *co = client_get_connection(cl);
483 ZOOM_connection link = connection_get_link(co);
485 char *databaseName = sdb->database->databases[0];
486 const char *opt_piggyback = session_setting_oneval(sdb, PZ_PIGGYBACK);
487 const char *opt_queryenc = session_setting_oneval(sdb, PZ_QUERYENCODING);
488 const char *opt_elements = session_setting_oneval(sdb, PZ_ELEMENTS);
489 const char *opt_requestsyn = session_setting_oneval(sdb, PZ_REQUESTSYNTAX);
490 const char *opt_maxrecs = session_setting_oneval(sdb, PZ_MAXRECS);
491 const char *opt_sru = session_setting_oneval(sdb, PZ_SRU);
496 cl->record_offset = 0;
498 client_set_state(cl, Client_Working);
501 ZOOM_connection_option_set(link, "piggyback", opt_piggyback);
503 ZOOM_connection_option_set(link, "piggyback", "1");
505 ZOOM_connection_option_set(link, "rpnCharset", opt_queryenc);
506 if (*opt_sru && *opt_elements)
507 ZOOM_connection_option_set(link, "schema", opt_elements);
508 else if (*opt_elements)
509 ZOOM_connection_option_set(link, "elementSetName", opt_elements);
511 ZOOM_connection_option_set(link, "preferredRecordSyntax", opt_requestsyn);
513 ZOOM_connection_option_set(link, "count", opt_maxrecs);
517 sprintf(n, "%d", global_parameters.toget);
518 ZOOM_connection_option_set(link, "count", n);
521 ZOOM_connection_option_set(link, "databaseName", databaseName);
523 ZOOM_connection_option_set(link, "presentChunk", "20");
527 ZOOM_query q = ZOOM_query_create();
528 yaz_log(YLOG_LOG, "Search %s CQL: %s", sdb->database->url, cl->cqlquery);
529 ZOOM_query_cql(q, cl->cqlquery);
530 rs = ZOOM_connection_search(link, q);
531 ZOOM_query_destroy(q);
535 yaz_log(YLOG_LOG, "Search %s PQF: %s", sdb->database->url, cl->pquery);
536 rs = ZOOM_connection_search_pqf(link, cl->pquery);
538 ZOOM_resultset_destroy(cl->resultset);
540 connection_continue(co);
543 struct client *client_create(void)
549 client_freelist = client_freelist->next;
552 r = xmalloc(sizeof(struct client));
559 r->record_offset = 0;
561 r->state = Client_Disconnected;
568 void client_destroy(struct client *c)
570 struct session *se = c->session;
571 if (c == se->clients)
572 se->clients = c->next;
576 for (cc = se->clients; cc && cc->next != c; cc = cc->next)
585 connection_release(c->connection);
587 ZOOM_resultset_destroy(c->resultset);
589 c->next = client_freelist;
593 void client_set_connection(struct client *cl, struct connection *con)
595 cl->connection = con;
598 void client_disconnect(struct client *cl)
600 if (cl->state != Client_Idle)
601 client_set_state(cl, Client_Disconnected);
602 client_set_connection(cl, 0);
605 // Extract terms from query into null-terminated termlist
606 static void extract_terms(NMEM nmem, struct ccl_rpn_node *query, char **termlist)
610 pull_terms(nmem, query, termlist, &num);
614 // Initialize CCL map for a target
615 static CCL_bibset prepare_cclmap(struct client *cl)
617 struct session_database *sdb = client_get_database(cl);
624 for (s = sdb->settings[PZ_CCLMAP]; s; s = s->next)
626 char *p = strchr(s->name + 3, ':');
629 yaz_log(YLOG_WARN, "Malformed cclmap name: %s", s->name);
634 ccl_qual_fitem(res, s->value, p);
639 // returns a xmalloced CQL query corresponding to the pquery in client
640 static char *make_cqlquery(struct client *cl)
642 cql_transform_t cqlt = cql_transform_create();
645 WRBUF wrb = wrbuf_alloc();
648 zquery = p_query_rpn(global_parameters.odr_out, cl->pquery);
649 if ((status = cql_transform_rpn2cql_wrbuf(cqlt, wrb, zquery)))
651 yaz_log(YLOG_WARN, "failed to generate CQL query, code=%d", status);
654 r = xstrdup(wrbuf_cstr(wrb));
657 odr_reset(global_parameters.odr_out); // releases the zquery
658 cql_transform_close(cqlt);
662 // Parse the query given the settings specific to this client
663 int client_parse_query(struct client *cl, const char *query)
665 struct session *se = client_get_session(cl);
666 struct session_database *sdb = client_get_database(cl);
667 struct ccl_rpn_node *cn;
669 CCL_bibset ccl_map = prepare_cclmap(cl);
670 const char *sru = session_setting_oneval(sdb, PZ_SRU);
671 const char *pqf_prefix = session_setting_oneval(sdb, PZ_PQF_PREFIX);
676 cn = ccl_find_str(ccl_map, query, &cerror, &cpos);
677 ccl_qual_rm(&ccl_map);
680 client_set_state(cl, Client_Error);
681 yaz_log(YLOG_WARN, "Failed to parse query for %s",
682 client_get_database(cl)->database->url);
685 wrbuf_rewind(se->wrbuf);
688 wrbuf_puts(se->wrbuf, pqf_prefix);
689 wrbuf_puts(se->wrbuf, " ");
691 ccl_pquery(se->wrbuf, cn);
693 cl->pquery = xstrdup(wrbuf_cstr(se->wrbuf));
698 if (!(cl->cqlquery = make_cqlquery(cl)))
706 // Initialize relevance structure with query terms
708 extract_terms(se->nmem, cn, p);
709 se->relevance = relevance_create(
710 se->service->relevance_pct,
711 se->nmem, (const char **) p,
712 se->expected_maxrecs);
719 void client_set_session(struct client *cl, struct session *se)
722 cl->next = se->clients;
726 int client_is_active(struct client *cl)
728 if (cl->connection && (cl->state == Client_Connecting ||
729 cl->state == Client_Working))
734 struct client *client_next_in_session(struct client *cl)
742 int client_get_hits(struct client *cl)
747 int client_get_num_records(struct client *cl)
749 return cl->record_offset;
752 int client_get_diagnostic(struct client *cl)
754 return cl->diagnostic;
757 void client_set_database(struct client *cl, struct session_database *db)
762 struct host *client_get_host(struct client *cl)
764 return client_get_database(cl)->database->host;
767 const char *client_get_url(struct client *cl)
769 return client_get_database(cl)->database->url;
775 * c-file-style: "Stroustrup"
776 * indent-tabs-mode: nil
778 * vim: shiftwidth=4 tabstop=8 expandtab