2 * Copyright (c) 2000, Index Data.
3 * See the file LICENSE for details.
5 * $Log: yaz-z-server.cpp,v $
6 * Revision 1.4 2000-10-11 11:58:17 adam
7 * Moved header files to include/yaz++. Switched to libtool and automake.
8 * Configure script creates yaz++-config script.
10 * Revision 1.3 2000/09/21 21:43:20 adam
11 * Better high-level server API.
13 * Revision 1.2 2000/09/12 12:09:53 adam
14 * More work on high-level server.
16 * Revision 1.1 2000/09/08 10:23:42 adam
17 * Added skeleton of yaz-z-server.
22 #include <yaz++/yaz-z-server.h>
25 Yaz_Z_Server::Yaz_Z_Server(IYaz_PDU_Observable *the_PDU_Observable)
26 : Yaz_Z_Assoc(the_PDU_Observable)
33 void Yaz_Z_Server::create_databaseRecord (
34 Z_NamePlusRecord *rec, const char *dbname, int format, const void *buf, int len)
36 rec->databaseName = dbname ? odr_strdup (odr_encode(), dbname) : 0;
37 rec->which = Z_NamePlusRecord_databaseRecord;
38 rec->u.databaseRecord = z_ext_record (odr_encode(), format,
39 (const char *) buf, len);
43 * surrogate diagnostic.
45 void Yaz_Z_Server::create_surrogateDiagnostics(
46 Z_NamePlusRecord *rec, const char *dbname, int error, char *const addinfo)
49 int *err = (int *)odr_malloc (odr_encode(), sizeof(*err));
51 Z_DiagRec *drec = (Z_DiagRec *)odr_malloc (odr_encode(), sizeof(*drec));
52 Z_DefaultDiagFormat *dr = (Z_DefaultDiagFormat *)
53 odr_malloc (odr_encode(), sizeof(*dr));
55 bib1.proto = PROTO_Z3950;
56 bib1.oclass = CLASS_DIAGSET;
57 bib1.value = VAL_BIB1;
59 yaz_log(LOG_DEBUG, "SurrogateDiagnotic: %d -- %s", error, addinfo);
61 rec->databaseName = dbname ? odr_strdup (odr_encode(), dbname) : 0;
62 rec->which = Z_NamePlusRecord_surrogateDiagnostic;
63 rec->u.surrogateDiagnostic = drec;
64 drec->which = Z_DiagRec_defaultFormat;
65 drec->u.defaultFormat = dr;
66 dr->diagnosticSetId = odr_oiddup (odr_encode(),
67 oid_ent_to_oid(&bib1, oid));
69 dr->which = Z_DefaultDiagFormat_v2Addinfo;
70 dr->u.v2Addinfo = odr_strdup (odr_encode(), addinfo ? addinfo : "");
73 Z_Records *Yaz_Z_Server::create_nonSurrogateDiagnostics (
74 int error, const char *addinfo)
77 Z_Records *rec = (Z_Records *)
78 odr_malloc (odr_encode(), sizeof(*rec));
81 odr_malloc (odr_encode(), sizeof(*err));
82 Z_DiagRec *drec = (Z_DiagRec *)
83 odr_malloc (odr_encode(), sizeof(*drec));
84 Z_DefaultDiagFormat *dr = (Z_DefaultDiagFormat *)
85 odr_malloc (odr_encode(), sizeof(*dr));
87 bib1.proto = PROTO_Z3950;
88 bib1.oclass = CLASS_DIAGSET;
89 bib1.value = VAL_BIB1;
92 rec->which = Z_Records_NSD;
93 rec->u.nonSurrogateDiagnostic = dr;
95 odr_oiddup (odr_encode(), oid_ent_to_oid(&bib1, oid));
97 dr->which = Z_DefaultDiagFormat_v2Addinfo;
98 dr->u.v2Addinfo = odr_strdup (odr_encode(), addinfo ? addinfo : "");
102 Z_Records *Yaz_Z_Server::pack_records (const char *resultSetName,
104 Z_RecordComposition *comp,
105 int *next, int *pres,
108 int recno, total_length = 0, toget = xnum, dumped_records = 0;
110 (Z_Records *) odr_malloc (odr_encode(), sizeof(*records));
111 Z_NamePlusRecordList *reclist =
112 (Z_NamePlusRecordList *) odr_malloc (odr_encode(), sizeof(*reclist));
113 Z_NamePlusRecord **list =
114 (Z_NamePlusRecord **) odr_malloc (odr_encode(), sizeof(*list) * toget);
116 records->which = Z_Records_DBOSD;
117 records->u.databaseOrSurDiagnostics = reclist;
118 reclist->num_records = 0;
119 reclist->records = list;
120 *pres = Z_PRES_SUCCESS;
123 yaz_log(LOG_LOG, "Request to pack %d+%d", start, toget);
124 yaz_log(LOG_LOG, "pms=%d, mrs=%d", m_preferredMessageSize,
125 m_maximumRecordSize);
126 for (recno = start; reclist->num_records < toget; recno++)
128 Z_NamePlusRecord *this_rec =
129 (Z_NamePlusRecord *) odr_malloc (odr_encode(), sizeof(*this_rec));
130 this_rec->databaseName = 0;
131 this_rec->which = Z_NamePlusRecord_databaseRecord;
132 this_rec->u.databaseRecord = 0;
136 recv_Z_record (resultSetName, recno, format, comp, this_rec, records);
138 if (records->which != Z_Records_DBOSD)
140 *pres = Z_PRES_FAILURE;
144 if (this_rec->which == Z_NamePlusRecord_databaseRecord &&
145 this_rec->u.databaseRecord == 0)
146 { // handler did not return a record..
147 create_surrogateDiagnostics(this_rec, 0, 14, 0);
150 * we get the number of bytes allocated on the stream before any
151 * allocation done by the backend - this should give us a reasonable
152 * idea of the total size of the data so far.
154 total_length = odr_total(odr_encode()) - dumped_records;
155 this_length = odr_total(odr_encode()) - total_length;
156 yaz_log(LOG_LOG, " fetched record, len=%d, total=%d",
157 this_length, total_length);
158 if (this_length + total_length > m_preferredMessageSize)
160 /* record is small enough, really */
161 if (this_length <= m_preferredMessageSize)
163 yaz_log(LOG_LOG, " Dropped last normal-sized record");
164 *pres = Z_PRES_PARTIAL_2;
167 if (this_length >= m_maximumRecordSize)
168 { /* too big entirely */
169 yaz_log(LOG_LOG, "Record > maxrcdsz");
170 reclist->records[reclist->num_records] = this_rec;
171 create_surrogateDiagnostics(this_rec,
172 this_rec->databaseName, 17, 0);
173 reclist->num_records++;
175 dumped_records += this_length;
178 else /* record can only be fetched by itself */
180 yaz_log(LOG_LOG, " Record > prefmsgsz");
183 yaz_log(LOG_DEBUG, " Dropped it");
184 reclist->records[reclist->num_records] = this_rec;
185 create_surrogateDiagnostics(this_rec,
186 this_rec->databaseName,
188 reclist->num_records++;
189 // *next = freq.last_in_set ? 0 : recno + 1;
191 dumped_records += this_length;
196 reclist->records[reclist->num_records] = this_rec;
197 reclist->num_records++;
203 void Yaz_Z_Server::fetch_via_piggyback (Z_SearchRequest *req,
204 Z_SearchResponse *res)
206 bool_t *sr = (bool_t *)odr_malloc (odr_encode(), sizeof(*sr));
211 Z_RecordComposition comp, *compp = 0;
212 int hits = *res->resultCount;
214 int *nulint = (int *)odr_malloc (odr_encode(), sizeof(*nulint));
217 comp.which = Z_RecordComp_simple;
218 /* how many records does the user agent want, then? */
219 if (hits <= *req->smallSetUpperBound)
222 if ((comp.u.simple = req->smallSetElementSetNames))
225 else if (hits < *req->largeSetLowerBound)
227 toget = *req->mediumSetPresentNumber;
230 if ((comp.u.simple = req->mediumSetElementSetNames))
234 if (toget && !res->records)
236 res->presentStatus = (int *) odr_malloc (odr_encode(), sizeof(int));
237 *res->presentStatus = Z_PRES_SUCCESS;
239 pack_records(req->resultSetName, 1, toget, compp,
240 res->nextResultSetPosition,
242 req->preferredRecordSyntax);
245 if (res->records->which == Z_Records_DBOSD)
246 *res->numberOfRecordsReturned =
247 res->records->u.databaseOrSurDiagnostics->num_records;
248 res->searchStatus = sr;
249 res->resultSetStatus = 0;
254 *res->nextResultSetPosition = 1;
255 res->numberOfRecordsReturned = nulint;
256 res->searchStatus = sr;
257 res->resultSetStatus = 0;
258 res->presentStatus = 0;
262 void Yaz_Z_Server::fetch_via_present (Z_PresentRequest *req,
263 Z_PresentResponse *res)
265 res->records = pack_records (req->resultSetId,*req->resultSetStartPoint,
266 *req->numberOfRecordsRequested,
267 req->recordComposition,
268 res->nextResultSetPosition,
270 req->preferredRecordSyntax);
271 if (res->records->which == Z_Records_DBOSD)
272 *res->numberOfRecordsReturned =
273 res->records->u.databaseOrSurDiagnostics->num_records;
276 void Yaz_Z_Server::recv_Z_PDU (Z_APDU *apdu_request)
278 Z_APDU *apdu_response;
279 switch (apdu_request->which)
281 case Z_APDU_initRequest:
282 logf (LOG_LOG, "got InitRequest");
283 apdu_response = create_Z_PDU(Z_APDU_initResponse);
284 recv_Z_init (apdu_request->u.initRequest,
285 apdu_response->u.initResponse);
286 m_preferredMessageSize =
287 *apdu_request->u.initRequest->preferredMessageSize;
288 m_maximumRecordSize =
289 *apdu_request->u.initRequest->maximumRecordSize;
290 send_Z_PDU(apdu_response);
292 case Z_APDU_searchRequest:
293 logf (LOG_LOG, "got SearchRequest");
294 apdu_response = create_Z_PDU(Z_APDU_searchResponse);
295 recv_Z_search (apdu_request->u.searchRequest,
296 apdu_response->u.searchResponse);
297 if (!apdu_response->u.searchResponse->records)
299 fetch_via_piggyback(apdu_request->u.searchRequest,
300 apdu_response->u.searchResponse);
302 send_Z_PDU(apdu_response);
304 case Z_APDU_presentRequest:
305 logf (LOG_LOG, "got PresentRequest");
306 apdu_response = create_Z_PDU(Z_APDU_presentResponse);
307 recv_Z_present (apdu_request->u.presentRequest,
308 apdu_response->u.presentResponse);
309 if (!apdu_response->u.presentResponse->records)
310 fetch_via_present(apdu_request->u.presentRequest,
311 apdu_response->u.presentResponse);
312 send_Z_PDU(apdu_response);