8b2638bb75b47ac9e5d99b573a56f20f23b0bc29
[pazpar2-moved-to-github.git] / src / pazpar2.c
1 /* $Id: pazpar2.c,v 1.63 2007-04-04 22:43:10 marc Exp $ */
2
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <string.h>
6 #include <sys/time.h>
7 #include <unistd.h>
8 #include <sys/socket.h>
9 #include <netdb.h>
10 #include <signal.h>
11 #include <ctype.h>
12 #include <assert.h>
13
14 #include <yaz/marcdisp.h>
15 #include <yaz/comstack.h>
16 #include <yaz/tcpip.h>
17 #include <yaz/proto.h>
18 #include <yaz/readconf.h>
19 #include <yaz/pquery.h>
20 #include <yaz/otherinfo.h>
21 #include <yaz/yaz-util.h>
22 #include <yaz/nmem.h>
23
24 #if HAVE_CONFIG_H
25 #include "cconfig.h"
26 #endif
27
28 #define USE_TIMING 0
29 #if USE_TIMING
30 #include <yaz/timing.h>
31 #endif
32
33 #include <netinet/in.h>
34
35 #include "pazpar2.h"
36 #include "eventl.h"
37 #include "http.h"
38 #include "termlists.h"
39 #include "reclists.h"
40 #include "relevance.h"
41 #include "config.h"
42 #include "database.h"
43 #include "settings.h"
44
45 #define MAX_CHUNK 15
46
47 static void client_fatal(struct client *cl);
48 static void connection_destroy(struct connection *co);
49 static int client_prep_connection(struct client *cl);
50 static void ingest_records(struct client *cl, Z_Records *r);
51 //static struct conf_retrievalprofile *database_retrieval_profile(struct database *db);
52 void session_alert_watch(struct session *s, int what);
53 char *session_setting_oneval(struct session *s, struct database *db, const char *name);
54
55 IOCHAN channel_list = 0;  // Master list of connections we're handling events to
56
57 static struct connection *connection_freelist = 0;
58 static struct client *client_freelist = 0;
59
60 static char *client_states[] = {
61     "Client_Connecting",
62     "Client_Connected",
63     "Client_Idle",
64     "Client_Initializing",
65     "Client_Searching",
66     "Client_Presenting",
67     "Client_Error",
68     "Client_Failed",
69     "Client_Disconnected",
70     "Client_Stopped"
71 };
72
73 // Note: Some things in this structure will eventually move to configuration
74 struct parameters global_parameters = 
75 {
76     "",
77     "",
78     "",
79     "",
80     0,
81     0,
82     30,
83     "81",
84     "Index Data PazPar2 (MasterKey)",
85     VERSION,
86     600, // 10 minutes
87     60,
88     100,
89     MAX_CHUNK,
90     0,
91     0,
92     0
93 };
94
95 static int send_apdu(struct client *c, Z_APDU *a)
96 {
97     struct connection *co = c->connection;
98     char *buf;
99     int len, r;
100
101     if (!z_APDU(global_parameters.odr_out, &a, 0, 0))
102     {
103         odr_perror(global_parameters.odr_out, "Encoding APDU");
104         abort();
105     }
106     buf = odr_getbuf(global_parameters.odr_out, &len, 0);
107     r = cs_put(co->link, buf, len);
108     if (r < 0)
109     {
110         yaz_log(YLOG_WARN, "cs_put: %s", cs_errmsg(cs_errno(co->link)));
111         return -1;
112     }
113     else if (r == 1)
114     {
115         fprintf(stderr, "cs_put incomplete (ParaZ does not handle that)\n");
116         exit(1);
117     }
118     odr_reset(global_parameters.odr_out); /* release the APDU structure  */
119     co->state = Conn_Waiting;
120     return 0;
121 }
122
123
124 static void send_init(IOCHAN i)
125 {
126
127     struct connection *co = iochan_getdata(i);
128     struct client *cl = co->client;
129     Z_APDU *a = zget_APDU(global_parameters.odr_out, Z_APDU_initRequest);
130
131     a->u.initRequest->implementationId = global_parameters.implementationId;
132     a->u.initRequest->implementationName = global_parameters.implementationName;
133     a->u.initRequest->implementationVersion =
134         global_parameters.implementationVersion;
135     ODR_MASK_SET(a->u.initRequest->options, Z_Options_search);
136     ODR_MASK_SET(a->u.initRequest->options, Z_Options_present);
137     ODR_MASK_SET(a->u.initRequest->options, Z_Options_namedResultSets);
138
139     ODR_MASK_SET(a->u.initRequest->protocolVersion, Z_ProtocolVersion_1);
140     ODR_MASK_SET(a->u.initRequest->protocolVersion, Z_ProtocolVersion_2);
141     ODR_MASK_SET(a->u.initRequest->protocolVersion, Z_ProtocolVersion_3);
142
143
144     /* add virtual host if tunneling through Z39.50 proxy */
145     
146     if (0 < strlen(global_parameters.zproxy_override) 
147         && 0 < strlen(cl->database->url))
148         yaz_oi_set_string_oidval(&a->u.initRequest->otherInfo, 
149                                  global_parameters.odr_out, VAL_PROXY,
150                                  1, cl->database->url);
151     
152
153
154     if (send_apdu(cl, a) >= 0)
155     {
156         iochan_setflags(i, EVENT_INPUT);
157         cl->state = Client_Initializing;
158     }
159     else
160         cl->state = Client_Error;
161     odr_reset(global_parameters.odr_out);
162 }
163
164 static void pull_terms(NMEM nmem, struct ccl_rpn_node *n, char **termlist, int *num)
165 {
166     switch (n->kind)
167     {
168         case CCL_RPN_AND:
169         case CCL_RPN_OR:
170         case CCL_RPN_NOT:
171         case CCL_RPN_PROX:
172             pull_terms(nmem, n->u.p[0], termlist, num);
173             pull_terms(nmem, n->u.p[1], termlist, num);
174             break;
175         case CCL_RPN_TERM:
176             termlist[(*num)++] = nmem_strdup(nmem, n->u.t.term);
177             break;
178         default: // NOOP
179             break;
180     }
181 }
182
183 // Extract terms from query into null-terminated termlist
184 static void extract_terms(NMEM nmem, struct ccl_rpn_node *query, char **termlist)
185 {
186     int num = 0;
187
188     pull_terms(nmem, query, termlist, &num);
189     termlist[num] = 0;
190 }
191
192 static void send_search(IOCHAN i)
193 {
194     struct connection *co = iochan_getdata(i);
195     struct client *cl = co->client; 
196     struct session *se = cl->session;
197     struct database *db = cl->database;
198     Z_APDU *a = zget_APDU(global_parameters.odr_out, Z_APDU_searchRequest);
199     int ndb, cerror, cpos;
200     char **databaselist;
201     Z_Query *zquery;
202     struct ccl_rpn_node *cn;
203     int ssub = 0, lslb = 100000, mspn = 10;
204     char *recsyn;
205     char *piggyback;
206
207     yaz_log(YLOG_DEBUG, "Sending search");
208
209     cn = ccl_find_str(db->ccl_map, se->query, &cerror, &cpos);
210     if (!cn)
211         return;
212
213     if (!se->relevance)
214     {
215         // Initialize relevance structure with query terms
216         char *p[512];
217         extract_terms(se->nmem, cn, p);
218         se->relevance = relevance_create(se->nmem, (const char **) p,
219                 se->expected_maxrecs);
220     }
221
222     a->u.searchRequest->query = zquery = odr_malloc(global_parameters.odr_out,
223             sizeof(Z_Query));
224     zquery->which = Z_Query_type_1;
225     zquery->u.type_1 = ccl_rpn_query(global_parameters.odr_out, cn);
226     ccl_rpn_delete(cn);
227
228     for (ndb = 0; db->databases[ndb]; ndb++)
229         ;
230     databaselist = odr_malloc(global_parameters.odr_out, sizeof(char*) * ndb);
231     for (ndb = 0; db->databases[ndb]; ndb++)
232         databaselist[ndb] = db->databases[ndb];
233
234     if (!(piggyback = session_setting_oneval(se, db, "pz:piggyback")) || *piggyback == '1')
235     {
236         if ((recsyn = session_setting_oneval(se, db, "pz:syntax")))
237             a->u.searchRequest->preferredRecordSyntax =
238                     yaz_str_to_z3950oid(global_parameters.odr_out,
239                     CLASS_RECSYN, recsyn);
240         a->u.searchRequest->smallSetUpperBound = &ssub;
241         a->u.searchRequest->largeSetLowerBound = &lslb;
242         a->u.searchRequest->mediumSetPresentNumber = &mspn;
243     }
244     a->u.searchRequest->resultSetName = "Default";
245     a->u.searchRequest->databaseNames = databaselist;
246     a->u.searchRequest->num_databaseNames = ndb;
247
248     if (send_apdu(cl, a) >= 0)
249     {
250         iochan_setflags(i, EVENT_INPUT);
251         cl->state = Client_Searching;
252         cl->requestid = se->requestid;
253     }
254     else
255         cl->state = Client_Error;
256
257     odr_reset(global_parameters.odr_out);
258 }
259
260 static void send_present(IOCHAN i)
261 {
262     struct connection *co = iochan_getdata(i);
263     struct client *cl = co->client; 
264     struct session *se = cl->session;
265     struct database *db = cl->database;
266     Z_APDU *a = zget_APDU(global_parameters.odr_out, Z_APDU_presentRequest);
267     int toget;
268     int start = cl->records + 1;
269     char *recsyn;
270
271     toget = global_parameters.chunk;
272     if (toget > global_parameters.toget - cl->records)
273         toget = global_parameters.toget - cl->records;
274     if (toget > cl->hits - cl->records)
275         toget = cl->hits - cl->records;
276
277     yaz_log(YLOG_DEBUG, "Trying to present %d records\n", toget);
278
279     a->u.presentRequest->resultSetStartPoint = &start;
280     a->u.presentRequest->numberOfRecordsRequested = &toget;
281
282     a->u.presentRequest->resultSetId = "Default";
283
284     if ((recsyn = session_setting_oneval(se, db, "pz:syntax")))
285         a->u.presentRequest->preferredRecordSyntax =
286                 yaz_str_to_z3950oid(global_parameters.odr_out,
287                 CLASS_RECSYN, recsyn);
288
289     if (send_apdu(cl, a) >= 0)
290     {
291         iochan_setflags(i, EVENT_INPUT);
292         cl->state = Client_Presenting;
293     }
294     else
295         cl->state = Client_Error;
296     odr_reset(global_parameters.odr_out);
297 }
298
299 static void do_initResponse(IOCHAN i, Z_APDU *a)
300 {
301     struct connection *co = iochan_getdata(i);
302     struct client *cl = co->client;
303     Z_InitResponse *r = a->u.initResponse;
304
305     yaz_log(YLOG_DEBUG, "Init response %s", cl->database->url);
306
307     if (*r->result)
308     {
309         cl->state = Client_Idle;
310     }
311     else
312         cl->state = Client_Failed; // FIXME need to do something to the connection
313 }
314
315 static void do_searchResponse(IOCHAN i, Z_APDU *a)
316 {
317     struct connection *co = iochan_getdata(i);
318     struct client *cl = co->client;
319     struct session *se = cl->session;
320     Z_SearchResponse *r = a->u.searchResponse;
321
322     yaz_log(YLOG_DEBUG, "Search response %s (status=%d)", 
323             cl->database->url, *r->searchStatus);
324
325     if (*r->searchStatus)
326     {
327         cl->hits = *r->resultCount;
328         se->total_hits += cl->hits;
329         if (r->presentStatus && !*r->presentStatus && r->records)
330         {
331             yaz_log(YLOG_DEBUG, "Records in search response %s", 
332                     cl->database->url);
333             ingest_records(cl, r->records);
334         }
335         cl->state = Client_Idle;
336     }
337     else
338     {          /*"FAILED"*/
339         cl->hits = 0;
340         cl->state = Client_Error;
341         if (r->records) {
342             Z_Records *recs = r->records;
343             if (recs->which == Z_Records_NSD)
344             {
345                 yaz_log(YLOG_WARN, 
346                         "Search response: Non-surrogate diagnostic %s",
347                         cl->database->url);
348                 cl->diagnostic = *recs->u.nonSurrogateDiagnostic->condition;
349                 cl->state = Client_Error;
350             }
351         }
352     }
353 }
354
355 static void do_closeResponse(IOCHAN i, Z_APDU *a)
356 {
357     struct connection *co = iochan_getdata(i);
358     struct client *cl = co->client;
359     /* Z_Close *r = a->u.close; */
360
361     yaz_log(YLOG_WARN, "Close response %s", cl->database->url);
362
363     cl->state = Client_Failed;
364     connection_destroy(co);
365 }
366
367
368 char *normalize_mergekey(char *buf, int skiparticle)
369 {
370     char *p = buf, *pout = buf;
371
372     if (skiparticle)
373     {
374         char firstword[64];
375         char articles[] = "the den der die des an a "; // must end in space
376
377         while (*p && !isalnum(*p))
378             p++;
379         pout = firstword;
380         while (*p && *p != ' ' && pout - firstword < 62)
381             *(pout++) = tolower(*(p++));
382         *(pout++) = ' ';
383         *(pout++) = '\0';
384         if (!strstr(articles, firstword))
385             p = buf;
386         pout = buf;
387     }
388
389     while (*p)
390     {
391         while (*p && !isalnum(*p))
392             p++;
393         while (isalnum(*p))
394             *(pout++) = tolower(*(p++));
395         if (*p)
396             *(pout++) = ' ';
397         while (*p && !isalnum(*p))
398             p++;
399     }
400     if (buf != pout)
401         do {
402             *(pout--) = '\0';
403         }
404         while (pout > buf && *pout == ' ');
405
406     return buf;
407 }
408
409 static void add_facet(struct session *s, const char *type, const char *value)
410 {
411     int i;
412
413     if (!*value)
414         return;
415     for (i = 0; i < s->num_termlists; i++)
416         if (!strcmp(s->termlists[i].name, type))
417             break;
418     if (i == s->num_termlists)
419     {
420         if (i == SESSION_MAX_TERMLISTS)
421         {
422             yaz_log(YLOG_FATAL, "Too many termlists");
423             exit(1);
424         }
425         s->termlists[i].name = nmem_strdup(s->nmem, type);
426         s->termlists[i].termlist = termlist_create(s->nmem, s->expected_maxrecs, 15);
427         s->num_termlists = i + 1;
428     }
429     termlist_insert(s->termlists[i].termlist, value);
430 }
431
432 static xmlDoc *normalize_record(struct client *cl, Z_External *rec)
433 {
434     struct conf_retrievalprofile *rprofile = cl->database->rprofile;
435     struct conf_retrievalmap *m;
436     xmlNode *res;
437     xmlDoc *rdoc;
438
439     // First normalize to XML
440     if (rprofile->native_syntax == Nativesyn_iso2709)
441     {
442         char *buf;
443         int len;
444         if (rec->which != Z_External_octet)
445         {
446             yaz_log(YLOG_WARN, "Unexpected external branch, probably BER %s",
447                     cl->database->url);
448             return 0;
449         }
450         buf = (char*) rec->u.octet_aligned->buf;
451         len = rec->u.octet_aligned->len;
452         if (yaz_marc_read_iso2709(rprofile->yaz_marc, buf, len) < 0)
453         {
454             yaz_log(YLOG_WARN, "Failed to decode MARC %s",
455                     cl->database->url);
456             return 0;
457         }
458         if (yaz_marc_write_xml(rprofile->yaz_marc, &res,
459                     "http://www.loc.gov/MARC21/slim", 0, 0) < 0)
460         {
461             yaz_log(YLOG_WARN, "Failed to encode as XML %s",
462                     cl->database->url);
463             return 0;
464         }
465         rdoc = xmlNewDoc((xmlChar *) "1.0");
466         xmlDocSetRootElement(rdoc, res);
467     }
468     else
469     {
470         yaz_log(YLOG_FATAL, "Unknown native_syntax in normalize_record");
471         exit(1);
472     }
473
474     if (global_parameters.dump_records)
475     {
476         fprintf(stderr, "Input Record (normalized):\n----------------\n");
477 #if LIBXML_VERSION >= 20600
478         xmlDocFormatDump(stderr, rdoc, 1);
479 #else
480         xmlDocDump(stderr, rdoc);
481 #endif
482     }
483
484     for (m = rprofile->maplist; m; m = m->next)
485     {
486         xmlDoc *new;
487         if (m->type != Map_xslt)
488         {
489             yaz_log(YLOG_WARN, "Unknown map type");
490             return 0;
491         }
492         if (!(new = xsltApplyStylesheet(m->stylesheet, rdoc, 0)))
493         {
494             yaz_log(YLOG_WARN, "XSLT transformation failed");
495             return 0;
496         }
497         xmlFreeDoc(rdoc);
498         rdoc = new;
499     }
500     if (global_parameters.dump_records)
501     {
502         fprintf(stderr, "Record:\n----------------\n");
503 #if LIBXML_VERSION >= 20600
504         xmlDocFormatDump(stderr, rdoc, 1);
505 #else
506         xmlDocDump(stderr, rdoc);
507 #endif
508     }
509     return rdoc;
510 }
511
512 // Extract what appears to be years from buf, storing highest and
513 // lowest values.
514 static int extract_years(const char *buf, int *first, int *last)
515 {
516     *first = -1;
517     *last = -1;
518     while (*buf)
519     {
520         const char *e;
521         int len;
522
523         while (*buf && !isdigit(*buf))
524             buf++;
525         len = 0;
526         for (e = buf; *e && isdigit(*e); e++)
527             len++;
528         if (len == 4)
529         {
530             int value = atoi(buf);
531             if (*first < 0 || value < *first)
532                 *first = value;
533             if (*last < 0 || value > *last)
534                 *last = value;
535         }
536         buf = e;
537     }
538     return *first;
539 }
540
541 static struct record *ingest_record(struct client *cl, Z_External *rec)
542 {
543     xmlDoc *xdoc = normalize_record(cl, rec);
544     xmlNode *root, *n;
545     struct record *res;
546     struct record_cluster *cluster;
547     struct session *se = cl->session;
548     xmlChar *mergekey, *mergekey_norm;
549     xmlChar *type = 0;
550     xmlChar *value = 0;
551     struct conf_service *service = global_parameters.server->service;
552
553     if (!xdoc)
554         return 0;
555
556     root = xmlDocGetRootElement(xdoc);
557     if (!(mergekey = xmlGetProp(root, (xmlChar *) "mergekey")))
558     {
559         yaz_log(YLOG_WARN, "No mergekey found in record");
560         xmlFreeDoc(xdoc);
561         return 0;
562     }
563
564     res = nmem_malloc(se->nmem, sizeof(struct record));
565     res->next = 0;
566     res->client = cl;
567     res->metadata = nmem_malloc(se->nmem,
568             sizeof(struct record_metadata*) * service->num_metadata);
569     memset(res->metadata, 0, sizeof(struct record_metadata*) * service->num_metadata);
570
571     mergekey_norm = (xmlChar *) nmem_strdup(se->nmem, (char*) mergekey);
572     xmlFree(mergekey);
573     normalize_mergekey((char *) mergekey_norm, 0);
574
575     cluster = reclist_insert(se->reclist, res, (char *) mergekey_norm, 
576                              &se->total_merged);
577     if (global_parameters.dump_records)
578         yaz_log(YLOG_LOG, "Cluster id %d from %s (#%d)", cluster->recid,
579                 cl->database->url, cl->records);
580     if (!cluster)
581     {
582         /* no room for record */
583         xmlFreeDoc(xdoc);
584         return 0;
585     }
586     relevance_newrec(se->relevance, cluster);
587
588     for (n = root->children; n; n = n->next)
589     {
590         if (type)
591             xmlFree(type);
592         if (value)
593             xmlFree(value);
594         type = value = 0;
595
596         if (n->type != XML_ELEMENT_NODE)
597             continue;
598         if (!strcmp((const char *) n->name, "metadata"))
599         {
600             struct conf_metadata *md = 0;
601             struct conf_sortkey *sk = 0;
602             struct record_metadata **wheretoput, *newm;
603             int imeta;
604             int first, last;
605
606             type = xmlGetProp(n, (xmlChar *) "type");
607             value = xmlNodeListGetString(xdoc, n->children, 0);
608
609             if (!type || !value)
610                 continue;
611
612             // First, find out what field we're looking at
613             for (imeta = 0; imeta < service->num_metadata; imeta++)
614                 if (!strcmp((const char *) type, service->metadata[imeta].name))
615                 {
616                     md = &service->metadata[imeta];
617                     if (md->sortkey_offset >= 0)
618                         sk = &service->sortkeys[md->sortkey_offset];
619                     break;
620                 }
621             if (!md)
622             {
623                 yaz_log(YLOG_WARN, "Ignoring unknown metadata element: %s", type);
624                 continue;
625             }
626
627             // Find out where we are putting it
628             if (md->merge == Metadata_merge_no)
629                 wheretoput = &res->metadata[imeta];
630             else
631                 wheretoput = &cluster->metadata[imeta];
632             
633             // Put it there
634             newm = nmem_malloc(se->nmem, sizeof(struct record_metadata));
635             newm->next = 0;
636             if (md->type == Metadata_type_generic)
637             {
638                 char *p, *pe;
639                 for (p = (char *) value; *p && isspace(*p); p++)
640                     ;
641                 for (pe = p + strlen(p) - 1;
642                         pe > p && strchr(" ,/.:([", *pe); pe--)
643                     *pe = '\0';
644                 newm->data.text = nmem_strdup(se->nmem, p);
645
646             }
647             else if (md->type == Metadata_type_year)
648             {
649                 if (extract_years((char *) value, &first, &last) < 0)
650                     continue;
651             }
652             else
653             {
654                 yaz_log(YLOG_WARN, "Unknown type in metadata element %s", type);
655                 continue;
656             }
657             if (md->type == Metadata_type_year && md->merge != Metadata_merge_range)
658             {
659                 yaz_log(YLOG_WARN, "Only range merging supported for years");
660                 continue;
661             }
662             if (md->merge == Metadata_merge_unique)
663             {
664                 struct record_metadata *mnode;
665                 for (mnode = *wheretoput; mnode; mnode = mnode->next)
666                     if (!strcmp((const char *) mnode->data.text, newm->data.text))
667                         break;
668                 if (!mnode)
669                 {
670                     newm->next = *wheretoput;
671                     *wheretoput = newm;
672                 }
673             }
674             else if (md->merge == Metadata_merge_longest)
675             {
676                 if (!*wheretoput ||
677                         strlen(newm->data.text) > strlen((*wheretoput)->data.text))
678                 {
679                     *wheretoput = newm;
680                     if (sk)
681                     {
682                         char *s = nmem_strdup(se->nmem, newm->data.text);
683                         if (!cluster->sortkeys[md->sortkey_offset])
684                             cluster->sortkeys[md->sortkey_offset] = 
685                                 nmem_malloc(se->nmem, sizeof(union data_types));
686                         normalize_mergekey(s,
687                                 (sk->type == Metadata_sortkey_skiparticle));
688                         cluster->sortkeys[md->sortkey_offset]->text = s;
689                     }
690                 }
691             }
692             else if (md->merge == Metadata_merge_all || md->merge == Metadata_merge_no)
693             {
694                 newm->next = *wheretoput;
695                 *wheretoput = newm;
696             }
697             else if (md->merge == Metadata_merge_range)
698             {
699                 assert(md->type == Metadata_type_year);
700                 if (!*wheretoput)
701                 {
702                     *wheretoput = newm;
703                     (*wheretoput)->data.number.min = first;
704                     (*wheretoput)->data.number.max = last;
705                     if (sk)
706                         cluster->sortkeys[md->sortkey_offset] = &newm->data;
707                 }
708                 else
709                 {
710                     if (first < (*wheretoput)->data.number.min)
711                         (*wheretoput)->data.number.min = first;
712                     if (last > (*wheretoput)->data.number.max)
713                         (*wheretoput)->data.number.max = last;
714                 }
715 #ifdef GAGA
716                 if (sk)
717                 {
718                     union data_types *sdata = cluster->sortkeys[md->sortkey_offset];
719                     yaz_log(YLOG_LOG, "SK range: %d-%d", sdata->number.min, sdata->number.max);
720                 }
721 #endif
722             }
723             else
724                 yaz_log(YLOG_WARN, "Don't know how to merge on element name %s", md->name);
725
726             if (md->rank)
727                 relevance_countwords(se->relevance, cluster, 
728                                      (char *) value, md->rank);
729             if (md->termlist)
730             {
731                 if (md->type == Metadata_type_year)
732                 {
733                     char year[64];
734                     sprintf(year, "%d", last);
735                     add_facet(se, (char *) type, year);
736                     if (first != last)
737                     {
738                         sprintf(year, "%d", first);
739                         add_facet(se, (char *) type, year);
740                     }
741                 }
742                 else
743                     add_facet(se, (char *) type, (char *) value);
744             }
745             xmlFree(type);
746             xmlFree(value);
747             type = value = 0;
748         }
749         else
750             yaz_log(YLOG_WARN, "Unexpected element %s in internal record", n->name);
751     }
752     if (type)
753         xmlFree(type);
754     if (value)
755         xmlFree(value);
756
757     xmlFreeDoc(xdoc);
758
759     relevance_donerecord(se->relevance, cluster);
760     se->total_records++;
761
762     return res;
763 }
764
765 // Retrieve first defined value for 'name' for given database.
766 // Will be extended to take into account user associated with session
767 char *session_setting_oneval(struct session *s, struct database *db, const char *name)
768 {
769     int offset = settings_offset(name);
770
771     if (offset < 0)
772         return 0;
773     if (!db->settings[offset])
774         return 0;
775     return db->settings[offset]->value;
776 }
777
778 static void ingest_records(struct client *cl, Z_Records *r)
779 {
780 #if USE_TIMING
781     yaz_timing_t t = yaz_timing_create();
782 #endif
783     struct record *rec;
784     struct session *s = cl->session;
785     Z_NamePlusRecordList *rlist;
786     int i;
787
788     if (r->which != Z_Records_DBOSD)
789         return;
790     rlist = r->u.databaseOrSurDiagnostics;
791     for (i = 0; i < rlist->num_records; i++)
792     {
793         Z_NamePlusRecord *npr = rlist->records[i];
794
795         cl->records++;
796         if (npr->which != Z_NamePlusRecord_databaseRecord)
797         {
798             yaz_log(YLOG_WARN, 
799                     "Unexpected record type, probably diagnostic %s",
800                     cl->database->url);
801             continue;
802         }
803
804         rec = ingest_record(cl, npr->u.databaseRecord);
805         if (!rec)
806             continue;
807     }
808     if (s->watchlist[SESSION_WATCH_RECORDS].fun && rlist->num_records)
809         session_alert_watch(s, SESSION_WATCH_RECORDS);
810
811 #if USE_TIMING
812     yaz_timing_stop(t);
813     yaz_log(YLOG_LOG, "ingest_records %6.5f %3.2f %3.2f", 
814             yaz_timing_get_real(t), yaz_timing_get_user(t),
815             yaz_timing_get_sys(t));
816     yaz_timing_destroy(&t);
817 #endif
818 }
819
820 static void do_presentResponse(IOCHAN i, Z_APDU *a)
821 {
822     struct connection *co = iochan_getdata(i);
823     struct client *cl = co->client;
824     Z_PresentResponse *r = a->u.presentResponse;
825
826     if (r->records) {
827         Z_Records *recs = r->records;
828         if (recs->which == Z_Records_NSD)
829         {
830             yaz_log(YLOG_WARN, "Non-surrogate diagnostic %s",
831                     cl->database->url);
832             cl->diagnostic = *recs->u.nonSurrogateDiagnostic->condition;
833             cl->state = Client_Error;
834         }
835     }
836
837     if (!*r->presentStatus && cl->state != Client_Error)
838     {
839         yaz_log(YLOG_DEBUG, "Good Present response %s",
840                 cl->database->url);
841         ingest_records(cl, r->records);
842         cl->state = Client_Idle;
843     }
844     else if (*r->presentStatus) 
845     {
846         yaz_log(YLOG_WARN, "Bad Present response %s",
847                 cl->database->url);
848         cl->state = Client_Error;
849     }
850 }
851
852 static void handler(IOCHAN i, int event)
853 {
854     struct connection *co = iochan_getdata(i);
855     struct client *cl = co->client;
856     struct session *se = 0;
857
858     if (cl)
859         se = cl->session;
860     else
861     {
862         yaz_log(YLOG_WARN, "Destroying orphan connection");
863         connection_destroy(co);
864         return;
865     }
866
867     if (co->state == Conn_Connecting && event & EVENT_OUTPUT)
868     {
869         int errcode;
870         socklen_t errlen = sizeof(errcode);
871
872         if (getsockopt(cs_fileno(co->link), SOL_SOCKET, SO_ERROR, &errcode,
873             &errlen) < 0 || errcode != 0)
874         {
875             client_fatal(cl);
876             return;
877         }
878         else
879         {
880             yaz_log(YLOG_DEBUG, "Connect OK");
881             co->state = Conn_Open;
882             if (cl)
883                 cl->state = Client_Connected;
884         }
885     }
886
887     else if (event & EVENT_INPUT)
888     {
889         int len = cs_get(co->link, &co->ibuf, &co->ibufsize);
890
891         if (len < 0)
892         {
893             yaz_log(YLOG_WARN|YLOG_ERRNO, "Error reading from %s", 
894                     cl->database->url);
895             connection_destroy(co);
896             return;
897         }
898         else if (len == 0)
899         {
900             yaz_log(YLOG_WARN, "EOF reading from %s", cl->database->url);
901             connection_destroy(co);
902             return;
903         }
904         else if (len > 1) // We discard input if we have no connection
905         {
906             co->state = Conn_Open;
907
908             if (cl && (cl->requestid == se->requestid || cl->state == Client_Initializing))
909             {
910                 Z_APDU *a;
911
912                 odr_reset(global_parameters.odr_in);
913                 odr_setbuf(global_parameters.odr_in, co->ibuf, len, 0);
914                 if (!z_APDU(global_parameters.odr_in, &a, 0, 0))
915                 {
916                     client_fatal(cl);
917                     return;
918                 }
919                 switch (a->which)
920                 {
921                     case Z_APDU_initResponse:
922                         do_initResponse(i, a);
923                         break;
924                     case Z_APDU_searchResponse:
925                         do_searchResponse(i, a);
926                         break;
927                     case Z_APDU_presentResponse:
928                         do_presentResponse(i, a);
929                         break;
930                     case Z_APDU_close:
931                         do_closeResponse(i, a);
932                         break;
933                     default:
934                         yaz_log(YLOG_WARN, 
935                                 "Unexpected Z39.50 response from %s",  
936                                 cl->database->url);
937                         client_fatal(cl);
938                         return;
939                 }
940                 // We aren't expecting staggered output from target
941                 // if (cs_more(t->link))
942                 //    iochan_setevent(i, EVENT_INPUT);
943             }
944             else  // we throw away response and go to idle mode
945             {
946                 yaz_log(YLOG_DEBUG, "Ignoring result of expired operation");
947                 cl->state = Client_Idle;
948             }
949         }
950         /* if len==1 we do nothing but wait for more input */
951     }
952
953     if (cl->state == Client_Connected) {
954         send_init(i);
955     }
956
957     if (cl->state == Client_Idle)
958     {
959         if (cl->requestid != se->requestid && *se->query) {
960             send_search(i);
961         }
962         else if (cl->hits > 0 && cl->records < global_parameters.toget &&
963             cl->records < cl->hits) {
964             send_present(i);
965         }
966     }
967 }
968
969 // Disassociate connection from client
970 static void connection_release(struct connection *co)
971 {
972     struct client *cl = co->client;
973
974     yaz_log(YLOG_DEBUG, "Connection release %s", co->host->hostport);
975     if (!cl)
976         return;
977     cl->connection = 0;
978     co->client = 0;
979 }
980
981 // Close connection and recycle structure
982 static void connection_destroy(struct connection *co)
983 {
984     struct host *h = co->host;
985     cs_close(co->link);
986     iochan_destroy(co->iochan);
987
988     yaz_log(YLOG_DEBUG, "Connection destroy %s", co->host->hostport);
989     if (h->connections == co)
990         h->connections = co->next;
991     else
992     {
993         struct connection *pco;
994         for (pco = h->connections; pco && pco->next != co; pco = pco->next)
995             ;
996         if (pco)
997             pco->next = co->next;
998         else
999             abort();
1000     }
1001     if (co->client)
1002     {
1003         if (co->client->state != Client_Idle)
1004             co->client->state = Client_Disconnected;
1005         co->client->connection = 0;
1006     }
1007     co->next = connection_freelist;
1008     connection_freelist = co;
1009 }
1010
1011 // Creates a new connection for client, associated with the host of 
1012 // client's database
1013 static struct connection *connection_create(struct client *cl)
1014 {
1015     struct connection *new;
1016     COMSTACK link; 
1017     int res;
1018     void *addr;
1019
1020
1021     if (!(link = cs_create(tcpip_type, 0, PROTO_Z3950)))
1022         {
1023             yaz_log(YLOG_FATAL|YLOG_ERRNO, "Failed to create comstack");
1024             exit(1);
1025         }
1026     
1027     if (0 == strlen(global_parameters.zproxy_override)){
1028         /* no Z39.50 proxy needed - direct connect */
1029         yaz_log(YLOG_DEBUG, "Connection create %s", cl->database->url);
1030         
1031         if (!(addr = cs_straddr(link, cl->database->host->ipport)))
1032             {
1033                 yaz_log(YLOG_WARN|YLOG_ERRNO, 
1034                         "Lookup of IP address %s failed", 
1035                         cl->database->host->ipport);
1036                 return 0;
1037             }
1038     
1039     } else {
1040         /* Z39.50 proxy connect */
1041         yaz_log(YLOG_DEBUG, "Connection create %s proxy %s", 
1042                 cl->database->url, global_parameters.zproxy_override);
1043
1044         if (!(addr = cs_straddr(link, global_parameters.zproxy_override)))
1045             {
1046                 yaz_log(YLOG_WARN|YLOG_ERRNO, 
1047                         "Lookup of IP address %s failed", 
1048                         global_parameters.zproxy_override);
1049                 return 0;
1050             }
1051     }
1052
1053     res = cs_connect(link, addr);
1054     if (res < 0)
1055     {
1056         yaz_log(YLOG_WARN|YLOG_ERRNO, "cs_connect %s", cl->database->url);
1057         return 0;
1058     }
1059
1060     if ((new = connection_freelist))
1061         connection_freelist = new->next;
1062     else
1063     {
1064         new = xmalloc(sizeof (struct connection));
1065         new->ibuf = 0;
1066         new->ibufsize = 0;
1067     }
1068     new->state = Conn_Connecting;
1069     new->host = cl->database->host;
1070     new->next = new->host->connections;
1071     new->host->connections = new;
1072     new->client = cl;
1073     cl->connection = new;
1074     new->link = link;
1075
1076     new->iochan = iochan_create(cs_fileno(link), 0, handler, 0);
1077     iochan_setdata(new->iochan, new);
1078     new->iochan->next = channel_list;
1079     channel_list = new->iochan;
1080     return new;
1081 }
1082
1083 // Close connection and set state to error
1084 static void client_fatal(struct client *cl)
1085 {
1086     yaz_log(YLOG_WARN, "Fatal error from %s", cl->database->url);
1087     connection_destroy(cl->connection);
1088     cl->state = Client_Error;
1089 }
1090
1091 // Ensure that client has a connection associated
1092 static int client_prep_connection(struct client *cl)
1093 {
1094     struct connection *co;
1095     struct session *se = cl->session;
1096     struct host *host = cl->database->host;
1097
1098     co = cl->connection;
1099
1100     yaz_log(YLOG_DEBUG, "Client prep %s", cl->database->url);
1101
1102     if (!co)
1103     {
1104         // See if someone else has an idle connection
1105         // We should look at timestamps here to select the longest-idle connection
1106         for (co = host->connections; co; co = co->next)
1107             if (co->state == Conn_Open && (!co->client || co->client->session != se))
1108                 break;
1109         if (co)
1110         {
1111             connection_release(co);
1112             cl->connection = co;
1113             co->client = cl;
1114         }
1115         else
1116             co = connection_create(cl);
1117     }
1118     if (co)
1119     {
1120         if (co->state == Conn_Connecting)
1121         {
1122             cl->state = Client_Connecting;
1123             iochan_setflag(co->iochan, EVENT_OUTPUT);
1124         }
1125         else if (co->state == Conn_Open)
1126         {
1127             if (cl->state == Client_Error || cl->state == Client_Disconnected)
1128                 cl->state = Client_Idle;
1129             iochan_setflag(co->iochan, EVENT_OUTPUT);
1130         }
1131         return 1;
1132     }
1133     else
1134         return 0;
1135 }
1136
1137 #ifdef GAGA // Moved to database.c
1138
1139 // This function will most likely vanish when a proper target profile mechanism is
1140 // introduced.
1141 void load_simpletargets(const char *fn)
1142 {
1143     FILE *f = fopen(fn, "r");
1144     char line[256];
1145
1146     if (!f)
1147     {
1148         yaz_log(YLOG_WARN|YLOG_ERRNO, "open %s", fn);
1149         exit(1);
1150     }
1151
1152     while (fgets(line, 255, f))
1153     {
1154         char *url, *db;
1155         char *name;
1156         struct host *host;
1157         struct database *database;
1158
1159         if (strncmp(line, "target ", 7))
1160             continue;
1161         line[strlen(line) - 1] = '\0';
1162
1163         if ((name = strchr(line, ';')))
1164             *(name++) = '\0';
1165
1166         url = line + 7;
1167         if ((db = strchr(url, '/')))
1168             *(db++) = '\0';
1169         else
1170             db = "Default";
1171
1172         yaz_log(YLOG_LOG, "Target: %s, '%s'", url, db);
1173         for (host = hosts; host; host = host->next)
1174             if (!strcmp((const char *) url, host->hostport))
1175                 break;
1176         if (!host)
1177         {
1178             struct addrinfo *addrinfo, hints;
1179             char *port;
1180             char ipport[128];
1181             unsigned char addrbuf[4];
1182             int res;
1183
1184             host = xmalloc(sizeof(struct host));
1185             host->hostport = xstrdup(url);
1186             host->connections = 0;
1187
1188             if ((port = strchr(url, ':')))
1189                 *(port++) = '\0';
1190             else
1191                 port = "210";
1192
1193             hints.ai_flags = 0;
1194             hints.ai_family = PF_INET;
1195             hints.ai_socktype = SOCK_STREAM;
1196             hints.ai_protocol = IPPROTO_TCP;
1197             hints.ai_addrlen = 0;
1198             hints.ai_addr = 0;
1199             hints.ai_canonname = 0;
1200             hints.ai_next = 0;
1201             // This is not robust code. It assumes that getaddrinfo returns AF_INET
1202             // address.
1203             if ((res = getaddrinfo(url, port, &hints, &addrinfo)))
1204             {
1205                 yaz_log(YLOG_WARN, "Failed to resolve %s: %s", url, gai_strerror(res));
1206                 xfree(host->hostport);
1207                 xfree(host);
1208                 continue;
1209             }
1210             assert(addrinfo->ai_family == PF_INET);
1211             memcpy(addrbuf, &((struct sockaddr_in*)addrinfo->ai_addr)->sin_addr.s_addr, 4);
1212             sprintf(ipport, "%u.%u.%u.%u:%s",
1213                     addrbuf[0], addrbuf[1], addrbuf[2], addrbuf[3], port);
1214             host->ipport = xstrdup(ipport);
1215             freeaddrinfo(addrinfo);
1216             host->next = hosts;
1217             hosts = host;
1218         }
1219         database = xmalloc(sizeof(struct database));
1220         database->host = host;
1221         database->url = xmalloc(strlen(url) + strlen(db) + 2);
1222         strcpy(database->url, url);
1223         strcat(database->url, "/");
1224         strcat(database->url, db);
1225         if (name)
1226             database->name = xstrdup(name);
1227         else
1228             database->name = 0;
1229         
1230         database->databases = xmalloc(2 * sizeof(char *));
1231         database->databases[0] = xstrdup(db);
1232         database->databases[1] = 0;
1233         database->errors = 0;
1234         database->qprofile = 0;
1235         database->rprofile = database_retrieval_profile(database);
1236         database->next = databases;
1237         databases = database;
1238
1239     }
1240     fclose(f);
1241 }
1242
1243 #endif
1244
1245 static struct client *client_create(void)
1246 {
1247     struct client *r;
1248     if (client_freelist)
1249     {
1250         r = client_freelist;
1251         client_freelist = client_freelist->next;
1252     }
1253     else
1254         r = xmalloc(sizeof(struct client));
1255     r->database = 0;
1256     r->connection = 0;
1257     r->session = 0;
1258     r->hits = 0;
1259     r->records = 0;
1260     r->setno = 0;
1261     r->requestid = -1;
1262     r->diagnostic = 0;
1263     r->state = Client_Disconnected;
1264     r->next = 0;
1265     return r;
1266 }
1267
1268 void client_destroy(struct client *c)
1269 {
1270     struct session *se = c->session;
1271     if (c == se->clients)
1272         se->clients = c->next;
1273     else
1274     {
1275         struct client *cc;
1276         for (cc = se->clients; cc && cc->next != c; cc = cc->next)
1277             ;
1278         if (cc)
1279             cc->next = c->next;
1280     }
1281     if (c->connection)
1282         connection_release(c->connection);
1283     c->next = client_freelist;
1284     client_freelist = c;
1285 }
1286
1287 void session_set_watch(struct session *s, int what, session_watchfun fun, void *data)
1288 {
1289     s->watchlist[what].fun = fun;
1290     s->watchlist[what].data = data;
1291 }
1292
1293 void session_alert_watch(struct session *s, int what)
1294 {
1295     if (!s->watchlist[what].fun)
1296         return;
1297     (*s->watchlist[what].fun)(s->watchlist[what].data);
1298     s->watchlist[what].fun = 0;
1299     s->watchlist[what].data = 0;
1300 }
1301
1302 //callback for grep_databases
1303 static void select_targets_callback(void *context, struct database *db)
1304 {
1305     struct session *se = (struct session*) context;
1306     struct client *cl = client_create();
1307     cl->database = db;
1308     cl->session = se;
1309     cl->next = se->clients;
1310     se->clients = cl;
1311 }
1312
1313 // This should be extended with parameters to control selection criteria
1314 // Associates a set of clients with a session;
1315 int select_targets(struct session *se, struct database_criterion *crit)
1316 {
1317     while (se->clients)
1318         client_destroy(se->clients);
1319
1320     return grep_databases(se, crit, select_targets_callback);
1321 }
1322
1323 int session_active_clients(struct session *s)
1324 {
1325     struct client *c;
1326     int res = 0;
1327
1328     for (c = s->clients; c; c = c->next)
1329         if (c->connection && (c->state == Client_Connecting ||
1330                     c->state == Client_Initializing ||
1331                     c->state == Client_Searching ||
1332                     c->state == Client_Presenting))
1333             res++;
1334
1335     return res;
1336 }
1337
1338 // parses crit1=val1,crit2=val2|val3,...
1339 static struct database_criterion *parse_filter(NMEM m, const char *buf)
1340 {
1341     struct database_criterion *res = 0;
1342     char **values;
1343     int num;
1344     int i;
1345
1346     if (!buf || !*buf)
1347         return 0;
1348     nmem_strsplit(m, ",", buf,  &values, &num);
1349     for (i = 0; i < num; i++)
1350     {
1351         char **subvalues;
1352         int subnum;
1353         int subi;
1354         struct database_criterion *new = nmem_malloc(m, sizeof(*new));
1355         char *eq = strchr(values[i], '=');
1356         if (!eq)
1357         {
1358             yaz_log(YLOG_WARN, "Missing equal-sign in filter");
1359             return 0;
1360         }
1361         *(eq++) = '\0';
1362         new->name = values[i];
1363         nmem_strsplit(m, "|", eq, &subvalues, &subnum);
1364         new->values = 0;
1365         for (subi = 0; subi < subnum; subi++)
1366         {
1367             struct database_criterion_value *newv = nmem_malloc(m, sizeof(*newv));
1368             newv->value = subvalues[subi];
1369             newv->next = new->values;
1370             new->values = newv;
1371         }
1372         new->next = res;
1373         res = new;
1374     }
1375     return res;
1376 }
1377
1378 char *search(struct session *se, char *query, char *filter)
1379 {
1380     int live_channels = 0;
1381     struct client *cl;
1382     struct database_criterion *criteria;
1383
1384     yaz_log(YLOG_DEBUG, "Search");
1385
1386     nmem_reset(se->nmem);
1387     criteria = parse_filter(se->nmem, filter);
1388     strcpy(se->query, query);
1389     se->requestid++;
1390     // Release any existing clients
1391     select_targets(se, criteria);
1392     for (cl = se->clients; cl; cl = cl->next)
1393     {
1394         if (client_prep_connection(cl))
1395             live_channels++;
1396     }
1397     if (live_channels)
1398     {
1399         int maxrecs = live_channels * global_parameters.toget;
1400         se->num_termlists = 0;
1401         se->reclist = reclist_create(se->nmem, maxrecs);
1402         // This will be initialized in send_search()
1403         se->relevance = 0;
1404         se->total_records = se->total_hits = se->total_merged = 0;
1405         se->expected_maxrecs = maxrecs;
1406     }
1407     else
1408         return "NOTARGETS";
1409
1410     return 0;
1411 }
1412
1413 void destroy_session(struct session *s)
1414 {
1415     yaz_log(YLOG_LOG, "Destroying session");
1416     while (s->clients)
1417         client_destroy(s->clients);
1418     nmem_destroy(s->nmem);
1419     wrbuf_destroy(s->wrbuf);
1420 }
1421
1422 struct session *new_session() 
1423 {
1424     int i;
1425     struct session *session = xmalloc(sizeof(*session));
1426
1427     yaz_log(YLOG_DEBUG, "New pazpar2 session");
1428     
1429     session->total_hits = 0;
1430     session->total_records = 0;
1431     session->num_termlists = 0;
1432     session->reclist = 0;
1433     session->requestid = -1;
1434     session->clients = 0;
1435     session->expected_maxrecs = 0;
1436     session->query[0] = '\0';
1437     session->nmem = nmem_create();
1438     session->wrbuf = wrbuf_alloc();
1439     for (i = 0; i <= SESSION_WATCH_MAX; i++)
1440     {
1441         session->watchlist[i].data = 0;
1442         session->watchlist[i].fun = 0;
1443     }
1444
1445     return session;
1446 }
1447
1448 struct hitsbytarget *hitsbytarget(struct session *se, int *count)
1449 {
1450     static struct hitsbytarget res[1000]; // FIXME MM
1451     struct client *cl;
1452
1453     *count = 0;
1454     for (cl = se->clients; cl; cl = cl->next)
1455     {
1456         res[*count].id = cl->database->url;
1457         res[*count].name = cl->database->name;
1458         res[*count].hits = cl->hits;
1459         res[*count].records = cl->records;
1460         res[*count].diagnostic = cl->diagnostic;
1461         res[*count].state = client_states[cl->state];
1462         res[*count].connected  = cl->connection ? 1 : 0;
1463         (*count)++;
1464     }
1465
1466     return res;
1467 }
1468
1469 struct termlist_score **termlist(struct session *s, const char *name, int *num)
1470 {
1471     int i;
1472
1473     for (i = 0; i < s->num_termlists; i++)
1474         if (!strcmp((const char *) s->termlists[i].name, name))
1475             return termlist_highscore(s->termlists[i].termlist, num);
1476     return 0;
1477 }
1478
1479 #ifdef MISSING_HEADERS
1480 void report_nmem_stats(void)
1481 {
1482     size_t in_use, is_free;
1483
1484     nmem_get_memory_in_use(&in_use);
1485     nmem_get_memory_free(&is_free);
1486
1487     yaz_log(YLOG_LOG, "nmem stat: use=%ld free=%ld", 
1488             (long) in_use, (long) is_free);
1489 }
1490 #endif
1491
1492 struct record_cluster *show_single(struct session *s, int id)
1493 {
1494     struct record_cluster *r;
1495
1496     reclist_rewind(s->reclist);
1497     while ((r = reclist_read_record(s->reclist)))
1498         if (r->recid == id)
1499             return r;
1500     return 0;
1501 }
1502
1503 struct record_cluster **show(struct session *s, struct reclist_sortparms *sp, int start,
1504         int *num, int *total, int *sumhits, NMEM nmem_show)
1505 {
1506     struct record_cluster **recs = nmem_malloc(nmem_show, *num 
1507                                        * sizeof(struct record_cluster *));
1508     struct reclist_sortparms *spp;
1509     int i;
1510 #if USE_TIMING    
1511     yaz_timing_t t = yaz_timing_create();
1512 #endif
1513
1514     for (spp = sp; spp; spp = spp->next)
1515         if (spp->type == Metadata_sortkey_relevance)
1516         {
1517             relevance_prepare_read(s->relevance, s->reclist);
1518             break;
1519         }
1520     reclist_sort(s->reclist, sp);
1521
1522     *total = s->reclist->num_records;
1523     *sumhits = s->total_hits;
1524
1525     for (i = 0; i < start; i++)
1526         if (!reclist_read_record(s->reclist))
1527         {
1528             *num = 0;
1529             recs = 0;
1530             break;
1531         }
1532
1533     for (i = 0; i < *num; i++)
1534     {
1535         struct record_cluster *r = reclist_read_record(s->reclist);
1536         if (!r)
1537         {
1538             *num = i;
1539             break;
1540         }
1541         recs[i] = r;
1542     }
1543 #if USE_TIMING
1544     yaz_timing_stop(t);
1545     yaz_log(YLOG_LOG, "show %6.5f %3.2f %3.2f", 
1546             yaz_timing_get_real(t), yaz_timing_get_user(t),
1547             yaz_timing_get_sys(t));
1548     yaz_timing_destroy(&t);
1549 #endif
1550     return recs;
1551 }
1552
1553 void statistics(struct session *se, struct statistics *stat)
1554 {
1555     struct client *cl;
1556     int count = 0;
1557
1558     memset(stat, 0, sizeof(*stat));
1559     for (cl = se->clients; cl; cl = cl->next)
1560     {
1561         if (!cl->connection)
1562             stat->num_no_connection++;
1563         switch (cl->state)
1564         {
1565             case Client_Connecting: stat->num_connecting++; break;
1566             case Client_Initializing: stat->num_initializing++; break;
1567             case Client_Searching: stat->num_searching++; break;
1568             case Client_Presenting: stat->num_presenting++; break;
1569             case Client_Idle: stat->num_idle++; break;
1570             case Client_Failed: stat->num_failed++; break;
1571             case Client_Error: stat->num_error++; break;
1572             default: break;
1573         }
1574         count++;
1575     }
1576     stat->num_hits = se->total_hits;
1577     stat->num_records = se->total_records;
1578
1579     stat->num_clients = count;
1580 }
1581
1582 static void start_http_listener(void)
1583 {
1584     char hp[128] = "";
1585     struct conf_server *ser = global_parameters.server;
1586
1587     if (*global_parameters.listener_override)
1588         strcpy(hp, global_parameters.listener_override);
1589     else
1590     {
1591         strcpy(hp, ser->host ? ser->host : "");
1592         if (ser->port)
1593         {
1594             if (*hp)
1595                 strcat(hp, ":");
1596             sprintf(hp + strlen(hp), "%d", ser->port);
1597         }
1598     }
1599     http_init(hp);
1600 }
1601
1602 // Initialize CCL map for a target
1603 // Note: This approach ignores user-specific CCL maps, for which I
1604 // don't presently see any application.
1605 static void prepare_cclmap(void *context, struct database *db)
1606 {
1607     struct setting *s;
1608
1609     if (!db->settings)
1610         return;
1611     db->ccl_map = ccl_qual_mk();
1612     for (s = db->settings[PZ_CCLMAP]; s; s = s->next)
1613         if (!*s->user)
1614         {
1615             char *p = strchr(s->name + 3, ':');
1616             if (!p)
1617             {
1618                 yaz_log(YLOG_FATAL, "Malformed cclmap name: %s", s->name);
1619                 exit(1);
1620             }
1621             p++;
1622             ccl_qual_fitem(db->ccl_map, s->value, p);
1623         }
1624 }
1625
1626 // Read settings for each database, and prepare a CCL map for that database
1627 static void prepare_cclmaps(void)
1628 {
1629     grep_databases(0, 0, prepare_cclmap);
1630 }
1631
1632 static void start_proxy(void)
1633 {
1634     char hp[128] = "";
1635     struct conf_server *ser = global_parameters.server;
1636
1637     if (*global_parameters.proxy_override)
1638         strcpy(hp, global_parameters.proxy_override);
1639     else if (ser->proxy_host || ser->proxy_port)
1640     {
1641         strcpy(hp, ser->proxy_host ? ser->proxy_host : "");
1642         if (ser->proxy_port)
1643         {
1644             if (*hp)
1645                 strcat(hp, ":");
1646             sprintf(hp + strlen(hp), "%d", ser->proxy_port);
1647         }
1648     }
1649     else
1650         return;
1651
1652     http_set_proxyaddr(hp, ser->myurl ? ser->myurl : "");
1653 }
1654
1655 static void start_zproxy(void)
1656 {
1657     struct conf_server *ser = global_parameters.server;
1658
1659     if (*global_parameters.zproxy_override){
1660         yaz_log(YLOG_LOG, "Z39.50 proxy  %s", 
1661                 global_parameters.zproxy_override);
1662         return;
1663     }
1664
1665     else if (ser->zproxy_host || ser->zproxy_port)
1666     {
1667         char hp[128] = "";
1668
1669         strcpy(hp, ser->zproxy_host ? ser->zproxy_host : "");
1670         if (ser->zproxy_port)
1671         {
1672             if (*hp)
1673                 strcat(hp, ":");
1674             else
1675                 strcat(hp, "@:");
1676
1677             sprintf(hp + strlen(hp), "%d", ser->zproxy_port);
1678         }
1679         strcpy(global_parameters.zproxy_override, hp);
1680         yaz_log(YLOG_LOG, "Z39.50 proxy  %s", 
1681                 global_parameters.zproxy_override);
1682
1683     }
1684     else
1685         return;
1686 }
1687
1688
1689
1690 int main(int argc, char **argv)
1691 {
1692     int ret;
1693     char *arg;
1694
1695     if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
1696         yaz_log(YLOG_WARN|YLOG_ERRNO, "signal");
1697
1698     yaz_log_init(YLOG_DEFAULT_LEVEL, "pazpar2", 0);
1699
1700     while ((ret = options("t:f:x:h:p:z:s:d", argv, argc, &arg)) != -2)
1701     {
1702         switch (ret) {
1703             case 'f':
1704                 if (!read_config(arg))
1705                     exit(1);
1706                 break;
1707             case 'h':
1708                 strcpy(global_parameters.listener_override, arg);
1709                 break;
1710             case 'p':
1711                 strcpy(global_parameters.proxy_override, arg);
1712                 break;
1713             case 'z':
1714                 strcpy(global_parameters.zproxy_override, arg);
1715                 break;
1716             case 't':
1717                 strcpy(global_parameters.settings_path_override, arg);
1718                 break;
1719             case 's':
1720                 load_simpletargets(arg);
1721                 break;
1722             case 'd':
1723                 global_parameters.dump_records = 1;
1724                 break;
1725             default:
1726                 fprintf(stderr, "Usage: pazpar2\n"
1727                         "    -f configfile\n"
1728                         "    -h [host:]port          (REST protocol listener)\n"
1729                         "    -C cclconfig\n"
1730                         "    -s simpletargetfile\n"
1731                         "    -p hostname[:portno]    (HTTP proxy)\n"
1732                         "    -z hostname[:portno]    (Z39.50 proxy)\n"
1733                         "    -d                      (show internal records)\n");
1734                 exit(1);
1735         }
1736     }
1737
1738     if (!config)
1739     {
1740         yaz_log(YLOG_FATAL, "Load config with -f");
1741         exit(1);
1742     }
1743     global_parameters.server = config->servers;
1744
1745     start_http_listener();
1746     start_proxy();
1747     start_zproxy();
1748
1749     if (*global_parameters.settings_path_override)
1750         settings_read(global_parameters.settings_path_override);
1751     else if (global_parameters.server->settings)
1752         settings_read(global_parameters.server->settings);
1753     else
1754         yaz_log(YLOG_WARN, "No settings-directory specified. Problems may ensue!");
1755     prepare_cclmaps();
1756     global_parameters.yaz_marc = yaz_marc_create();
1757     yaz_marc_subfield_str(global_parameters.yaz_marc, "\t");
1758     global_parameters.odr_in = odr_createmem(ODR_DECODE);
1759     global_parameters.odr_out = odr_createmem(ODR_ENCODE);
1760
1761     event_loop(&channel_list);
1762
1763     return 0;
1764 }
1765
1766 /*
1767  * Local variables:
1768  * c-basic-offset: 4
1769  * indent-tabs-mode: nil
1770  * End:
1771  * vim: shiftwidth=4 tabstop=8 expandtab
1772  */