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