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>
64 #include <yaz/timing.h>
68 #include <netinet/in.h>
74 #include "connection.h"
77 /** \brief Represents client state for a connection to one search target */
79 struct session_database *database;
80 struct connection *connection;
81 struct session *session;
82 char *pquery; // Current search
86 int requestid; // ID of current outstanding request
88 enum client_state state;
89 struct show_raw *show_raw;
90 struct client *next; // next client in session or next in free list
94 int active; // whether this request has been sent to the server
99 void (*error_handler)(void *data, const char *addinfo);
100 void (*record_handler)(void *data, const char *buf, size_t sz);
102 struct show_raw *next;
105 static const char *client_states[] = {
109 "Client_Initializing",
114 "Client_Disconnected",
119 static struct client *client_freelist = 0;
121 const char *client_get_state_str(struct client *cl)
123 return client_states[cl->state];
126 enum client_state client_get_state(struct client *cl)
131 void client_set_state(struct client *cl, enum client_state st)
136 int no_active = session_active_clients(cl->session);
138 session_alert_watch(cl->session, SESSION_WATCH_SHOW);
142 static void client_show_raw_error(struct client *cl, const char *addinfo);
144 // Close connection and set state to error
145 void client_fatal(struct client *cl)
147 //client_show_raw_error(cl, "client connection failure");
148 yaz_log(YLOG_WARN, "Fatal error from %s", client_get_url(cl));
149 connection_destroy(cl->connection);
150 client_set_state(cl, Client_Error);
153 struct connection *client_get_connection(struct client *cl)
155 return cl->connection;
158 struct session_database *client_get_database(struct client *cl)
163 struct session *client_get_session(struct client *cl)
168 const char *client_get_pquery(struct client *cl)
173 void client_set_requestid(struct client *cl, int id)
179 static void client_send_raw_present(struct client *cl);
181 int client_show_raw_begin(struct client *cl, int position,
182 const char *syntax, const char *esn,
184 void (*error_handler)(void *data, const char *addinfo),
185 void (*record_handler)(void *data, const char *buf,
190 struct show_raw *rr, **rrp;
192 { /* the client has no connection */
195 rr = xmalloc(sizeof(*rr));
197 rr->position = position;
200 rr->error_handler = error_handler;
201 rr->record_handler = record_handler;
204 rr->syntax = xstrdup(syntax);
208 rr->esn = xstrdup(esn);
213 for (rrp = &cl->show_raw; *rrp; rrp = &(*rrp)->next)
217 if (cl->state == Client_Failed)
219 client_show_raw_error(cl, "client failed");
221 else if (cl->state == Client_Disconnected)
223 client_show_raw_error(cl, "client disconnected");
227 client_send_raw_present(cl);
232 void client_show_raw_remove(struct client *cl, void *data)
234 struct show_raw *rr = data;
235 struct show_raw **rrp = &cl->show_raw;
245 void client_show_raw_dequeue(struct client *cl)
247 struct show_raw *rr = cl->show_raw;
249 cl->show_raw = rr->next;
253 static void client_show_raw_error(struct client *cl, const char *addinfo)
257 cl->show_raw->error_handler(cl->show_raw->data, addinfo);
258 client_show_raw_dequeue(cl);
262 static void client_show_raw_cancel(struct client *cl)
266 cl->show_raw->error_handler(cl->show_raw->data, "cancel");
267 client_show_raw_dequeue(cl);
271 static void client_send_raw_present(struct client *cl)
273 struct session_database *sdb = client_get_database(cl);
274 struct connection *co = client_get_connection(cl);
275 ZOOM_resultset set = connection_get_resultset(co);
277 int offset = cl->show_raw->position;
278 const char *syntax = 0;
279 const char *elements = 0;
281 assert(cl->show_raw);
284 yaz_log(YLOG_DEBUG, "%s: trying to present %d record(s) from %d",
285 client_get_url(cl), 1, offset);
287 if (cl->show_raw->syntax)
288 syntax = cl->show_raw->syntax;
290 syntax = session_setting_oneval(sdb, PZ_REQUESTSYNTAX);
291 ZOOM_resultset_option_set(set, "preferredRecordSyntax", syntax);
293 if (cl->show_raw->esn)
294 elements = cl->show_raw->esn;
296 elements = session_setting_oneval(sdb, PZ_ELEMENTS);
297 if (elements && *elements)
298 ZOOM_resultset_option_set(set, "elementSetName", elements);
300 ZOOM_resultset_records(set, 0, offset-1, 1);
301 cl->show_raw->active = 1;
303 connection_continue(co);
306 static void ingest_raw_record(struct client *cl, ZOOM_record rec)
312 if (cl->show_raw->binary)
316 struct session_database *sdb = client_get_database(cl);
319 const char *nativesyntax = session_setting_oneval(sdb, PZ_NATIVESYNTAX);
320 if (*nativesyntax && (cset = strchr(nativesyntax, ';')))
321 yaz_snprintf(type, sizeof(type)-1, "xml; charset=%s", cset);
326 buf = ZOOM_record_get(rec, type, &len);
327 cl->show_raw->record_handler(cl->show_raw->data, buf, len);
328 client_show_raw_dequeue(cl);
333 static void ingest_raw_records(struct client *cl, Z_Records *r)
335 Z_NamePlusRecordList *rlist;
336 Z_NamePlusRecord *npr;
340 if (r->which != Z_Records_DBOSD)
342 client_show_raw_error(cl, "non-surrogate diagnostics");
346 rlist = r->u.databaseOrSurDiagnostics;
347 if (rlist->num_records != 1 || !rlist->records || !rlist->records[0])
349 client_show_raw_error(cl, "no records");
352 npr = rlist->records[0];
353 if (npr->which != Z_NamePlusRecord_databaseRecord)
355 client_show_raw_error(cl, "surrogate diagnostic");
359 if (cl->show_raw && cl->show_raw->binary)
361 Z_External *rec = npr->u.databaseRecord;
362 if (rec->which == Z_External_octet)
364 cl->show_raw->record_handler(cl->show_raw->data,
366 rec->u.octet_aligned->buf,
367 rec->u.octet_aligned->len);
368 client_show_raw_dequeue(cl);
371 client_show_raw_error(cl, "no records");
374 doc = record_to_xml(client_get_database(cl), npr->u.databaseRecord);
377 client_show_raw_error(cl, "unable to convert record to xml");
381 xmlDocDumpMemory(doc, &buf_out, &len_out);
386 cl->show_raw->record_handler(cl->show_raw->data,
387 (const char *) buf_out, len_out);
388 client_show_raw_dequeue(cl);
393 #endif // RETIRED show raw
395 void client_search_response(struct client *cl)
397 struct connection *co = cl->connection;
398 struct session *se = cl->session;
399 ZOOM_connection link = connection_get_link(co);
400 ZOOM_resultset resultset = connection_get_resultset(co);
401 const char *error, *addinfo;
403 if (ZOOM_connection_error(link, &error, &addinfo))
406 cl->state = Client_Error;
407 yaz_log(YLOG_WARN, "Search error %s (%s): %s",
408 error, addinfo, client_get_url(cl));
412 cl->hits = ZOOM_resultset_size(resultset);
413 se->total_hits += cl->hits;
417 void client_record_response(struct client *cl)
419 struct connection *co = cl->connection;
420 ZOOM_connection link = connection_get_link(co);
421 ZOOM_resultset resultset = connection_get_resultset(co);
422 const char *error, *addinfo;
424 yaz_log(YLOG_LOG, "client_record_response");
425 if (ZOOM_connection_error(link, &error, &addinfo))
427 cl->state = Client_Error;
428 yaz_log(YLOG_WARN, "Search error %s (%s): %s",
429 error, addinfo, client_get_url(cl));
434 int offset = cl->records;
435 const char *msg, *addinfo;
437 if ((rec = ZOOM_resultset_record(resultset, offset)))
439 yaz_log(YLOG_LOG, "Record with offset %d", offset);
441 yaz_log(YLOG_LOG, "show_raw=%p show_raw->active=%d",
442 cl->show_raw, cl->show_raw ? cl->show_raw->active : 0);
443 if (cl->show_raw && cl->show_raw->active)
445 cl->show_raw->active = 0;
446 ingest_raw_record(cl, rec);
451 if (ZOOM_record_error(rec, &msg, &addinfo, 0))
452 yaz_log(YLOG_WARN, "Record error %s (%s): %s (rec #%d)",
453 error, addinfo, client_get_url(cl), cl->records);
456 struct session_database *sdb = client_get_database(cl);
458 char type[128] = "xml";
459 const char *nativesyntax =
460 session_setting_oneval(sdb, PZ_NATIVESYNTAX);
463 if (*nativesyntax && (cset = strchr(nativesyntax, ';')))
464 sprintf(type, "xml; charset=%s", cset + 1);
466 if ((xmlrec = ZOOM_record_get(rec, type, NULL)))
468 if (ingest_record(cl, xmlrec, cl->records))
470 session_alert_watch(cl->session, SESSION_WATCH_SHOW);
471 session_alert_watch(cl->session, SESSION_WATCH_RECORD);
474 yaz_log(YLOG_WARN, "Failed to ingest");
477 yaz_log(YLOG_WARN, "Failed to extract ZOOM record");
483 yaz_log(YLOG_WARN, "Expected record, but got NULL");
489 void client_present_response(struct client *cl, Z_APDU *a)
491 Z_PresentResponse *r = a->u.presentResponse;
492 Z_Records *recs = r->records;
494 if (recs && recs->which == Z_Records_NSD)
496 WRBUF w = wrbuf_alloc();
498 Z_DiagRec dr, *dr_p = &dr;
499 dr.which = Z_DiagRec_defaultFormat;
500 dr.u.defaultFormat = recs->u.nonSurrogateDiagnostic;
502 wrbuf_printf(w, "Present response NSD %s: ",
503 cl->database->database->url);
505 cl->diagnostic = diag_to_wrbuf(&dr_p, 1, w);
507 yaz_log(YLOG_WARN, "%s", wrbuf_cstr(w));
509 cl->state = Client_Error;
512 client_show_raw_error(cl, "non surrogate diagnostics");
514 else if (recs && recs->which == Z_Records_multipleNSD)
516 WRBUF w = wrbuf_alloc();
518 wrbuf_printf(w, "Present response multipleNSD %s: ",
519 cl->database->database->url);
521 diag_to_wrbuf(recs->u.multipleNonSurDiagnostics->diagRecs,
522 recs->u.multipleNonSurDiagnostics->num_diagRecs,
524 yaz_log(YLOG_WARN, "%s", wrbuf_cstr(w));
525 cl->state = Client_Error;
528 else if (recs && !*r->presentStatus && cl->state != Client_Error)
530 yaz_log(YLOG_DEBUG, "Good Present response %s",
531 cl->database->database->url);
533 // we can mix show raw and normal show ..
534 if (cl->show_raw && cl->show_raw->active)
536 cl->show_raw->active = 0; // no longer active
537 ingest_raw_records(cl, recs);
540 ingest_records(cl, recs);
541 cl->state = Client_Continue;
543 else if (*r->presentStatus)
545 yaz_log(YLOG_WARN, "Bad Present response %s",
546 cl->database->database->url);
547 cl->state = Client_Error;
548 client_show_raw_error(cl, "bad present response");
552 void client_close_response(struct client *cl, Z_APDU *a)
554 struct connection *co = cl->connection;
555 /* Z_Close *r = a->u.close; */
557 yaz_log(YLOG_WARN, "Close response %s", cl->database->database->url);
559 cl->state = Client_Failed;
560 connection_destroy(co);
563 #endif // RETIRED show raw
566 int client_is_our_response(struct client *cl)
568 struct session *se = client_get_session(cl);
570 if (cl && (cl->requestid == se->requestid ||
571 cl->state == Client_Initializing))
577 void client_start_search(struct client *cl)
579 struct session_database *sdb = client_get_database(cl);
580 struct connection *co = client_get_connection(cl);
581 ZOOM_connection link = connection_get_link(co);
583 char *databaseName = sdb->database->databases[0];
584 const char *opt_piggyback = session_setting_oneval(sdb, PZ_PIGGYBACK);
585 const char *opt_queryenc = session_setting_oneval(sdb, PZ_QUERYENCODING);
586 const char *opt_elements = session_setting_oneval(sdb, PZ_ELEMENTS);
587 const char *opt_requestsyn = session_setting_oneval(sdb, PZ_REQUESTSYNTAX);
588 const char *opt_maxrecs = session_setting_oneval(sdb, PZ_MAXRECS);
597 ZOOM_connection_option_set(link, "piggyback", opt_piggyback);
599 ZOOM_connection_option_set(link, "piggyback", "1");
601 ZOOM_connection_option_set(link, "rpnCharset", opt_queryenc);
603 ZOOM_connection_option_set(link, "elementSetName", opt_elements);
605 ZOOM_connection_option_set(link, "preferredRecordSyntax", opt_requestsyn);
607 ZOOM_connection_option_set(link, "count", opt_maxrecs);
611 sprintf(n, "%d", global_parameters.toget);
612 ZOOM_connection_option_set(link, "count", n);
614 if (!databaseName || !*databaseName)
615 databaseName = "Default";
616 ZOOM_connection_option_set(link, "databaseName", databaseName);
618 ZOOM_connection_option_set(link, "presentChunk", "20");
620 rs = ZOOM_connection_search_pqf(link, cl->pquery);
621 connection_set_resultset(co, rs);
622 connection_continue(co);
625 struct client *client_create(void)
631 client_freelist = client_freelist->next;
634 r = xmalloc(sizeof(struct client));
644 r->state = Client_Disconnected;
650 void client_destroy(struct client *c)
652 struct session *se = c->session;
653 if (c == se->clients)
654 se->clients = c->next;
658 for (cc = se->clients; cc && cc->next != c; cc = cc->next)
666 connection_release(c->connection);
667 c->next = client_freelist;
671 void client_set_connection(struct client *cl, struct connection *con)
673 cl->connection = con;
676 void client_disconnect(struct client *cl)
678 if (cl->state != Client_Idle)
679 client_set_state(cl, Client_Disconnected);
680 client_set_connection(cl, 0);
683 // Extract terms from query into null-terminated termlist
684 static void extract_terms(NMEM nmem, struct ccl_rpn_node *query, char **termlist)
688 pull_terms(nmem, query, termlist, &num);
692 // Initialize CCL map for a target
693 static CCL_bibset prepare_cclmap(struct client *cl)
695 struct session_database *sdb = client_get_database(cl);
702 for (s = sdb->settings[PZ_CCLMAP]; s; s = s->next)
704 char *p = strchr(s->name + 3, ':');
707 yaz_log(YLOG_WARN, "Malformed cclmap name: %s", s->name);
712 ccl_qual_fitem(res, s->value, p);
717 // Parse the query given the settings specific to this client
718 int client_parse_query(struct client *cl, const char *query)
720 struct session *se = client_get_session(cl);
721 struct ccl_rpn_node *cn;
723 CCL_bibset ccl_map = prepare_cclmap(cl);
728 cn = ccl_find_str(ccl_map, query, &cerror, &cpos);
729 ccl_qual_rm(&ccl_map);
732 cl->state = Client_Error;
733 yaz_log(YLOG_WARN, "Failed to parse query for %s",
734 client_get_database(cl)->database->url);
737 wrbuf_rewind(se->wrbuf);
738 ccl_pquery(se->wrbuf, cn);
740 cl->pquery = xstrdup(wrbuf_cstr(se->wrbuf));
744 // Initialize relevance structure with query terms
746 extract_terms(se->nmem, cn, p);
747 se->relevance = relevance_create(
748 global_parameters.server->relevance_pct,
749 se->nmem, (const char **) p,
750 se->expected_maxrecs);
757 void client_set_session(struct client *cl, struct session *se)
760 cl->next = se->clients;
764 int client_is_active(struct client *cl)
766 if (cl->connection && (cl->state == Client_Continue ||
767 cl->state == Client_Connecting ||
768 cl->state == Client_Working))
773 struct client *client_next_in_session(struct client *cl)
781 int client_get_hits(struct client *cl)
786 int client_get_num_records(struct client *cl)
791 int client_get_diagnostic(struct client *cl)
793 return cl->diagnostic;
796 void client_set_database(struct client *cl, struct session_database *db)
801 struct host *client_get_host(struct client *cl)
803 return client_get_database(cl)->database->host;
806 const char *client_get_url(struct client *cl)
808 return client_get_database(cl)->database->url;
814 * indent-tabs-mode: nil
816 * vim: shiftwidth=4 tabstop=8 expandtab