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>
63 #include <yaz/timing.h>
67 #include <netinet/in.h>
73 #include "connection.h"
76 /** \brief Represents client state for a connection to one search target */
78 struct session_database *database;
79 struct connection *connection;
80 struct session *session;
81 char *pquery; // Current search
85 int requestid; // ID of current outstanding request
87 enum client_state state;
88 struct show_raw *show_raw;
89 struct client *next; // next client in session or next in free list
93 int active; // whether this request has been sent to the server
98 void (*error_handler)(void *data, const char *addinfo);
99 void (*record_handler)(void *data, const char *buf, size_t sz);
101 struct show_raw *next;
104 static const char *client_states[] = {
108 "Client_Initializing",
113 "Client_Disconnected",
118 static struct client *client_freelist = 0;
120 const char *client_get_state_str(struct client *cl)
122 return client_states[cl->state];
125 enum client_state client_get_state(struct client *cl)
130 void client_set_state(struct client *cl, enum client_state st)
135 int no_active = session_active_clients(cl->session);
137 session_alert_watch(cl->session, SESSION_WATCH_SHOW);
141 static void client_show_raw_error(struct client *cl, const char *addinfo);
143 // Close connection and set state to error
144 void client_fatal(struct client *cl)
146 //client_show_raw_error(cl, "client connection failure");
147 yaz_log(YLOG_WARN, "Fatal error from %s", client_get_url(cl));
148 connection_destroy(cl->connection);
149 client_set_state(cl, Client_Error);
152 struct connection *client_get_connection(struct client *cl)
154 return cl->connection;
157 struct session_database *client_get_database(struct client *cl)
162 struct session *client_get_session(struct client *cl)
167 const char *client_get_pquery(struct client *cl)
172 void client_set_requestid(struct client *cl, int id)
178 int client_show_raw_begin(struct client *cl, int position,
179 const char *syntax, const char *esn,
181 void (*error_handler)(void *data, const char *addinfo),
182 void (*record_handler)(void *data, const char *buf,
187 struct show_raw *rr, **rrp;
189 { /* the client has no connection */
192 rr = xmalloc(sizeof(*rr));
194 rr->position = position;
197 rr->error_handler = error_handler;
198 rr->record_handler = record_handler;
201 rr->syntax = xstrdup(syntax);
205 rr->esn = xstrdup(esn);
210 for (rrp = &cl->show_raw; *rrp; rrp = &(*rrp)->next)
214 if (cl->state == Client_Failed)
216 client_show_raw_error(cl, "client failed");
218 else if (cl->state == Client_Disconnected)
220 client_show_raw_error(cl, "client disconnected");
225 void client_show_raw_remove(struct client *cl, void *data)
227 struct show_raw *rr = data;
228 struct show_raw **rrp = &cl->show_raw;
238 void client_show_raw_dequeue(struct client *cl)
240 struct show_raw *rr = cl->show_raw;
242 cl->show_raw = rr->next;
246 static void client_show_raw_error(struct client *cl, const char *addinfo)
250 cl->show_raw->error_handler(cl->show_raw->data, addinfo);
251 client_show_raw_dequeue(cl);
255 static void client_show_raw_cancel(struct client *cl)
259 cl->show_raw->error_handler(cl->show_raw->data, "cancel");
260 client_show_raw_dequeue(cl);
264 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 ZOOM_resultset_option_set(set, "elementSetName", elements);
292 ZOOM_resultset_records(set, 0, offset, 1);
293 cl->show_raw->active = 1;
298 static void ingest_raw_records(struct client *cl, Z_Records *r)
300 Z_NamePlusRecordList *rlist;
301 Z_NamePlusRecord *npr;
305 if (r->which != Z_Records_DBOSD)
307 client_show_raw_error(cl, "non-surrogate diagnostics");
311 rlist = r->u.databaseOrSurDiagnostics;
312 if (rlist->num_records != 1 || !rlist->records || !rlist->records[0])
314 client_show_raw_error(cl, "no records");
317 npr = rlist->records[0];
318 if (npr->which != Z_NamePlusRecord_databaseRecord)
320 client_show_raw_error(cl, "surrogate diagnostic");
324 if (cl->show_raw && cl->show_raw->binary)
326 Z_External *rec = npr->u.databaseRecord;
327 if (rec->which == Z_External_octet)
329 cl->show_raw->record_handler(cl->show_raw->data,
331 rec->u.octet_aligned->buf,
332 rec->u.octet_aligned->len);
333 client_show_raw_dequeue(cl);
336 client_show_raw_error(cl, "no records");
339 doc = record_to_xml(client_get_database(cl), npr->u.databaseRecord);
342 client_show_raw_error(cl, "unable to convert record to xml");
346 xmlDocDumpMemory(doc, &buf_out, &len_out);
351 cl->show_raw->record_handler(cl->show_raw->data,
352 (const char *) buf_out, len_out);
353 client_show_raw_dequeue(cl);
358 #endif // RETIRED show raw
360 void client_search_response(struct client *cl)
362 struct connection *co = cl->connection;
363 struct session *se = cl->session;
364 ZOOM_connection link = connection_get_link(co);
365 ZOOM_resultset resultset = connection_get_resultset(co);
366 const char *error, *addinfo;
368 if (ZOOM_connection_error(link, &error, &addinfo))
371 cl->state = Client_Error;
372 yaz_log(YLOG_WARN, "Search error %s (%s): %s",
373 error, addinfo, client_get_url(cl));
377 cl->hits = ZOOM_resultset_size(resultset);
378 se->total_hits += cl->hits;
382 void client_record_response(struct client *cl)
384 struct connection *co = cl->connection;
385 ZOOM_connection link = connection_get_link(co);
386 ZOOM_resultset resultset = connection_get_resultset(co);
387 const char *error, *addinfo;
389 if (ZOOM_connection_error(link, &error, &addinfo))
391 cl->state = Client_Error;
392 yaz_log(YLOG_WARN, "Search error %s (%s): %s",
393 error, addinfo, client_get_url(cl));
398 int offset = cl->records;
399 const char *msg, *addinfo;
401 if ((rec = ZOOM_resultset_record(resultset, offset)))
403 yaz_log(YLOG_LOG, "Record with offset %d", offset);
405 if (ZOOM_record_error(rec, &msg, &addinfo, 0))
406 yaz_log(YLOG_WARN, "Record error %s (%s): %s (rec #%d)",
407 error, addinfo, client_get_url(cl), cl->records);
410 struct session_database *sdb = client_get_database(cl);
412 char type[128] = "xml";
413 const char *nativesyntax =
414 session_setting_oneval(sdb, PZ_NATIVESYNTAX);
417 if (*nativesyntax && (cset = strchr(nativesyntax, ';')))
418 sprintf(type, "xml; charset=%s", cset + 1);
420 if ((xmlrec = ZOOM_record_get(rec, type, NULL)))
422 if (ingest_record(cl, xmlrec, cl->records))
424 session_alert_watch(cl->session, SESSION_WATCH_SHOW);
425 session_alert_watch(cl->session, SESSION_WATCH_RECORD);
428 yaz_log(YLOG_WARN, "Failed to ingest");
431 yaz_log(YLOG_WARN, "Failed to extract ZOOM record");
436 yaz_log(YLOG_WARN, "Expected record, but got NULL");
442 void client_present_response(struct client *cl, Z_APDU *a)
444 Z_PresentResponse *r = a->u.presentResponse;
445 Z_Records *recs = r->records;
447 if (recs && recs->which == Z_Records_NSD)
449 WRBUF w = wrbuf_alloc();
451 Z_DiagRec dr, *dr_p = &dr;
452 dr.which = Z_DiagRec_defaultFormat;
453 dr.u.defaultFormat = recs->u.nonSurrogateDiagnostic;
455 wrbuf_printf(w, "Present response NSD %s: ",
456 cl->database->database->url);
458 cl->diagnostic = diag_to_wrbuf(&dr_p, 1, w);
460 yaz_log(YLOG_WARN, "%s", wrbuf_cstr(w));
462 cl->state = Client_Error;
465 client_show_raw_error(cl, "non surrogate diagnostics");
467 else if (recs && recs->which == Z_Records_multipleNSD)
469 WRBUF w = wrbuf_alloc();
471 wrbuf_printf(w, "Present response multipleNSD %s: ",
472 cl->database->database->url);
474 diag_to_wrbuf(recs->u.multipleNonSurDiagnostics->diagRecs,
475 recs->u.multipleNonSurDiagnostics->num_diagRecs,
477 yaz_log(YLOG_WARN, "%s", wrbuf_cstr(w));
478 cl->state = Client_Error;
481 else if (recs && !*r->presentStatus && cl->state != Client_Error)
483 yaz_log(YLOG_DEBUG, "Good Present response %s",
484 cl->database->database->url);
486 // we can mix show raw and normal show ..
487 if (cl->show_raw && cl->show_raw->active)
489 cl->show_raw->active = 0; // no longer active
490 ingest_raw_records(cl, recs);
493 ingest_records(cl, recs);
494 cl->state = Client_Continue;
496 else if (*r->presentStatus)
498 yaz_log(YLOG_WARN, "Bad Present response %s",
499 cl->database->database->url);
500 cl->state = Client_Error;
501 client_show_raw_error(cl, "bad present response");
505 void client_close_response(struct client *cl, Z_APDU *a)
507 struct connection *co = cl->connection;
508 /* Z_Close *r = a->u.close; */
510 yaz_log(YLOG_WARN, "Close response %s", cl->database->database->url);
512 cl->state = Client_Failed;
513 connection_destroy(co);
516 #endif // RETIRED show raw
519 int client_is_our_response(struct client *cl)
521 struct session *se = client_get_session(cl);
523 if (cl && (cl->requestid == se->requestid ||
524 cl->state == Client_Initializing))
530 void client_start_search(struct client *cl)
532 struct session_database *sdb = client_get_database(cl);
533 struct connection *co = client_get_connection(cl);
534 ZOOM_connection link = connection_get_link(co);
536 char *databaseName = sdb->database->databases[0];
537 const char *opt_piggyback = session_setting_oneval(sdb, PZ_PIGGYBACK);
538 const char *opt_queryenc = session_setting_oneval(sdb, PZ_QUERYENCODING);
539 const char *opt_elements = session_setting_oneval(sdb, PZ_ELEMENTS);
540 const char *opt_requestsyn = session_setting_oneval(sdb, PZ_REQUESTSYNTAX);
541 const char *opt_maxrecs = session_setting_oneval(sdb, PZ_MAXRECS);
550 ZOOM_connection_option_set(link, "piggyback", opt_piggyback);
552 ZOOM_connection_option_set(link, "piggyback", "1");
554 ZOOM_connection_option_set(link, "rpnCharset", opt_queryenc);
556 ZOOM_connection_option_set(link, "elementSetName", opt_elements);
558 ZOOM_connection_option_set(link, "preferredRecordSyntax", opt_requestsyn);
560 ZOOM_connection_option_set(link, "count", opt_maxrecs);
564 sprintf(n, "%d", global_parameters.toget);
565 ZOOM_connection_option_set(link, "count", n);
567 if (!databaseName || !*databaseName)
568 databaseName = "Default";
569 ZOOM_connection_option_set(link, "databaseName", databaseName);
571 ZOOM_connection_option_set(link, "presentChunk", "20");
573 rs = ZOOM_connection_search_pqf(link, cl->pquery);
574 connection_set_resultset(co, rs);
577 struct client *client_create(void)
583 client_freelist = client_freelist->next;
586 r = xmalloc(sizeof(struct client));
596 r->state = Client_Disconnected;
602 void client_destroy(struct client *c)
604 struct session *se = c->session;
605 if (c == se->clients)
606 se->clients = c->next;
610 for (cc = se->clients; cc && cc->next != c; cc = cc->next)
618 connection_release(c->connection);
619 c->next = client_freelist;
623 void client_set_connection(struct client *cl, struct connection *con)
625 cl->connection = con;
628 void client_disconnect(struct client *cl)
630 if (cl->state != Client_Idle)
631 client_set_state(cl, Client_Disconnected);
632 client_set_connection(cl, 0);
635 // Extract terms from query into null-terminated termlist
636 static void extract_terms(NMEM nmem, struct ccl_rpn_node *query, char **termlist)
640 pull_terms(nmem, query, termlist, &num);
644 // Initialize CCL map for a target
645 static CCL_bibset prepare_cclmap(struct client *cl)
647 struct session_database *sdb = client_get_database(cl);
654 for (s = sdb->settings[PZ_CCLMAP]; s; s = s->next)
656 char *p = strchr(s->name + 3, ':');
659 yaz_log(YLOG_WARN, "Malformed cclmap name: %s", s->name);
664 ccl_qual_fitem(res, s->value, p);
669 // Parse the query given the settings specific to this client
670 int client_parse_query(struct client *cl, const char *query)
672 struct session *se = client_get_session(cl);
673 struct ccl_rpn_node *cn;
675 CCL_bibset ccl_map = prepare_cclmap(cl);
680 cn = ccl_find_str(ccl_map, query, &cerror, &cpos);
681 ccl_qual_rm(&ccl_map);
684 cl->state = Client_Error;
685 yaz_log(YLOG_WARN, "Failed to parse query for %s",
686 client_get_database(cl)->database->url);
689 wrbuf_rewind(se->wrbuf);
690 ccl_pquery(se->wrbuf, cn);
692 cl->pquery = xstrdup(wrbuf_cstr(se->wrbuf));
696 // Initialize relevance structure with query terms
698 extract_terms(se->nmem, cn, p);
699 se->relevance = relevance_create(
700 global_parameters.server->relevance_pct,
701 se->nmem, (const char **) p,
702 se->expected_maxrecs);
709 void client_set_session(struct client *cl, struct session *se)
712 cl->next = se->clients;
716 int client_is_active(struct client *cl)
718 if (cl->connection && (cl->state == Client_Continue ||
719 cl->state == Client_Connecting ||
720 cl->state == Client_Working))
725 struct client *client_next_in_session(struct client *cl)
733 int client_get_hits(struct client *cl)
738 int client_get_num_records(struct client *cl)
743 int client_get_diagnostic(struct client *cl)
745 return cl->diagnostic;
748 void client_set_database(struct client *cl, struct session_database *db)
753 struct host *client_get_host(struct client *cl)
755 return client_get_database(cl)->database->host;
758 const char *client_get_url(struct client *cl)
760 return client_get_database(cl)->database->url;
766 * indent-tabs-mode: nil
768 * vim: shiftwidth=4 tabstop=8 expandtab