Work on proxy.
[yazpp-moved-to-github.git] / src / yaz-proxy.cpp
1 /*
2  * Copyright (c) 1998-1999, Index Data.
3  * See the file LICENSE for details.
4  * Sebastian Hammer, Adam Dickmeiss
5  * 
6  * $Log: yaz-proxy.cpp,v $
7  * Revision 1.10  1999-11-10 10:02:34  adam
8  * Work on proxy.
9  *
10  * Revision 1.9  1999/09/13 12:53:44  adam
11  * Proxy removes OtherInfo Proxy Address and Session ID. Other
12  * Otherinfo remains untouched.
13  *
14  * Revision 1.8  1999/05/04 10:53:00  adam
15  * Changed the way the PROXY behaves when lost cookie is received.
16  *
17  * Revision 1.7  1999/04/28 13:31:17  adam
18  * Better result set optimisation for proxy.
19  *
20  * Revision 1.6  1999/04/27 07:52:13  adam
21  * Improved proxy; added query match for result set re-use.
22  *
23  * Revision 1.5  1999/04/21 12:09:01  adam
24  * Many improvements. Modified to proxy server to work with "sessions"
25  * based on cookies.
26  *
27  * Revision 1.4  1999/04/20 10:30:05  adam
28  * Implemented various stuff for client and proxy. Updated calls
29  * to ODR to reflect new name parameter.
30  *
31  * Revision 1.3  1999/04/09 11:46:57  adam
32  * Added object Yaz_Z_Assoc. Much more functional client.
33  *
34  * Revision 1.2  1999/01/28 13:08:46  adam
35  * Yaz_PDU_Assoc better encapsulated. Memory leak fix in
36  * yaz-socket-manager.cc.
37  *
38  * Revision 1.1.1.1  1999/01/28 09:41:07  adam
39  * First implementation of YAZ++.
40  *
41  */
42
43 #include <assert.h>
44
45 #include <log.h>
46
47 #include <yaz-proxy.h>
48
49 Yaz_Proxy::Yaz_Proxy(IYaz_PDU_Observable *the_PDU_Observable) :
50     Yaz_Z_Assoc(the_PDU_Observable)
51 {
52     m_PDU_Observable = the_PDU_Observable;
53     m_client = 0;
54     m_parent = 0;
55     m_clientPool = 0;
56     m_seqno = 1;
57     m_keepalive = 1;
58     m_proxyTarget = 0;
59 }
60
61 Yaz_Proxy::~Yaz_Proxy()
62 {
63     xfree (m_proxyTarget);
64 }
65
66
67 void Yaz_Proxy::proxyTarget(const char *target)
68 {
69     xfree (m_proxyTarget);
70     m_proxyTarget = 0;
71     if (target)
72         m_proxyTarget = (char *) xstrdup (target);
73 }
74
75 IYaz_PDU_Observer *Yaz_Proxy::clone(IYaz_PDU_Observable
76                                     *the_PDU_Observable)
77 {
78     Yaz_Proxy *new_proxy = new Yaz_Proxy(the_PDU_Observable);
79     new_proxy->m_parent = this;
80     new_proxy->timeout(120);
81     return new_proxy;
82 }
83
84 char *Yaz_Proxy::get_cookie(Z_OtherInformation **otherInfo)
85 {
86     int oid[OID_SIZE];
87     Z_OtherInformationUnit *oi;
88     struct oident ent;
89     ent.proto = PROTO_Z3950;
90     ent.oclass = CLASS_USERINFO;
91     ent.value = (oid_value) VAL_COOKIE;
92     assert (oid_ent_to_oid (&ent, oid));
93
94     if (oid_ent_to_oid (&ent, oid) && 
95         (oi = update_otherInformation(otherInfo, 0, oid, 1, 1)) &&
96         oi->which == Z_OtherInfo_characterInfo)
97         return oi->information.characterInfo;
98     return 0;
99 }
100
101 char *Yaz_Proxy::get_proxy(Z_OtherInformation **otherInfo)
102 {
103     int oid[OID_SIZE];
104     Z_OtherInformationUnit *oi;
105     struct oident ent;
106     ent.proto = PROTO_Z3950;
107     ent.oclass = CLASS_USERINFO;
108     ent.value = (oid_value) VAL_PROXY;
109     if (oid_ent_to_oid (&ent, oid) &&
110         (oi = update_otherInformation(otherInfo, 0, oid, 1, 1)) &&
111         oi->which == Z_OtherInfo_characterInfo)
112         return oi->information.characterInfo;
113     return 0;
114 }
115
116 Yaz_ProxyClient *Yaz_Proxy::get_client(Z_APDU *apdu)
117 {
118     assert (m_parent);
119     Yaz_Proxy *parent = m_parent;
120     Z_OtherInformation **oi;
121     Yaz_ProxyClient *c = m_client;
122     
123     get_otherInfoAPDU(apdu, &oi);
124     char *cookie = get_cookie(oi);
125     logf (LOG_LOG, "Yaz_Proxy::get_client cookie=%s", cookie ? cookie :
126           "null");
127     if (cookie)
128     {
129         for (c = parent->m_clientPool; c; c = c->m_next)
130         {
131             assert (c->m_prev);
132             assert (*c->m_prev == c);
133             if (!strcmp(cookie,c->m_cookie))
134             {
135                 logf (LOG_LOG, "Yaz_Proxy::get_client cached");
136                 return c;
137             }
138         }
139         
140     }
141     if (!m_client)
142     {
143         const char *proxy_host = 0;
144         if (apdu->which == Z_APDU_initRequest)
145         {
146             logf (LOG_LOG, "got InitRequest");
147             
148             char *proxy_host = get_proxy(&apdu->u.initRequest->otherInfo);
149             if (!proxy_host)
150                 proxy_host = m_proxyTarget;
151             if (!proxy_host)
152                 return 0;
153         }
154         logf (LOG_LOG, "Yaz_Proxy::get_client creating new");
155         c = new Yaz_ProxyClient(m_PDU_Observable->clone());
156         c->m_next = parent->m_clientPool;
157         if (c->m_next)
158             c->m_next->m_prev = &c->m_next;
159         parent->m_clientPool = c;
160         c->m_prev = &parent->m_clientPool;
161
162         sprintf (c->m_cookie, "%d", parent->m_seqno);
163         (parent->m_seqno)++;
164
165         if (apdu->which == Z_APDU_initRequest)
166         {
167             logf (LOG_LOG, "got InitRequest");
168             
169             c->client(proxy_host);
170         }
171         c->timeout(600);
172     }
173     return c;
174 }
175
176 Z_APDU *Yaz_Proxy::result_set_optimize(Z_APDU *apdu)
177 {
178     if (apdu->which != Z_APDU_searchRequest)
179         return apdu;
180     Z_SearchRequest *sr = apdu->u.searchRequest;
181     Yaz_Z_Query *this_query = new Yaz_Z_Query;
182     
183     this_query->set_Z_Query(sr->query);
184     
185     if (m_client->m_last_query &&
186         m_client->m_last_query->match(this_query))
187     {
188         delete this_query;
189         if (m_client->m_last_resultCount > *sr->smallSetUpperBound &&
190             m_client->m_last_resultCount < *sr->largeSetLowerBound)
191         {
192             // medium Set
193             Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentRequest);
194             Z_PresentRequest *pr = new_apdu->u.presentRequest;
195             pr->referenceId = sr->referenceId;
196             pr->resultSetId = sr->resultSetName;
197             pr->preferredRecordSyntax = sr->preferredRecordSyntax;
198             *pr->numberOfRecordsRequested = *sr->mediumSetPresentNumber;
199             if (sr->mediumSetElementSetNames)
200             {
201                 pr->recordComposition = (Z_RecordComposition *)
202                     odr_malloc(odr_encode(), sizeof(Z_RecordComposition));
203                 pr->recordComposition->which = Z_RecordComp_simple;
204                 pr->recordComposition->u.simple = sr->mediumSetElementSetNames;
205             }
206             m_client->m_sr_transform = 1;
207             return new_apdu;
208         }
209         else if (m_client->m_last_resultCount >= *sr->largeSetLowerBound ||
210             m_client->m_last_resultCount == 0)
211         {
212             // large set
213             Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
214             new_apdu->u.searchResponse->referenceId = sr->referenceId;
215             new_apdu->u.searchResponse->resultCount =
216                 &m_client->m_last_resultCount;
217             send_Z_PDU(new_apdu);
218             return 0;
219         }
220         else
221         {
222             // small set
223             Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentRequest);
224             Z_PresentRequest *pr = new_apdu->u.presentRequest;
225             pr->referenceId = sr->referenceId;
226             pr->resultSetId = sr->resultSetName;
227             pr->preferredRecordSyntax = sr->preferredRecordSyntax;
228             *pr->numberOfRecordsRequested = m_client->m_last_resultCount;
229             if (sr->smallSetElementSetNames)
230             {
231                 pr->recordComposition = (Z_RecordComposition *)
232                     odr_malloc(odr_encode(), sizeof(Z_RecordComposition));
233                 pr->recordComposition->which = Z_RecordComp_simple;
234                 pr->recordComposition->u.simple = sr->smallSetElementSetNames;
235             }
236             m_client->m_sr_transform = 1;
237             return new_apdu;
238         }
239     }
240     else
241     {
242         logf (LOG_LOG, "query doesn't match");
243         delete m_client->m_last_query;
244         m_client->m_last_query = this_query;
245     }
246     return apdu;
247 }
248
249 void Yaz_Proxy::recv_Z_PDU(Z_APDU *apdu)
250 {
251     logf (LOG_LOG, "Yaz_Proxy::recv_Z_PDU");
252     // Determine our client.
253     m_client = get_client(apdu);
254     if (!m_client)
255     {
256         delete this;
257         return;
258     }
259     m_client->m_server = this;
260
261 #if 0
262     Z_OtherInformation **oi;
263     get_otherInfoAPDU(apdu, &oi);
264     *oi = 0;
265 #endif
266
267     if (apdu->which == Z_APDU_initRequest)
268     {
269         if (m_client->m_init_flag)
270         {
271             Z_APDU *apdu = create_Z_PDU(Z_APDU_initResponse);
272             if (m_client->m_cookie)
273                 set_otherInformationString(apdu, VAL_COOKIE, 1,
274                                            m_client->m_cookie);
275             send_Z_PDU(apdu);
276             return;
277         }
278         m_client->m_init_flag = 1;
279     }
280     apdu = result_set_optimize(apdu);
281     if (!apdu)
282         return;
283
284     logf (LOG_LOG, "Yaz_ProxyClient::send_Z_PDU");
285     if (m_client->send_Z_PDU(apdu) < 0)
286     {
287         delete m_client;
288         delete this;
289     }
290 }
291
292 void Yaz_Proxy::failNotify()
293 {
294     logf (LOG_LOG, "failNotity server");
295     if (m_keepalive)
296     {
297         // Tell client (if any) that no server connection is there..
298         if (m_client)
299             m_client->m_server = 0;
300     }
301     else
302     {
303         delete m_client;
304     }
305     delete this;
306 }
307
308 void Yaz_ProxyClient::failNotify()
309 {
310     logf (LOG_LOG, "failNotity client");
311     delete m_server;
312     delete this;
313 }
314
315 IYaz_PDU_Observer *Yaz_ProxyClient::clone(IYaz_PDU_Observable
316                                           *the_PDU_Observable)
317 {
318     return new Yaz_ProxyClient(the_PDU_Observable);
319 }
320
321 Yaz_ProxyClient::~Yaz_ProxyClient()
322 {
323     if (m_prev)
324     {
325         *m_prev = m_next;
326         if (m_next)
327             m_next->m_prev = m_prev;
328     }
329     delete m_last_query;
330 }
331
332 void Yaz_Proxy::timeoutNotify()
333 {
334     failNotify();
335 }
336
337 void Yaz_ProxyClient::timeoutNotify()
338 {
339     failNotify();
340 }
341
342 Yaz_ProxyClient::Yaz_ProxyClient(IYaz_PDU_Observable *the_PDU_Observable) :
343     Yaz_Z_Assoc (the_PDU_Observable)
344 {
345     m_cookie[0] = 0;
346     m_next = 0;
347     m_prev = 0;
348     m_init_flag = 0;
349     m_last_query = 0;
350     m_last_resultCount = 0;
351     m_sr_transform = 0;
352 }
353
354 void Yaz_ProxyClient::recv_Z_PDU(Z_APDU *apdu)
355 {
356     logf (LOG_LOG, "Yaz_ProxyClient::recv_Z_PDU");
357     if (apdu->which == Z_APDU_searchResponse)
358         m_last_resultCount = *apdu->u.searchResponse->resultCount;
359     if (apdu->which == Z_APDU_presentResponse && m_sr_transform)
360     {
361         m_sr_transform = 0;
362         Z_PresentResponse *pr = apdu->u.presentResponse;
363         Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
364         Z_SearchResponse *sr = new_apdu->u.searchResponse;
365         sr->referenceId = pr->referenceId;
366         *sr->resultCount = m_last_resultCount;
367         sr->records = pr->records;
368         sr->nextResultSetPosition = pr->nextResultSetPosition;
369         sr->numberOfRecordsReturned = pr->numberOfRecordsReturned;
370         apdu = new_apdu;
371     }
372     if (m_cookie)
373         set_otherInformationString (apdu, VAL_COOKIE, 1, m_cookie);
374     if (m_server)
375     {
376         logf (LOG_LOG, "Yaz_Proxy::send_Z_PDU");
377         m_server->send_Z_PDU(apdu);
378     }
379 }