2 * Copyright (c) 2000, Index Data.
3 * See the file LICENSE for details.
5 * $Log: yaz-z-server.cpp,v $
6 * Revision 1.5 2000-10-24 12:29:57 adam
7 * Fixed bug in proxy where a Yaz_ProxyClient could be owned by
8 * two Yaz_Proxy's (fatal).
10 * Revision 1.4 2000/10/11 11:58:17 adam
11 * Moved header files to include/yaz++. Switched to libtool and automake.
12 * Configure script creates yaz++-config script.
14 * Revision 1.3 2000/09/21 21:43:20 adam
15 * Better high-level server API.
17 * Revision 1.2 2000/09/12 12:09:53 adam
18 * More work on high-level server.
20 * Revision 1.1 2000/09/08 10:23:42 adam
21 * Added skeleton of yaz-z-server.
26 #include <yaz++/yaz-z-server.h>
29 Yaz_Z_Server::Yaz_Z_Server(IYaz_PDU_Observable *the_PDU_Observable)
30 : Yaz_Z_Assoc(the_PDU_Observable)
37 void Yaz_Z_Server::create_databaseRecord (
38 Z_NamePlusRecord *rec, const char *dbname, int format, const void *buf, int len)
40 rec->databaseName = dbname ? odr_strdup (odr_encode(), dbname) : 0;
41 rec->which = Z_NamePlusRecord_databaseRecord;
42 rec->u.databaseRecord = z_ext_record (odr_encode(), format,
43 (const char *) buf, len);
47 * surrogate diagnostic.
49 void Yaz_Z_Server::create_surrogateDiagnostics(
50 Z_NamePlusRecord *rec, const char *dbname, int error, char *const addinfo)
53 int *err = (int *)odr_malloc (odr_encode(), sizeof(*err));
55 Z_DiagRec *drec = (Z_DiagRec *)odr_malloc (odr_encode(), sizeof(*drec));
56 Z_DefaultDiagFormat *dr = (Z_DefaultDiagFormat *)
57 odr_malloc (odr_encode(), sizeof(*dr));
59 bib1.proto = PROTO_Z3950;
60 bib1.oclass = CLASS_DIAGSET;
61 bib1.value = VAL_BIB1;
63 yaz_log(LOG_DEBUG, "SurrogateDiagnotic: %d -- %s", error, addinfo);
65 rec->databaseName = dbname ? odr_strdup (odr_encode(), dbname) : 0;
66 rec->which = Z_NamePlusRecord_surrogateDiagnostic;
67 rec->u.surrogateDiagnostic = drec;
68 drec->which = Z_DiagRec_defaultFormat;
69 drec->u.defaultFormat = dr;
70 dr->diagnosticSetId = odr_oiddup (odr_encode(),
71 oid_ent_to_oid(&bib1, oid));
73 dr->which = Z_DefaultDiagFormat_v2Addinfo;
74 dr->u.v2Addinfo = odr_strdup (odr_encode(), addinfo ? addinfo : "");
77 Z_Records *Yaz_Z_Server::create_nonSurrogateDiagnostics (
78 int error, const char *addinfo)
81 Z_Records *rec = (Z_Records *)
82 odr_malloc (odr_encode(), sizeof(*rec));
85 odr_malloc (odr_encode(), sizeof(*err));
86 Z_DiagRec *drec = (Z_DiagRec *)
87 odr_malloc (odr_encode(), sizeof(*drec));
88 Z_DefaultDiagFormat *dr = (Z_DefaultDiagFormat *)
89 odr_malloc (odr_encode(), sizeof(*dr));
91 bib1.proto = PROTO_Z3950;
92 bib1.oclass = CLASS_DIAGSET;
93 bib1.value = VAL_BIB1;
96 rec->which = Z_Records_NSD;
97 rec->u.nonSurrogateDiagnostic = dr;
99 odr_oiddup (odr_encode(), oid_ent_to_oid(&bib1, oid));
101 dr->which = Z_DefaultDiagFormat_v2Addinfo;
102 dr->u.v2Addinfo = odr_strdup (odr_encode(), addinfo ? addinfo : "");
106 Z_Records *Yaz_Z_Server::pack_records (const char *resultSetName,
108 Z_RecordComposition *comp,
109 int *next, int *pres,
112 int recno, total_length = 0, toget = xnum, dumped_records = 0;
114 (Z_Records *) odr_malloc (odr_encode(), sizeof(*records));
115 Z_NamePlusRecordList *reclist =
116 (Z_NamePlusRecordList *) odr_malloc (odr_encode(), sizeof(*reclist));
117 Z_NamePlusRecord **list =
118 (Z_NamePlusRecord **) odr_malloc (odr_encode(), sizeof(*list) * toget);
120 records->which = Z_Records_DBOSD;
121 records->u.databaseOrSurDiagnostics = reclist;
122 reclist->num_records = 0;
123 reclist->records = list;
124 *pres = Z_PRES_SUCCESS;
127 yaz_log(LOG_LOG, "Request to pack %d+%d", start, toget);
128 yaz_log(LOG_LOG, "pms=%d, mrs=%d", m_preferredMessageSize,
129 m_maximumRecordSize);
130 for (recno = start; reclist->num_records < toget; recno++)
132 Z_NamePlusRecord *this_rec =
133 (Z_NamePlusRecord *) odr_malloc (odr_encode(), sizeof(*this_rec));
134 this_rec->databaseName = 0;
135 this_rec->which = Z_NamePlusRecord_databaseRecord;
136 this_rec->u.databaseRecord = 0;
140 recv_Z_record (resultSetName, recno, format, comp, this_rec, records);
142 if (records->which != Z_Records_DBOSD)
144 *pres = Z_PRES_FAILURE;
148 if (this_rec->which == Z_NamePlusRecord_databaseRecord &&
149 this_rec->u.databaseRecord == 0)
150 { // handler did not return a record..
151 create_surrogateDiagnostics(this_rec, 0, 14, 0);
154 * we get the number of bytes allocated on the stream before any
155 * allocation done by the backend - this should give us a reasonable
156 * idea of the total size of the data so far.
158 total_length = odr_total(odr_encode()) - dumped_records;
159 this_length = odr_total(odr_encode()) - total_length;
160 yaz_log(LOG_LOG, " fetched record, len=%d, total=%d",
161 this_length, total_length);
162 if (this_length + total_length > m_preferredMessageSize)
164 /* record is small enough, really */
165 if (this_length <= m_preferredMessageSize)
167 yaz_log(LOG_LOG, " Dropped last normal-sized record");
168 *pres = Z_PRES_PARTIAL_2;
171 if (this_length >= m_maximumRecordSize)
172 { /* too big entirely */
173 yaz_log(LOG_LOG, "Record > maxrcdsz");
174 reclist->records[reclist->num_records] = this_rec;
175 create_surrogateDiagnostics(this_rec,
176 this_rec->databaseName, 17, 0);
177 reclist->num_records++;
179 dumped_records += this_length;
182 else /* record can only be fetched by itself */
184 yaz_log(LOG_LOG, " Record > prefmsgsz");
187 yaz_log(LOG_DEBUG, " Dropped it");
188 reclist->records[reclist->num_records] = this_rec;
189 create_surrogateDiagnostics(this_rec,
190 this_rec->databaseName,
192 reclist->num_records++;
193 // *next = freq.last_in_set ? 0 : recno + 1;
195 dumped_records += this_length;
200 reclist->records[reclist->num_records] = this_rec;
201 reclist->num_records++;
207 void Yaz_Z_Server::fetch_via_piggyback (Z_SearchRequest *req,
208 Z_SearchResponse *res)
210 bool_t *sr = (bool_t *)odr_malloc (odr_encode(), sizeof(*sr));
215 Z_RecordComposition comp, *compp = 0;
216 int hits = *res->resultCount;
218 int *nulint = (int *)odr_malloc (odr_encode(), sizeof(*nulint));
221 comp.which = Z_RecordComp_simple;
222 /* how many records does the user agent want, then? */
223 if (hits <= *req->smallSetUpperBound)
226 if ((comp.u.simple = req->smallSetElementSetNames))
229 else if (hits < *req->largeSetLowerBound)
231 toget = *req->mediumSetPresentNumber;
234 if ((comp.u.simple = req->mediumSetElementSetNames))
238 if (toget && !res->records)
240 res->presentStatus = (int *) odr_malloc (odr_encode(), sizeof(int));
241 *res->presentStatus = Z_PRES_SUCCESS;
243 pack_records(req->resultSetName, 1, toget, compp,
244 res->nextResultSetPosition,
246 req->preferredRecordSyntax);
249 if (res->records->which == Z_Records_DBOSD)
250 *res->numberOfRecordsReturned =
251 res->records->u.databaseOrSurDiagnostics->num_records;
252 res->searchStatus = sr;
253 res->resultSetStatus = 0;
258 *res->nextResultSetPosition = 1;
259 res->numberOfRecordsReturned = nulint;
260 res->searchStatus = sr;
261 res->resultSetStatus = 0;
262 res->presentStatus = 0;
266 void Yaz_Z_Server::fetch_via_present (Z_PresentRequest *req,
267 Z_PresentResponse *res)
269 res->records = pack_records (req->resultSetId,*req->resultSetStartPoint,
270 *req->numberOfRecordsRequested,
271 req->recordComposition,
272 res->nextResultSetPosition,
274 req->preferredRecordSyntax);
275 if (res->records->which == Z_Records_DBOSD)
276 *res->numberOfRecordsReturned =
277 res->records->u.databaseOrSurDiagnostics->num_records;
280 void Yaz_Z_Server::recv_Z_PDU (Z_APDU *apdu_request)
282 Z_APDU *apdu_response;
283 switch (apdu_request->which)
285 case Z_APDU_initRequest:
286 logf (LOG_LOG, "got InitRequest p=%p", this);
287 apdu_response = create_Z_PDU(Z_APDU_initResponse);
288 recv_Z_init (apdu_request->u.initRequest,
289 apdu_response->u.initResponse);
290 m_preferredMessageSize =
291 *apdu_request->u.initRequest->preferredMessageSize;
292 m_maximumRecordSize =
293 *apdu_request->u.initRequest->maximumRecordSize;
294 send_Z_PDU(apdu_response);
296 case Z_APDU_searchRequest:
297 logf (LOG_LOG, "got SearchRequest p=%p", this);
298 apdu_response = create_Z_PDU(Z_APDU_searchResponse);
299 recv_Z_search (apdu_request->u.searchRequest,
300 apdu_response->u.searchResponse);
301 if (!apdu_response->u.searchResponse->records)
303 fetch_via_piggyback(apdu_request->u.searchRequest,
304 apdu_response->u.searchResponse);
306 send_Z_PDU(apdu_response);
308 case Z_APDU_presentRequest:
309 logf (LOG_LOG, "got PresentRequest p=%p", this);
310 apdu_response = create_Z_PDU(Z_APDU_presentResponse);
311 recv_Z_present (apdu_request->u.presentRequest,
312 apdu_response->u.presentResponse);
313 if (!apdu_response->u.presentResponse->records)
314 fetch_via_present(apdu_request->u.presentRequest,
315 apdu_response->u.presentResponse);
316 send_Z_PDU(apdu_response);