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_send_raw_present(struct client *cl)
264 struct session_database *sdb = client_get_database(cl);
265 struct connection *co = client_get_connection(cl);
266 ZOOM_resultset set = connection_get_resultset(co);
268 int offset = cl->show_raw->position;
269 const char *syntax = 0;
270 const char *elements = 0;
272 assert(cl->show_raw);
275 yaz_log(YLOG_DEBUG, "%s: trying to present %d record(s) from %d",
276 client_get_url(cl), 1, offset);
278 if (cl->show_raw->syntax)
279 syntax = cl->show_raw->syntax;
281 syntax = session_setting_oneval(sdb, PZ_REQUESTSYNTAX);
282 ZOOM_resultset_option_set(set, "preferredRecordSyntax", syntax);
284 if (cl->show_raw->esn)
285 elements = cl->show_raw->esn;
287 elements = session_setting_oneval(sdb, PZ_ELEMENTS);
288 if (elements && *elements)
289 ZOOM_resultset_option_set(set, "elementSetName", elements);
291 ZOOM_resultset_records(set, 0, offset-1, 1);
292 cl->show_raw->active = 1;
294 connection_continue(co);
297 static int nativesyntax_to_type(struct session_database *sdb, char *type)
299 const char *s = session_setting_oneval(sdb, PZ_NATIVESYNTAX);
303 if (!strncmp(s, "iso2709", 7))
305 const char *cp = strchr(s, ';');
306 yaz_snprintf(type, 80, "xml; charset=%s", cp ? cp+1 : "marc-8s");
308 else if (!strncmp(s, "xml", 3))
314 yaz_log(YLOG_LOG, "Returned type %s", type);
320 static void ingest_raw_record(struct client *cl, ZOOM_record rec)
326 if (cl->show_raw->binary)
330 struct session_database *sdb = client_get_database(cl);
331 nativesyntax_to_type(sdb, type);
334 buf = ZOOM_record_get(rec, type, &len);
335 cl->show_raw->record_handler(cl->show_raw->data, buf, len);
336 client_show_raw_dequeue(cl);
339 void client_search_response(struct client *cl)
341 struct connection *co = cl->connection;
342 struct session *se = cl->session;
343 ZOOM_connection link = connection_get_link(co);
344 ZOOM_resultset resultset = connection_get_resultset(co);
345 const char *error, *addinfo;
347 if (ZOOM_connection_error(link, &error, &addinfo))
350 cl->state = Client_Error;
351 yaz_log(YLOG_WARN, "Search error %s (%s): %s",
352 error, addinfo, client_get_url(cl));
356 cl->hits = ZOOM_resultset_size(resultset);
357 se->total_hits += cl->hits;
362 void client_record_response(struct client *cl)
364 struct connection *co = cl->connection;
365 ZOOM_connection link = connection_get_link(co);
366 ZOOM_resultset resultset = connection_get_resultset(co);
367 const char *error, *addinfo;
369 yaz_log(YLOG_LOG, "client_record_response");
370 if (ZOOM_connection_error(link, &error, &addinfo))
372 cl->state = Client_Error;
373 yaz_log(YLOG_WARN, "Search error %s (%s): %s",
374 error, addinfo, client_get_url(cl));
379 const char *msg, *addinfo;
381 yaz_log(YLOG_LOG, "show_raw=%p show_raw->active=%d",
382 cl->show_raw, cl->show_raw ? cl->show_raw->active : 0);
383 if (cl->show_raw && cl->show_raw->active)
385 if ((rec = ZOOM_resultset_record(resultset,
386 cl->show_raw->position-1)))
388 cl->show_raw->active = 0;
389 ingest_raw_record(cl, rec);
394 int offset = cl->records;
395 if ((rec = ZOOM_resultset_record(resultset, offset)))
397 yaz_log(YLOG_LOG, "Record with offset %d", offset);
400 if (ZOOM_record_error(rec, &msg, &addinfo, 0))
401 yaz_log(YLOG_WARN, "Record error %s (%s): %s (rec #%d)",
402 error, addinfo, client_get_url(cl), cl->records);
405 struct session_database *sdb = client_get_database(cl);
408 nativesyntax_to_type(sdb, type);
409 if ((xmlrec = ZOOM_record_get(rec, type, NULL)))
411 if (ingest_record(cl, xmlrec, cl->records))
413 session_alert_watch(cl->session, SESSION_WATCH_SHOW);
414 session_alert_watch(cl->session, SESSION_WATCH_RECORD);
417 yaz_log(YLOG_WARN, "Failed to ingest");
420 yaz_log(YLOG_WARN, "Failed to extract ZOOM record");
426 yaz_log(YLOG_WARN, "Expected record, but got NULL");
430 void client_start_search(struct client *cl)
432 struct session_database *sdb = client_get_database(cl);
433 struct connection *co = client_get_connection(cl);
434 ZOOM_connection link = connection_get_link(co);
436 char *databaseName = sdb->database->databases[0];
437 const char *opt_piggyback = session_setting_oneval(sdb, PZ_PIGGYBACK);
438 const char *opt_queryenc = session_setting_oneval(sdb, PZ_QUERYENCODING);
439 const char *opt_elements = session_setting_oneval(sdb, PZ_ELEMENTS);
440 const char *opt_requestsyn = session_setting_oneval(sdb, PZ_REQUESTSYNTAX);
441 const char *opt_maxrecs = session_setting_oneval(sdb, PZ_MAXRECS);
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);
456 ZOOM_connection_option_set(link, "elementSetName", opt_elements);
458 ZOOM_connection_option_set(link, "preferredRecordSyntax", opt_requestsyn);
460 ZOOM_connection_option_set(link, "count", opt_maxrecs);
464 sprintf(n, "%d", global_parameters.toget);
465 ZOOM_connection_option_set(link, "count", n);
467 if (!databaseName || !*databaseName)
468 databaseName = "Default";
469 ZOOM_connection_option_set(link, "databaseName", databaseName);
471 ZOOM_connection_option_set(link, "presentChunk", "20");
473 rs = ZOOM_connection_search_pqf(link, cl->pquery);
474 connection_set_resultset(co, rs);
475 connection_continue(co);
478 struct client *client_create(void)
484 client_freelist = client_freelist->next;
487 r = xmalloc(sizeof(struct client));
497 r->state = Client_Disconnected;
503 void client_destroy(struct client *c)
505 struct session *se = c->session;
506 if (c == se->clients)
507 se->clients = c->next;
511 for (cc = se->clients; cc && cc->next != c; cc = cc->next)
519 connection_release(c->connection);
520 c->next = client_freelist;
524 void client_set_connection(struct client *cl, struct connection *con)
526 cl->connection = con;
529 void client_disconnect(struct client *cl)
531 if (cl->state != Client_Idle)
532 client_set_state(cl, Client_Disconnected);
533 client_set_connection(cl, 0);
536 // Extract terms from query into null-terminated termlist
537 static void extract_terms(NMEM nmem, struct ccl_rpn_node *query, char **termlist)
541 pull_terms(nmem, query, termlist, &num);
545 // Initialize CCL map for a target
546 static CCL_bibset prepare_cclmap(struct client *cl)
548 struct session_database *sdb = client_get_database(cl);
555 for (s = sdb->settings[PZ_CCLMAP]; s; s = s->next)
557 char *p = strchr(s->name + 3, ':');
560 yaz_log(YLOG_WARN, "Malformed cclmap name: %s", s->name);
565 ccl_qual_fitem(res, s->value, p);
570 // Parse the query given the settings specific to this client
571 int client_parse_query(struct client *cl, const char *query)
573 struct session *se = client_get_session(cl);
574 struct ccl_rpn_node *cn;
576 CCL_bibset ccl_map = prepare_cclmap(cl);
581 cn = ccl_find_str(ccl_map, query, &cerror, &cpos);
582 ccl_qual_rm(&ccl_map);
585 cl->state = Client_Error;
586 yaz_log(YLOG_WARN, "Failed to parse query for %s",
587 client_get_database(cl)->database->url);
590 wrbuf_rewind(se->wrbuf);
591 ccl_pquery(se->wrbuf, cn);
593 cl->pquery = xstrdup(wrbuf_cstr(se->wrbuf));
597 // Initialize relevance structure with query terms
599 extract_terms(se->nmem, cn, p);
600 se->relevance = relevance_create(
601 global_parameters.server->relevance_pct,
602 se->nmem, (const char **) p,
603 se->expected_maxrecs);
610 void client_set_session(struct client *cl, struct session *se)
613 cl->next = se->clients;
617 int client_is_active(struct client *cl)
619 if (cl->connection && (cl->state == Client_Continue ||
620 cl->state == Client_Connecting ||
621 cl->state == Client_Working))
626 struct client *client_next_in_session(struct client *cl)
634 int client_get_hits(struct client *cl)
639 int client_get_num_records(struct client *cl)
644 int client_get_diagnostic(struct client *cl)
646 return cl->diagnostic;
649 void client_set_database(struct client *cl, struct session_database *db)
654 struct host *client_get_host(struct client *cl)
656 return client_get_database(cl)->database->host;
659 const char *client_get_url(struct client *cl)
661 return client_get_database(cl)->database->url;
667 * indent-tabs-mode: nil
669 * vim: shiftwidth=4 tabstop=8 expandtab