Better high-level server API.
[yazpp-moved-to-github.git] / src / yaz-z-server.cpp
1 /*
2  * Copyright (c) 2000, Index Data.
3  * See the file LICENSE for details.
4  * 
5  * $Log: yaz-z-server.cpp,v $
6  * Revision 1.3  2000-09-21 21:43:20  adam
7  * Better high-level server API.
8  *
9  * Revision 1.2  2000/09/12 12:09:53  adam
10  * More work on high-level server.
11  *
12  * Revision 1.1  2000/09/08 10:23:42  adam
13  * Added skeleton of yaz-z-server.
14  *
15  */
16
17 #include <yaz/log.h>
18 #include <yaz-z-server.h>
19
20
21 Yaz_Z_Server::Yaz_Z_Server(IYaz_PDU_Observable *the_PDU_Observable)
22     : Yaz_Z_Assoc(the_PDU_Observable)
23 {
24 }
25
26 /*
27  * database record.
28  */
29 void Yaz_Z_Server::create_databaseRecord (
30     Z_NamePlusRecord *rec, const char *dbname, int format, const void *buf, int len)
31 {
32     rec->databaseName = dbname ? odr_strdup (odr_encode(), dbname) : 0;
33     rec->which = Z_NamePlusRecord_databaseRecord;
34     rec->u.databaseRecord = z_ext_record (odr_encode(), format,
35                                           (const char *) buf, len);
36 }
37
38 /*
39  * surrogate diagnostic.
40  */
41 void Yaz_Z_Server::create_surrogateDiagnostics(
42     Z_NamePlusRecord *rec, const char *dbname, int error, char *const addinfo)
43 {
44     int oid[OID_SIZE];
45     int *err = (int *)odr_malloc (odr_encode(), sizeof(*err));
46     oident bib1;
47     Z_DiagRec *drec = (Z_DiagRec *)odr_malloc (odr_encode(), sizeof(*drec));
48     Z_DefaultDiagFormat *dr = (Z_DefaultDiagFormat *)
49         odr_malloc (odr_encode(), sizeof(*dr));
50     
51     bib1.proto = PROTO_Z3950;
52     bib1.oclass = CLASS_DIAGSET;
53     bib1.value = VAL_BIB1;
54
55     yaz_log(LOG_DEBUG, "SurrogateDiagnotic: %d -- %s", error, addinfo);
56     *err = error;
57     rec->databaseName = dbname ? odr_strdup (odr_encode(), dbname) : 0;
58     rec->which = Z_NamePlusRecord_surrogateDiagnostic;
59     rec->u.surrogateDiagnostic = drec;
60     drec->which = Z_DiagRec_defaultFormat;
61     drec->u.defaultFormat = dr;
62     dr->diagnosticSetId = odr_oiddup (odr_encode(),
63                                       oid_ent_to_oid(&bib1, oid));
64     dr->condition = err;
65     dr->which = Z_DefaultDiagFormat_v2Addinfo;
66     dr->u.v2Addinfo = odr_strdup (odr_encode(), addinfo ? addinfo : "");
67 }
68
69 Z_Records *Yaz_Z_Server::create_nonSurrogateDiagnostics (
70     int error, const char *addinfo)
71 {
72     int oid[OID_SIZE];
73     Z_Records *rec = (Z_Records *)
74         odr_malloc (odr_encode(), sizeof(*rec));
75     oident bib1;
76     int *err = (int *)
77         odr_malloc (odr_encode(), sizeof(*err));
78     Z_DiagRec *drec = (Z_DiagRec *)
79         odr_malloc (odr_encode(), sizeof(*drec));
80     Z_DefaultDiagFormat *dr = (Z_DefaultDiagFormat *)
81         odr_malloc (odr_encode(), sizeof(*dr));
82
83     bib1.proto = PROTO_Z3950;
84     bib1.oclass = CLASS_DIAGSET;
85     bib1.value = VAL_BIB1;
86
87     *err = error;
88     rec->which = Z_Records_NSD;
89     rec->u.nonSurrogateDiagnostic = dr;
90     dr->diagnosticSetId =
91         odr_oiddup (odr_encode(), oid_ent_to_oid(&bib1, oid));
92     dr->condition = err;
93     dr->which = Z_DefaultDiagFormat_v2Addinfo;
94     dr->u.v2Addinfo = odr_strdup (odr_encode(), addinfo ? addinfo : "");
95     return rec;
96 }
97
98 Z_Records *Yaz_Z_Server::pack_records (const char *resultSetName,
99                                        int start, int xnum,
100                                        Z_RecordComposition *comp,
101                                        int *next, int *pres,
102                                        int *format)
103 {
104     int recno, total_length = 0, toget = xnum, dumped_records = 0;
105     Z_Records *records =
106         (Z_Records *) odr_malloc (odr_encode(), sizeof(*records));
107     Z_NamePlusRecordList *reclist =
108         (Z_NamePlusRecordList *) odr_malloc (odr_encode(), sizeof(*reclist));
109     Z_NamePlusRecord **list =
110         (Z_NamePlusRecord **) odr_malloc (odr_encode(), sizeof(*list) * toget);
111
112     records->which = Z_Records_DBOSD;
113     records->u.databaseOrSurDiagnostics = reclist;
114     reclist->num_records = 0;
115     reclist->records = list;
116     *pres = Z_PRES_SUCCESS;
117     *next = 0;
118
119     yaz_log(LOG_LOG, "Request to pack %d+%d", start, toget);
120     yaz_log(LOG_LOG, "pms=%d, mrs=%d", m_preferredMessageSize,
121             m_maximumRecordSize);
122     for (recno = start; reclist->num_records < toget; recno++)
123     {
124         Z_NamePlusRecord *this_rec =
125             (Z_NamePlusRecord *) odr_malloc (odr_encode(), sizeof(*this_rec));
126         this_rec->databaseName = 0;
127         this_rec->which = Z_NamePlusRecord_databaseRecord;
128         this_rec->u.databaseRecord = 0;
129
130         int this_length = 0;
131
132         recv_Z_record (resultSetName, recno, format, comp, this_rec, records);
133
134         if (records->which != Z_Records_DBOSD)
135         {
136             *pres = Z_PRES_FAILURE;
137             return records;
138         }
139
140         if (this_rec->which == Z_NamePlusRecord_databaseRecord &&
141             this_rec->u.databaseRecord == 0)
142         {   // handler did not return a record..
143             create_surrogateDiagnostics(this_rec, 0, 14, 0);
144         }
145         /*
146          * we get the number of bytes allocated on the stream before any
147          * allocation done by the backend - this should give us a reasonable
148          * idea of the total size of the data so far.
149          */
150         total_length = odr_total(odr_encode()) - dumped_records;
151         this_length = odr_total(odr_encode()) - total_length;
152         yaz_log(LOG_LOG, "  fetched record, len=%d, total=%d",
153                 this_length, total_length);
154         if (this_length + total_length > m_preferredMessageSize)
155         {
156             /* record is small enough, really */
157             if (this_length <= m_preferredMessageSize)
158             {
159                 yaz_log(LOG_LOG, "  Dropped last normal-sized record");
160                 *pres = Z_PRES_PARTIAL_2;
161                 break;
162             }
163             if (this_length >= m_maximumRecordSize)
164             {   /* too big entirely */
165                 yaz_log(LOG_LOG, "Record > maxrcdsz");
166                 reclist->records[reclist->num_records] = this_rec;
167                 create_surrogateDiagnostics(this_rec,
168                                             this_rec->databaseName, 17, 0);
169                 reclist->num_records++;
170                 *next = recno + 1;
171                 dumped_records += this_length;
172                 continue;
173             }
174             else /* record can only be fetched by itself */
175             {
176                 yaz_log(LOG_LOG, "  Record > prefmsgsz");
177                 if (toget > 1)
178                 {
179                     yaz_log(LOG_DEBUG, "  Dropped it");
180                     reclist->records[reclist->num_records] = this_rec;
181                     create_surrogateDiagnostics(this_rec,
182                                                 this_rec->databaseName,
183                                                 16, 0);
184                     reclist->num_records++;
185                     // *next = freq.last_in_set ? 0 : recno + 1;
186                     *next = recno + 1;
187                     dumped_records += this_length;
188                     continue;
189                 }
190             }
191         }
192         reclist->records[reclist->num_records] = this_rec;
193         reclist->num_records++;
194         *next = recno + 1;
195     }
196     return records;
197 }
198
199 void Yaz_Z_Server::fetch_via_piggyback (Z_SearchRequest *req,
200                                         Z_SearchResponse *res)
201 {
202     bool_t *sr = (bool_t *)odr_malloc (odr_encode(), sizeof(*sr));
203     *sr = 1;
204     
205     int toget = 0;
206     
207     Z_RecordComposition comp, *compp = 0;
208     int hits = *res->resultCount;
209     
210     int *nulint = (int *)odr_malloc (odr_encode(), sizeof(*nulint));
211     *nulint = 0;
212     
213     comp.which = Z_RecordComp_simple;
214     /* how many records does the user agent want, then? */
215     if (hits <= *req->smallSetUpperBound)
216     {
217         toget = hits;
218         if ((comp.u.simple = req->smallSetElementSetNames))
219             compp = &comp;
220     }
221     else if (hits < *req->largeSetLowerBound)
222     {
223         toget = *req->mediumSetPresentNumber;
224         if (toget > hits)
225             toget = hits;
226         if ((comp.u.simple = req->mediumSetElementSetNames))
227             compp = &comp;
228     }
229     
230     if (toget && !res->records)
231     {
232         res->presentStatus = (int *) odr_malloc (odr_encode(), sizeof(int));
233         *res->presentStatus = Z_PRES_SUCCESS;
234         res->records =
235             pack_records(req->resultSetName, 1, toget, compp, 
236                          res->nextResultSetPosition,
237                          res->presentStatus,
238                          req->preferredRecordSyntax);
239         if (!res->records)
240             return;
241         if (res->records->which == Z_Records_DBOSD)
242             *res->numberOfRecordsReturned =
243                 res->records->u.databaseOrSurDiagnostics->num_records;
244         res->searchStatus = sr;
245         res->resultSetStatus = 0;
246     }
247     else
248     {
249         if (hits)
250             *res->nextResultSetPosition = 1;
251         res->numberOfRecordsReturned = nulint;
252         res->searchStatus = sr;
253         res->resultSetStatus = 0;
254         res->presentStatus = 0;
255     }
256 }
257
258 void Yaz_Z_Server::fetch_via_present (Z_PresentRequest *req,
259                                       Z_PresentResponse *res)
260 {
261     res->records = pack_records (req->resultSetId,*req->resultSetStartPoint, 
262                                  *req->numberOfRecordsRequested,
263                                  req->recordComposition,
264                                  res->nextResultSetPosition,
265                                  res->presentStatus,
266                                  req->preferredRecordSyntax);
267     if (res->records->which == Z_Records_DBOSD)
268         *res->numberOfRecordsReturned =
269             res->records->u.databaseOrSurDiagnostics->num_records;
270 }
271     
272 void Yaz_Z_Server::recv_Z_PDU (Z_APDU *apdu_request)
273 {   
274     Z_APDU *apdu_response;
275     switch (apdu_request->which)
276     {
277     case Z_APDU_initRequest:
278         logf (LOG_LOG, "got InitRequest");
279         apdu_response = create_Z_PDU(Z_APDU_initResponse);
280         recv_Z_init (apdu_request->u.initRequest,
281                      apdu_response->u.initResponse);
282         m_preferredMessageSize =
283             *apdu_request->u.initRequest->preferredMessageSize;
284         m_maximumRecordSize =
285             *apdu_request->u.initRequest->maximumRecordSize;
286         send_Z_PDU(apdu_response);
287         break;
288     case Z_APDU_searchRequest:
289         logf (LOG_LOG, "got SearchRequest");
290         apdu_response = create_Z_PDU(Z_APDU_searchResponse);
291         recv_Z_search (apdu_request->u.searchRequest,
292                        apdu_response->u.searchResponse);
293         if (!apdu_response->u.searchResponse->records)
294         {
295             fetch_via_piggyback(apdu_request->u.searchRequest,
296                                 apdu_response->u.searchResponse);
297         }
298         send_Z_PDU(apdu_response);
299         break;
300     case Z_APDU_presentRequest:
301         logf (LOG_LOG, "got PresentRequest");
302         apdu_response = create_Z_PDU(Z_APDU_presentResponse);
303         recv_Z_present (apdu_request->u.presentRequest,
304                         apdu_response->u.presentResponse);
305         if (!apdu_response->u.presentResponse->records)
306             fetch_via_present(apdu_request->u.presentRequest,
307                               apdu_response->u.presentResponse);
308         send_Z_PDU(apdu_response);
309         break;
310     }
311 }
312