2 * Copyright (c) 1998-1999, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
6 * $Log: yaz-proxy.cpp,v $
7 * Revision 1.10 1999-11-10 10:02:34 adam
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.
14 * Revision 1.8 1999/05/04 10:53:00 adam
15 * Changed the way the PROXY behaves when lost cookie is received.
17 * Revision 1.7 1999/04/28 13:31:17 adam
18 * Better result set optimisation for proxy.
20 * Revision 1.6 1999/04/27 07:52:13 adam
21 * Improved proxy; added query match for result set re-use.
23 * Revision 1.5 1999/04/21 12:09:01 adam
24 * Many improvements. Modified to proxy server to work with "sessions"
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.
31 * Revision 1.3 1999/04/09 11:46:57 adam
32 * Added object Yaz_Z_Assoc. Much more functional client.
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.
38 * Revision 1.1.1.1 1999/01/28 09:41:07 adam
39 * First implementation of YAZ++.
47 #include <yaz-proxy.h>
49 Yaz_Proxy::Yaz_Proxy(IYaz_PDU_Observable *the_PDU_Observable) :
50 Yaz_Z_Assoc(the_PDU_Observable)
52 m_PDU_Observable = the_PDU_Observable;
61 Yaz_Proxy::~Yaz_Proxy()
63 xfree (m_proxyTarget);
67 void Yaz_Proxy::proxyTarget(const char *target)
69 xfree (m_proxyTarget);
72 m_proxyTarget = (char *) xstrdup (target);
75 IYaz_PDU_Observer *Yaz_Proxy::clone(IYaz_PDU_Observable
78 Yaz_Proxy *new_proxy = new Yaz_Proxy(the_PDU_Observable);
79 new_proxy->m_parent = this;
80 new_proxy->timeout(120);
84 char *Yaz_Proxy::get_cookie(Z_OtherInformation **otherInfo)
87 Z_OtherInformationUnit *oi;
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));
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;
101 char *Yaz_Proxy::get_proxy(Z_OtherInformation **otherInfo)
104 Z_OtherInformationUnit *oi;
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;
116 Yaz_ProxyClient *Yaz_Proxy::get_client(Z_APDU *apdu)
119 Yaz_Proxy *parent = m_parent;
120 Z_OtherInformation **oi;
121 Yaz_ProxyClient *c = m_client;
123 get_otherInfoAPDU(apdu, &oi);
124 char *cookie = get_cookie(oi);
125 logf (LOG_LOG, "Yaz_Proxy::get_client cookie=%s", cookie ? cookie :
129 for (c = parent->m_clientPool; c; c = c->m_next)
132 assert (*c->m_prev == c);
133 if (!strcmp(cookie,c->m_cookie))
135 logf (LOG_LOG, "Yaz_Proxy::get_client cached");
143 const char *proxy_host = 0;
144 if (apdu->which == Z_APDU_initRequest)
146 logf (LOG_LOG, "got InitRequest");
148 char *proxy_host = get_proxy(&apdu->u.initRequest->otherInfo);
150 proxy_host = m_proxyTarget;
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;
158 c->m_next->m_prev = &c->m_next;
159 parent->m_clientPool = c;
160 c->m_prev = &parent->m_clientPool;
162 sprintf (c->m_cookie, "%d", parent->m_seqno);
165 if (apdu->which == Z_APDU_initRequest)
167 logf (LOG_LOG, "got InitRequest");
169 c->client(proxy_host);
176 Z_APDU *Yaz_Proxy::result_set_optimize(Z_APDU *apdu)
178 if (apdu->which != Z_APDU_searchRequest)
180 Z_SearchRequest *sr = apdu->u.searchRequest;
181 Yaz_Z_Query *this_query = new Yaz_Z_Query;
183 this_query->set_Z_Query(sr->query);
185 if (m_client->m_last_query &&
186 m_client->m_last_query->match(this_query))
189 if (m_client->m_last_resultCount > *sr->smallSetUpperBound &&
190 m_client->m_last_resultCount < *sr->largeSetLowerBound)
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)
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;
206 m_client->m_sr_transform = 1;
209 else if (m_client->m_last_resultCount >= *sr->largeSetLowerBound ||
210 m_client->m_last_resultCount == 0)
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);
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)
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;
236 m_client->m_sr_transform = 1;
242 logf (LOG_LOG, "query doesn't match");
243 delete m_client->m_last_query;
244 m_client->m_last_query = this_query;
249 void Yaz_Proxy::recv_Z_PDU(Z_APDU *apdu)
251 logf (LOG_LOG, "Yaz_Proxy::recv_Z_PDU");
252 // Determine our client.
253 m_client = get_client(apdu);
259 m_client->m_server = this;
262 Z_OtherInformation **oi;
263 get_otherInfoAPDU(apdu, &oi);
267 if (apdu->which == Z_APDU_initRequest)
269 if (m_client->m_init_flag)
271 Z_APDU *apdu = create_Z_PDU(Z_APDU_initResponse);
272 if (m_client->m_cookie)
273 set_otherInformationString(apdu, VAL_COOKIE, 1,
278 m_client->m_init_flag = 1;
280 apdu = result_set_optimize(apdu);
284 logf (LOG_LOG, "Yaz_ProxyClient::send_Z_PDU");
285 if (m_client->send_Z_PDU(apdu) < 0)
292 void Yaz_Proxy::failNotify()
294 logf (LOG_LOG, "failNotity server");
297 // Tell client (if any) that no server connection is there..
299 m_client->m_server = 0;
308 void Yaz_ProxyClient::failNotify()
310 logf (LOG_LOG, "failNotity client");
315 IYaz_PDU_Observer *Yaz_ProxyClient::clone(IYaz_PDU_Observable
318 return new Yaz_ProxyClient(the_PDU_Observable);
321 Yaz_ProxyClient::~Yaz_ProxyClient()
327 m_next->m_prev = m_prev;
332 void Yaz_Proxy::timeoutNotify()
337 void Yaz_ProxyClient::timeoutNotify()
342 Yaz_ProxyClient::Yaz_ProxyClient(IYaz_PDU_Observable *the_PDU_Observable) :
343 Yaz_Z_Assoc (the_PDU_Observable)
350 m_last_resultCount = 0;
354 void Yaz_ProxyClient::recv_Z_PDU(Z_APDU *apdu)
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)
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;
373 set_otherInformationString (apdu, VAL_COOKIE, 1, m_cookie);
376 logf (LOG_LOG, "Yaz_Proxy::send_Z_PDU");
377 m_server->send_Z_PDU(apdu);