1 /* $Id: client.c,v 1.8 2007-06-06 11:56:35 marc Exp $
2 Copyright (c) 2006-2007, Index Data.
4 This file is part of Pazpar2.
6 Pazpar2 is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
11 Pazpar2 is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with Pazpar2; see the file LICENSE. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
31 #include <sys/socket.h>
37 #include <yaz/marcdisp.h>
38 #include <yaz/comstack.h>
39 #include <yaz/tcpip.h>
40 #include <yaz/proto.h>
41 #include <yaz/readconf.h>
42 #include <yaz/pquery.h>
43 #include <yaz/otherinfo.h>
44 #include <yaz/yaz-util.h>
46 #include <yaz/query-charset.h>
47 #include <yaz/querytowrbuf.h>
48 #if YAZ_VERSIONL >= 0x020163
49 #include <yaz/oid_db.h>
58 #include <yaz/timing.h>
61 #include <netinet/in.h>
66 #include "connection.h"
69 /** \brief Represents client state for a connection to one search target */
71 struct session_database *database;
72 struct connection *connection;
73 struct session *session;
74 char *pquery; // Current search
78 int requestid; // ID of current outstanding request
80 enum client_state state;
81 struct client *next; // next client in session or next in free list
84 static const char *client_states[] = {
88 "Client_Initializing",
93 "Client_Disconnected",
97 static struct client *client_freelist = 0;
99 static int send_apdu(struct client *c, Z_APDU *a)
101 return connection_send_apdu(client_get_connection(c), a);
105 const char *client_get_state_str(struct client *cl)
107 return client_states[cl->state];
110 enum client_state client_get_state(struct client *cl)
115 void client_set_state(struct client *cl, enum client_state st)
120 // Close connection and set state to error
121 void client_fatal(struct client *cl)
123 yaz_log(YLOG_WARN, "Fatal error from %s", client_get_url(cl));
124 connection_destroy(cl->connection);
125 cl->state = Client_Error;
128 struct connection *client_get_connection(struct client *cl)
130 return cl->connection;
133 struct session_database *client_get_database(struct client *cl)
138 struct session *client_get_session(struct client *cl)
143 const char *client_get_pquery(struct client *cl)
148 void client_set_requestid(struct client *cl, int id)
153 void client_send_present(struct client *cl)
155 struct session_database *sdb = client_get_database(cl);
156 Z_APDU *a = zget_APDU(global_parameters.odr_out, Z_APDU_presentRequest);
158 int start = cl->records + 1;
161 toget = global_parameters.chunk;
162 if (toget > global_parameters.toget - cl->records)
163 toget = global_parameters.toget - cl->records;
164 if (toget > cl->hits - cl->records)
165 toget = cl->hits - cl->records;
167 yaz_log(YLOG_DEBUG, "Trying to present %d records\n", toget);
169 a->u.presentRequest->resultSetStartPoint = &start;
170 a->u.presentRequest->numberOfRecordsRequested = &toget;
172 a->u.presentRequest->resultSetId = "Default";
174 if ((recsyn = session_setting_oneval(sdb, PZ_REQUESTSYNTAX)))
176 #if YAZ_VERSIONL >= 0x020163
177 a->u.presentRequest->preferredRecordSyntax =
178 yaz_string_to_oid_odr(yaz_oid_std(),
179 CLASS_RECSYN, recsyn,
180 global_parameters.odr_out);
182 a->u.presentRequest->preferredRecordSyntax =
183 yaz_str_to_z3950oid(global_parameters.odr_out,
184 CLASS_RECSYN, recsyn);
188 if (send_apdu(cl, a) >= 0)
189 cl->state = Client_Presenting;
191 cl->state = Client_Error;
192 odr_reset(global_parameters.odr_out);
196 void client_send_search(struct client *cl)
198 struct session *se = client_get_session(cl);
199 struct session_database *sdb = client_get_database(cl);
200 Z_APDU *a = zget_APDU(global_parameters.odr_out, Z_APDU_searchRequest);
204 int ssub = 0, lslb = 100000, mspn = 10;
208 yaz_iconv_t iconv = 0;
210 yaz_log(YLOG_DEBUG, "Sending search to %s", sdb->database->url);
212 // constructing RPN query
213 a->u.searchRequest->query = zquery = odr_malloc(global_parameters.odr_out,
215 zquery->which = Z_Query_type_1;
216 zquery->u.type_1 = p_query_rpn(global_parameters.odr_out,
217 client_get_pquery(cl));
219 // converting to target encoding
220 if ((queryenc = session_setting_oneval(sdb, PZ_QUERYENCODING))){
221 iconv = yaz_iconv_open(queryenc, "UTF-8");
223 yaz_query_charset_convert_rpnquery(zquery->u.type_1,
224 global_parameters.odr_out,
226 yaz_iconv_close(iconv);
228 yaz_log(YLOG_WARN, "Query encoding failed %s %s",
229 client_get_database(cl)->database->url, queryenc);
232 for (ndb = 0; sdb->database->databases[ndb]; ndb++)
234 databaselist = odr_malloc(global_parameters.odr_out, sizeof(char*) * ndb);
235 for (ndb = 0; sdb->database->databases[ndb]; ndb++)
236 databaselist[ndb] = sdb->database->databases[ndb];
238 if (!(piggyback = session_setting_oneval(sdb, PZ_PIGGYBACK))
239 || *piggyback == '1')
241 if ((recsyn = session_setting_oneval(sdb, PZ_REQUESTSYNTAX)))
243 #if YAZ_VERSIONL >= 0x020163
244 a->u.searchRequest->preferredRecordSyntax =
245 yaz_string_to_oid_odr(yaz_oid_std(),
246 CLASS_RECSYN, recsyn,
247 global_parameters.odr_out);
249 a->u.searchRequest->preferredRecordSyntax =
250 yaz_str_to_z3950oid(global_parameters.odr_out,
251 CLASS_RECSYN, recsyn);
254 a->u.searchRequest->smallSetUpperBound = &ssub;
255 a->u.searchRequest->largeSetLowerBound = &lslb;
256 a->u.searchRequest->mediumSetPresentNumber = &mspn;
258 a->u.searchRequest->resultSetName = "Default";
259 a->u.searchRequest->databaseNames = databaselist;
260 a->u.searchRequest->num_databaseNames = ndb;
263 { //scope for sending and logging queries
264 WRBUF wbquery = wrbuf_alloc();
265 yaz_query_to_wrbuf(wbquery, a->u.searchRequest->query);
268 if (send_apdu(cl, a) >= 0)
270 client_set_state(cl, Client_Searching);
271 client_set_requestid(cl, se->requestid);
272 yaz_log(YLOG_LOG, "SearchRequest %s %s %s",
273 client_get_database(cl)->database->url,
274 queryenc ? queryenc : "UTF-8",
275 wrbuf_cstr(wbquery));
278 client_set_state(cl, Client_Error);
279 yaz_log(YLOG_WARN, "Failed SearchRequest %s %s %s",
280 client_get_database(cl)->database->url,
281 queryenc ? queryenc : "UTF-8",
282 wrbuf_cstr(wbquery));
285 wrbuf_destroy(wbquery);
288 odr_reset(global_parameters.odr_out);
291 void client_init_response(struct client *cl, Z_APDU *a)
293 Z_InitResponse *r = a->u.initResponse;
295 yaz_log(YLOG_DEBUG, "Init response %s", cl->database->database->url);
299 cl->state = Client_Idle;
302 cl->state = Client_Failed; // FIXME need to do something to the connection
306 static void ingest_records(struct client *cl, Z_Records *r)
309 yaz_timing_t t = yaz_timing_create();
312 struct session *s = client_get_session(cl);
313 Z_NamePlusRecordList *rlist;
316 if (r->which != Z_Records_DBOSD)
318 rlist = r->u.databaseOrSurDiagnostics;
319 for (i = 0; i < rlist->num_records; i++)
321 Z_NamePlusRecord *npr = rlist->records[i];
324 if (npr->which != Z_NamePlusRecord_databaseRecord)
327 "Unexpected record type, probably diagnostic %s",
328 cl->database->database->url);
332 rec = ingest_record(cl, npr->u.databaseRecord, cl->records);
336 if (rlist->num_records)
337 session_alert_watch(s, SESSION_WATCH_RECORDS);
341 yaz_log(YLOG_LOG, "ingest_records %6.5f %3.2f %3.2f",
342 yaz_timing_get_real(t), yaz_timing_get_user(t),
343 yaz_timing_get_sys(t));
344 yaz_timing_destroy(&t);
349 void client_search_response(struct client *cl, Z_APDU *a)
351 struct session *se = cl->session;
352 Z_SearchResponse *r = a->u.searchResponse;
354 yaz_log(YLOG_DEBUG, "Search response %s (status=%d)",
355 cl->database->database->url, *r->searchStatus);
357 if (*r->searchStatus)
359 cl->hits = *r->resultCount;
360 se->total_hits += cl->hits;
361 if (r->presentStatus && !*r->presentStatus && r->records)
363 yaz_log(YLOG_DEBUG, "Records in search response %s",
364 cl->database->database->url);
365 ingest_records(cl, r->records);
367 cl->state = Client_Idle;
372 cl->state = Client_Error;
374 Z_Records *recs = r->records;
375 if (recs->which == Z_Records_NSD)
378 "Search response: Non-surrogate diagnostic %s (%d)",
379 cl->database->database->url,
380 *recs->u.nonSurrogateDiagnostic->condition);
381 cl->diagnostic = *recs->u.nonSurrogateDiagnostic->condition;
382 cl->state = Client_Error;
388 void client_present_response(struct client *cl, Z_APDU *a)
390 Z_PresentResponse *r = a->u.presentResponse;
393 Z_Records *recs = r->records;
394 if (recs->which == Z_Records_NSD)
396 yaz_log(YLOG_WARN, "Non-surrogate diagnostic %s",
397 cl->database->database->url);
398 cl->diagnostic = *recs->u.nonSurrogateDiagnostic->condition;
399 cl->state = Client_Error;
403 if (!*r->presentStatus && cl->state != Client_Error)
405 yaz_log(YLOG_DEBUG, "Good Present response %s",
406 cl->database->database->url);
407 ingest_records(cl, r->records);
408 cl->state = Client_Idle;
410 else if (*r->presentStatus)
412 yaz_log(YLOG_WARN, "Bad Present response %s",
413 cl->database->database->url);
414 cl->state = Client_Error;
418 void client_close_response(struct client *cl, Z_APDU *a)
420 struct connection *co = cl->connection;
421 /* Z_Close *r = a->u.close; */
423 yaz_log(YLOG_WARN, "Close response %s", cl->database->database->url);
425 cl->state = Client_Failed;
426 connection_destroy(co);
429 int client_is_our_response(struct client *cl)
431 struct session *se = client_get_session(cl);
433 if (cl && (cl->requestid == se->requestid ||
434 cl->state == Client_Initializing))
439 // Set authentication token in init if one is set for the client
440 // TODO: Extend this to handle other schemes than open (should be simple)
441 static void init_authentication(struct client *cl, Z_InitRequest *req)
443 struct session_database *sdb = client_get_database(cl);
444 char *auth = session_setting_oneval(sdb, PZ_AUTHENTICATION);
448 struct connection *co = client_get_connection(cl);
449 struct session *se = client_get_session(cl);
450 Z_IdAuthentication *idAuth = odr_malloc(global_parameters.odr_out,
452 idAuth->which = Z_IdAuthentication_open;
453 idAuth->u.open = auth;
454 req->idAuthentication = idAuth;
455 connection_set_authentication(co, nmem_strdup(se->session_nmem, auth));
459 static void init_zproxy(struct client *cl, Z_InitRequest *req)
461 struct session_database *sdb = client_get_database(cl);
462 char *ztarget = sdb->database->url;
463 //char *ztarget = sdb->url;
464 char *zproxy = session_setting_oneval(sdb, PZ_ZPROXY);
467 #if YAZ_VERSIONL >= 0x020163
468 yaz_oi_set_string_oid(&req->otherInfo,
469 global_parameters.odr_out,
470 yaz_oid_userinfo_proxy,
473 yaz_oi_set_string_oidval(&req->otherInfo,
474 global_parameters.odr_out, VAL_PROXY,
480 static void client_init_request(struct client *cl)
482 Z_APDU *a = zget_APDU(global_parameters.odr_out, Z_APDU_initRequest);
484 a->u.initRequest->implementationId = global_parameters.implementationId;
485 a->u.initRequest->implementationName = global_parameters.implementationName;
486 a->u.initRequest->implementationVersion =
487 global_parameters.implementationVersion;
488 ODR_MASK_SET(a->u.initRequest->options, Z_Options_search);
489 ODR_MASK_SET(a->u.initRequest->options, Z_Options_present);
490 ODR_MASK_SET(a->u.initRequest->options, Z_Options_namedResultSets);
492 ODR_MASK_SET(a->u.initRequest->protocolVersion, Z_ProtocolVersion_1);
493 ODR_MASK_SET(a->u.initRequest->protocolVersion, Z_ProtocolVersion_2);
494 ODR_MASK_SET(a->u.initRequest->protocolVersion, Z_ProtocolVersion_3);
496 init_authentication(cl, a->u.initRequest);
497 init_zproxy(cl, a->u.initRequest);
501 if (send_apdu(cl, a) >= 0)
502 client_set_state(cl, Client_Initializing);
504 client_set_state(cl, Client_Error);
505 odr_reset(global_parameters.odr_out);
508 void client_continue(struct client *cl)
510 if (cl->state == Client_Connected) {
511 client_init_request(cl);
514 if (cl->state == Client_Idle)
516 struct session *se = client_get_session(cl);
517 if (cl->requestid != se->requestid && cl->pquery) {
518 client_send_search(cl);
520 else if (cl->hits > 0 && cl->records < global_parameters.toget &&
521 cl->records < cl->hits) {
522 client_send_present(cl);
527 struct client *client_create(void)
533 client_freelist = client_freelist->next;
536 r = xmalloc(sizeof(struct client));
546 r->state = Client_Disconnected;
551 void client_destroy(struct client *c)
553 struct session *se = c->session;
554 if (c == se->clients)
555 se->clients = c->next;
559 for (cc = se->clients; cc && cc->next != c; cc = cc->next)
565 connection_release(c->connection);
566 c->next = client_freelist;
570 void client_set_connection(struct client *cl, struct connection *con)
572 cl->connection = con;
575 void client_disconnect(struct client *cl)
577 if (cl->state != Client_Idle)
578 cl->state = Client_Disconnected;
579 client_set_connection(cl, 0);
582 // Extract terms from query into null-terminated termlist
583 static void extract_terms(NMEM nmem, struct ccl_rpn_node *query, char **termlist)
587 pull_terms(nmem, query, termlist, &num);
591 // Initialize CCL map for a target
592 static CCL_bibset prepare_cclmap(struct client *cl)
594 struct session_database *sdb = client_get_database(cl);
601 for (s = sdb->settings[PZ_CCLMAP]; s; s = s->next)
603 char *p = strchr(s->name + 3, ':');
606 yaz_log(YLOG_WARN, "Malformed cclmap name: %s", s->name);
611 ccl_qual_fitem(res, s->value, p);
616 // Parse the query given the settings specific to this client
617 int client_parse_query(struct client *cl, const char *query)
619 struct session *se = client_get_session(cl);
620 struct ccl_rpn_node *cn;
622 CCL_bibset ccl_map = prepare_cclmap(cl);
626 cn = ccl_find_str(ccl_map, query, &cerror, &cpos);
627 ccl_qual_rm(&ccl_map);
630 cl->state = Client_Error;
631 yaz_log(YLOG_WARN, "Failed to parse query for %s",
632 client_get_database(cl)->database->url);
635 wrbuf_rewind(se->wrbuf);
636 ccl_pquery(se->wrbuf, cn);
638 cl->pquery = xstrdup(wrbuf_cstr(se->wrbuf));
642 // Initialize relevance structure with query terms
644 extract_terms(se->nmem, cn, p);
645 se->relevance = relevance_create(client_get_database(cl)->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_Connecting ||
664 cl->state == Client_Initializing ||
665 cl->state == Client_Searching ||
666 cl->state == Client_Presenting))
671 struct client *client_next_in_session(struct client *cl)
679 int client_get_hits(struct client *cl)
684 int client_get_num_records(struct client *cl)
689 int client_get_diagnostic(struct client *cl)
691 return cl->diagnostic;
694 void client_set_database(struct client *cl, struct session_database *db)
699 struct host *client_get_host(struct client *cl)
701 return client_get_database(cl)->database->host;
704 const char *client_get_url(struct client *cl)
706 return client_get_database(cl)->database->url;
712 * indent-tabs-mode: nil
714 * vim: shiftwidth=4 tabstop=8 expandtab