1 /* This file is part of Pazpar2.
2 Copyright (C) 2006-2008 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
38 #include <sys/socket.h>
47 #include <yaz/marcdisp.h>
48 #include <yaz/comstack.h>
49 #include <yaz/tcpip.h>
50 #include <yaz/proto.h>
51 #include <yaz/readconf.h>
52 #include <yaz/pquery.h>
53 #include <yaz/otherinfo.h>
54 #include <yaz/yaz-util.h>
56 #include <yaz/query-charset.h>
57 #include <yaz/querytowrbuf.h>
58 #include <yaz/oid_db.h>
59 #include <yaz/diagbib1.h>
60 #include <yaz/snprintf.h>
61 #include <yaz/rpn2cql.h>
65 #include <yaz/timing.h>
69 #include <netinet/in.h>
75 #include "connection.h"
78 /** \brief Represents client state for a connection to one search target */
80 struct session_database *database;
81 struct connection *connection;
82 struct session *session;
83 char *pquery; // Current search
84 char *cqlquery; // used for SRU targets only
88 int requestid; // ID of current outstanding request
90 enum client_state state;
91 struct show_raw *show_raw;
92 struct client *next; // next client in session or next in free list
96 int active; // whether this request has been sent to the server
101 void (*error_handler)(void *data, const char *addinfo);
102 void (*record_handler)(void *data, const char *buf, size_t sz);
104 struct show_raw *next;
107 static const char *client_states[] = {
111 "Client_Initializing",
116 "Client_Disconnected",
121 static struct client *client_freelist = 0;
123 const char *client_get_state_str(struct client *cl)
125 return client_states[cl->state];
128 enum client_state client_get_state(struct client *cl)
133 void client_set_state(struct client *cl, enum client_state st)
138 int no_active = session_active_clients(cl->session);
140 session_alert_watch(cl->session, SESSION_WATCH_SHOW);
144 static void client_show_raw_error(struct client *cl, const char *addinfo);
146 // Close connection and set state to error
147 void client_fatal(struct client *cl)
149 //client_show_raw_error(cl, "client connection failure");
150 yaz_log(YLOG_WARN, "Fatal error from %s", client_get_url(cl));
151 connection_destroy(cl->connection);
152 client_set_state(cl, Client_Error);
155 struct connection *client_get_connection(struct client *cl)
157 return cl->connection;
160 struct session_database *client_get_database(struct client *cl)
165 struct session *client_get_session(struct client *cl)
170 const char *client_get_pquery(struct client *cl)
175 void client_set_requestid(struct client *cl, int id)
181 static void client_send_raw_present(struct client *cl);
183 int client_show_raw_begin(struct client *cl, int position,
184 const char *syntax, const char *esn,
186 void (*error_handler)(void *data, const char *addinfo),
187 void (*record_handler)(void *data, const char *buf,
192 struct show_raw *rr, **rrp;
194 { /* the client has no connection */
197 rr = xmalloc(sizeof(*rr));
199 rr->position = position;
202 rr->error_handler = error_handler;
203 rr->record_handler = record_handler;
206 rr->syntax = xstrdup(syntax);
210 rr->esn = xstrdup(esn);
215 for (rrp = &cl->show_raw; *rrp; rrp = &(*rrp)->next)
219 if (cl->state == Client_Failed)
221 client_show_raw_error(cl, "client failed");
223 else if (cl->state == Client_Disconnected)
225 client_show_raw_error(cl, "client disconnected");
229 client_send_raw_present(cl);
234 void client_show_raw_remove(struct client *cl, void *data)
236 struct show_raw *rr = data;
237 struct show_raw **rrp = &cl->show_raw;
247 void client_show_raw_dequeue(struct client *cl)
249 struct show_raw *rr = cl->show_raw;
251 cl->show_raw = rr->next;
255 static void client_show_raw_error(struct client *cl, const char *addinfo)
259 cl->show_raw->error_handler(cl->show_raw->data, addinfo);
260 client_show_raw_dequeue(cl);
264 static void client_send_raw_present(struct client *cl)
266 struct session_database *sdb = client_get_database(cl);
267 struct connection *co = client_get_connection(cl);
268 ZOOM_resultset set = connection_get_resultset(co);
270 int offset = cl->show_raw->position;
271 const char *syntax = 0;
272 const char *elements = 0;
274 assert(cl->show_raw);
277 yaz_log(YLOG_DEBUG, "%s: trying to present %d record(s) from %d",
278 client_get_url(cl), 1, offset);
280 if (cl->show_raw->syntax)
281 syntax = cl->show_raw->syntax;
283 syntax = session_setting_oneval(sdb, PZ_REQUESTSYNTAX);
284 ZOOM_resultset_option_set(set, "preferredRecordSyntax", syntax);
286 if (cl->show_raw->esn)
287 elements = cl->show_raw->esn;
289 elements = session_setting_oneval(sdb, PZ_ELEMENTS);
290 if (elements && *elements)
291 ZOOM_resultset_option_set(set, "elementSetName", elements);
293 ZOOM_resultset_records(set, 0, offset-1, 1);
294 cl->show_raw->active = 1;
296 connection_continue(co);
299 static int nativesyntax_to_type(struct session_database *sdb, char *type)
301 const char *s = session_setting_oneval(sdb, PZ_NATIVESYNTAX);
305 if (!strncmp(s, "iso2709", 7))
307 const char *cp = strchr(s, ';');
308 yaz_snprintf(type, 80, "xml; charset=%s", cp ? cp+1 : "marc-8s");
310 else if (!strncmp(s, "xml", 3))
321 static void ingest_raw_record(struct client *cl, ZOOM_record rec)
327 if (cl->show_raw->binary)
331 struct session_database *sdb = client_get_database(cl);
332 nativesyntax_to_type(sdb, type);
335 buf = ZOOM_record_get(rec, type, &len);
336 cl->show_raw->record_handler(cl->show_raw->data, buf, len);
337 client_show_raw_dequeue(cl);
340 void client_search_response(struct client *cl)
342 struct connection *co = cl->connection;
343 struct session *se = cl->session;
344 ZOOM_connection link = connection_get_link(co);
345 ZOOM_resultset resultset = connection_get_resultset(co);
346 const char *error, *addinfo;
348 if (ZOOM_connection_error(link, &error, &addinfo))
351 cl->state = Client_Error;
352 yaz_log(YLOG_WARN, "Search error %s (%s): %s",
353 error, addinfo, client_get_url(cl));
357 cl->hits = ZOOM_resultset_size(resultset);
358 se->total_hits += cl->hits;
363 void client_record_response(struct client *cl)
365 struct connection *co = cl->connection;
366 ZOOM_connection link = connection_get_link(co);
367 ZOOM_resultset resultset = connection_get_resultset(co);
368 const char *error, *addinfo;
370 yaz_log(YLOG_LOG, "client_record_response");
371 if (ZOOM_connection_error(link, &error, &addinfo))
373 cl->state = Client_Error;
374 yaz_log(YLOG_WARN, "Search error %s (%s): %s",
375 error, addinfo, client_get_url(cl));
380 const char *msg, *addinfo;
382 if (cl->show_raw && cl->show_raw->active)
384 if ((rec = ZOOM_resultset_record(resultset,
385 cl->show_raw->position-1)))
387 cl->show_raw->active = 0;
388 ingest_raw_record(cl, rec);
393 int offset = cl->records;
394 if ((rec = ZOOM_resultset_record(resultset, offset)))
396 yaz_log(YLOG_LOG, "Record with offset %d", offset);
399 if (ZOOM_record_error(rec, &msg, &addinfo, 0))
400 yaz_log(YLOG_WARN, "Record error %s (%s): %s (rec #%d)",
401 error, addinfo, client_get_url(cl), cl->records);
404 struct session_database *sdb = client_get_database(cl);
407 nativesyntax_to_type(sdb, type);
408 if ((xmlrec = ZOOM_record_get(rec, type, NULL)))
410 if (ingest_record(cl, xmlrec, cl->records))
412 session_alert_watch(cl->session, SESSION_WATCH_SHOW);
413 session_alert_watch(cl->session, SESSION_WATCH_RECORD);
416 yaz_log(YLOG_WARN, "Failed to ingest");
419 yaz_log(YLOG_WARN, "Failed to extract ZOOM record");
425 yaz_log(YLOG_WARN, "Expected record, but got NULL");
429 void client_start_search(struct client *cl)
431 struct session_database *sdb = client_get_database(cl);
432 struct connection *co = client_get_connection(cl);
433 ZOOM_connection link = connection_get_link(co);
435 char *databaseName = sdb->database->databases[0];
436 const char *opt_piggyback = session_setting_oneval(sdb, PZ_PIGGYBACK);
437 const char *opt_queryenc = session_setting_oneval(sdb, PZ_QUERYENCODING);
438 const char *opt_elements = session_setting_oneval(sdb, PZ_ELEMENTS);
439 const char *opt_requestsyn = session_setting_oneval(sdb, PZ_REQUESTSYNTAX);
440 const char *opt_maxrecs = session_setting_oneval(sdb, PZ_MAXRECS);
441 const char *opt_sru = session_setting_oneval(sdb, PZ_SRU);
450 ZOOM_connection_option_set(link, "piggyback", opt_piggyback);
452 ZOOM_connection_option_set(link, "piggyback", "1");
454 ZOOM_connection_option_set(link, "rpnCharset", opt_queryenc);
455 if (*opt_sru && *opt_elements)
456 ZOOM_connection_option_set(link, "schema", opt_elements);
457 else if (*opt_elements)
458 ZOOM_connection_option_set(link, "elementSetName", opt_elements);
460 ZOOM_connection_option_set(link, "preferredRecordSyntax", opt_requestsyn);
462 ZOOM_connection_option_set(link, "count", opt_maxrecs);
466 sprintf(n, "%d", global_parameters.toget);
467 ZOOM_connection_option_set(link, "count", n);
470 ZOOM_connection_option_set(link, "databaseName", databaseName);
472 ZOOM_connection_option_set(link, "presentChunk", "20");
476 ZOOM_query q = ZOOM_query_create();
477 ZOOM_query_cql(q, cl->cqlquery);
478 rs = ZOOM_connection_search(link, q);
481 rs = ZOOM_connection_search_pqf(link, cl->pquery);
482 connection_set_resultset(co, rs);
483 connection_continue(co);
486 struct client *client_create(void)
492 client_freelist = client_freelist->next;
495 r = xmalloc(sizeof(struct client));
506 r->state = Client_Disconnected;
512 void client_destroy(struct client *c)
514 struct session *se = c->session;
515 if (c == se->clients)
516 se->clients = c->next;
520 for (cc = se->clients; cc && cc->next != c; cc = cc->next)
529 connection_release(c->connection);
530 c->next = client_freelist;
534 void client_set_connection(struct client *cl, struct connection *con)
536 cl->connection = con;
539 void client_disconnect(struct client *cl)
541 if (cl->state != Client_Idle)
542 client_set_state(cl, Client_Disconnected);
543 client_set_connection(cl, 0);
546 // Extract terms from query into null-terminated termlist
547 static void extract_terms(NMEM nmem, struct ccl_rpn_node *query, char **termlist)
551 pull_terms(nmem, query, termlist, &num);
555 // Initialize CCL map for a target
556 static CCL_bibset prepare_cclmap(struct client *cl)
558 struct session_database *sdb = client_get_database(cl);
565 for (s = sdb->settings[PZ_CCLMAP]; s; s = s->next)
567 char *p = strchr(s->name + 3, ':');
570 yaz_log(YLOG_WARN, "Malformed cclmap name: %s", s->name);
575 ccl_qual_fitem(res, s->value, p);
580 // returns a xmalloced CQL query corresponding to the pquery in client
581 static char *make_cqlquery(struct client *cl)
583 cql_transform_t cqlt = cql_transform_create();
586 WRBUF wrb = wrbuf_alloc();
589 zquery = p_query_rpn(global_parameters.odr_out, cl->pquery);
590 if ((status = cql_transform_rpn2cql_wrbuf(cqlt, wrb, zquery)))
592 yaz_log(YLOG_WARN, "failed to generate CQL query, code=%d", status);
595 r = xstrdup(wrbuf_cstr(wrb));
598 odr_reset(global_parameters.odr_out); // releases the zquery
599 cql_transform_close(cqlt);
603 // Parse the query given the settings specific to this client
604 int client_parse_query(struct client *cl, const char *query)
606 struct session *se = client_get_session(cl);
607 struct session_database *sdb = client_get_database(cl);
608 struct ccl_rpn_node *cn;
610 CCL_bibset ccl_map = prepare_cclmap(cl);
611 const char *sru = session_setting_oneval(sdb, PZ_SRU);
616 cn = ccl_find_str(ccl_map, query, &cerror, &cpos);
617 ccl_qual_rm(&ccl_map);
620 cl->state = Client_Error;
621 yaz_log(YLOG_WARN, "Failed to parse query for %s",
622 client_get_database(cl)->database->url);
625 wrbuf_rewind(se->wrbuf);
626 ccl_pquery(se->wrbuf, cn);
628 cl->pquery = xstrdup(wrbuf_cstr(se->wrbuf));
633 if (!(cl->cqlquery = make_cqlquery(cl)))
641 // Initialize relevance structure with query terms
643 extract_terms(se->nmem, cn, p);
644 se->relevance = relevance_create(
645 global_parameters.server->relevance_pct,
646 se->nmem, (const char **) p,
647 se->expected_maxrecs);
654 void client_set_session(struct client *cl, struct session *se)
657 cl->next = se->clients;
661 int client_is_active(struct client *cl)
663 if (cl->connection && (cl->state == Client_Continue ||
664 cl->state == Client_Connecting ||
665 cl->state == Client_Working))
670 struct client *client_next_in_session(struct client *cl)
678 int client_get_hits(struct client *cl)
683 int client_get_num_records(struct client *cl)
688 int client_get_diagnostic(struct client *cl)
690 return cl->diagnostic;
693 void client_set_database(struct client *cl, struct session_database *db)
698 struct host *client_get_host(struct client *cl)
700 return client_get_database(cl)->database->host;
703 const char *client_get_url(struct client *cl)
705 return client_get_database(cl)->database->url;
711 * indent-tabs-mode: nil
713 * vim: shiftwidth=4 tabstop=8 expandtab