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.13 2000-08-07 14:19:59 adam
8 * Fixed serious bug regarding timeouts. Improved logging for proxy.
10 * Revision 1.12 2000/07/04 13:48:49 adam
11 * Implemented upper-limit on proxy-to-target sessions.
13 * Revision 1.11 1999/12/06 13:52:45 adam
14 * Modified for new location of YAZ header files. Experimental threaded
17 * Revision 1.10 1999/11/10 10:02:34 adam
20 * Revision 1.9 1999/09/13 12:53:44 adam
21 * Proxy removes OtherInfo Proxy Address and Session ID. Other
22 * Otherinfo remains untouched.
24 * Revision 1.8 1999/05/04 10:53:00 adam
25 * Changed the way the PROXY behaves when lost cookie is received.
27 * Revision 1.7 1999/04/28 13:31:17 adam
28 * Better result set optimisation for proxy.
30 * Revision 1.6 1999/04/27 07:52:13 adam
31 * Improved proxy; added query match for result set re-use.
33 * Revision 1.5 1999/04/21 12:09:01 adam
34 * Many improvements. Modified to proxy server to work with "sessions"
37 * Revision 1.4 1999/04/20 10:30:05 adam
38 * Implemented various stuff for client and proxy. Updated calls
39 * to ODR to reflect new name parameter.
41 * Revision 1.3 1999/04/09 11:46:57 adam
42 * Added object Yaz_Z_Assoc. Much more functional client.
44 * Revision 1.2 1999/01/28 13:08:46 adam
45 * Yaz_PDU_Assoc better encapsulated. Memory leak fix in
46 * yaz-socket-manager.cc.
48 * Revision 1.1.1.1 1999/01/28 09:41:07 adam
49 * First implementation of YAZ++.
57 #include <yaz-proxy.h>
59 Yaz_Proxy::Yaz_Proxy(IYaz_PDU_Observable *the_PDU_Observable) :
60 Yaz_Z_Assoc(the_PDU_Observable)
62 m_PDU_Observable = the_PDU_Observable;
74 Yaz_Proxy::~Yaz_Proxy()
76 xfree (m_proxyTarget);
79 void Yaz_Proxy::set_proxyTarget(const char *target)
81 xfree (m_proxyTarget);
84 m_proxyTarget = (char *) xstrdup (target);
87 IYaz_PDU_Observer *Yaz_Proxy::clone(IYaz_PDU_Observable
90 Yaz_Proxy *new_proxy = new Yaz_Proxy(the_PDU_Observable);
91 new_proxy->m_parent = this;
92 new_proxy->timeout(120);
93 new_proxy->set_proxyTarget(m_proxyTarget);
94 new_proxy->set_APDU_log(get_APDU_log());
98 char *Yaz_Proxy::get_cookie(Z_OtherInformation **otherInfo)
101 Z_OtherInformationUnit *oi;
103 ent.proto = PROTO_Z3950;
104 ent.oclass = CLASS_USERINFO;
105 ent.value = (oid_value) VAL_COOKIE;
106 assert (oid_ent_to_oid (&ent, oid));
108 if (oid_ent_to_oid (&ent, oid) &&
109 (oi = update_otherInformation(otherInfo, 0, oid, 1, 1)) &&
110 oi->which == Z_OtherInfo_characterInfo)
111 return oi->information.characterInfo;
115 char *Yaz_Proxy::get_proxy(Z_OtherInformation **otherInfo)
118 Z_OtherInformationUnit *oi;
120 ent.proto = PROTO_Z3950;
121 ent.oclass = CLASS_USERINFO;
122 ent.value = (oid_value) VAL_PROXY;
123 if (oid_ent_to_oid (&ent, oid) &&
124 (oi = update_otherInformation(otherInfo, 0, oid, 1, 1)) &&
125 oi->which == Z_OtherInfo_characterInfo)
126 return oi->information.characterInfo;
130 Yaz_ProxyClient *Yaz_Proxy::get_client(Z_APDU *apdu)
133 Yaz_Proxy *parent = m_parent;
134 Z_OtherInformation **oi;
135 Yaz_ProxyClient *c = m_client;
137 get_otherInfoAPDU(apdu, &oi);
138 char *cookie = get_cookie(oi);
139 logf (LOG_LOG, "Yaz_Proxy::get_client cookie=%s", cookie ? cookie :
143 for (c = parent->m_clientPool; c; c = c->m_next)
146 assert (*c->m_prev == c);
147 if (!strcmp(cookie,c->m_cookie))
149 logf (LOG_LOG, "Yaz_Proxy::get_client cached %s",
151 c->m_seqno = parent->m_seqno;
158 if (apdu->which == Z_APDU_initRequest)
160 logf (LOG_LOG, "got InitRequest");
162 const char *proxy_host =
163 get_proxy(&apdu->u.initRequest->otherInfo);
165 set_proxyTarget(proxy_host);
166 logf (LOG_LOG, "proxy_host = %s", m_proxyTarget ?
167 m_proxyTarget:"none");
171 logf (LOG_LOG, "no first INIT!");
177 Yaz_ProxyClient *c_min = 0;
179 int no_of_clients = 0;
180 for (c = parent->m_clientPool; c; c = c->m_next)
183 logf (LOG_LOG, "found seqno = %d", c->m_seqno);
184 if (min_seq < 0 || c->m_seqno < min_seq)
186 min_seq = c->m_seqno;
190 if (no_of_clients >= parent->m_max_clients)
193 logf (LOG_LOG, "Yaz_Proxy::get_client re-using session %d",
201 logf (LOG_LOG, "Yaz_Proxy::get_client making session %d",
203 c = new Yaz_ProxyClient(m_PDU_Observable->clone());
204 c->m_next = parent->m_clientPool;
206 c->m_next->m_prev = &c->m_next;
207 parent->m_clientPool = c;
208 c->m_prev = &parent->m_clientPool;
210 sprintf (c->m_cookie, "%lx.%d", m_seed, parent->m_seqno);
211 logf (LOG_LOG, "Yaz_Proxy::get_client connect to %s", m_proxyTarget);
212 c->m_seqno = parent->m_seqno;
213 c->client(m_proxyTarget);
216 delete c->m_last_query;
218 c->m_last_resultCount = 0;
219 c->m_sr_transform = 0;
228 Z_APDU *Yaz_Proxy::result_set_optimize(Z_APDU *apdu)
230 if (apdu->which != Z_APDU_searchRequest)
232 Z_SearchRequest *sr = apdu->u.searchRequest;
233 Yaz_Z_Query *this_query = new Yaz_Z_Query;
235 this_query->set_Z_Query(sr->query);
237 if (m_client->m_last_query &&
238 m_client->m_last_query->match(this_query))
241 if (m_client->m_last_resultCount > *sr->smallSetUpperBound &&
242 m_client->m_last_resultCount < *sr->largeSetLowerBound)
245 logf (LOG_LOG, "Yaz_Proxy::result_set_optimize medium set");
246 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentRequest);
247 Z_PresentRequest *pr = new_apdu->u.presentRequest;
248 pr->referenceId = sr->referenceId;
249 pr->resultSetId = sr->resultSetName;
250 pr->preferredRecordSyntax = sr->preferredRecordSyntax;
251 *pr->numberOfRecordsRequested = *sr->mediumSetPresentNumber;
252 if (sr->mediumSetElementSetNames)
254 pr->recordComposition = (Z_RecordComposition *)
255 odr_malloc(odr_encode(), sizeof(Z_RecordComposition));
256 pr->recordComposition->which = Z_RecordComp_simple;
257 pr->recordComposition->u.simple = sr->mediumSetElementSetNames;
259 m_client->m_sr_transform = 1;
262 else if (m_client->m_last_resultCount >= *sr->largeSetLowerBound ||
263 m_client->m_last_resultCount == 0)
266 logf (LOG_LOG, "Yaz_Proxy::result_set_optimize large set");
267 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
268 new_apdu->u.searchResponse->referenceId = sr->referenceId;
269 new_apdu->u.searchResponse->resultCount =
270 &m_client->m_last_resultCount;
271 send_Z_PDU(new_apdu);
277 logf (LOG_LOG, "Yaz_Proxy::result_set_optimize small set");
278 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentRequest);
279 Z_PresentRequest *pr = new_apdu->u.presentRequest;
280 pr->referenceId = sr->referenceId;
281 pr->resultSetId = sr->resultSetName;
282 pr->preferredRecordSyntax = sr->preferredRecordSyntax;
283 *pr->numberOfRecordsRequested = m_client->m_last_resultCount;
284 if (sr->smallSetElementSetNames)
286 pr->recordComposition = (Z_RecordComposition *)
287 odr_malloc(odr_encode(), sizeof(Z_RecordComposition));
288 pr->recordComposition->which = Z_RecordComp_simple;
289 pr->recordComposition->u.simple = sr->smallSetElementSetNames;
291 m_client->m_sr_transform = 1;
297 logf (LOG_LOG, "Yaz_Proxy::result_set_optimize new set");
298 delete m_client->m_last_query;
299 m_client->m_last_query = this_query;
304 void Yaz_Proxy::recv_Z_PDU(Z_APDU *apdu)
306 logf (LOG_LOG, "Yaz_Proxy::recv_Z_PDU");
307 // Determine our client.
308 m_client = get_client(apdu);
314 m_client->m_server = this;
317 Z_OtherInformation **oi;
318 get_otherInfoAPDU(apdu, &oi);
322 if (apdu->which == Z_APDU_initRequest)
324 if (m_client->m_init_flag)
326 Z_APDU *apdu = create_Z_PDU(Z_APDU_initResponse);
327 if (m_client->m_cookie)
328 set_otherInformationString(apdu, VAL_COOKIE, 1,
333 m_client->m_init_flag = 1;
335 apdu = result_set_optimize(apdu);
339 logf (LOG_LOG, "Yaz_ProxyClient::send_Z_PDU %s", m_client->get_hostname());
340 if (m_client->send_Z_PDU(apdu) < 0)
347 void Yaz_Proxy::connectNotify()
351 void Yaz_Proxy::shutdown()
353 logf (LOG_LOG, "shutdown (client to proxy)");
356 // Tell client (if any) that no server connection is there..
358 m_client->m_server = 0;
367 void Yaz_ProxyClient::shutdown()
369 logf (LOG_LOG, "shutdown (proxy to server) %s", get_hostname());
374 void Yaz_Proxy::failNotify()
376 logf (LOG_LOG, "connection closed by client");
380 void Yaz_ProxyClient::failNotify()
382 logf (LOG_LOG, "Yaz_ProxyClient connection closed by %s", get_hostname());
386 void Yaz_ProxyClient::connectNotify()
388 logf (LOG_LOG, "Yaz_ProxyClient connection accept by %s", get_hostname());
391 IYaz_PDU_Observer *Yaz_ProxyClient::clone(IYaz_PDU_Observable
394 return new Yaz_ProxyClient(the_PDU_Observable);
397 Yaz_ProxyClient::~Yaz_ProxyClient()
403 m_next->m_prev = m_prev;
408 void Yaz_Proxy::timeoutNotify()
410 logf (LOG_LOG, "timeout (client to proxy)");
414 void Yaz_ProxyClient::timeoutNotify()
416 logf (LOG_LOG, "timeout (proxy to target) %s", get_hostname());
420 Yaz_ProxyClient::Yaz_ProxyClient(IYaz_PDU_Observable *the_PDU_Observable) :
421 Yaz_Z_Assoc (the_PDU_Observable)
428 m_last_resultCount = 0;
432 void Yaz_ProxyClient::recv_Z_PDU(Z_APDU *apdu)
434 logf (LOG_LOG, "Yaz_ProxyClient::recv_Z_PDU %s", get_hostname());
435 if (apdu->which == Z_APDU_searchResponse)
436 m_last_resultCount = *apdu->u.searchResponse->resultCount;
437 if (apdu->which == Z_APDU_presentResponse && m_sr_transform)
440 Z_PresentResponse *pr = apdu->u.presentResponse;
441 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
442 Z_SearchResponse *sr = new_apdu->u.searchResponse;
443 sr->referenceId = pr->referenceId;
444 *sr->resultCount = m_last_resultCount;
445 sr->records = pr->records;
446 sr->nextResultSetPosition = pr->nextResultSetPosition;
447 sr->numberOfRecordsReturned = pr->numberOfRecordsReturned;
451 set_otherInformationString (apdu, VAL_COOKIE, 1, m_cookie);
454 logf (LOG_LOG, "Yaz_Proxy::send_Z_PDU");
455 m_server->send_Z_PDU(apdu);