Moved header files to include/yaz++. Switched to libtool and automake.
[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.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.
9  *
10  * Revision 1.3  2000/09/21 21:43:20  adam
11  * Better high-level server API.
12  *
13  * Revision 1.2  2000/09/12 12:09:53  adam
14  * More work on high-level server.
15  *
16  * Revision 1.1  2000/09/08 10:23:42  adam
17  * Added skeleton of yaz-z-server.
18  *
19  */
20
21 #include <yaz/log.h>
22 #include <yaz++/yaz-z-server.h>
23
24
25 Yaz_Z_Server::Yaz_Z_Server(IYaz_PDU_Observable *the_PDU_Observable)
26     : Yaz_Z_Assoc(the_PDU_Observable)
27 {
28 }
29
30 /*
31  * database record.
32  */
33 void Yaz_Z_Server::create_databaseRecord (
34     Z_NamePlusRecord *rec, const char *dbname, int format, const void *buf, int len)
35 {
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);
40 }
41
42 /*
43  * surrogate diagnostic.
44  */
45 void Yaz_Z_Server::create_surrogateDiagnostics(
46     Z_NamePlusRecord *rec, const char *dbname, int error, char *const addinfo)
47 {
48     int oid[OID_SIZE];
49     int *err = (int *)odr_malloc (odr_encode(), sizeof(*err));
50     oident bib1;
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));
54     
55     bib1.proto = PROTO_Z3950;
56     bib1.oclass = CLASS_DIAGSET;
57     bib1.value = VAL_BIB1;
58
59     yaz_log(LOG_DEBUG, "SurrogateDiagnotic: %d -- %s", error, addinfo);
60     *err = error;
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));
68     dr->condition = err;
69     dr->which = Z_DefaultDiagFormat_v2Addinfo;
70     dr->u.v2Addinfo = odr_strdup (odr_encode(), addinfo ? addinfo : "");
71 }
72
73 Z_Records *Yaz_Z_Server::create_nonSurrogateDiagnostics (
74     int error, const char *addinfo)
75 {
76     int oid[OID_SIZE];
77     Z_Records *rec = (Z_Records *)
78         odr_malloc (odr_encode(), sizeof(*rec));
79     oident bib1;
80     int *err = (int *)
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));
86
87     bib1.proto = PROTO_Z3950;
88     bib1.oclass = CLASS_DIAGSET;
89     bib1.value = VAL_BIB1;
90
91     *err = error;
92     rec->which = Z_Records_NSD;
93     rec->u.nonSurrogateDiagnostic = dr;
94     dr->diagnosticSetId =
95         odr_oiddup (odr_encode(), oid_ent_to_oid(&bib1, oid));
96     dr->condition = err;
97     dr->which = Z_DefaultDiagFormat_v2Addinfo;
98     dr->u.v2Addinfo = odr_strdup (odr_encode(), addinfo ? addinfo : "");
99     return rec;
100 }
101
102 Z_Records *Yaz_Z_Server::pack_records (const char *resultSetName,
103                                        int start, int xnum,
104                                        Z_RecordComposition *comp,
105                                        int *next, int *pres,
106                                        int *format)
107 {
108     int recno, total_length = 0, toget = xnum, dumped_records = 0;
109     Z_Records *records =
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);
115
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;
121     *next = 0;
122
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++)
127     {
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;
133
134         int this_length = 0;
135
136         recv_Z_record (resultSetName, recno, format, comp, this_rec, records);
137
138         if (records->which != Z_Records_DBOSD)
139         {
140             *pres = Z_PRES_FAILURE;
141             return records;
142         }
143
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);
148         }
149         /*
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.
153          */
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)
159         {
160             /* record is small enough, really */
161             if (this_length <= m_preferredMessageSize)
162             {
163                 yaz_log(LOG_LOG, "  Dropped last normal-sized record");
164                 *pres = Z_PRES_PARTIAL_2;
165                 break;
166             }
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++;
174                 *next = recno + 1;
175                 dumped_records += this_length;
176                 continue;
177             }
178             else /* record can only be fetched by itself */
179             {
180                 yaz_log(LOG_LOG, "  Record > prefmsgsz");
181                 if (toget > 1)
182                 {
183                     yaz_log(LOG_DEBUG, "  Dropped it");
184                     reclist->records[reclist->num_records] = this_rec;
185                     create_surrogateDiagnostics(this_rec,
186                                                 this_rec->databaseName,
187                                                 16, 0);
188                     reclist->num_records++;
189                     // *next = freq.last_in_set ? 0 : recno + 1;
190                     *next = recno + 1;
191                     dumped_records += this_length;
192                     continue;
193                 }
194             }
195         }
196         reclist->records[reclist->num_records] = this_rec;
197         reclist->num_records++;
198         *next = recno + 1;
199     }
200     return records;
201 }
202
203 void Yaz_Z_Server::fetch_via_piggyback (Z_SearchRequest *req,
204                                         Z_SearchResponse *res)
205 {
206     bool_t *sr = (bool_t *)odr_malloc (odr_encode(), sizeof(*sr));
207     *sr = 1;
208     
209     int toget = 0;
210     
211     Z_RecordComposition comp, *compp = 0;
212     int hits = *res->resultCount;
213     
214     int *nulint = (int *)odr_malloc (odr_encode(), sizeof(*nulint));
215     *nulint = 0;
216     
217     comp.which = Z_RecordComp_simple;
218     /* how many records does the user agent want, then? */
219     if (hits <= *req->smallSetUpperBound)
220     {
221         toget = hits;
222         if ((comp.u.simple = req->smallSetElementSetNames))
223             compp = &comp;
224     }
225     else if (hits < *req->largeSetLowerBound)
226     {
227         toget = *req->mediumSetPresentNumber;
228         if (toget > hits)
229             toget = hits;
230         if ((comp.u.simple = req->mediumSetElementSetNames))
231             compp = &comp;
232     }
233     
234     if (toget && !res->records)
235     {
236         res->presentStatus = (int *) odr_malloc (odr_encode(), sizeof(int));
237         *res->presentStatus = Z_PRES_SUCCESS;
238         res->records =
239             pack_records(req->resultSetName, 1, toget, compp, 
240                          res->nextResultSetPosition,
241                          res->presentStatus,
242                          req->preferredRecordSyntax);
243         if (!res->records)
244             return;
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;
250     }
251     else
252     {
253         if (hits)
254             *res->nextResultSetPosition = 1;
255         res->numberOfRecordsReturned = nulint;
256         res->searchStatus = sr;
257         res->resultSetStatus = 0;
258         res->presentStatus = 0;
259     }
260 }
261
262 void Yaz_Z_Server::fetch_via_present (Z_PresentRequest *req,
263                                       Z_PresentResponse *res)
264 {
265     res->records = pack_records (req->resultSetId,*req->resultSetStartPoint, 
266                                  *req->numberOfRecordsRequested,
267                                  req->recordComposition,
268                                  res->nextResultSetPosition,
269                                  res->presentStatus,
270                                  req->preferredRecordSyntax);
271     if (res->records->which == Z_Records_DBOSD)
272         *res->numberOfRecordsReturned =
273             res->records->u.databaseOrSurDiagnostics->num_records;
274 }
275     
276 void Yaz_Z_Server::recv_Z_PDU (Z_APDU *apdu_request)
277 {   
278     Z_APDU *apdu_response;
279     switch (apdu_request->which)
280     {
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);
291         break;
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)
298         {
299             fetch_via_piggyback(apdu_request->u.searchRequest,
300                                 apdu_response->u.searchResponse);
301         }
302         send_Z_PDU(apdu_response);
303         break;
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);
313         break;
314     }
315 }
316