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