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.11 1999-12-06 13:52:45 adam
8 * Modified for new location of YAZ header files. Experimental threaded
11 * Revision 1.10 1999/11/10 10:02:34 adam
14 * Revision 1.9 1999/09/13 12:53:44 adam
15 * Proxy removes OtherInfo Proxy Address and Session ID. Other
16 * Otherinfo remains untouched.
18 * Revision 1.8 1999/05/04 10:53:00 adam
19 * Changed the way the PROXY behaves when lost cookie is received.
21 * Revision 1.7 1999/04/28 13:31:17 adam
22 * Better result set optimisation for proxy.
24 * Revision 1.6 1999/04/27 07:52:13 adam
25 * Improved proxy; added query match for result set re-use.
27 * Revision 1.5 1999/04/21 12:09:01 adam
28 * Many improvements. Modified to proxy server to work with "sessions"
31 * Revision 1.4 1999/04/20 10:30:05 adam
32 * Implemented various stuff for client and proxy. Updated calls
33 * to ODR to reflect new name parameter.
35 * Revision 1.3 1999/04/09 11:46:57 adam
36 * Added object Yaz_Z_Assoc. Much more functional client.
38 * Revision 1.2 1999/01/28 13:08:46 adam
39 * Yaz_PDU_Assoc better encapsulated. Memory leak fix in
40 * yaz-socket-manager.cc.
42 * Revision 1.1.1.1 1999/01/28 09:41:07 adam
43 * First implementation of YAZ++.
50 #include <yaz-proxy.h>
52 Yaz_Proxy::Yaz_Proxy(IYaz_PDU_Observable *the_PDU_Observable) :
53 Yaz_Z_Assoc(the_PDU_Observable)
55 m_PDU_Observable = the_PDU_Observable;
64 Yaz_Proxy::~Yaz_Proxy()
66 xfree (m_proxyTarget);
70 void Yaz_Proxy::set_proxyTarget(const char *target)
72 xfree (m_proxyTarget);
75 m_proxyTarget = (char *) xstrdup (target);
78 IYaz_PDU_Observer *Yaz_Proxy::clone(IYaz_PDU_Observable
81 Yaz_Proxy *new_proxy = new Yaz_Proxy(the_PDU_Observable);
82 new_proxy->m_parent = this;
83 new_proxy->timeout(20);
84 new_proxy->set_proxyTarget(m_proxyTarget);
88 char *Yaz_Proxy::get_cookie(Z_OtherInformation **otherInfo)
91 Z_OtherInformationUnit *oi;
93 ent.proto = PROTO_Z3950;
94 ent.oclass = CLASS_USERINFO;
95 ent.value = (oid_value) VAL_COOKIE;
96 assert (oid_ent_to_oid (&ent, oid));
98 if (oid_ent_to_oid (&ent, oid) &&
99 (oi = update_otherInformation(otherInfo, 0, oid, 1, 1)) &&
100 oi->which == Z_OtherInfo_characterInfo)
101 return oi->information.characterInfo;
105 char *Yaz_Proxy::get_proxy(Z_OtherInformation **otherInfo)
108 Z_OtherInformationUnit *oi;
110 ent.proto = PROTO_Z3950;
111 ent.oclass = CLASS_USERINFO;
112 ent.value = (oid_value) VAL_PROXY;
113 if (oid_ent_to_oid (&ent, oid) &&
114 (oi = update_otherInformation(otherInfo, 0, oid, 1, 1)) &&
115 oi->which == Z_OtherInfo_characterInfo)
116 return oi->information.characterInfo;
120 Yaz_ProxyClient *Yaz_Proxy::get_client(Z_APDU *apdu)
123 Yaz_Proxy *parent = m_parent;
124 Z_OtherInformation **oi;
125 Yaz_ProxyClient *c = m_client;
127 get_otherInfoAPDU(apdu, &oi);
128 char *cookie = get_cookie(oi);
129 logf (LOG_LOG, "Yaz_Proxy::get_client cookie=%s", cookie ? cookie :
133 for (c = parent->m_clientPool; c; c = c->m_next)
136 assert (*c->m_prev == c);
137 if (!strcmp(cookie,c->m_cookie))
139 logf (LOG_LOG, "Yaz_Proxy::get_client found cached target");
146 if (apdu->which == Z_APDU_initRequest)
148 logf (LOG_LOG, "got InitRequest");
150 const char *proxy_host =
151 get_proxy(&apdu->u.initRequest->otherInfo);
153 set_proxyTarget(proxy_host);
154 logf (LOG_LOG, "proxy_host = %s", m_proxyTarget ?
155 m_proxyTarget:"none");
159 logf (LOG_LOG, "no first INIT!");
164 logf (LOG_LOG, "Yaz_Proxy::get_client creating new");
165 c = new Yaz_ProxyClient(m_PDU_Observable->clone());
166 c->m_next = parent->m_clientPool;
168 c->m_next->m_prev = &c->m_next;
169 parent->m_clientPool = c;
170 c->m_prev = &parent->m_clientPool;
172 sprintf (c->m_cookie, "%d", parent->m_seqno);
175 c->client(m_proxyTarget);
181 Z_APDU *Yaz_Proxy::result_set_optimize(Z_APDU *apdu)
183 if (apdu->which != Z_APDU_searchRequest)
185 Z_SearchRequest *sr = apdu->u.searchRequest;
186 Yaz_Z_Query *this_query = new Yaz_Z_Query;
188 this_query->set_Z_Query(sr->query);
190 if (m_client->m_last_query &&
191 m_client->m_last_query->match(this_query))
194 if (m_client->m_last_resultCount > *sr->smallSetUpperBound &&
195 m_client->m_last_resultCount < *sr->largeSetLowerBound)
198 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentRequest);
199 Z_PresentRequest *pr = new_apdu->u.presentRequest;
200 pr->referenceId = sr->referenceId;
201 pr->resultSetId = sr->resultSetName;
202 pr->preferredRecordSyntax = sr->preferredRecordSyntax;
203 *pr->numberOfRecordsRequested = *sr->mediumSetPresentNumber;
204 if (sr->mediumSetElementSetNames)
206 pr->recordComposition = (Z_RecordComposition *)
207 odr_malloc(odr_encode(), sizeof(Z_RecordComposition));
208 pr->recordComposition->which = Z_RecordComp_simple;
209 pr->recordComposition->u.simple = sr->mediumSetElementSetNames;
211 m_client->m_sr_transform = 1;
214 else if (m_client->m_last_resultCount >= *sr->largeSetLowerBound ||
215 m_client->m_last_resultCount == 0)
218 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
219 new_apdu->u.searchResponse->referenceId = sr->referenceId;
220 new_apdu->u.searchResponse->resultCount =
221 &m_client->m_last_resultCount;
222 send_Z_PDU(new_apdu);
228 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentRequest);
229 Z_PresentRequest *pr = new_apdu->u.presentRequest;
230 pr->referenceId = sr->referenceId;
231 pr->resultSetId = sr->resultSetName;
232 pr->preferredRecordSyntax = sr->preferredRecordSyntax;
233 *pr->numberOfRecordsRequested = m_client->m_last_resultCount;
234 if (sr->smallSetElementSetNames)
236 pr->recordComposition = (Z_RecordComposition *)
237 odr_malloc(odr_encode(), sizeof(Z_RecordComposition));
238 pr->recordComposition->which = Z_RecordComp_simple;
239 pr->recordComposition->u.simple = sr->smallSetElementSetNames;
241 m_client->m_sr_transform = 1;
247 logf (LOG_LOG, "query doesn't match");
248 delete m_client->m_last_query;
249 m_client->m_last_query = this_query;
254 void Yaz_Proxy::recv_Z_PDU(Z_APDU *apdu)
256 logf (LOG_LOG, "Yaz_Proxy::recv_Z_PDU");
257 // Determine our client.
258 m_client = get_client(apdu);
264 m_client->m_server = this;
267 Z_OtherInformation **oi;
268 get_otherInfoAPDU(apdu, &oi);
272 if (apdu->which == Z_APDU_initRequest)
274 if (m_client->m_init_flag)
276 Z_APDU *apdu = create_Z_PDU(Z_APDU_initResponse);
277 if (m_client->m_cookie)
278 set_otherInformationString(apdu, VAL_COOKIE, 1,
283 m_client->m_init_flag = 1;
285 apdu = result_set_optimize(apdu);
289 logf (LOG_LOG, "Yaz_ProxyClient::send_Z_PDU");
290 if (m_client->send_Z_PDU(apdu) < 0)
297 void Yaz_Proxy::connectNotify()
301 void Yaz_Proxy::shutdown()
303 logf (LOG_LOG, "shutdown (client to proxy)");
306 // Tell client (if any) that no server connection is there..
308 m_client->m_server = 0;
317 void Yaz_ProxyClient::shutdown()
319 logf (LOG_LOG, "shutdown (proxy to server)");
324 void Yaz_Proxy::failNotify()
326 logf (LOG_LOG, "connection closed by client");
330 void Yaz_ProxyClient::failNotify()
332 logf (LOG_LOG, "connection closed by server");
336 void Yaz_ProxyClient::connectNotify()
338 logf (LOG_LOG, "connection accepted by target");
341 IYaz_PDU_Observer *Yaz_ProxyClient::clone(IYaz_PDU_Observable
344 return new Yaz_ProxyClient(the_PDU_Observable);
347 Yaz_ProxyClient::~Yaz_ProxyClient()
353 m_next->m_prev = m_prev;
358 void Yaz_Proxy::timeoutNotify()
360 logf (LOG_LOG, "timeout (client to proxy)");
364 void Yaz_ProxyClient::timeoutNotify()
366 logf (LOG_LOG, "timeout (proxy to target)");
370 Yaz_ProxyClient::Yaz_ProxyClient(IYaz_PDU_Observable *the_PDU_Observable) :
371 Yaz_Z_Assoc (the_PDU_Observable)
378 m_last_resultCount = 0;
382 void Yaz_ProxyClient::recv_Z_PDU(Z_APDU *apdu)
384 logf (LOG_LOG, "Yaz_ProxyClient::recv_Z_PDU");
385 if (apdu->which == Z_APDU_searchResponse)
386 m_last_resultCount = *apdu->u.searchResponse->resultCount;
387 if (apdu->which == Z_APDU_presentResponse && m_sr_transform)
390 Z_PresentResponse *pr = apdu->u.presentResponse;
391 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
392 Z_SearchResponse *sr = new_apdu->u.searchResponse;
393 sr->referenceId = pr->referenceId;
394 *sr->resultCount = m_last_resultCount;
395 sr->records = pr->records;
396 sr->nextResultSetPosition = pr->nextResultSetPosition;
397 sr->numberOfRecordsReturned = pr->numberOfRecordsReturned;
401 set_otherInformationString (apdu, VAL_COOKIE, 1, m_cookie);
404 logf (LOG_LOG, "Yaz_Proxy::send_Z_PDU");
405 m_server->send_Z_PDU(apdu);