Pazpar2 logs more details about non-surrogate diagnostics. Deals with
[pazpar2-moved-to-github.git] / src / client.c
1 /* $Id: client.c,v 1.12 2007-06-19 12:25:29 adam Exp $
2    Copyright (c) 2006-2007, Index Data.
3
4 This file is part of Pazpar2.
5
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
9 version.
10
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
14 for more details.
15
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
19 02111-1307, USA.
20  */
21
22 /** \file client.c
23     \brief Z39.50 client 
24 */
25
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <sys/time.h>
30 #include <unistd.h>
31 #include <sys/socket.h>
32 #include <netdb.h>
33 #include <signal.h>
34 #include <ctype.h>
35 #include <assert.h>
36
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>
45 #include <yaz/nmem.h>
46 #include <yaz/query-charset.h>
47 #include <yaz/querytowrbuf.h>
48 #include <yaz/oid_db.h>
49 #include <yaz/diagbib1.h>
50
51 #if HAVE_CONFIG_H
52 #include "cconfig.h"
53 #endif
54
55 #define USE_TIMING 0
56 #if USE_TIMING
57 #include <yaz/timing.h>
58 #endif
59
60 #include <netinet/in.h>
61
62 #include "pazpar2.h"
63
64 #include "client.h"
65 #include "connection.h"
66 #include "settings.h"
67
68 /** \brief Represents client state for a connection to one search target */
69 struct client {
70     struct session_database *database;
71     struct connection *connection;
72     struct session *session;
73     char *pquery; // Current search
74     int hits;
75     int records;
76     int setno;
77     int requestid;            // ID of current outstanding request
78     int diagnostic;
79     enum client_state state;
80     struct show_raw *show_raw;
81     struct client *next;     // next client in session or next in free list
82 };
83
84 struct show_raw {
85     int active; // whether this request has been sent to the server
86     int position;
87     char *syntax;
88     char *esn;
89     void (*error_handler)(void *data, const char *addinfo);
90     void (*record_handler)(void *data, const char *buf, size_t sz);
91     void *data;
92 };
93
94 static const char *client_states[] = {
95     "Client_Connecting",
96     "Client_Connected",
97     "Client_Idle",
98     "Client_Initializing",
99     "Client_Searching",
100     "Client_Presenting",
101     "Client_Error",
102     "Client_Failed",
103     "Client_Disconnected",
104     "Client_Stopped"
105 };
106
107 static struct client *client_freelist = 0;
108
109 static int send_apdu(struct client *c, Z_APDU *a)
110 {
111     return connection_send_apdu(client_get_connection(c), a);
112 }
113
114
115 const char *client_get_state_str(struct client *cl)
116 {
117     return client_states[cl->state];
118 }
119
120 enum client_state client_get_state(struct client *cl)
121 {
122     return cl->state;
123 }
124
125 void client_set_state(struct client *cl, enum client_state st)
126 {
127     cl->state = st;
128 }
129
130 static void client_show_raw_error(struct client *cl, const char *addinfo);
131
132 // Close connection and set state to error
133 void client_fatal(struct client *cl)
134 {
135     client_show_raw_error(cl, "client connection failure");
136     yaz_log(YLOG_WARN, "Fatal error from %s", client_get_url(cl));
137     connection_destroy(cl->connection);
138     cl->state = Client_Error;
139 }
140
141
142 static int diag_to_wrbuf(Z_DiagRec **pp, int num, WRBUF w)
143 {
144     int code = 0;
145     int i;
146     for (i = 0; i<num; i++)
147     {
148         Z_DiagRec *p = pp[i];
149         if (i)
150             wrbuf_puts(w, "; ");
151         if (p->which != Z_DiagRec_defaultFormat)
152         {
153             wrbuf_puts(w, "? Not in default format");
154         }
155         else
156         {
157             Z_DefaultDiagFormat *r = p->u.defaultFormat;
158             
159             if (!r->diagnosticSetId)
160                 wrbuf_puts(w, "? Missing diagset");
161             else
162             {
163                 oid_class oclass;
164                 char diag_name_buf[OID_STR_MAX];
165                 const char *diag_name = 0;
166                 diag_name = yaz_oid_to_string_buf
167                     (r->diagnosticSetId, &oclass, diag_name_buf);
168                 wrbuf_puts(w, diag_name);
169             }
170             if (!code)
171                 code = *r->condition;
172             wrbuf_printf(w, " %d %s", *r->condition,
173                          diagbib1_str(*r->condition));
174             switch (r->which)
175             {
176             case Z_DefaultDiagFormat_v2Addinfo:
177                 wrbuf_printf(w, " -- v2 addinfo '%s'", r->u.v2Addinfo);
178                 break;
179             case Z_DefaultDiagFormat_v3Addinfo:
180                 wrbuf_printf(w, " -- v3 addinfo '%s'", r->u.v3Addinfo);
181                 break;
182             }
183         }
184     }
185     return code;
186 }
187
188
189
190 struct connection *client_get_connection(struct client *cl)
191 {
192     return cl->connection;
193 }
194
195 struct session_database *client_get_database(struct client *cl)
196 {
197     return cl->database;
198 }
199
200 struct session *client_get_session(struct client *cl)
201 {
202     return cl->session;
203 }
204
205 const char *client_get_pquery(struct client *cl)
206 {
207     return cl->pquery;
208 }
209
210 void client_set_requestid(struct client *cl, int id)
211 {
212     cl->requestid = id;
213 }
214
215 int client_show_raw_begin(struct client *cl, int position,
216                           const char *syntax, const char *esn,
217                           void *data,
218                           void (*error_handler)(void *data, const char *addinfo),
219                           void (*record_handler)(void *data, const char *buf,
220                                                  size_t sz))
221 {
222     if (cl->show_raw)
223         return -1;
224     cl->show_raw = xmalloc(sizeof(*cl->show_raw));
225     cl->show_raw->position = position;
226     cl->show_raw->active = 0;
227     cl->show_raw->data = data;
228     cl->show_raw->error_handler = error_handler;
229     cl->show_raw->record_handler = record_handler;
230     if (syntax)
231         cl->show_raw->syntax = xstrdup(syntax);
232     else
233         cl->show_raw->syntax = 0;
234     if (esn)
235         cl->show_raw->esn = xstrdup(esn);
236     else
237         cl->show_raw->esn = 0;
238     client_continue(cl);
239     return 0;
240 }
241
242 void client_show_raw_reset(struct client *cl)
243 {
244     xfree(cl->show_raw);
245     cl->show_raw = 0;
246 }
247
248 static void client_show_raw_error(struct client *cl, const char *addinfo)
249 {
250     if (cl->show_raw)
251     {
252         cl->show_raw->error_handler(cl->show_raw->data, addinfo);
253         client_show_raw_reset(cl);
254     }
255 }
256
257 static void client_show_raw_cancel(struct client *cl)
258 {
259     if (cl->show_raw)
260     {
261         cl->show_raw->error_handler(cl->show_raw->data, "cancel");
262         client_show_raw_reset(cl);
263     }
264 }
265
266 void client_send_raw_present(struct client *cl)
267 {
268     Z_APDU *a = zget_APDU(global_parameters.odr_out, Z_APDU_presentRequest);
269     int toget = 1;
270     int start = cl->show_raw->position;
271
272     assert(cl->show_raw);
273
274     yaz_log(YLOG_DEBUG, "Trying to present %d record(s) from %d",
275             toget, start);
276
277     a->u.presentRequest->resultSetStartPoint = &start;
278     a->u.presentRequest->numberOfRecordsRequested = &toget;
279
280     if (cl->show_raw->syntax)  // syntax is optional
281         a->u.presentRequest->preferredRecordSyntax =
282             yaz_string_to_oid_odr(yaz_oid_std(),
283                                   CLASS_RECSYN, cl->show_raw->syntax,
284                                   global_parameters.odr_out);
285     if (cl->show_raw->esn)  // element set is optional
286     {
287         Z_ElementSetNames *elementSetNames =
288             odr_malloc(global_parameters.odr_out, sizeof(*elementSetNames));
289         Z_RecordComposition *compo = 
290             odr_malloc(global_parameters.odr_out, sizeof(*compo));
291         a->u.presentRequest->recordComposition = compo;
292
293         compo->which = Z_RecordComp_simple;
294         compo->u.simple = elementSetNames;
295
296         elementSetNames->which = Z_ElementSetNames_generic;
297         elementSetNames->u.generic = 
298             odr_strdup(global_parameters.odr_out, cl->show_raw->esn);
299     }
300     if (send_apdu(cl, a) >= 0)
301     {
302         cl->show_raw->active = 1;
303         cl->state = Client_Presenting;
304     }
305     else
306     {
307         client_show_raw_error(cl, "send_apdu failed");
308         cl->state = Client_Error;
309     }
310     odr_reset(global_parameters.odr_out);
311 }
312
313 void client_send_present(struct client *cl)
314 {
315     struct session_database *sdb = client_get_database(cl);
316     Z_APDU *a = zget_APDU(global_parameters.odr_out, Z_APDU_presentRequest);
317     int toget;
318     int start = cl->records + 1;
319     char *recsyn;
320
321     toget = global_parameters.chunk;
322     if (toget > global_parameters.toget - cl->records)
323         toget = global_parameters.toget - cl->records;
324     if (toget > cl->hits - cl->records)
325         toget = cl->hits - cl->records;
326
327     yaz_log(YLOG_DEBUG, "Trying to present %d record(s) from %d",
328             toget, start);
329
330     a->u.presentRequest->resultSetStartPoint = &start;
331     a->u.presentRequest->numberOfRecordsRequested = &toget;
332
333     if ((recsyn = session_setting_oneval(sdb, PZ_REQUESTSYNTAX)))
334     {
335         a->u.presentRequest->preferredRecordSyntax =
336             yaz_string_to_oid_odr(yaz_oid_std(),
337                                   CLASS_RECSYN, recsyn,
338                                   global_parameters.odr_out);
339     }
340
341     if (send_apdu(cl, a) >= 0)
342         cl->state = Client_Presenting;
343     else
344         cl->state = Client_Error;
345     odr_reset(global_parameters.odr_out);
346 }
347
348
349 void client_send_search(struct client *cl)
350 {
351     struct session *se = client_get_session(cl);
352     struct session_database *sdb = client_get_database(cl);
353     Z_APDU *a = zget_APDU(global_parameters.odr_out, Z_APDU_searchRequest);
354     int ndb;
355     char **databaselist;
356     Z_Query *zquery;
357     int ssub = 0, lslb = 100000, mspn = 10;
358     char *recsyn = 0;
359     char *piggyback = 0;
360     char *queryenc = 0;
361     yaz_iconv_t iconv = 0;
362
363     yaz_log(YLOG_DEBUG, "Sending search to %s", sdb->database->url);
364
365     
366     // constructing RPN query
367     a->u.searchRequest->query = zquery = odr_malloc(global_parameters.odr_out,
368                                                     sizeof(Z_Query));
369     zquery->which = Z_Query_type_1;
370     zquery->u.type_1 = p_query_rpn(global_parameters.odr_out, 
371                                    client_get_pquery(cl));
372
373     // converting to target encoding
374     if ((queryenc = session_setting_oneval(sdb, PZ_QUERYENCODING))){
375         iconv = yaz_iconv_open(queryenc, "UTF-8");
376         if (iconv){
377             yaz_query_charset_convert_rpnquery(zquery->u.type_1, 
378                                                global_parameters.odr_out, 
379                                                iconv);
380             yaz_iconv_close(iconv);
381         } else
382             yaz_log(YLOG_WARN, "Query encoding failed %s %s", 
383                     client_get_database(cl)->database->url, queryenc);
384     }
385
386     for (ndb = 0; sdb->database->databases[ndb]; ndb++)
387         ;
388     databaselist = odr_malloc(global_parameters.odr_out, sizeof(char*) * ndb);
389     for (ndb = 0; sdb->database->databases[ndb]; ndb++)
390         databaselist[ndb] = sdb->database->databases[ndb];
391
392     if (!(piggyback = session_setting_oneval(sdb, PZ_PIGGYBACK)) 
393         || *piggyback == '1')
394     {
395         if ((recsyn = session_setting_oneval(sdb, PZ_REQUESTSYNTAX)))
396         {
397             a->u.searchRequest->preferredRecordSyntax =
398                 yaz_string_to_oid_odr(yaz_oid_std(),
399                                       CLASS_RECSYN, recsyn,
400                                       global_parameters.odr_out);
401         }
402         a->u.searchRequest->smallSetUpperBound = &ssub;
403         a->u.searchRequest->largeSetLowerBound = &lslb;
404         a->u.searchRequest->mediumSetPresentNumber = &mspn;
405     }
406     a->u.searchRequest->databaseNames = databaselist;
407     a->u.searchRequest->num_databaseNames = ndb;
408
409     
410     {  //scope for sending and logging queries 
411         WRBUF wbquery = wrbuf_alloc();
412         yaz_query_to_wrbuf(wbquery, a->u.searchRequest->query);
413
414
415         if (send_apdu(cl, a) >= 0)
416         {
417             client_set_state(cl, Client_Searching);
418             client_set_requestid(cl, se->requestid);
419             yaz_log(YLOG_LOG, "SearchRequest %s %s %s", 
420                     client_get_database(cl)->database->url,
421                     queryenc ? queryenc : "UTF-8",
422                     wrbuf_cstr(wbquery));
423         }
424         else {
425             client_set_state(cl, Client_Error);
426             yaz_log(YLOG_WARN, "Failed SearchRequest %s  %s %s", 
427                     client_get_database(cl)->database->url, 
428                     queryenc ? queryenc : "UTF-8",
429                     wrbuf_cstr(wbquery));
430         }
431         
432         wrbuf_destroy(wbquery);
433     }    
434
435     odr_reset(global_parameters.odr_out);
436 }
437
438 void client_init_response(struct client *cl, Z_APDU *a)
439 {
440     Z_InitResponse *r = a->u.initResponse;
441
442     yaz_log(YLOG_DEBUG, "Init response %s", cl->database->database->url);
443
444     if (*r->result)
445     {
446         cl->state = Client_Idle;
447     }
448     else
449         cl->state = Client_Failed; // FIXME need to do something to the connection
450 }
451
452
453 static void ingest_raw_records(struct client *cl, Z_Records *r)
454 {
455     Z_NamePlusRecordList *rlist;
456     Z_NamePlusRecord *npr;
457     xmlDoc *doc;
458     xmlChar *buf_out;
459     int len_out;
460     if (r->which != Z_Records_DBOSD)
461     {
462         client_show_raw_error(cl, "non-surrogate diagnostics");
463         return;
464     }
465
466     rlist = r->u.databaseOrSurDiagnostics;
467     if (rlist->num_records != 1 || !rlist->records || !rlist->records[0])
468     {
469         client_show_raw_error(cl, "no records");
470         return;
471     }
472     npr = rlist->records[0];
473     if (npr->which != Z_NamePlusRecord_databaseRecord)
474     {
475         client_show_raw_error(cl, "surrogate diagnostic");
476         return;
477     }
478
479     doc = record_to_xml(client_get_database(cl), npr->u.databaseRecord);
480     if (!doc)
481     {
482         client_show_raw_error(cl, "unable to convert record to xml");
483         return;
484     }
485
486     xmlDocDumpMemory(doc, &buf_out, &len_out);
487     xmlFreeDoc(doc);
488
489     cl->show_raw->record_handler(cl->show_raw->data,
490                                  (const char *) buf_out, len_out);
491     
492     xmlFree(buf_out);
493     xfree(cl->show_raw);
494     cl->show_raw = 0;
495 }
496
497 static void ingest_records(struct client *cl, Z_Records *r)
498 {
499 #if USE_TIMING
500     yaz_timing_t t = yaz_timing_create();
501 #endif
502     struct record *rec;
503     struct session *s = client_get_session(cl);
504     Z_NamePlusRecordList *rlist;
505     int i;
506
507     if (r->which != Z_Records_DBOSD)
508         return;
509     rlist = r->u.databaseOrSurDiagnostics;
510     for (i = 0; i < rlist->num_records; i++)
511     {
512         Z_NamePlusRecord *npr = rlist->records[i];
513
514         cl->records++;
515         if (npr->which != Z_NamePlusRecord_databaseRecord)
516         {
517             yaz_log(YLOG_WARN, 
518                     "Unexpected record type, probably diagnostic %s",
519                     cl->database->database->url);
520             continue;
521         }
522
523         rec = ingest_record(cl, npr->u.databaseRecord, cl->records);
524         if (!rec)
525             continue;
526     }
527     if (rlist->num_records)
528         session_alert_watch(s, SESSION_WATCH_RECORDS);
529
530 #if USE_TIMING
531     yaz_timing_stop(t);
532     yaz_log(YLOG_LOG, "ingest_records %6.5f %3.2f %3.2f", 
533             yaz_timing_get_real(t), yaz_timing_get_user(t),
534             yaz_timing_get_sys(t));
535     yaz_timing_destroy(&t);
536 #endif
537 }
538
539
540 void client_search_response(struct client *cl, Z_APDU *a)
541 {
542     struct session *se = cl->session;
543     Z_SearchResponse *r = a->u.searchResponse;
544
545     yaz_log(YLOG_DEBUG, "Search response %s (status=%d)", 
546             cl->database->database->url, *r->searchStatus);
547
548     if (*r->searchStatus)
549     {
550         cl->hits = *r->resultCount;
551         se->total_hits += cl->hits;
552         if (r->presentStatus && !*r->presentStatus && r->records)
553         {
554             yaz_log(YLOG_DEBUG, "Records in search response %s", 
555                     cl->database->database->url);
556             ingest_records(cl, r->records);
557         }
558         cl->state = Client_Idle;
559     }
560     else
561     {          /*"FAILED"*/
562         Z_Records *recs = r->records;
563         cl->hits = 0;
564         cl->state = Client_Error;
565         if (recs && recs->which == Z_Records_NSD)
566         {
567             WRBUF w = wrbuf_alloc();
568
569             Z_DiagRec dr, *dr_p = &dr;
570             dr.which = Z_DiagRec_defaultFormat;
571             dr.u.defaultFormat = recs->u.nonSurrogateDiagnostic;
572             
573             wrbuf_printf(w, "Search response NSD %s: ",
574                          cl->database->database->url);
575             
576             cl->diagnostic = diag_to_wrbuf(&dr_p, 1, w);
577
578             yaz_log(YLOG_WARN, "%s", wrbuf_cstr(w));
579
580             cl->state = Client_Error;
581             wrbuf_destroy(w);
582         }
583         else if (recs && recs->which == Z_Records_multipleNSD)
584         {
585             WRBUF w = wrbuf_alloc();
586
587             wrbuf_printf(w, "Search response multipleNSD %s: ",
588                          cl->database->database->url);
589             cl->diagnostic = 
590                 diag_to_wrbuf(recs->u.multipleNonSurDiagnostics->diagRecs,
591                               recs->u.multipleNonSurDiagnostics->num_diagRecs,
592                               w);
593             yaz_log(YLOG_WARN, "%s", wrbuf_cstr(w));
594             cl->state = Client_Error;
595             wrbuf_destroy(w);
596         }
597     }
598 }
599
600 void client_present_response(struct client *cl, Z_APDU *a)
601 {
602     Z_PresentResponse *r = a->u.presentResponse;
603     Z_Records *recs = r->records;
604         
605     if (recs && recs->which == Z_Records_NSD)
606     {
607         WRBUF w = wrbuf_alloc();
608         
609         Z_DiagRec dr, *dr_p = &dr;
610         dr.which = Z_DiagRec_defaultFormat;
611         dr.u.defaultFormat = recs->u.nonSurrogateDiagnostic;
612         
613         wrbuf_printf(w, "Present response NSD %s: ",
614                      cl->database->database->url);
615         
616         cl->diagnostic = diag_to_wrbuf(&dr_p, 1, w);
617         
618         yaz_log(YLOG_WARN, "%s", wrbuf_cstr(w));
619         
620         cl->state = Client_Error;
621         wrbuf_destroy(w);
622
623         client_show_raw_error(cl, "non surrogate diagnostics");
624     }
625     else if (recs && recs->which == Z_Records_multipleNSD)
626     {
627         WRBUF w = wrbuf_alloc();
628         
629         wrbuf_printf(w, "Present response multipleNSD %s: ",
630                      cl->database->database->url);
631         cl->diagnostic = 
632             diag_to_wrbuf(recs->u.multipleNonSurDiagnostics->diagRecs,
633                           recs->u.multipleNonSurDiagnostics->num_diagRecs,
634                           w);
635         yaz_log(YLOG_WARN, "%s", wrbuf_cstr(w));
636         cl->state = Client_Error;
637         wrbuf_destroy(w);
638     }
639     else if (recs && !*r->presentStatus && cl->state != Client_Error)
640     {
641         yaz_log(YLOG_DEBUG, "Good Present response %s",
642                 cl->database->database->url);
643
644         // we can mix show raw and normal show ..
645         if (cl->show_raw && cl->show_raw->active)
646         {
647             cl->show_raw->active = 0; // no longer active
648             ingest_raw_records(cl, recs);
649         }
650         else
651             ingest_records(cl, recs);
652         cl->state = Client_Idle;
653     }
654     else if (*r->presentStatus) 
655     {
656         yaz_log(YLOG_WARN, "Bad Present response %s",
657                 cl->database->database->url);
658         cl->state = Client_Error;
659         client_show_raw_error(cl, "bad present response");
660     }
661 }
662
663 void client_close_response(struct client *cl, Z_APDU *a)
664 {
665     struct connection *co = cl->connection;
666     /* Z_Close *r = a->u.close; */
667
668     yaz_log(YLOG_WARN, "Close response %s", cl->database->database->url);
669
670     cl->state = Client_Failed;
671     connection_destroy(co);
672 }
673
674 int client_is_our_response(struct client *cl)
675 {
676     struct session *se = client_get_session(cl);
677
678     if (cl && (cl->requestid == se->requestid || 
679                cl->state == Client_Initializing))
680         return 1;
681     return 0;
682 }
683
684 // Set authentication token in init if one is set for the client
685 // TODO: Extend this to handle other schemes than open (should be simple)
686 static void init_authentication(struct client *cl, Z_InitRequest *req)
687 {
688     struct session_database *sdb = client_get_database(cl);
689     char *auth = session_setting_oneval(sdb, PZ_AUTHENTICATION);
690
691     if (*auth)
692     {
693         struct connection *co = client_get_connection(cl);
694         struct session *se = client_get_session(cl);
695         Z_IdAuthentication *idAuth = odr_malloc(global_parameters.odr_out,
696                 sizeof(*idAuth));
697         idAuth->which = Z_IdAuthentication_open;
698         idAuth->u.open = auth;
699         req->idAuthentication = idAuth;
700         connection_set_authentication(co, nmem_strdup(se->session_nmem, auth));
701     }
702 }
703
704 static void init_zproxy(struct client *cl, Z_InitRequest *req)
705 {
706     struct session_database *sdb = client_get_database(cl);
707     char *ztarget = sdb->database->url;
708     //char *ztarget = sdb->url;    
709     char *zproxy = session_setting_oneval(sdb, PZ_ZPROXY);
710
711     if (*zproxy)
712         yaz_oi_set_string_oid(&req->otherInfo,
713                               global_parameters.odr_out,
714                               yaz_oid_userinfo_proxy,
715                               1, ztarget);
716 }
717
718
719 static void client_init_request(struct client *cl)
720 {
721     Z_APDU *a = zget_APDU(global_parameters.odr_out, Z_APDU_initRequest);
722
723     a->u.initRequest->implementationId = global_parameters.implementationId;
724     a->u.initRequest->implementationName = global_parameters.implementationName;
725     a->u.initRequest->implementationVersion =
726         global_parameters.implementationVersion;
727     ODR_MASK_SET(a->u.initRequest->options, Z_Options_search);
728     ODR_MASK_SET(a->u.initRequest->options, Z_Options_present);
729     ODR_MASK_SET(a->u.initRequest->options, Z_Options_namedResultSets);
730
731     ODR_MASK_SET(a->u.initRequest->protocolVersion, Z_ProtocolVersion_1);
732     ODR_MASK_SET(a->u.initRequest->protocolVersion, Z_ProtocolVersion_2);
733     ODR_MASK_SET(a->u.initRequest->protocolVersion, Z_ProtocolVersion_3);
734
735     init_authentication(cl, a->u.initRequest);
736     init_zproxy(cl, a->u.initRequest);
737
738     if (send_apdu(cl, a) >= 0)
739         client_set_state(cl, Client_Initializing);
740     else
741         client_set_state(cl, Client_Error);
742     odr_reset(global_parameters.odr_out);
743 }
744
745 void client_continue(struct client *cl)
746 {
747     if (cl->state == Client_Connected) {
748         client_init_request(cl);
749     }
750
751     if (cl->state == Client_Idle)
752     {
753         struct session *se = client_get_session(cl);
754         if (cl->requestid != se->requestid && cl->pquery) {
755             // we'll have to abort this because result set is to be deleted
756             client_show_raw_cancel(cl);   
757             client_send_search(cl);
758         }
759         else if (cl->show_raw)
760         {
761             client_send_raw_present(cl);
762         }
763         else if (cl->hits > 0 && cl->records < global_parameters.toget &&
764             cl->records < cl->hits) {
765             client_send_present(cl);
766         }
767     }
768 }
769
770 struct client *client_create(void)
771 {
772     struct client *r;
773     if (client_freelist)
774     {
775         r = client_freelist;
776         client_freelist = client_freelist->next;
777     }
778     else
779         r = xmalloc(sizeof(struct client));
780     r->pquery = 0;
781     r->database = 0;
782     r->connection = 0;
783     r->session = 0;
784     r->hits = 0;
785     r->records = 0;
786     r->setno = 0;
787     r->requestid = -1;
788     r->diagnostic = 0;
789     r->state = Client_Disconnected;
790     r->show_raw = 0;
791     r->next = 0;
792     return r;
793 }
794
795 void client_destroy(struct client *c)
796 {
797     struct session *se = c->session;
798     if (c == se->clients)
799         se->clients = c->next;
800     else
801     {
802         struct client *cc;
803         for (cc = se->clients; cc && cc->next != c; cc = cc->next)
804             ;
805         if (cc)
806             cc->next = c->next;
807     }
808     xfree(c->pquery);
809
810     if (c->connection)
811         connection_release(c->connection);
812     c->next = client_freelist;
813     client_freelist = c;
814 }
815
816 void client_set_connection(struct client *cl, struct connection *con)
817 {
818     cl->connection = con;
819 }
820
821 void client_disconnect(struct client *cl)
822 {
823     if (cl->state != Client_Idle)
824         cl->state = Client_Disconnected;
825     client_set_connection(cl, 0);
826 }
827
828 // Extract terms from query into null-terminated termlist
829 static void extract_terms(NMEM nmem, struct ccl_rpn_node *query, char **termlist)
830 {
831     int num = 0;
832
833     pull_terms(nmem, query, termlist, &num);
834     termlist[num] = 0;
835 }
836
837 // Initialize CCL map for a target
838 static CCL_bibset prepare_cclmap(struct client *cl)
839 {
840     struct session_database *sdb = client_get_database(cl);
841     struct setting *s;
842     CCL_bibset res;
843
844     if (!sdb->settings)
845         return 0;
846     res = ccl_qual_mk();
847     for (s = sdb->settings[PZ_CCLMAP]; s; s = s->next)
848     {
849         char *p = strchr(s->name + 3, ':');
850         if (!p)
851         {
852             yaz_log(YLOG_WARN, "Malformed cclmap name: %s", s->name);
853             ccl_qual_rm(&res);
854             return 0;
855         }
856         p++;
857         ccl_qual_fitem(res, s->value, p);
858     }
859     return res;
860 }
861
862 // Parse the query given the settings specific to this client
863 int client_parse_query(struct client *cl, const char *query)
864 {
865     struct session *se = client_get_session(cl);
866     struct ccl_rpn_node *cn;
867     int cerror, cpos;
868     CCL_bibset ccl_map = prepare_cclmap(cl);
869
870     if (!ccl_map)
871         return -1;
872     cn = ccl_find_str(ccl_map, query, &cerror, &cpos);
873     ccl_qual_rm(&ccl_map);
874     if (!cn)
875     {
876         cl->state = Client_Error;
877         yaz_log(YLOG_WARN, "Failed to parse query for %s",
878                          client_get_database(cl)->database->url);
879         return -1;
880     }
881     wrbuf_rewind(se->wrbuf);
882     ccl_pquery(se->wrbuf, cn);
883     xfree(cl->pquery);
884     cl->pquery = xstrdup(wrbuf_cstr(se->wrbuf));
885
886     if (!se->relevance)
887     {
888         // Initialize relevance structure with query terms
889         char *p[512];
890         extract_terms(se->nmem, cn, p);
891         se->relevance = relevance_create(client_get_database(cl)->pct,
892                                          se->nmem, (const char **) p,
893                                          se->expected_maxrecs);
894     }
895
896     ccl_rpn_delete(cn);
897     return 0;
898 }
899
900 void client_set_session(struct client *cl, struct session *se)
901 {
902     cl->session = se;
903     cl->next = se->clients;
904     se->clients = cl;
905 }
906
907 int client_is_active(struct client *cl)
908 {
909     if (cl->connection && (cl->state == Client_Connecting ||
910                            cl->state == Client_Initializing ||
911                            cl->state == Client_Searching ||
912                            cl->state == Client_Presenting))
913         return 1;
914     return 0;
915 }
916
917 struct client *client_next_in_session(struct client *cl)
918 {
919     if (cl)
920         return cl->next;
921     return 0;
922
923 }
924
925 int client_get_hits(struct client *cl)
926 {
927     return cl->hits;
928 }
929
930 int client_get_num_records(struct client *cl)
931 {
932     return cl->records;
933 }
934
935 int client_get_diagnostic(struct client *cl)
936 {
937     return cl->diagnostic;
938 }
939
940 void client_set_database(struct client *cl, struct session_database *db)
941 {
942     cl->database = db;
943 }
944
945 struct host *client_get_host(struct client *cl)
946 {
947     return client_get_database(cl)->database->host;
948 }
949
950 const char *client_get_url(struct client *cl)
951 {
952     return client_get_database(cl)->database->url;
953 }
954
955 /*
956  * Local variables:
957  * c-basic-offset: 4
958  * indent-tabs-mode: nil
959  * End:
960  * vim: shiftwidth=4 tabstop=8 expandtab
961  */