2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.23 1995-05-15 13:25:10 quinn
10 * Revision 1.22 1995/05/15 11:56:39 quinn
11 * Asynchronous facilities. Restructuring of seshigh code.
13 * Revision 1.21 1995/05/02 08:53:19 quinn
14 * Trying in vain to fix comm with ISODE
16 * Revision 1.20 1995/04/20 15:13:00 quinn
19 * Revision 1.19 1995/04/18 08:15:34 quinn
20 * Added dynamic memory allocation on encoding (whew). Code is now somewhat
21 * neater. We'll make the same change for decoding one day.
23 * Revision 1.18 1995/04/17 11:28:25 quinn
26 * Revision 1.17 1995/04/10 10:23:36 quinn
27 * Some work to add scan and other things.
29 * Revision 1.16 1995/03/31 09:18:55 quinn
32 * Revision 1.15 1995/03/30 14:03:23 quinn
33 * Added RFC1006 as separate library
35 * Revision 1.14 1995/03/30 12:18:17 quinn
38 * Revision 1.13 1995/03/30 09:09:24 quinn
39 * Added state-handle and some support for asynchronous activities.
41 * Revision 1.12 1995/03/29 15:40:16 quinn
42 * Ongoing work. Statserv is now dynamic by default
44 * Revision 1.11 1995/03/28 09:16:21 quinn
45 * Added record packing to the search request
47 * Revision 1.10 1995/03/27 08:34:24 quinn
48 * Added dynamic server functionality.
49 * Released bindings to session.c (is now redundant)
51 * Revision 1.9 1995/03/22 15:01:26 quinn
52 * Adjusting record packing.
54 * Revision 1.8 1995/03/22 10:13:21 quinn
55 * Working on record packer
57 * Revision 1.7 1995/03/21 15:53:31 quinn
60 * Revision 1.6 1995/03/21 12:30:09 quinn
61 * Beginning to add support for record packing.
63 * Revision 1.5 1995/03/17 10:44:13 quinn
64 * Added catch of null-string in makediagrec
66 * Revision 1.4 1995/03/17 10:18:08 quinn
67 * Added memory management.
69 * Revision 1.3 1995/03/16 17:42:39 quinn
72 * Revision 1.2 1995/03/16 13:29:01 quinn
75 * Revision 1.1 1995/03/15 16:02:10 quinn
76 * Modded session.c to seshigh.c
81 * Frontend server logic.
83 * This code receives incoming APDUs, and handles client requests by means
86 * Some of the code is getting quite involved, compared to simpler servers -
87 * primarily because it is asynchronous both in the communication with
88 * the user and the backend. We think the complexity will pay off in
89 * the form of greater flexibility when more asynchronous facilities
92 * Memory management has become somewhat involved. In the simple case, where
93 * only one PDU is pending at a time, it will simply reuse the same memory,
94 * once it has found its working size. When we enable multiple concurrent
95 * operations, perhaps even with multiple parallel calls to the backend, it
96 * will maintain a pool of buffers for encoding and decoding, trying to
97 * minimize memory allocation/deallocation during normal operation.
99 * TODOs include (and will be done in order of public interest):
101 * Support for EXPLAIN - provide simple meta-database system.
102 * Support for access control.
103 * Support for resource control.
104 * Support for extended services - primarily Item Order.
105 * Rest of Z39.50-1994
115 #include <comstack.h>
121 #include <statserv.h>
125 static int process_request(association *assoc);
126 void backend_response(IOCHAN i, int event);
127 static int process_response(association *assoc, request *req, Z_APDU *res);
128 static Z_APDU *process_initRequest(association *assoc, request *reqb);
129 static Z_APDU *process_searchRequest(association *assoc, request *reqb,
131 static Z_APDU *response_searchRequest(association *assoc, request *reqb,
132 bend_searchresult *bsrt, int *fd);
133 static Z_APDU *process_presentRequest(association *assoc, request *reqb,
135 static Z_APDU *process_scanRequest(association *assoc, request *reqb, int *fd);
137 static FILE *apduf = 0; /* for use in static mode */
138 static statserv_options_block *control_block = 0;
141 * Create and initialize a new association-handle.
142 * channel : iochannel for the current line.
143 * link : communications channel.
144 * Returns: 0 or a new association handle.
146 association *create_association(IOCHAN channel, COMSTACK link)
151 control_block = statserv_getcontrol();
152 if (!(new = malloc(sizeof(*new))))
154 new->client_chan = channel;
155 new->client_link = link;
156 if (!(new->decode = odr_createmem(ODR_DECODE)) ||
157 !(new->encode = odr_createmem(ODR_ENCODE)))
159 if (*control_block->apdufile)
164 if (!(new->print = odr_createmem(ODR_PRINT)))
166 if (*control_block->apdufile != '-')
168 strcpy(filename, control_block->apdufile);
169 if (!control_block->dynamic)
173 if (!(apduf = fopen(filename, "w")))
175 logf(LOG_WARN|LOG_ERRNO, "%s", filename);
178 setvbuf(apduf, 0, _IONBF, 0);
184 sprintf(filename + strlen(filename), ".%d", getpid());
185 if (!(f = fopen(filename, "w")))
187 logf(LOG_WARN|LOG_ERRNO, "%s", filename);
190 setvbuf(f, 0, _IONBF, 0);
192 odr_setprint(new->print, f);
197 new->input_buffer = 0;
198 new->input_buffer_len = 0;
200 request_initq(&new->incoming);
201 request_initq(&new->outgoing);
202 if (cs_getproto(link) == CS_Z3950)
203 new->proto = PROTO_Z3950;
205 new->proto = PROTO_SR;
210 * Free association and release resources.
212 void destroy_association(association *h)
214 odr_destroy(h->decode);
215 odr_destroy(h->encode);
217 odr_destroy(h->print);
219 free(h->input_buffer);
221 bend_close(h->backend);
222 while (request_deq(&h->incoming));
223 while (request_deq(&h->outgoing));
228 * This is where PDUs from the client are read and the further
229 * processing is initiated. Flow of control moves down through the
230 * various process_* functions below, until the encoded result comes back up
231 * to the output handler in here.
233 * h : the I/O channel that has an outstanding event.
234 * event : the current outstanding event.
236 void ir_session(IOCHAN h, int event)
239 association *assoc = iochan_getdata(h);
240 COMSTACK conn = assoc->client_link;
243 assert(h && conn && assoc);
244 if (event & EVENT_INPUT || event & EVENT_WORK) /* input */
246 if (event & EVENT_INPUT)
248 logf(LOG_DEBUG, "ir_session (input)");
249 assert(assoc && conn);
250 if ((res = cs_get(conn, &assoc->input_buffer,
251 &assoc->input_buffer_len)) <= 0)
253 logf(LOG_LOG, "Connection closed by client");
255 destroy_association(assoc);
259 else if (res == 1) /* incomplete read - wait for more */
261 if (cs_more(conn)) /* more stuff - call us again later, please */
262 iochan_setevent(h, EVENT_INPUT);
264 /* we got a complete PDU. Let's decode it */
265 req = request_get(); /* get a new request structure */
266 odr_reset(assoc->decode);
267 odr_setbuf(assoc->decode, assoc->input_buffer, res, 0);
268 if (!z_APDU(assoc->decode, &req->request, 0))
270 logf(LOG_WARN, "ODR error: %s",
271 odr_errlist[odr_geterror(assoc->decode)]);
273 destroy_association(assoc);
277 req->request_mem = odr_extract_mem(assoc->decode);
278 if (assoc->print && !z_APDU(assoc->print, &req->request, 0))
280 logf(LOG_WARN, "ODR print error: %s",
281 odr_errlist[odr_geterror(assoc->print)]);
282 odr_reset(assoc->print);
284 request_enq(&assoc->incoming, req);
287 /* can we do something yet? */
288 req = request_head(&assoc->incoming);
289 if (req->state == REQUEST_IDLE)
290 if (process_request(assoc) < 0)
293 destroy_association(assoc);
297 if (event & EVENT_OUTPUT)
299 request *req = request_head(&assoc->outgoing);
301 logf(LOG_DEBUG, "ir_session (output)");
302 req->state = REQUEST_PENDING;
303 switch (res = cs_put(conn, req->response, req->len_response))
306 logf(LOG_LOG, "Connection closed by client");
308 destroy_association(assoc);
311 case 0: /* all sent - release the request structure */
312 odr_release_mem(req->request_mem);
313 request_deq(&assoc->outgoing);
314 request_release(req);
315 if (!request_head(&assoc->outgoing))
316 iochan_clearflag(h, EVENT_OUTPUT);
318 /* value of 1 -- partial send -- is simply ignored */
321 if (event & EVENT_EXCEPT)
323 logf(LOG_DEBUG, "ir_session (exception)");
325 destroy_association(assoc);
331 * Initiate request processing.
333 static int process_request(association *assoc)
335 request *req = request_head(&assoc->incoming);
339 logf(LOG_DEBUG, "process_request");
340 assert(req && req->state == REQUEST_IDLE);
341 switch (req->request->which)
343 case Z_APDU_initRequest:
344 res = process_initRequest(assoc, req); break;
345 case Z_APDU_searchRequest:
346 res = process_searchRequest(assoc, req, &fd); break;
347 case Z_APDU_presentRequest:
348 res = process_presentRequest(assoc, req, &fd); break;
349 case Z_APDU_scanRequest:
350 res = process_scanRequest(assoc, req, &fd); break;
352 logf(LOG_WARN, "Bad APDU");
357 logf(LOG_DEBUG, " result immediately available");
358 return process_response(assoc, req, res);
362 logf(LOG_WARN, " bad result");
365 else /* no result yet - one will be provided later */
369 /* Set up an I/O handler for the fd supplied by the backend */
371 logf(LOG_DEBUG, " establishing handler for result");
372 req->state = REQUEST_PENDING;
373 if (!(chan = iochan_create(fd, backend_response, EVENT_INPUT)))
375 iochan_setdata(chan, assoc);
381 * Handle message from the backend.
383 void backend_response(IOCHAN i, int event)
385 association *assoc = iochan_getdata(i);
386 request *req = request_head(&assoc->incoming);
390 logf(LOG_DEBUG, "backend_response");
391 assert(assoc && req && req->state != REQUEST_IDLE);
392 /* determine what it is we're waiting for */
393 switch (req->request->which)
395 case Z_APDU_searchRequest:
396 res = response_searchRequest(assoc, req, 0, &fd); break;
398 case Z_APDU_presentRequest:
399 res = response_presentRequest(assoc, req, 0, &fd); break;
400 case Z_APDU_scanRequest:
401 res = response_scanRequest(assoc, req, 0, &fd); break;
404 logf(LOG_WARN, "Serious programmer's lapse or bug");
407 if ((res && process_response(assoc, req, res) < 0) || fd < 0)
409 logf(LOG_LOG, "Fatal error when talking to backend");
410 cs_close(assoc->client_link);
411 destroy_association(assoc);
412 iochan_destroy(assoc->client_chan);
416 else if (!res) /* no result yet - try again later */
418 logf(LOG_DEBUG, " no result yet");
419 iochan_setfd(i, fd); /* in case fd has changed */
424 * Encode response, and transfer the request structure to the outgoing queue.
426 static int process_response(association *assoc, request *req, Z_APDU *res)
428 odr_setbuf(assoc->encode, req->response, req->size_response, 1);
429 if (!z_APDU(assoc->encode, &res, 0))
431 logf(LOG_WARN, "ODR error when encoding response: %s",
432 odr_errlist[odr_geterror(assoc->decode)]);
435 req->response = odr_getbuf(assoc->encode, &req->len_response,
436 &req->size_response);
437 odr_reset(assoc->encode);
438 /* change this when we make the backend reentrant */
439 assert(req == request_head(&assoc->incoming));
440 req->state = REQUEST_IDLE;
441 request_deq(&assoc->incoming);
442 request_enq(&assoc->outgoing, req);
443 /* turn the work over to the ir_session handler */
444 iochan_setflag(assoc->client_chan, EVENT_OUTPUT);
445 /* Is there more work to be done? */
446 if (request_head(&assoc->incoming))
447 iochan_setevent(assoc->client_chan, EVENT_WORK);
451 static Z_APDU *process_initRequest(association *assoc, request *reqb)
453 Z_InitRequest *req = reqb->request->u.initRequest;
455 static Z_InitResponse resp;
456 static bool_t result = 1;
457 static Odr_bitmask options, protocolVersion;
458 bend_initrequest binitreq;
459 bend_initresult *binitres;
461 logf(LOG_LOG, "Got initRequest");
462 if (req->implementationId)
463 logf(LOG_LOG, "Id: %s", req->implementationId);
464 if (req->implementationName)
465 logf(LOG_LOG, "Name: %s", req->implementationName);
466 if (req->implementationVersion)
467 logf(LOG_LOG, "Version: %s", req->implementationVersion);
469 binitreq.configname = "default-config";
470 if (!(binitres = bend_init(&binitreq)) || binitres->errcode)
472 logf(LOG_WARN, "Negative response from backend");
476 assoc->backend = binitres->handle;
477 apdu.which = Z_APDU_initResponse;
478 apdu.u.initResponse = &resp;
479 resp.referenceId = req->referenceId;
480 /* let's tell the client what we can do */
481 ODR_MASK_ZERO(&options);
482 if (ODR_MASK_GET(req->options, Z_Options_search))
483 ODR_MASK_SET(&options, Z_Options_search);
484 if (ODR_MASK_GET(req->options, Z_Options_present))
485 ODR_MASK_SET(&options, Z_Options_present);
487 if (ODR_MASK_GET(req->options, Z_Options_delSet))
488 ODR_MASK_SET(&options, Z_Options_delSet);
490 if (ODR_MASK_GET(req->options, Z_Options_namedResultSets))
491 ODR_MASK_SET(&options, Z_Options_namedResultSets);
492 if (ODR_MASK_GET(req->options, Z_Options_scan))
493 ODR_MASK_SET(&options, Z_Options_scan);
494 if (ODR_MASK_GET(req->options, Z_Options_concurrentOperations))
495 ODR_MASK_SET(&options, Z_Options_concurrentOperations);
496 resp.options = &options;
498 ODR_MASK_ZERO(&protocolVersion);
499 if (ODR_MASK_GET(req->protocolVersion, Z_ProtocolVersion_1))
500 ODR_MASK_SET(&protocolVersion, Z_ProtocolVersion_1);
501 if (ODR_MASK_GET(req->protocolVersion, Z_ProtocolVersion_2))
502 ODR_MASK_SET(&protocolVersion, Z_ProtocolVersion_2);
503 resp.protocolVersion = &protocolVersion;
504 assoc->maximumRecordSize = *req->maximumRecordSize;
505 if (assoc->maximumRecordSize > control_block->maxrecordsize)
506 assoc->maximumRecordSize = control_block->maxrecordsize;
507 assoc->preferredMessageSize = *req->preferredMessageSize;
508 if (assoc->preferredMessageSize > assoc->maximumRecordSize)
509 assoc->preferredMessageSize = assoc->maximumRecordSize;
510 resp.preferredMessageSize = &assoc->preferredMessageSize;
511 resp.maximumRecordSize = &assoc->maximumRecordSize;
512 resp.result = &result;
513 resp.implementationId = "YAZ";
514 resp.implementationName = "Index Data/YAZ Generic Frontend Server";
515 resp.implementationVersion = "$Revision: 1.23 $";
516 resp.userInformationField = 0;
520 static Z_Records *diagrec(oid_proto proto, int error, char *addinfo)
522 static Z_Records rec;
528 bib1.class = CLASS_DIAGSET;
529 bib1.value = VAL_BIB1;
531 logf(LOG_DEBUG, "Diagnostic: %d -- %s", error, addinfo ? addinfo :
534 rec.which = Z_Records_NSD;
535 rec.u.nonSurrogateDiagnostic = &dr;
536 dr.diagnosticSetId = oid_getoidbyent(&bib1);
538 dr.addinfo = addinfo ? addinfo : "";
542 static Z_NamePlusRecord *surrogatediagrec(oid_proto proto, char *dbname,
543 int error, char *addinfo)
545 static Z_NamePlusRecord rec;
551 bib1.class = CLASS_DIAGSET;
552 bib1.value = VAL_BIB1;
554 logf(LOG_DEBUG, "SurrogateDiagnotic: %d -- %s", error, addinfo);
556 rec.databaseName = dbname;
557 rec.which = Z_NamePlusRecord_surrogateDiagnostic;
558 rec.u.surrogateDiagnostic = &dr;
559 dr.diagnosticSetId = oid_getoidbyent(&bib1);
561 dr.addinfo = addinfo ? addinfo : "";
565 static Z_DiagRecs *diagrecs(oid_proto proto, int error, char *addinfo)
567 static Z_DiagRecs recs;
568 static Z_DiagRec *recp[1], rec;
572 logf(LOG_DEBUG, "DiagRecs: %d -- %s", error, addinfo);
574 bib1.class = CLASS_DIAGSET;
575 bib1.value = VAL_BIB1;
578 recs.num_diagRecs = 1;
579 recs.diagRecs = recp;
581 rec.diagnosticSetId = oid_getoidbyent(&bib1);
582 rec.condition = &err;
583 rec.addinfo = addinfo ? addinfo : "";
587 #define MAX_RECORDS 256
589 static Z_Records *pack_records(association *a, char *setname, int start,
590 int *num, Z_ElementSetNames *esn,
591 int *next, int *pres)
593 int recno, total_length = 0, toget = *num;
594 static Z_Records records;
595 static Z_NamePlusRecordList reclist;
596 static Z_NamePlusRecord *list[MAX_RECORDS];
600 records.which = Z_Records_DBOSD;
601 records.u.databaseOrSurDiagnostics = &reclist;
602 reclist.num_records = 0;
603 reclist.records = list;
604 *pres = Z_PRES_SUCCESS;
608 recform.proto = a->proto;
609 recform.class = CLASS_RECSYN;
610 recform.value = VAL_USMARC;
611 if (!(oid = odr_oiddup(a->encode, oid_getoidbyent(&recform))))
614 logf(LOG_DEBUG, "Request to pack %d+%d", start, toget);
615 logf(LOG_DEBUG, "pms=%d, mrs=%d", a->preferredMessageSize,
616 a->maximumRecordSize);
617 for (recno = start; reclist.num_records < toget; recno++)
619 bend_fetchrequest freq;
620 bend_fetchresult *fres;
621 Z_NamePlusRecord *thisrec;
622 Z_DatabaseRecord *thisext;
624 if (reclist.num_records == MAX_RECORDS - 1)
626 *pres = Z_PRES_PARTIAL_2;
629 freq.setname = setname;
631 if (!(fres = bend_fetch(a->backend, &freq, 0)))
633 *pres = Z_PRES_FAILURE;
634 return diagrec(a->proto, 2, "Backend interface problem");
636 /* backend should be able to signal whether error is system-wide
637 or only pertaining to current record */
640 *pres = Z_PRES_FAILURE;
641 return diagrec(a->proto, fres->errcode, fres->errstring);
643 logf(LOG_DEBUG, " fetched record, len=%d, total=%d",
644 fres->len, total_length);
645 if (fres->len + total_length > a->preferredMessageSize)
647 /* record is small enough, really */
648 if (fres->len <= a->preferredMessageSize)
650 logf(LOG_DEBUG, " Dropped last normal-sized record");
651 *pres = Z_PRES_PARTIAL_2;
654 /* record can only be fetched by itself */
655 if (fres->len < a->maximumRecordSize)
657 logf(LOG_DEBUG, " Record > prefmsgsz");
660 logf(LOG_DEBUG, " Dropped it");
661 reclist.records[reclist.num_records] =
662 surrogatediagrec(a->proto, fres->basename, 16, 0);
663 reclist.num_records++;
664 *pres = Z_PRES_PARTIAL_2;
668 else /* too big entirely */
670 logf(LOG_DEBUG, "Record > maxrcdsz");
671 reclist.records[reclist.num_records] =
672 surrogatediagrec(a->proto, fres->basename, 17, 0);
673 reclist.num_records++;
674 *pres = Z_PRES_PARTIAL_2;
678 if (!(thisrec = odr_malloc(a->encode, sizeof(*thisrec))))
680 if (!(thisrec->databaseName = odr_malloc(a->encode,
681 strlen(fres->basename) + 1)))
683 strcpy(thisrec->databaseName, fres->basename);
684 thisrec->which = Z_NamePlusRecord_databaseRecord;
685 if (!(thisrec->u.databaseRecord = thisext = odr_malloc(a->encode,
686 sizeof(Z_DatabaseRecord))))
688 thisext->direct_reference = oid; /* should be OID for current MARC */
689 thisext->indirect_reference = 0;
690 thisext->descriptor = 0;
691 thisext->which = ODR_EXTERNAL_octet;
692 if (!(thisext->u.octet_aligned = odr_malloc(a->encode,
695 if (!(thisext->u.octet_aligned->buf = odr_malloc(a->encode, fres->len)))
697 memcpy(thisext->u.octet_aligned->buf, fres->record, fres->len);
698 thisext->u.octet_aligned->len = thisext->u.octet_aligned->size =
700 reclist.records[reclist.num_records] = thisrec;
701 reclist.num_records++;
702 total_length += fres->len;
704 *next = fres->last_in_set ? 0 : recno + 1;
709 static Z_APDU *process_searchRequest(association *assoc, request *reqb,
712 Z_SearchRequest *req = reqb->request->u.searchRequest;
713 bend_searchrequest bsrq;
714 bend_searchresult *bsrt;
716 logf(LOG_LOG, "Got SearchRequest.");
718 bsrq.setname = req->resultSetName;
719 bsrq.replace_set = *req->replaceIndicator;
720 bsrq.num_bases = req->num_databaseNames;
721 bsrq.basenames = req->databaseNames;
722 bsrq.query = req->query;
724 if (!(bsrt = bend_search(assoc->backend, &bsrq, fd)))
726 return response_searchRequest(assoc, reqb, bsrt, fd);
729 bend_searchresult *bend_searchresponse(void *handle) {return 0;}
732 * Prepare a searchresponse based on the backend results. We probably want
733 * to look at making the fetching of records nonblocking as well, but
734 * so far, we'll keep things simple.
735 * If bsrt is null, that means we're called in response to a communications
736 * event, and we'll have to get the response for ourselves.
738 static Z_APDU *response_searchRequest(association *assoc, request *reqb,
739 bend_searchresult *bsrt, int *fd)
741 Z_SearchRequest *req = reqb->request->u.searchRequest;
743 static Z_SearchResponse resp;
744 static int nulint = 0;
745 static bool_t sr = 1;
747 static int none = Z_RES_NONE;
749 apdu.which = Z_APDU_searchResponse;
750 apdu.u.searchResponse = &resp;
751 resp.referenceId = req->referenceId;
753 if (!bsrt && !(bsrt = bend_searchresponse(assoc->backend)))
755 logf(LOG_FATAL, "Bad result from backend");
758 else if (bsrt->errcode)
760 resp.records = diagrec(assoc->proto, bsrt->errcode,
762 resp.resultCount = &nulint;
763 resp.numberOfRecordsReturned = &nulint;
764 resp.nextResultSetPosition = &nulint;
765 resp.searchStatus = &nulint;
766 resp.resultSetStatus = &none;
767 resp.presentStatus = 0;
772 Z_ElementSetNames *setnames;
776 resp.resultCount = &bsrt->hits;
778 /* how many records does the user agent want, then? */
779 if (bsrt->hits <= *req->smallSetUpperBound)
782 setnames = req->smallSetElementSetNames;
784 else if (bsrt->hits < *req->largeSetLowerBound)
786 toget = *req->mediumSetPresentNumber;
787 if (toget > bsrt->hits)
789 setnames = req->mediumSetElementSetNames;
794 if (toget && !resp.records)
796 resp.records = pack_records(assoc, req->resultSetName, 1,
797 &toget, setnames, &next, &presst);
800 resp.numberOfRecordsReturned = &toget;
801 resp.nextResultSetPosition = &next;
802 resp.searchStatus = &sr;
803 resp.resultSetStatus = 0;
804 resp.presentStatus = &presst;
808 resp.numberOfRecordsReturned = &nulint;
809 resp.nextResultSetPosition = &next;
810 resp.searchStatus = &sr;
811 resp.resultSetStatus = 0;
812 resp.presentStatus = 0;
819 * Maybe we got a little over-friendly when we designed bend_fetch to
820 * get only one record at a time. Some backends can optimise multiple-record
821 * fetches, and at any rate, there is some overhead involved in
822 * all that selecting and hopping around. Problem is, of course, that the
823 * frontend can't know ahead of time how many records it'll need to
824 * fill the negotiated PDU size. Annoying. Segmentation or not, Z/SR
825 * is downright lousy as a bulk data transfer protocol.
827 * To start with, we'll do the fetching of records from the backend
828 * in one operation: To save some trips in and out of the event-handler,
829 * and to simplify the interface to pack_records. At any rate, asynch
830 * operation is more fun in operations that have an unpredictable execution
831 * speed - which is normally more true for search than for present.
833 static Z_APDU *process_presentRequest(association *assoc, request *reqb,
836 Z_PresentRequest *req = reqb->request->u.presentRequest;
838 static Z_PresentResponse resp;
839 static int presst, next, num;
841 logf(LOG_LOG, "Got PresentRequest.");
842 apdu.which = Z_APDU_presentResponse;
843 apdu.u.presentResponse = &resp;
844 resp.referenceId = req->referenceId;
846 num = *req->numberOfRecordsRequested;
847 resp.records = pack_records(assoc, req->resultSetId,
848 *req->resultSetStartPoint, &num, req->elementSetNames, &next, &presst);
851 resp.numberOfRecordsReturned = #
852 resp.presentStatus = &presst;
853 resp.nextResultSetPosition = &next;
859 * Scan was implemented rather in a hurry, and with support for only the basic
860 * elements of the service in the backend API. Suggestions are welcome.
862 static Z_APDU *process_scanRequest(association *assoc, request *reqb, int *fd)
864 Z_ScanRequest *req = reqb->request->u.scanRequest;
866 static Z_ScanResponse res;
867 static int scanStatus = Z_Scan_failure;
868 static int numberOfEntriesReturned = 0;
870 static Z_ListEntries ents;
871 #define SCAN_MAX_ENTRIES 200
872 static Z_Entry *tab[SCAN_MAX_ENTRIES];
873 bend_scanrequest srq;
874 bend_scanresult *srs;
876 apdu.which = Z_APDU_scanResponse;
877 apdu.u.scanResponse = &res;
878 res.referenceId = req->referenceId;
880 res.scanStatus = &scanStatus;
881 res.numberOfEntriesReturned = &numberOfEntriesReturned;
882 res.positionOfTerm = 0;
884 ents.which = Z_ListEntries_nonSurrogateDiagnostics;
885 res.attributeSet = 0;
887 if (req->attributeSet && (!(attent = oid_getentbyoid(req->attributeSet)) ||
888 attent->class != CLASS_ATTSET || attent->value != VAL_BIB1))
889 ents.u.nonSurrogateDiagnostics = diagrecs(assoc->proto, 121, 0);
890 else if (req->stepSize && *req->stepSize > 0)
891 ents.u.nonSurrogateDiagnostics = diagrecs(assoc->proto, 205, 0);
894 srq.num_bases = req->num_databaseNames;
895 srq.basenames = req->databaseNames;
896 srq.num_entries = *req->numberOfTermsRequested;
897 srq.term = req->termListAndStartPoint;
898 srq.term_position = req->preferredPositionInResponse ?
899 *req->preferredPositionInResponse : 1;
900 if (!(srs = bend_scan(assoc->backend, &srq, 0)))
901 ents.u.nonSurrogateDiagnostics = diagrecs(assoc->proto, 2, 0);
902 else if (srs->errcode)
903 ents.u.nonSurrogateDiagnostics = diagrecs(assoc->proto,
904 srs->errcode, srs->errstring);
908 static Z_Entries list;
910 if (srs->status == BEND_SCAN_PARTIAL)
911 scanStatus = Z_Scan_partial_5;
913 scanStatus = Z_Scan_success;
914 ents.which = Z_ListEntries_entries;
915 ents.u.entries = &list;
917 for (i = 0; i < srs->num_entries; i++)
923 if (i >= SCAN_MAX_ENTRIES)
925 scanStatus = Z_Scan_partial_4;
928 list.entries[i] = e = odr_malloc(assoc->encode, sizeof(*e));
929 e->which = Z_Entry_termInfo;
930 e->u.termInfo = t = odr_malloc(assoc->encode, sizeof(*t));
931 t->suggestedAttributes = 0;
932 t->alternativeTerm = 0;
934 t->globalOccurrences = &srs->entries[i].occurrences;
935 t->term = odr_malloc(assoc->encode, sizeof(*t->term));
936 t->term->which = Z_Term_general;
937 t->term->u.general = o = odr_malloc(assoc->encode,
939 o->buf = odr_malloc(assoc->encode, o->len = o->size =
940 strlen(srs->entries[i].term));
941 memcpy(o->buf, srs->entries[i].term, o->len);
943 list.num_entries = i;
944 res.numberOfEntriesReturned = &list.num_entries;
945 res.positionOfTerm = &srs->term_position;