Fixed bug in proxy where a Yaz_ProxyClient could be owned by
[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.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).
9  *
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.
13  *
14  * Revision 1.3  2000/09/21 21:43:20  adam
15  * Better high-level server API.
16  *
17  * Revision 1.2  2000/09/12 12:09:53  adam
18  * More work on high-level server.
19  *
20  * Revision 1.1  2000/09/08 10:23:42  adam
21  * Added skeleton of yaz-z-server.
22  *
23  */
24
25 #include <yaz/log.h>
26 #include <yaz++/yaz-z-server.h>
27
28
29 Yaz_Z_Server::Yaz_Z_Server(IYaz_PDU_Observable *the_PDU_Observable)
30     : Yaz_Z_Assoc(the_PDU_Observable)
31 {
32 }
33
34 /*
35  * database record.
36  */
37 void Yaz_Z_Server::create_databaseRecord (
38     Z_NamePlusRecord *rec, const char *dbname, int format, const void *buf, int len)
39 {
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);
44 }
45
46 /*
47  * surrogate diagnostic.
48  */
49 void Yaz_Z_Server::create_surrogateDiagnostics(
50     Z_NamePlusRecord *rec, const char *dbname, int error, char *const addinfo)
51 {
52     int oid[OID_SIZE];
53     int *err = (int *)odr_malloc (odr_encode(), sizeof(*err));
54     oident bib1;
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));
58     
59     bib1.proto = PROTO_Z3950;
60     bib1.oclass = CLASS_DIAGSET;
61     bib1.value = VAL_BIB1;
62
63     yaz_log(LOG_DEBUG, "SurrogateDiagnotic: %d -- %s", error, addinfo);
64     *err = error;
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));
72     dr->condition = err;
73     dr->which = Z_DefaultDiagFormat_v2Addinfo;
74     dr->u.v2Addinfo = odr_strdup (odr_encode(), addinfo ? addinfo : "");
75 }
76
77 Z_Records *Yaz_Z_Server::create_nonSurrogateDiagnostics (
78     int error, const char *addinfo)
79 {
80     int oid[OID_SIZE];
81     Z_Records *rec = (Z_Records *)
82         odr_malloc (odr_encode(), sizeof(*rec));
83     oident bib1;
84     int *err = (int *)
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));
90
91     bib1.proto = PROTO_Z3950;
92     bib1.oclass = CLASS_DIAGSET;
93     bib1.value = VAL_BIB1;
94
95     *err = error;
96     rec->which = Z_Records_NSD;
97     rec->u.nonSurrogateDiagnostic = dr;
98     dr->diagnosticSetId =
99         odr_oiddup (odr_encode(), oid_ent_to_oid(&bib1, oid));
100     dr->condition = err;
101     dr->which = Z_DefaultDiagFormat_v2Addinfo;
102     dr->u.v2Addinfo = odr_strdup (odr_encode(), addinfo ? addinfo : "");
103     return rec;
104 }
105
106 Z_Records *Yaz_Z_Server::pack_records (const char *resultSetName,
107                                        int start, int xnum,
108                                        Z_RecordComposition *comp,
109                                        int *next, int *pres,
110                                        int *format)
111 {
112     int recno, total_length = 0, toget = xnum, dumped_records = 0;
113     Z_Records *records =
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);
119
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;
125     *next = 0;
126
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++)
131     {
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;
137
138         int this_length = 0;
139
140         recv_Z_record (resultSetName, recno, format, comp, this_rec, records);
141
142         if (records->which != Z_Records_DBOSD)
143         {
144             *pres = Z_PRES_FAILURE;
145             return records;
146         }
147
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);
152         }
153         /*
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.
157          */
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)
163         {
164             /* record is small enough, really */
165             if (this_length <= m_preferredMessageSize)
166             {
167                 yaz_log(LOG_LOG, "  Dropped last normal-sized record");
168                 *pres = Z_PRES_PARTIAL_2;
169                 break;
170             }
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++;
178                 *next = recno + 1;
179                 dumped_records += this_length;
180                 continue;
181             }
182             else /* record can only be fetched by itself */
183             {
184                 yaz_log(LOG_LOG, "  Record > prefmsgsz");
185                 if (toget > 1)
186                 {
187                     yaz_log(LOG_DEBUG, "  Dropped it");
188                     reclist->records[reclist->num_records] = this_rec;
189                     create_surrogateDiagnostics(this_rec,
190                                                 this_rec->databaseName,
191                                                 16, 0);
192                     reclist->num_records++;
193                     // *next = freq.last_in_set ? 0 : recno + 1;
194                     *next = recno + 1;
195                     dumped_records += this_length;
196                     continue;
197                 }
198             }
199         }
200         reclist->records[reclist->num_records] = this_rec;
201         reclist->num_records++;
202         *next = recno + 1;
203     }
204     return records;
205 }
206
207 void Yaz_Z_Server::fetch_via_piggyback (Z_SearchRequest *req,
208                                         Z_SearchResponse *res)
209 {
210     bool_t *sr = (bool_t *)odr_malloc (odr_encode(), sizeof(*sr));
211     *sr = 1;
212     
213     int toget = 0;
214     
215     Z_RecordComposition comp, *compp = 0;
216     int hits = *res->resultCount;
217     
218     int *nulint = (int *)odr_malloc (odr_encode(), sizeof(*nulint));
219     *nulint = 0;
220     
221     comp.which = Z_RecordComp_simple;
222     /* how many records does the user agent want, then? */
223     if (hits <= *req->smallSetUpperBound)
224     {
225         toget = hits;
226         if ((comp.u.simple = req->smallSetElementSetNames))
227             compp = &comp;
228     }
229     else if (hits < *req->largeSetLowerBound)
230     {
231         toget = *req->mediumSetPresentNumber;
232         if (toget > hits)
233             toget = hits;
234         if ((comp.u.simple = req->mediumSetElementSetNames))
235             compp = &comp;
236     }
237     
238     if (toget && !res->records)
239     {
240         res->presentStatus = (int *) odr_malloc (odr_encode(), sizeof(int));
241         *res->presentStatus = Z_PRES_SUCCESS;
242         res->records =
243             pack_records(req->resultSetName, 1, toget, compp, 
244                          res->nextResultSetPosition,
245                          res->presentStatus,
246                          req->preferredRecordSyntax);
247         if (!res->records)
248             return;
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;
254     }
255     else
256     {
257         if (hits)
258             *res->nextResultSetPosition = 1;
259         res->numberOfRecordsReturned = nulint;
260         res->searchStatus = sr;
261         res->resultSetStatus = 0;
262         res->presentStatus = 0;
263     }
264 }
265
266 void Yaz_Z_Server::fetch_via_present (Z_PresentRequest *req,
267                                       Z_PresentResponse *res)
268 {
269     res->records = pack_records (req->resultSetId,*req->resultSetStartPoint, 
270                                  *req->numberOfRecordsRequested,
271                                  req->recordComposition,
272                                  res->nextResultSetPosition,
273                                  res->presentStatus,
274                                  req->preferredRecordSyntax);
275     if (res->records->which == Z_Records_DBOSD)
276         *res->numberOfRecordsReturned =
277             res->records->u.databaseOrSurDiagnostics->num_records;
278 }
279     
280 void Yaz_Z_Server::recv_Z_PDU (Z_APDU *apdu_request)
281 {   
282     Z_APDU *apdu_response;
283     switch (apdu_request->which)
284     {
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);
295         break;
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)
302         {
303             fetch_via_piggyback(apdu_request->u.searchRequest,
304                                 apdu_response->u.searchResponse);
305         }
306         send_Z_PDU(apdu_response);
307         break;
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);
317         break;
318     }
319 }
320