1 /* $Id: pazpar2.c,v 1.8 2006-12-03 06:43:24 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>
21 #include <yaz/yaz-ccl.h>
27 #include "termlists.h"
29 #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);
38 IOCHAN channel_list = 0; // Master list of connections we're listening to.
40 static struct connection *connection_freelist = 0;
41 static struct client *client_freelist = 0;
43 static struct host *hosts = 0; // The hosts we know about
44 static struct database *databases = 0; // The databases we know about
46 static char *client_states[] = {
50 "Client_Initializing",
55 "Client_Disconnected",
59 static struct parameters {
60 int timeout; /* operations timeout, in seconds */
61 char implementationId[128];
62 char implementationName[128];
63 char implementationVersion[128];
64 int target_timeout; // seconds
67 CCL_bibset ccl_filter;
75 "Index Data PazPar2 (MasterKey)",
87 static int send_apdu(struct client *c, Z_APDU *a)
89 struct connection *co = c->connection;
93 if (!z_APDU(global_parameters.odr_out, &a, 0, 0))
95 odr_perror(global_parameters.odr_out, "Encoding APDU");
98 buf = odr_getbuf(global_parameters.odr_out, &len, 0);
99 r = cs_put(co->link, buf, len);
102 yaz_log(YLOG_WARN, "cs_put: %s", cs_errmsg(cs_errno(co->link)));
107 fprintf(stderr, "cs_put incomplete (ParaZ does not handle that)\n");
110 odr_reset(global_parameters.odr_out); /* release the APDU structure */
111 co->state = Conn_Waiting;
116 static void send_init(IOCHAN i)
118 struct connection *co = iochan_getdata(i);
119 struct client *cl = co->client;
120 Z_APDU *a = zget_APDU(global_parameters.odr_out, Z_APDU_initRequest);
122 a->u.initRequest->implementationId = global_parameters.implementationId;
123 a->u.initRequest->implementationName = global_parameters.implementationName;
124 a->u.initRequest->implementationVersion =
125 global_parameters.implementationVersion;
126 ODR_MASK_SET(a->u.initRequest->options, Z_Options_search);
127 ODR_MASK_SET(a->u.initRequest->options, Z_Options_present);
128 ODR_MASK_SET(a->u.initRequest->options, Z_Options_namedResultSets);
130 ODR_MASK_SET(a->u.initRequest->protocolVersion, Z_ProtocolVersion_1);
131 ODR_MASK_SET(a->u.initRequest->protocolVersion, Z_ProtocolVersion_2);
132 ODR_MASK_SET(a->u.initRequest->protocolVersion, Z_ProtocolVersion_3);
133 if (send_apdu(cl, a) >= 0)
135 iochan_setflags(i, EVENT_INPUT);
136 cl->state = Client_Initializing;
139 cl->state = Client_Error;
140 odr_reset(global_parameters.odr_out);
143 static void send_search(IOCHAN i)
145 struct connection *co = iochan_getdata(i);
146 struct client *cl = co->client;
147 struct session *se = cl->session;
148 struct database *db = cl->database;
149 Z_APDU *a = zget_APDU(global_parameters.odr_out, Z_APDU_searchRequest);
150 int ndb, cerror, cpos;
153 struct ccl_rpn_node *cn;
155 yaz_log(YLOG_DEBUG, "Sending search");
157 cn = ccl_find_str(global_parameters.ccl_filter, se->query, &cerror, &cpos);
160 a->u.searchRequest->query = zquery = odr_malloc(global_parameters.odr_out,
162 zquery->which = Z_Query_type_1;
163 zquery->u.type_1 = ccl_rpn_query(global_parameters.odr_out, cn);
166 for (ndb = 0; *db->databases[ndb]; ndb++)
168 databaselist = odr_malloc(global_parameters.odr_out, sizeof(char*) * ndb);
169 for (ndb = 0; *db->databases[ndb]; ndb++)
170 databaselist[ndb] = db->databases[ndb];
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 cl->state = Client_Idle;
250 se->total_hits += cl->hits;
255 cl->state = Client_Error;
257 Z_Records *recs = r->records;
258 if (recs->which == Z_Records_NSD)
260 yaz_log(YLOG_WARN, "Non-surrogate diagnostic");
261 cl->diagnostic = *recs->u.nonSurrogateDiagnostic->condition;
262 cl->state = Client_Error;
268 const char *find_field(const char *rec, const char *field)
274 strcpy(lbuf + 1, field);
276 if ((line = strstr(rec, lbuf)))
282 const char *find_subfield(const char *field, char subfield)
284 const char *p = field;
286 while (*p && *p != '\n')
288 while (*p != '\n' && *p != '\t')
290 if (*p == '\t' && *(++p) == subfield) {
302 // Extract 245 $a $b 100 $a
303 char *extract_title(struct session *s, const char *rec)
305 const char *field, *subfield;
307 unsigned char *obuf, *p;
309 wrbuf_rewind(s->wrbuf);
311 if (!(field = find_field(rec, "245")))
313 if (!(subfield = find_subfield(field, 'a')))
315 ef = index(subfield, '\n');
316 if ((e = index(subfield, '\t')) && e < ef)
320 wrbuf_write(s->wrbuf, subfield, ef - subfield);
321 if ((subfield = find_subfield(field, 'b')))
323 ef = index(subfield, '\n');
324 if ((e = index(subfield, '\t')) && e < ef)
328 wrbuf_putc(s->wrbuf, ' ');
329 wrbuf_write(s->wrbuf, subfield, ef - subfield);
333 if ((field = find_field(rec, "100")))
335 if ((subfield = find_subfield(field, 'a')))
337 ef = index(subfield, '\n');
338 if ((e = index(subfield, '\t')) && e < ef)
342 wrbuf_puts(s->wrbuf, ", by ");
343 wrbuf_write(s->wrbuf, subfield, ef - subfield);
347 wrbuf_putc(s->wrbuf, '\0');
348 obuf = nmem_strdup(s->nmem, wrbuf_buf(s->wrbuf));
349 for (p = obuf; *p; p++)
350 if (*p == '&' || *p == '<' || *p > 122 || *p < ' ')
355 // Extract 245 $a $b 100 $a
356 char *extract_mergekey(struct session *s, const char *rec)
358 const char *field, *subfield;
360 char *out, *p, *pout;
362 wrbuf_rewind(s->wrbuf);
364 if (!(field = find_field(rec, "245")))
366 if (!(subfield = find_subfield(field, 'a')))
368 ef = index(subfield, '\n');
369 if ((e = index(subfield, '\t')) && e < ef)
373 wrbuf_write(s->wrbuf, subfield, ef - subfield);
374 if ((subfield = find_subfield(field, 'b')))
376 ef = index(subfield, '\n');
377 if ((e = index(subfield, '\t')) && e < ef)
381 wrbuf_puts(s->wrbuf, " field ");
382 wrbuf_write(s->wrbuf, subfield, ef - subfield);
386 if ((field = find_field(rec, "100")))
388 if ((subfield = find_subfield(field, 'a')))
390 ef = index(subfield, '\n');
391 if ((e = index(subfield, '\t')) && e < ef)
395 wrbuf_puts(s->wrbuf, " field ");
396 wrbuf_write(s->wrbuf, subfield, ef - subfield);
400 wrbuf_putc(s->wrbuf, '\0');
401 p = wrbuf_buf(s->wrbuf);
402 out = pout = nmem_malloc(s->nmem, strlen(p) + 1);
407 *(pout++) = tolower(*(p++));
408 while (*p && !isalnum(*p))
419 static void push_record(struct session *s, struct record *r)
422 assert(s->recheap_max + 1 < s->recheap_size);
424 s->recheap[p = ++s->recheap_max] = r;
427 int parent = (p - 1) >> 1;
428 if (strcmp(s->recheap[p]->merge_key, s->recheap[parent]->merge_key) < 0)
431 tmp = s->recheap[parent];
432 s->recheap[parent] = s->recheap[p];
441 static struct record *top_record(struct session *s)
443 return s-> recheap_max >= 0 ? s->recheap[0] : 0;
446 static struct record *pop_record(struct session *s)
450 int lastnonleaf = (s->recheap_max - 1) >> 1;
452 if (s->recheap_max < 0)
457 s->recheap[p] = s->recheap[s->recheap_max--];
459 while (p <= lastnonleaf)
461 int right = (p + 1) << 1;
462 int left = right - 1;
465 if (right < s->recheap_max &&
466 strcmp(s->recheap[right]->merge_key, s->recheap[left]->merge_key) < 0)
468 if (strcmp(s->recheap[min]->merge_key, s->recheap[p]->merge_key) < 0)
470 struct record *tmp = s->recheap[min];
471 s->recheap[min] = s->recheap[p];
481 // Like pop_record but collapses identical (merge_key) records
482 // The heap will contain multiple independent matching records and possibly
483 // one cluster, created the last time the list was scanned
484 static struct record *pop_mrecord(struct session *s)
489 if (!(this = pop_record(s)))
492 // Collapse identical records
493 while ((next = top_record(s)))
495 struct record *p, *tmpnext;
496 if (strcmp(this->merge_key, next->merge_key))
498 // Absorb record (and clustersiblings) into a supercluster
499 for (p = next; p; p = tmpnext) {
500 tmpnext = p->next_cluster;
501 p->next_cluster = this->next_cluster;
502 this->next_cluster = p;
510 // Reads records in sort order. Store records in top of heapspace until rewind is called.
511 static struct record *read_recheap(struct session *s)
513 struct record *r = pop_mrecord(s);
517 if (s->recheap_scratch < 0)
518 s->recheap_scratch = s->recheap_size;
519 s->recheap[--s->recheap_scratch] = r;
525 // Return records to heap after read
526 static void rewind_recheap(struct session *s)
528 while (s->recheap_scratch >= 0) {
529 push_record(s, s->recheap[s->recheap_scratch++]);
530 if (s->recheap_scratch >= s->recheap_size)
531 s->recheap_scratch = -1;
537 // FIXME needs to be generalized. Should flexibly generate X lists per search
538 static void extract_subject(struct session *s, const char *rec)
540 const char *field, *subfield;
542 while ((field = find_field(rec, "650")))
545 if ((subfield = find_subfield(field, 'a')))
551 ef = index(subfield, '\n');
554 if ((e = index(subfield, '\t')) && e < ef)
556 while (ef > subfield && !isalpha(*(ef - 1)) && *(ef - 1) != ')')
560 memcpy(buf, subfield, len);
563 termlist_insert(s->termlist, buf);
568 static void pull_relevance_field(struct session *s, struct record *head, const char *rec,
569 char *field, int mult)
572 while ((fb = find_field(rec, field)))
574 char *ffield = strchr(fb, '\t');
577 char *eol = strchr(ffield, '\n');
580 relevance_countwords(s->relevance, head, ffield, eol - ffield, mult);
581 rec = field + 1; // Crude way to cause a loop through repeating fields
585 static void pull_relevance_keys(struct session *s, struct record *head, struct record *rec)
587 relevance_newrec(s->relevance, head);
588 pull_relevance_field(s, head, rec->buf, "100", 2);
589 pull_relevance_field(s, head, rec->buf, "245", 4);
590 //pull_relevance_field(s, head, rec->buf, "530", 1);
591 pull_relevance_field(s, head, rec->buf, "630", 1);
592 pull_relevance_field(s, head, rec->buf, "650", 1);
593 pull_relevance_field(s, head, rec->buf, "700", 1);
594 relevance_donerecord(s->relevance, head);
597 struct record *ingest_record(struct client *cl, char *buf, int len)
599 struct session *se = cl->session;
604 wrbuf_rewind(se->wrbuf);
605 yaz_marc_xml(global_parameters.yaz_marc, YAZ_MARC_LINE);
606 if (yaz_marc_decode_wrbuf(global_parameters.yaz_marc, buf, len, se->wrbuf) < 0)
608 yaz_log(YLOG_WARN, "Failed to decode MARC record");
611 wrbuf_putc(se->wrbuf, '\0');
612 recbuf = wrbuf_buf(se->wrbuf);
614 res = nmem_malloc(se->nmem, sizeof(struct record));
615 res->buf = nmem_strdup(se->nmem, recbuf);
617 extract_subject(se, res->buf);
619 res->title = extract_title(se, res->buf);
620 res->merge_key = extract_mergekey(se, res->buf);
624 res->next_cluster = 0;
625 res->target_offset = -1;
626 res->term_frequency_vec = 0;
628 head = reclist_insert(se->reclist, res);
630 pull_relevance_keys(se, head, res);
637 void ingest_records(struct client *cl, Z_Records *r)
640 Z_NamePlusRecordList *rlist;
643 if (r->which != Z_Records_DBOSD)
645 rlist = r->u.databaseOrSurDiagnostics;
646 for (i = 0; i < rlist->num_records; i++)
648 Z_NamePlusRecord *npr = rlist->records[i];
653 if (npr->which != Z_NamePlusRecord_databaseRecord)
655 yaz_log(YLOG_WARN, "Unexpected record type, probably diagnostic");
658 e = npr->u.databaseRecord;
659 if (e->which != Z_External_octet)
661 yaz_log(YLOG_WARN, "Unexpected external branch, probably BER");
664 buf = (char*) e->u.octet_aligned->buf;
665 len = e->u.octet_aligned->len;
667 rec = ingest_record(cl, buf, len);
673 static void do_presentResponse(IOCHAN i, Z_APDU *a)
675 struct connection *co = iochan_getdata(i);
676 struct client *cl = co->client;
677 Z_PresentResponse *r = a->u.presentResponse;
680 Z_Records *recs = r->records;
681 if (recs->which == Z_Records_NSD)
683 yaz_log(YLOG_WARN, "Non-surrogate diagnostic");
684 cl->diagnostic = *recs->u.nonSurrogateDiagnostic->condition;
685 cl->state = Client_Error;
689 if (!*r->presentStatus && cl->state != Client_Error)
691 yaz_log(YLOG_DEBUG, "Good Present response");
692 cl->records += *r->numberOfRecordsReturned;
693 ingest_records(cl, r->records);
694 cl->state = Client_Idle;
696 else if (*r->presentStatus)
698 yaz_log(YLOG_WARN, "Bad Present response");
699 cl->state = Client_Error;
703 static void handler(IOCHAN i, int event)
705 struct connection *co = iochan_getdata(i);
706 struct client *cl = co->client;
707 struct session *se = 0;
712 if (co->state == Conn_Connecting && event & EVENT_OUTPUT)
715 socklen_t errlen = sizeof(errcode);
717 if (getsockopt(cs_fileno(co->link), SOL_SOCKET, SO_ERROR, &errcode,
718 &errlen) < 0 || errcode != 0)
725 yaz_log(YLOG_DEBUG, "Connect OK");
726 co->state = Conn_Open;
728 cl->state = Client_Connected;
732 else if (event & EVENT_INPUT)
734 int len = cs_get(co->link, &co->ibuf, &co->ibufsize);
746 else if (len > 1) // We discard input if we have no connection
748 co->state = Conn_Open;
750 if (cl && (cl->requestid == se->requestid || cl->state == Client_Initializing))
754 odr_reset(global_parameters.odr_in);
755 odr_setbuf(global_parameters.odr_in, co->ibuf, len, 0);
756 if (!z_APDU(global_parameters.odr_in, &a, 0, 0))
763 case Z_APDU_initResponse:
764 do_initResponse(i, a);
766 case Z_APDU_searchResponse:
767 do_searchResponse(i, a);
769 case Z_APDU_presentResponse:
770 do_presentResponse(i, a);
773 yaz_log(YLOG_WARN, "Unexpected result from server");
777 // We aren't expecting staggered output from target
778 // if (cs_more(t->link))
779 // iochan_setevent(i, EVENT_INPUT);
781 else // we throw away response and go to idle mode
783 yaz_log(YLOG_DEBUG, "Ignoring result of expired operation");
784 cl->state = Client_Idle;
787 /* if len==1 we do nothing but wait for more input */
790 if (cl->state == Client_Connected) {
794 if (cl->state == Client_Idle)
796 if (cl->requestid != se->requestid && *se->query) {
799 else if (cl->hits > 0 && cl->records < global_parameters.toget &&
800 cl->records < cl->hits) {
806 // Disassociate connection from client
807 static void connection_release(struct connection *co)
809 struct client *cl = co->client;
811 yaz_log(YLOG_DEBUG, "Connection release %s", co->host->hostport);
818 // Close connection and recycle structure
819 static void connection_destroy(struct connection *co)
821 struct host *h = co->host;
823 iochan_destroy(co->iochan);
825 yaz_log(YLOG_DEBUG, "Connection destroy %s", co->host->hostport);
826 if (h->connections == co)
827 h->connections = co->next;
830 struct connection *pco;
831 for (pco = h->connections; pco && pco->next != co; pco = pco->next)
834 pco->next = co->next;
840 if (co->client->state != Client_Idle)
841 co->client->state = Client_Disconnected;
842 co->client->connection = 0;
844 co->next = connection_freelist;
845 connection_freelist = co;
848 // Creates a new connection for client, associated with the host of
850 static struct connection *connection_create(struct client *cl)
852 struct connection *new;
857 yaz_log(YLOG_DEBUG, "Connection create %s", cl->database->url);
858 if (!(link = cs_create(tcpip_type, 0, PROTO_Z3950)))
860 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Failed to create comstack");
864 if (!(addr = cs_straddr(link, cl->database->host->ipport)))
866 yaz_log(YLOG_WARN|YLOG_ERRNO, "Lookup of IP address failed?");
870 res = cs_connect(link, addr);
873 yaz_log(YLOG_WARN|YLOG_ERRNO, "cs_connect %s", cl->database->url);
877 if ((new = connection_freelist))
878 connection_freelist = new->next;
881 new = xmalloc(sizeof (struct connection));
885 new->state = Conn_Connecting;
886 new->host = cl->database->host;
887 new->next = new->host->connections;
888 new->host->connections = new;
890 cl->connection = new;
893 new->iochan = iochan_create(cs_fileno(link), handler, 0);
894 iochan_setdata(new->iochan, new);
895 new->iochan->next = channel_list;
896 channel_list = new->iochan;
900 // Close connection and set state to error
901 static void client_fatal(struct client *cl)
903 yaz_log(YLOG_WARN, "Fatal error from %s", cl->database->url);
904 connection_destroy(cl->connection);
905 cl->state = Client_Error;
908 // Ensure that client has a connection associated
909 static int client_prep_connection(struct client *cl)
911 struct connection *co;
912 struct session *se = cl->session;
913 struct host *host = cl->database->host;
917 yaz_log(YLOG_DEBUG, "Client prep %s", cl->database->url);
921 // See if someone else has an idle connection
922 // We should look at timestamps here to select the longest-idle connection
923 for (co = host->connections; co; co = co->next)
924 if (co->state == Conn_Open && (!co->client || co->client->session != se))
928 connection_release(co);
933 co = connection_create(cl);
937 if (co->state == Conn_Connecting)
938 cl->state = Client_Connecting;
939 else if (co->state == Conn_Open)
941 if (cl->state == Client_Error || cl->state == Client_Disconnected)
942 cl->state = Client_Idle;
944 iochan_setflag(co->iochan, EVENT_OUTPUT);
951 void load_simpletargets(const char *fn)
953 FILE *f = fopen(fn, "r");
958 yaz_log(YLOG_WARN|YLOG_ERRNO, "open %s", fn);
962 while (fgets(line, 255, f))
966 struct database *database;
968 if (strncmp(line, "target ", 7))
971 url[strlen(url) - 1] = '\0';
972 yaz_log(LOG_DEBUG, "Target: %s", url);
973 if ((db = strchr(url, '/')))
978 for (host = hosts; host; host = host->next)
979 if (!strcmp(url, host->hostport))
983 struct addrinfo *addrinfo, hints;
986 unsigned char addrbuf[4];
989 host = xmalloc(sizeof(struct host));
990 host->hostport = xstrdup(url);
991 host->connections = 0;
993 if ((port = strchr(url, ':')))
999 hints.ai_family = PF_INET;
1000 hints.ai_socktype = SOCK_STREAM;
1001 hints.ai_protocol = IPPROTO_TCP;
1002 hints.ai_addrlen = 0;
1004 hints.ai_canonname = 0;
1006 // This is not robust code. It assumes that getaddrinfo returns AF_INET
1008 if ((res = getaddrinfo(url, port, &hints, &addrinfo)))
1010 yaz_log(YLOG_WARN, "Failed to resolve %s: %s", url, gai_strerror(res));
1013 assert(addrinfo->ai_family == PF_INET);
1014 memcpy(addrbuf, &((struct sockaddr_in*)addrinfo->ai_addr)->sin_addr.s_addr, 4);
1015 sprintf(ipport, "%hhd.%hhd.%hhd.%hhd:%s",
1016 addrbuf[0], addrbuf[1], addrbuf[2], addrbuf[3], port);
1017 host->ipport = xstrdup(ipport);
1018 freeaddrinfo(addrinfo);
1022 database = xmalloc(sizeof(struct database));
1023 database->host = host;
1024 database->url = xmalloc(strlen(url) + strlen(db) + 2);
1025 strcpy(database->url, url);
1026 strcat(database->url, "/");
1027 strcat(database->url, db);
1028 strcpy(database->databases[0], db);
1029 *database->databases[1] = '\0';
1030 database->errors = 0;
1031 database->next = databases;
1032 databases = database;
1038 static void pull_terms(NMEM nmem, struct ccl_rpn_node *n, char **termlist, int *num)
1046 pull_terms(nmem, n->u.p[0], termlist, num);
1047 pull_terms(nmem, n->u.p[1], termlist, num);
1050 termlist[(*num)++] = nmem_strdup(nmem, n->u.t.term);
1057 // Extract terms from query into null-terminated termlist
1058 static int extract_terms(NMEM nmem, char *query, char **termlist)
1061 struct ccl_rpn_node *n;
1064 n = ccl_find_str(global_parameters.ccl_filter, query, &error, &pos);
1067 pull_terms(nmem, n, termlist, &num);
1073 static struct client *client_create(void)
1076 if (client_freelist)
1078 r = client_freelist;
1079 client_freelist = client_freelist->next;
1082 r = xmalloc(sizeof(struct client));
1091 r->state = Client_Disconnected;
1096 void client_destroy(struct client *c)
1098 struct session *se = c->session;
1099 if (c == se->clients)
1100 se->clients = c->next;
1104 for (cc = se->clients; cc && cc->next != c; cc = cc->next)
1110 connection_destroy(c->connection);
1111 c->next = client_freelist;
1112 client_freelist = c;
1115 // This should be extended with parameters to control selection criteria
1116 // Associates a set of clients with a session;
1117 int select_targets(struct session *se)
1119 struct database *db;
1123 client_destroy(se->clients);
1124 for (db = databases; db; db = db->next)
1126 struct client *cl = client_create();
1129 cl->next = se->clients;
1136 char *search(struct session *se, char *query)
1138 int live_channels = 0;
1141 yaz_log(YLOG_DEBUG, "Search");
1143 strcpy(se->query, query);
1145 nmem_reset(se->nmem);
1146 for (cl = se->clients; cl; cl = cl->next)
1152 if (client_prep_connection(cl))
1158 int maxrecs = live_channels * global_parameters.toget;
1159 se->termlist = termlist_create(se->nmem, maxrecs, 15);
1160 se->reclist = reclist_create(se->nmem, maxrecs);
1161 extract_terms(se->nmem, query, p);
1162 se->relevance = relevance_create(se->nmem, (const char **) p, maxrecs);
1163 se->total_records = se->total_hits = 0;
1171 struct session *new_session()
1173 struct session *session = xmalloc(sizeof(*session));
1175 yaz_log(YLOG_DEBUG, "New pazpar2 session");
1177 session->total_hits = 0;
1178 session->total_records = 0;
1179 session->termlist = 0;
1180 session->reclist = 0;
1181 session->requestid = -1;
1182 session->clients = 0;
1183 session->query[0] = '\0';
1184 session->nmem = nmem_create();
1185 session->wrbuf = wrbuf_alloc();
1187 select_targets(session);
1192 void session_destroy(struct session *s)
1194 // FIXME do some shit here!!!!
1197 struct hitsbytarget *hitsbytarget(struct session *se, int *count)
1199 static struct hitsbytarget res[1000]; // FIXME MM
1203 for (cl = se->clients; cl; cl = cl->next)
1205 strcpy(res[*count].id, cl->database->host->hostport);
1206 res[*count].hits = cl->hits;
1207 res[*count].records = cl->records;
1208 res[*count].diagnostic = cl->diagnostic;
1209 res[*count].state = client_states[cl->state];
1210 res[*count].connected = cl->connection ? 1 : 0;
1217 struct termlist_score **termlist(struct session *s, int *num)
1219 return termlist_highscore(s->termlist, num);
1222 struct record **show(struct session *s, int start, int *num, int *total, int *sumhits)
1224 struct record **recs = nmem_malloc(s->nmem, *num * sizeof(struct record *));
1227 relevance_prepare_read(s->relevance, s->reclist);
1229 *total = s->reclist->num_records;
1230 *sumhits = s->total_hits;
1232 for (i = 0; i < start; i++)
1233 if (!reclist_read_record(s->reclist))
1239 for (i = 0; i < *num; i++)
1241 struct record *r = reclist_read_record(s->reclist);
1252 void statistics(struct session *se, struct statistics *stat)
1257 bzero(stat, sizeof(*stat));
1258 for (cl = se->clients; cl; cl = cl->next)
1260 if (!cl->connection)
1261 stat->num_no_connection++;
1264 case Client_Connecting: stat->num_connecting++; break;
1265 case Client_Initializing: stat->num_initializing++; break;
1266 case Client_Searching: stat->num_searching++; break;
1267 case Client_Presenting: stat->num_presenting++; break;
1268 case Client_Idle: stat->num_idle++; break;
1269 case Client_Failed: stat->num_failed++; break;
1270 case Client_Error: stat->num_error++; break;
1275 stat->num_hits = se->total_hits;
1276 stat->num_records = se->total_records;
1278 stat->num_clients = count;
1281 static CCL_bibset load_cclfile(const char *fn)
1283 CCL_bibset res = ccl_qual_mk();
1284 if (ccl_qual_fname(res, fn) < 0)
1286 yaz_log(YLOG_FATAL|YLOG_ERRNO, "%s", fn);
1292 int main(int argc, char **argv)
1297 if (signal(SIGPIPE, SIG_IGN) < 0)
1298 yaz_log(YLOG_WARN|YLOG_ERRNO, "signal");
1300 yaz_log_init(YLOG_DEFAULT_LEVEL|YLOG_DEBUG, "pazpar2", 0);
1302 while ((ret = options("c:h:p:C:s:", argv, argc, &arg)) != -2)
1308 command_init(atoi(arg));
1311 global_parameters.ccl_filter = load_cclfile(arg);
1314 http_init(atoi(arg));
1317 http_set_proxyaddr(arg);
1320 load_simpletargets(arg);
1323 fprintf(stderr, "Usage: pazpar2 -d comport");
1328 global_parameters.ccl_filter = load_cclfile("default.bib");
1329 global_parameters.yaz_marc = yaz_marc_create();
1330 yaz_marc_subfield_str(global_parameters.yaz_marc, "\t");
1331 global_parameters.odr_in = odr_createmem(ODR_DECODE);
1332 global_parameters.odr_out = odr_createmem(ODR_ENCODE);
1334 event_loop(&channel_list);
1342 * indent-tabs-mode: nil
1344 * vim: shiftwidth=4 tabstop=8 expandtab