1 /* $Id: pazpar2.c,v 1.9 2007-01-04 02:35:42 quinn Exp $ */;
8 #include <sys/socket.h>
14 #include <yaz/comstack.h>
15 #include <yaz/tcpip.h>
16 #include <yaz/proto.h>
17 #include <yaz/readconf.h>
18 #include <yaz/pquery.h>
19 #include <yaz/yaz-util.h>
26 #include "termlists.h"
28 #include "relevance.h"
31 #define PAZPAR2_VERSION "0.1"
34 static void client_fatal(struct client *cl);
35 static void connection_destroy(struct connection *co);
36 static int client_prep_connection(struct client *cl);
37 static void ingest_records(struct client *cl, Z_Records *r);
38 static struct conf_retrievalprofile *database_retrieval_profile(struct database *db);
39 void session_alert_watch(struct session *s, int what);
41 IOCHAN channel_list = 0; // Master list of connections we're handling events to
43 static struct connection *connection_freelist = 0;
44 static struct client *client_freelist = 0;
46 static struct host *hosts = 0; // The hosts we know about
47 static struct database *databases = 0; // The databases we know about
49 static char *client_states[] = {
53 "Client_Initializing",
58 "Client_Disconnected",
62 struct parameters global_parameters =
67 "Index Data PazPar2 (MasterKey)",
80 static int send_apdu(struct client *c, Z_APDU *a)
82 struct connection *co = c->connection;
86 if (!z_APDU(global_parameters.odr_out, &a, 0, 0))
88 odr_perror(global_parameters.odr_out, "Encoding APDU");
91 buf = odr_getbuf(global_parameters.odr_out, &len, 0);
92 r = cs_put(co->link, buf, len);
95 yaz_log(YLOG_WARN, "cs_put: %s", cs_errmsg(cs_errno(co->link)));
100 fprintf(stderr, "cs_put incomplete (ParaZ does not handle that)\n");
103 odr_reset(global_parameters.odr_out); /* release the APDU structure */
104 co->state = Conn_Waiting;
109 static void send_init(IOCHAN i)
111 struct connection *co = iochan_getdata(i);
112 struct client *cl = co->client;
113 Z_APDU *a = zget_APDU(global_parameters.odr_out, Z_APDU_initRequest);
115 a->u.initRequest->implementationId = global_parameters.implementationId;
116 a->u.initRequest->implementationName = global_parameters.implementationName;
117 a->u.initRequest->implementationVersion =
118 global_parameters.implementationVersion;
119 ODR_MASK_SET(a->u.initRequest->options, Z_Options_search);
120 ODR_MASK_SET(a->u.initRequest->options, Z_Options_present);
121 ODR_MASK_SET(a->u.initRequest->options, Z_Options_namedResultSets);
123 ODR_MASK_SET(a->u.initRequest->protocolVersion, Z_ProtocolVersion_1);
124 ODR_MASK_SET(a->u.initRequest->protocolVersion, Z_ProtocolVersion_2);
125 ODR_MASK_SET(a->u.initRequest->protocolVersion, Z_ProtocolVersion_3);
126 if (send_apdu(cl, a) >= 0)
128 iochan_setflags(i, EVENT_INPUT);
129 cl->state = Client_Initializing;
132 cl->state = Client_Error;
133 odr_reset(global_parameters.odr_out);
136 static void send_search(IOCHAN i)
138 struct connection *co = iochan_getdata(i);
139 struct client *cl = co->client;
140 struct session *se = cl->session;
141 struct database *db = cl->database;
142 Z_APDU *a = zget_APDU(global_parameters.odr_out, Z_APDU_searchRequest);
143 int ndb, cerror, cpos;
146 struct ccl_rpn_node *cn;
147 int ssub = 0, lslb = 100000, mspn = 10;
149 yaz_log(YLOG_DEBUG, "Sending search");
151 cn = ccl_find_str(global_parameters.ccl_filter, se->query, &cerror, &cpos);
154 a->u.searchRequest->query = zquery = odr_malloc(global_parameters.odr_out,
156 zquery->which = Z_Query_type_1;
157 zquery->u.type_1 = ccl_rpn_query(global_parameters.odr_out, cn);
160 for (ndb = 0; db->databases[ndb]; ndb++)
162 databaselist = odr_malloc(global_parameters.odr_out, sizeof(char*) * ndb);
163 for (ndb = 0; db->databases[ndb]; ndb++)
164 databaselist[ndb] = db->databases[ndb];
166 a->u.presentRequest->preferredRecordSyntax =
167 yaz_oidval_to_z3950oid(global_parameters.odr_out,
168 CLASS_RECSYN, VAL_USMARC);
169 a->u.searchRequest->smallSetUpperBound = &ssub;
170 a->u.searchRequest->largeSetLowerBound = &lslb;
171 a->u.searchRequest->mediumSetPresentNumber = &mspn;
172 a->u.searchRequest->resultSetName = "Default";
173 a->u.searchRequest->databaseNames = databaselist;
174 a->u.searchRequest->num_databaseNames = ndb;
176 if (send_apdu(cl, a) >= 0)
178 iochan_setflags(i, EVENT_INPUT);
179 cl->state = Client_Searching;
180 cl->requestid = se->requestid;
183 cl->state = Client_Error;
185 odr_reset(global_parameters.odr_out);
188 static void send_present(IOCHAN i)
190 struct connection *co = iochan_getdata(i);
191 struct client *cl = co->client;
192 Z_APDU *a = zget_APDU(global_parameters.odr_out, Z_APDU_presentRequest);
194 int start = cl->records + 1;
196 toget = global_parameters.chunk;
197 if (toget > cl->hits - cl->records)
198 toget = cl->hits - cl->records;
200 yaz_log(YLOG_DEBUG, "Trying to present %d records\n", toget);
202 a->u.presentRequest->resultSetStartPoint = &start;
203 a->u.presentRequest->numberOfRecordsRequested = &toget;
205 a->u.presentRequest->resultSetId = "Default";
207 a->u.presentRequest->preferredRecordSyntax =
208 yaz_oidval_to_z3950oid(global_parameters.odr_out,
209 CLASS_RECSYN, VAL_USMARC);
211 if (send_apdu(cl, a) >= 0)
213 iochan_setflags(i, EVENT_INPUT);
214 cl->state = Client_Presenting;
217 cl->state = Client_Error;
218 odr_reset(global_parameters.odr_out);
221 static void do_initResponse(IOCHAN i, Z_APDU *a)
223 struct connection *co = iochan_getdata(i);
224 struct client *cl = co->client;
225 Z_InitResponse *r = a->u.initResponse;
227 yaz_log(YLOG_DEBUG, "Received init response");
231 cl->state = Client_Idle;
234 cl->state = Client_Failed; // FIXME need to do something to the connection
237 static void do_searchResponse(IOCHAN i, Z_APDU *a)
239 struct connection *co = iochan_getdata(i);
240 struct client *cl = co->client;
241 struct session *se = cl->session;
242 Z_SearchResponse *r = a->u.searchResponse;
244 yaz_log(YLOG_DEBUG, "Searchresponse (status=%d)", *r->searchStatus);
246 if (*r->searchStatus)
248 cl->hits = *r->resultCount;
249 se->total_hits += cl->hits;
250 if (r->presentStatus && !*r->presentStatus && r->records)
252 yaz_log(YLOG_DEBUG, "Records in search response");
253 cl->records += *r->numberOfRecordsReturned;
254 ingest_records(cl, r->records);
256 cl->state = Client_Idle;
261 cl->state = Client_Error;
263 Z_Records *recs = r->records;
264 if (recs->which == Z_Records_NSD)
266 yaz_log(YLOG_WARN, "Non-surrogate diagnostic");
267 cl->diagnostic = *recs->u.nonSurrogateDiagnostic->condition;
268 cl->state = Client_Error;
274 char *normalize_mergekey(char *buf)
276 char *p = buf, *pout = buf;
280 while (*p && !isalnum(*p))
283 *(pout++) = tolower(*(p++));
286 while (*p && !isalnum(*p))
297 // FIXME needs to be generalized. Should flexibly generate X lists per search
298 static void extract_subject(struct session *s, const char *rec)
300 const char *field, *subfield;
302 while ((field = find_field(rec, "650")))
305 if ((subfield = find_subfield(field, 'a')))
311 ef = index(subfield, '\n');
314 if ((e = index(subfield, '\t')) && e < ef)
316 while (ef > subfield && !isalpha(*(ef - 1)) && *(ef - 1) != ')')
320 memcpy(buf, subfield, len);
324 termlist_insert(s->termlist, buf);
331 static void add_facet(struct session *s, const char *type, const char *value)
335 for (i = 0; i < s->num_termlists; i++)
336 if (!strcmp(s->termlists[i].name, type))
338 if (i == s->num_termlists)
340 if (i == SESSION_MAX_TERMLISTS)
342 yaz_log(YLOG_FATAL, "Too many termlists");
345 s->termlists[i].name = nmem_strdup(s->nmem, type);
346 s->termlists[i].termlist = termlist_create(s->nmem, s->expected_maxrecs, 15);
347 s->num_termlists = i + 1;
349 termlist_insert(s->termlists[i].termlist, value);
352 static xmlDoc *normalize_record(struct client *cl, Z_External *rec)
354 struct conf_retrievalprofile *rprofile = cl->database->rprofile;
355 struct conf_retrievalmap *m;
359 // First normalize to XML
360 if (rprofile->native_syntax == Nativesyn_iso2709)
364 if (rec->which != Z_External_octet)
366 yaz_log(YLOG_WARN, "Unexpected external branch, probably BER");
369 buf = (char*) rec->u.octet_aligned->buf;
370 len = rec->u.octet_aligned->len;
371 if (yaz_marc_read_iso2709(rprofile->yaz_marc, buf, len) < 0)
373 yaz_log(YLOG_WARN, "Failed to decode MARC");
376 if (yaz_marc_write_xml(rprofile->yaz_marc, &res,
377 "http://www.loc.gov/MARC21/slim", 0, 0) < 0)
379 yaz_log(YLOG_WARN, "Failed to encode as XML");
382 rdoc = xmlNewDoc("1.0");
383 xmlDocSetRootElement(rdoc, res);
387 yaz_log(YLOG_FATAL, "Unknown native_syntax in normalize_record");
390 for (m = rprofile->maplist; m; m = m->next)
393 if (m->type != Map_xslt)
395 yaz_log(YLOG_WARN, "Unknown map type");
398 if (!(new = xsltApplyStylesheet(m->stylesheet, rdoc, 0)))
400 yaz_log(YLOG_WARN, "XSLT transformation failed");
406 if (global_parameters.dump_records)
408 fprintf(stderr, "Record:\n----------------\n");
409 xmlDocFormatDump(stderr, rdoc, 1);
414 static struct record *ingest_record(struct client *cl, Z_External *rec)
416 xmlDoc *xdoc = normalize_record(cl, rec);
418 struct record *res, *head;
419 struct session *se = cl->session;
420 xmlChar *mergekey, *mergekey_norm;
425 root = xmlDocGetRootElement(xdoc);
426 if (!(mergekey = xmlGetProp(root, "mergekey")))
428 yaz_log(YLOG_WARN, "No mergekey found in record");
432 res = nmem_malloc(se->nmem, sizeof(struct record));
433 res->next_cluster = 0;
434 res->target_offset = -1;
435 res->term_frequency_vec = 0;
436 res->title = "Unknown";
439 mergekey_norm = nmem_strdup(se->nmem, (char*) mergekey);
441 res->merge_key = normalize_mergekey(mergekey_norm);
443 head = reclist_insert(se->reclist, res);
444 relevance_newrec(se->relevance, head);
446 for (n = root->children; n; n = n->next)
448 if (n->type != XML_ELEMENT_NODE)
450 if (!strcmp(n->name, "facet"))
452 xmlChar *type = xmlGetProp(n, "type");
453 xmlChar *value = xmlNodeListGetString(xdoc, n->children, 0);
454 add_facet(se, type, value);
455 relevance_countwords(se->relevance, head, value, 1);
459 else if (!strcmp(n->name, "metadata"))
461 xmlChar *type = xmlGetProp(n, "type"), *value;
462 if (!strcmp(type, "title"))
463 res->title = nmem_strdup(se->nmem,
464 value = xmlNodeListGetString(xdoc, n->children, 0));
466 relevance_countwords(se->relevance, head, value, 4);
471 yaz_log(YLOG_WARN, "Unexpected element %s in internal record", n->name);
474 relevance_donerecord(se->relevance, head);
480 static void ingest_records(struct client *cl, Z_Records *r)
483 struct session *s = cl->session;
484 Z_NamePlusRecordList *rlist;
487 if (r->which != Z_Records_DBOSD)
489 rlist = r->u.databaseOrSurDiagnostics;
490 for (i = 0; i < rlist->num_records; i++)
492 Z_NamePlusRecord *npr = rlist->records[i];
494 if (npr->which != Z_NamePlusRecord_databaseRecord)
496 yaz_log(YLOG_WARN, "Unexpected record type, probably diagnostic");
500 rec = ingest_record(cl, npr->u.databaseRecord);
504 if (s->watchlist[SESSION_WATCH_RECORDS].fun && rlist->num_records)
505 session_alert_watch(s, SESSION_WATCH_RECORDS);
508 static void do_presentResponse(IOCHAN i, Z_APDU *a)
510 struct connection *co = iochan_getdata(i);
511 struct client *cl = co->client;
512 Z_PresentResponse *r = a->u.presentResponse;
515 Z_Records *recs = r->records;
516 if (recs->which == Z_Records_NSD)
518 yaz_log(YLOG_WARN, "Non-surrogate diagnostic");
519 cl->diagnostic = *recs->u.nonSurrogateDiagnostic->condition;
520 cl->state = Client_Error;
524 if (!*r->presentStatus && cl->state != Client_Error)
526 yaz_log(YLOG_DEBUG, "Good Present response");
527 cl->records += *r->numberOfRecordsReturned;
528 ingest_records(cl, r->records);
529 cl->state = Client_Idle;
531 else if (*r->presentStatus)
533 yaz_log(YLOG_WARN, "Bad Present response");
534 cl->state = Client_Error;
538 static void handler(IOCHAN i, int event)
540 struct connection *co = iochan_getdata(i);
541 struct client *cl = co->client;
542 struct session *se = 0;
548 yaz_log(YLOG_WARN, "Destroying orphan connection");
549 connection_destroy(co);
553 if (co->state == Conn_Connecting && event & EVENT_OUTPUT)
556 socklen_t errlen = sizeof(errcode);
558 if (getsockopt(cs_fileno(co->link), SOL_SOCKET, SO_ERROR, &errcode,
559 &errlen) < 0 || errcode != 0)
566 yaz_log(YLOG_DEBUG, "Connect OK");
567 co->state = Conn_Open;
569 cl->state = Client_Connected;
573 else if (event & EVENT_INPUT)
575 int len = cs_get(co->link, &co->ibuf, &co->ibufsize);
579 yaz_log(YLOG_WARN|YLOG_ERRNO, "Error reading from Z server");
580 connection_destroy(co);
585 yaz_log(YLOG_WARN, "EOF reading from Z server");
586 connection_destroy(co);
589 else if (len > 1) // We discard input if we have no connection
591 co->state = Conn_Open;
593 if (cl && (cl->requestid == se->requestid || cl->state == Client_Initializing))
597 odr_reset(global_parameters.odr_in);
598 odr_setbuf(global_parameters.odr_in, co->ibuf, len, 0);
599 if (!z_APDU(global_parameters.odr_in, &a, 0, 0))
606 case Z_APDU_initResponse:
607 do_initResponse(i, a);
609 case Z_APDU_searchResponse:
610 do_searchResponse(i, a);
612 case Z_APDU_presentResponse:
613 do_presentResponse(i, a);
616 yaz_log(YLOG_WARN, "Unexpected result from server");
620 // We aren't expecting staggered output from target
621 // if (cs_more(t->link))
622 // iochan_setevent(i, EVENT_INPUT);
624 else // we throw away response and go to idle mode
626 yaz_log(YLOG_DEBUG, "Ignoring result of expired operation");
627 cl->state = Client_Idle;
630 /* if len==1 we do nothing but wait for more input */
633 if (cl->state == Client_Connected) {
637 if (cl->state == Client_Idle)
639 if (cl->requestid != se->requestid && *se->query) {
642 else if (cl->hits > 0 && cl->records < global_parameters.toget &&
643 cl->records < cl->hits) {
649 // Disassociate connection from client
650 static void connection_release(struct connection *co)
652 struct client *cl = co->client;
654 yaz_log(YLOG_DEBUG, "Connection release %s", co->host->hostport);
661 // Close connection and recycle structure
662 static void connection_destroy(struct connection *co)
664 struct host *h = co->host;
666 iochan_destroy(co->iochan);
668 yaz_log(YLOG_DEBUG, "Connection destroy %s", co->host->hostport);
669 if (h->connections == co)
670 h->connections = co->next;
673 struct connection *pco;
674 for (pco = h->connections; pco && pco->next != co; pco = pco->next)
677 pco->next = co->next;
683 if (co->client->state != Client_Idle)
684 co->client->state = Client_Disconnected;
685 co->client->connection = 0;
687 co->next = connection_freelist;
688 connection_freelist = co;
691 // Creates a new connection for client, associated with the host of
693 static struct connection *connection_create(struct client *cl)
695 struct connection *new;
700 yaz_log(YLOG_DEBUG, "Connection create %s", cl->database->url);
701 if (!(link = cs_create(tcpip_type, 0, PROTO_Z3950)))
703 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Failed to create comstack");
707 if (!(addr = cs_straddr(link, cl->database->host->ipport)))
709 yaz_log(YLOG_WARN|YLOG_ERRNO, "Lookup of IP address failed?");
713 res = cs_connect(link, addr);
716 yaz_log(YLOG_WARN|YLOG_ERRNO, "cs_connect %s", cl->database->url);
720 if ((new = connection_freelist))
721 connection_freelist = new->next;
724 new = xmalloc(sizeof (struct connection));
728 new->state = Conn_Connecting;
729 new->host = cl->database->host;
730 new->next = new->host->connections;
731 new->host->connections = new;
733 cl->connection = new;
736 new->iochan = iochan_create(cs_fileno(link), handler, 0);
737 iochan_setdata(new->iochan, new);
738 new->iochan->next = channel_list;
739 channel_list = new->iochan;
743 // Close connection and set state to error
744 static void client_fatal(struct client *cl)
746 yaz_log(YLOG_WARN, "Fatal error from %s", cl->database->url);
747 connection_destroy(cl->connection);
748 cl->state = Client_Error;
751 // Ensure that client has a connection associated
752 static int client_prep_connection(struct client *cl)
754 struct connection *co;
755 struct session *se = cl->session;
756 struct host *host = cl->database->host;
760 yaz_log(YLOG_DEBUG, "Client prep %s", cl->database->url);
764 // See if someone else has an idle connection
765 // We should look at timestamps here to select the longest-idle connection
766 for (co = host->connections; co; co = co->next)
767 if (co->state == Conn_Open && (!co->client || co->client->session != se))
771 connection_release(co);
776 co = connection_create(cl);
780 if (co->state == Conn_Connecting)
782 cl->state = Client_Connecting;
783 iochan_setflag(co->iochan, EVENT_OUTPUT);
785 else if (co->state == Conn_Open)
787 if (cl->state == Client_Error || cl->state == Client_Disconnected)
788 cl->state = Client_Idle;
789 iochan_setflag(co->iochan, EVENT_OUTPUT);
797 // This function will most likely vanish when a proper target profile mechanism is
799 void load_simpletargets(const char *fn)
801 FILE *f = fopen(fn, "r");
806 yaz_log(YLOG_WARN|YLOG_ERRNO, "open %s", fn);
810 while (fgets(line, 255, f))
814 struct database *database;
816 if (strncmp(line, "target ", 7))
819 url[strlen(url) - 1] = '\0';
820 yaz_log(YLOG_DEBUG, "Target: %s", url);
821 if ((db = strchr(url, '/')))
826 for (host = hosts; host; host = host->next)
827 if (!strcmp(url, host->hostport))
831 struct addrinfo *addrinfo, hints;
834 unsigned char addrbuf[4];
837 host = xmalloc(sizeof(struct host));
838 host->hostport = xstrdup(url);
839 host->connections = 0;
841 if ((port = strchr(url, ':')))
847 hints.ai_family = PF_INET;
848 hints.ai_socktype = SOCK_STREAM;
849 hints.ai_protocol = IPPROTO_TCP;
850 hints.ai_addrlen = 0;
852 hints.ai_canonname = 0;
854 // This is not robust code. It assumes that getaddrinfo returns AF_INET
856 if ((res = getaddrinfo(url, port, &hints, &addrinfo)))
858 yaz_log(YLOG_WARN, "Failed to resolve %s: %s", url, gai_strerror(res));
859 xfree(host->hostport);
863 assert(addrinfo->ai_family == PF_INET);
864 memcpy(addrbuf, &((struct sockaddr_in*)addrinfo->ai_addr)->sin_addr.s_addr, 4);
865 sprintf(ipport, "%hhd.%hhd.%hhd.%hhd:%s",
866 addrbuf[0], addrbuf[1], addrbuf[2], addrbuf[3], port);
867 host->ipport = xstrdup(ipport);
868 freeaddrinfo(addrinfo);
872 database = xmalloc(sizeof(struct database));
873 database->host = host;
874 database->url = xmalloc(strlen(url) + strlen(db) + 2);
875 strcpy(database->url, url);
876 strcat(database->url, "/");
877 strcat(database->url, db);
879 database->databases = xmalloc(2 * sizeof(char *));
880 database->databases[0] = xstrdup(db);
881 database->databases[1] = 0;
882 database->errors = 0;
883 database->qprofile = 0;
884 database->rprofile = database_retrieval_profile(database);
885 database->next = databases;
886 databases = database;
892 static void pull_terms(NMEM nmem, struct ccl_rpn_node *n, char **termlist, int *num)
900 pull_terms(nmem, n->u.p[0], termlist, num);
901 pull_terms(nmem, n->u.p[1], termlist, num);
904 termlist[(*num)++] = nmem_strdup(nmem, n->u.t.term);
911 // Extract terms from query into null-terminated termlist
912 static int extract_terms(NMEM nmem, char *query, char **termlist)
915 struct ccl_rpn_node *n;
918 n = ccl_find_str(global_parameters.ccl_filter, query, &error, &pos);
921 pull_terms(nmem, n, termlist, &num);
927 static struct client *client_create(void)
933 client_freelist = client_freelist->next;
936 r = xmalloc(sizeof(struct client));
945 r->state = Client_Disconnected;
950 void client_destroy(struct client *c)
952 struct session *se = c->session;
953 if (c == se->clients)
954 se->clients = c->next;
958 for (cc = se->clients; cc && cc->next != c; cc = cc->next)
964 connection_release(c->connection);
965 c->next = client_freelist;
969 void session_set_watch(struct session *s, int what, session_watchfun fun, void *data)
971 s->watchlist[what].fun = fun;
972 s->watchlist[what].data = data;
975 void session_alert_watch(struct session *s, int what)
977 if (!s->watchlist[what].fun)
979 (*s->watchlist[what].fun)(s->watchlist[what].data);
980 s->watchlist[what].fun = 0;
981 s->watchlist[what].data = 0;
984 // This needs to be extended with selection criteria
985 static struct conf_retrievalprofile *database_retrieval_profile(struct database *db)
989 yaz_log(YLOG_FATAL, "Must load configuration (-f)");
992 if (!config->retrievalprofiles)
994 yaz_log(YLOG_FATAL, "No retrieval profiles defined");
996 return config->retrievalprofiles;
999 // This should be extended with parameters to control selection criteria
1000 // Associates a set of clients with a session;
1001 int select_targets(struct session *se)
1003 struct database *db;
1007 client_destroy(se->clients);
1008 for (db = databases; db; db = db->next)
1010 struct client *cl = client_create();
1013 cl->next = se->clients;
1020 int session_active_clients(struct session *s)
1025 for (c = s->clients; c; c = c->next)
1026 if (c->connection && (c->state == Client_Connecting ||
1027 c->state == Client_Initializing ||
1028 c->state == Client_Searching ||
1029 c->state == Client_Presenting))
1035 char *search(struct session *se, char *query)
1037 int live_channels = 0;
1040 yaz_log(YLOG_DEBUG, "Search");
1042 strcpy(se->query, query);
1044 nmem_reset(se->nmem);
1045 for (cl = se->clients; cl; cl = cl->next)
1051 if (client_prep_connection(cl))
1057 int maxrecs = live_channels * global_parameters.toget;
1058 se->num_termlists = 0;
1059 se->reclist = reclist_create(se->nmem, maxrecs);
1060 extract_terms(se->nmem, query, p);
1061 se->relevance = relevance_create(se->nmem, (const char **) p, maxrecs);
1062 se->total_records = se->total_hits = 0;
1063 se->expected_maxrecs = maxrecs;
1071 void destroy_session(struct session *s)
1073 yaz_log(YLOG_LOG, "Destroying session");
1075 client_destroy(s->clients);
1076 nmem_destroy(s->nmem);
1077 wrbuf_free(s->wrbuf, 1);
1080 struct session *new_session()
1083 struct session *session = xmalloc(sizeof(*session));
1085 yaz_log(YLOG_DEBUG, "New pazpar2 session");
1087 session->total_hits = 0;
1088 session->total_records = 0;
1089 session->num_termlists = 0;
1090 session->reclist = 0;
1091 session->requestid = -1;
1092 session->clients = 0;
1093 session->expected_maxrecs = 0;
1094 session->query[0] = '\0';
1095 session->nmem = nmem_create();
1096 session->wrbuf = wrbuf_alloc();
1097 for (i = 0; i <= SESSION_WATCH_MAX; i++)
1099 session->watchlist[i].data = 0;
1100 session->watchlist[i].fun = 0;
1103 select_targets(session);
1108 struct hitsbytarget *hitsbytarget(struct session *se, int *count)
1110 static struct hitsbytarget res[1000]; // FIXME MM
1114 for (cl = se->clients; cl; cl = cl->next)
1116 strcpy(res[*count].id, cl->database->host->hostport);
1117 res[*count].hits = cl->hits;
1118 res[*count].records = cl->records;
1119 res[*count].diagnostic = cl->diagnostic;
1120 res[*count].state = client_states[cl->state];
1121 res[*count].connected = cl->connection ? 1 : 0;
1128 struct termlist_score **termlist(struct session *s, const char *name, int *num)
1132 for (i = 0; i < s->num_termlists; i++)
1133 if (!strcmp(s->termlists[i].name, name))
1134 return termlist_highscore(s->termlists[i].termlist, num);
1139 // conditional compilation by SH: This lead to a warning with currently installed
1140 // YAZ header files on us1
1141 void report_nmem_stats(void)
1143 size_t in_use, is_free;
1145 nmem_get_memory_in_use(&in_use);
1146 nmem_get_memory_free(&is_free);
1148 yaz_log(YLOG_LOG, "nmem stat: use=%ld free=%ld",
1149 (long) in_use, (long) is_free);
1153 struct record **show(struct session *s, int start, int *num, int *total,
1154 int *sumhits, NMEM nmem_show)
1156 struct record **recs = nmem_malloc(nmem_show, *num
1157 * sizeof(struct record *));
1160 relevance_prepare_read(s->relevance, s->reclist);
1162 *total = s->reclist->num_records;
1163 *sumhits = s->total_hits;
1165 for (i = 0; i < start; i++)
1166 if (!reclist_read_record(s->reclist))
1172 for (i = 0; i < *num; i++)
1174 struct record *r = reclist_read_record(s->reclist);
1185 void statistics(struct session *se, struct statistics *stat)
1190 bzero(stat, sizeof(*stat));
1191 for (cl = se->clients; cl; cl = cl->next)
1193 if (!cl->connection)
1194 stat->num_no_connection++;
1197 case Client_Connecting: stat->num_connecting++; break;
1198 case Client_Initializing: stat->num_initializing++; break;
1199 case Client_Searching: stat->num_searching++; break;
1200 case Client_Presenting: stat->num_presenting++; break;
1201 case Client_Idle: stat->num_idle++; break;
1202 case Client_Failed: stat->num_failed++; break;
1203 case Client_Error: stat->num_error++; break;
1208 stat->num_hits = se->total_hits;
1209 stat->num_records = se->total_records;
1211 stat->num_clients = count;
1214 static CCL_bibset load_cclfile(const char *fn)
1216 CCL_bibset res = ccl_qual_mk();
1217 if (ccl_qual_fname(res, fn) < 0)
1219 yaz_log(YLOG_FATAL|YLOG_ERRNO, "%s", fn);
1225 int main(int argc, char **argv)
1231 if (signal(SIGPIPE, SIG_IGN) < 0)
1232 yaz_log(YLOG_WARN|YLOG_ERRNO, "signal");
1234 yaz_log_init(YLOG_DEFAULT_LEVEL, "pazpar2", 0);
1236 while ((ret = options("f:x:c:h:p:C:s:d", argv, argc, &arg)) != -2)
1240 if (!read_config(arg))
1244 command_init(atoi(arg));
1252 global_parameters.ccl_filter = load_cclfile(arg);
1255 http_set_proxyaddr(arg);
1258 load_simpletargets(arg);
1261 global_parameters.dump_records = 1;
1264 fprintf(stderr, "Usage: pazpar2\n"
1266 " -h [host:]port (REST protocol listener)\n"
1267 " -c cmdport (telnet-style)\n"
1269 " -s simpletargetfile\n"
1270 " -p hostname[:portno] (HTTP proxy)\n");
1277 fprintf(stderr, "Set command port with -h or -c\n");
1281 global_parameters.ccl_filter = load_cclfile("../etc/default.bib");
1282 global_parameters.yaz_marc = yaz_marc_create();
1283 yaz_marc_subfield_str(global_parameters.yaz_marc, "\t");
1284 global_parameters.odr_in = odr_createmem(ODR_DECODE);
1285 global_parameters.odr_out = odr_createmem(ODR_ENCODE);
1287 event_loop(&channel_list);
1295 * indent-tabs-mode: nil
1297 * vim: shiftwidth=4 tabstop=8 expandtab