2 * Copyright (c) 1998-2000, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
6 * $Log: yaz-proxy.cpp,v $
7 * Revision 1.16 2000-09-04 08:29:22 adam
8 * Fixed memory leak(s). Added re-use of associations, rather than
9 * re-init, when maximum number of targets are in use.
11 * Revision 1.15 2000/08/31 14:41:55 adam
12 * Proxy no longer generates cookies (it's up to the client). Proxy
13 * re-opens if target new op is started before previous operation finishes.
15 * Revision 1.14 2000/08/10 08:42:42 adam
16 * Fixes for {set,get}_APDU_log.
18 * Revision 1.13 2000/08/07 14:19:59 adam
19 * Fixed serious bug regarding timeouts. Improved logging for proxy.
21 * Revision 1.12 2000/07/04 13:48:49 adam
22 * Implemented upper-limit on proxy-to-target sessions.
24 * Revision 1.11 1999/12/06 13:52:45 adam
25 * Modified for new location of YAZ header files. Experimental threaded
28 * Revision 1.10 1999/11/10 10:02:34 adam
31 * Revision 1.9 1999/09/13 12:53:44 adam
32 * Proxy removes OtherInfo Proxy Address and Session ID. Other
33 * Otherinfo remains untouched.
35 * Revision 1.8 1999/05/04 10:53:00 adam
36 * Changed the way the PROXY behaves when lost cookie is received.
38 * Revision 1.7 1999/04/28 13:31:17 adam
39 * Better result set optimisation for proxy.
41 * Revision 1.6 1999/04/27 07:52:13 adam
42 * Improved proxy; added query match for result set re-use.
44 * Revision 1.5 1999/04/21 12:09:01 adam
45 * Many improvements. Modified to proxy server to work with "sessions"
48 * Revision 1.4 1999/04/20 10:30:05 adam
49 * Implemented various stuff for client and proxy. Updated calls
50 * to ODR to reflect new name parameter.
52 * Revision 1.3 1999/04/09 11:46:57 adam
53 * Added object Yaz_Z_Assoc. Much more functional client.
55 * Revision 1.2 1999/01/28 13:08:46 adam
56 * Yaz_PDU_Assoc better encapsulated. Memory leak fix in
57 * yaz-socket-manager.cc.
59 * Revision 1.1.1.1 1999/01/28 09:41:07 adam
60 * First implementation of YAZ++.
68 #include <yaz-proxy.h>
70 Yaz_Proxy::Yaz_Proxy(IYaz_PDU_Observable *the_PDU_Observable) :
71 Yaz_Z_Assoc(the_PDU_Observable)
73 m_PDU_Observable = the_PDU_Observable;
84 Yaz_Proxy::~Yaz_Proxy()
86 xfree (m_proxyTarget);
89 void Yaz_Proxy::set_proxyTarget(const char *target)
91 xfree (m_proxyTarget);
94 m_proxyTarget = (char *) xstrdup (target);
97 IYaz_PDU_Observer *Yaz_Proxy::clone(IYaz_PDU_Observable
100 Yaz_Proxy *new_proxy = new Yaz_Proxy(the_PDU_Observable);
101 new_proxy->m_parent = this;
102 new_proxy->timeout(120);
103 new_proxy->set_proxyTarget(m_proxyTarget);
104 new_proxy->set_APDU_log(get_APDU_log());
108 char *Yaz_Proxy::get_cookie(Z_OtherInformation **otherInfo)
111 Z_OtherInformationUnit *oi;
113 ent.proto = PROTO_Z3950;
114 ent.oclass = CLASS_USERINFO;
115 ent.value = (oid_value) VAL_COOKIE;
116 assert (oid_ent_to_oid (&ent, oid));
118 if (oid_ent_to_oid (&ent, oid) &&
119 (oi = update_otherInformation(otherInfo, 0, oid, 1, 1)) &&
120 oi->which == Z_OtherInfo_characterInfo)
121 return oi->information.characterInfo;
125 char *Yaz_Proxy::get_proxy(Z_OtherInformation **otherInfo)
128 Z_OtherInformationUnit *oi;
130 ent.proto = PROTO_Z3950;
131 ent.oclass = CLASS_USERINFO;
132 ent.value = (oid_value) VAL_PROXY;
133 if (oid_ent_to_oid (&ent, oid) &&
134 (oi = update_otherInformation(otherInfo, 0, oid, 1, 1)) &&
135 oi->which == Z_OtherInfo_characterInfo)
136 return oi->information.characterInfo;
140 Yaz_ProxyClient *Yaz_Proxy::get_client(Z_APDU *apdu)
143 Yaz_Proxy *parent = m_parent;
144 Z_OtherInformation **oi;
145 Yaz_ProxyClient *c = m_client;
147 get_otherInfoAPDU(apdu, &oi);
148 char *cookie = get_cookie(oi);
149 logf (LOG_LOG, "Yaz_Proxy::get_client cookie=%s", cookie ? cookie :
152 const char *proxy_host = get_proxy(oi);
154 set_proxyTarget(proxy_host);
155 logf (LOG_LOG, "proxy_host = %s", m_proxyTarget ? m_proxyTarget:"none");
157 // no target specified at all?
163 logf (LOG_LOG, "lookup of clients cookie=%s target=%s",
164 cookie, m_proxyTarget);
165 Yaz_ProxyClient *cc = 0;
167 for (c = parent->m_clientPool; c; c = c->m_next)
169 logf (LOG_LOG, " found client cookie = %s target=%s seqno=%d",
170 c->m_cookie, c->get_hostname(), c->m_seqno);
172 assert (*c->m_prev == c);
173 if (!strcmp(cookie,c->m_cookie) &&
174 !strcmp(m_proxyTarget, c->get_hostname()))
176 logf (LOG_LOG, "found!");
184 // The following handles "cancel"
185 // If connection is busy (waiting for PDU) and
186 // we have an initRequest we can safely do re-open
187 if (c->m_waiting && apdu->which == Z_APDU_initRequest)
189 logf (LOG_LOG, "reopen target=%s", c->get_hostname());
191 c->client(m_proxyTarget);
194 delete c->m_last_query;
196 c->m_last_resultCount = 0;
197 c->m_sr_transform = 0;
201 c->m_seqno = parent->m_seqno;
208 if (apdu->which != Z_APDU_initRequest)
210 logf (LOG_LOG, "no first INIT!");
213 logf (LOG_LOG, "got InitRequest");
215 // go through list of clients - and find the lowest/oldest one.
216 Yaz_ProxyClient *c_min = 0;
218 int no_of_clients = 0;
219 for (c = parent->m_clientPool; c; c = c->m_next)
222 if (min_seq < 0 || c->m_seqno < min_seq)
224 min_seq = c->m_seqno;
228 if (no_of_clients >= parent->m_max_clients)
231 if (c->m_waiting || strcmp(m_proxyTarget, c->get_hostname()))
233 logf (LOG_LOG, "Yaz_Proxy::get_client re-init session %d",
241 logf (LOG_LOG, "Yaz_Proxy::get_client re-use session %d to %d",
242 c->m_seqno, parent->m_seqno);
244 strcpy (c->m_cookie, cookie);
246 c->m_cookie[0] = '\0';
247 c->m_seqno = parent->m_seqno;
254 logf (LOG_LOG, "Yaz_Proxy::get_client making session %d",
256 c = new Yaz_ProxyClient(m_PDU_Observable->clone());
257 c->m_next = parent->m_clientPool;
259 c->m_next->m_prev = &c->m_next;
260 parent->m_clientPool = c;
261 c->m_prev = &parent->m_clientPool;
264 strcpy (c->m_cookie, cookie);
266 c->m_cookie[0] = '\0';
267 logf (LOG_LOG, "Yaz_Proxy::get_client connect to %s", m_proxyTarget);
268 c->m_seqno = parent->m_seqno;
269 c->client(m_proxyTarget);
272 delete c->m_last_query;
274 c->m_last_resultCount = 0;
275 c->m_sr_transform = 0;
284 Z_APDU *Yaz_Proxy::result_set_optimize(Z_APDU *apdu)
286 if (apdu->which != Z_APDU_searchRequest)
288 Z_SearchRequest *sr = apdu->u.searchRequest;
289 Yaz_Z_Query *this_query = new Yaz_Z_Query;
291 this_query->set_Z_Query(sr->query);
293 if (m_client->m_last_query &&
294 m_client->m_last_query->match(this_query))
297 if (m_client->m_last_resultCount > *sr->smallSetUpperBound &&
298 m_client->m_last_resultCount < *sr->largeSetLowerBound)
301 logf (LOG_LOG, "Yaz_Proxy::result_set_optimize medium set");
302 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentRequest);
303 Z_PresentRequest *pr = new_apdu->u.presentRequest;
304 pr->referenceId = sr->referenceId;
305 pr->resultSetId = sr->resultSetName;
306 pr->preferredRecordSyntax = sr->preferredRecordSyntax;
307 *pr->numberOfRecordsRequested = *sr->mediumSetPresentNumber;
308 if (sr->mediumSetElementSetNames)
310 pr->recordComposition = (Z_RecordComposition *)
311 odr_malloc(odr_encode(), sizeof(Z_RecordComposition));
312 pr->recordComposition->which = Z_RecordComp_simple;
313 pr->recordComposition->u.simple = sr->mediumSetElementSetNames;
315 m_client->m_sr_transform = 1;
318 else if (m_client->m_last_resultCount > *sr->largeSetLowerBound ||
319 m_client->m_last_resultCount == 0)
322 logf (LOG_LOG, "Yaz_Proxy::result_set_optimize large set");
323 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
324 new_apdu->u.searchResponse->referenceId = sr->referenceId;
325 new_apdu->u.searchResponse->resultCount =
326 &m_client->m_last_resultCount;
327 send_Z_PDU(new_apdu);
333 logf (LOG_LOG, "Yaz_Proxy::result_set_optimize small set");
334 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentRequest);
335 Z_PresentRequest *pr = new_apdu->u.presentRequest;
336 pr->referenceId = sr->referenceId;
337 pr->resultSetId = sr->resultSetName;
338 pr->preferredRecordSyntax = sr->preferredRecordSyntax;
339 *pr->numberOfRecordsRequested = m_client->m_last_resultCount;
340 if (sr->smallSetElementSetNames)
342 pr->recordComposition = (Z_RecordComposition *)
343 odr_malloc(odr_encode(), sizeof(Z_RecordComposition));
344 pr->recordComposition->which = Z_RecordComp_simple;
345 pr->recordComposition->u.simple = sr->smallSetElementSetNames;
347 m_client->m_sr_transform = 1;
353 logf (LOG_LOG, "Yaz_Proxy::result_set_optimize new set");
354 delete m_client->m_last_query;
355 m_client->m_last_query = this_query;
360 void Yaz_Proxy::recv_Z_PDU(Z_APDU *apdu)
362 logf (LOG_LOG, "Yaz_Proxy::recv_Z_PDU");
363 // Determine our client.
364 m_client = get_client(apdu);
370 m_client->m_server = this;
372 if (apdu->which == Z_APDU_initRequest)
374 if (m_client->m_init_flag)
376 Z_APDU *apdu = create_Z_PDU(Z_APDU_initResponse);
377 if (m_client->m_cookie)
378 set_otherInformationString(apdu, VAL_COOKIE, 1,
383 m_client->m_init_flag = 1;
385 apdu = result_set_optimize(apdu);
389 logf (LOG_LOG, "Yaz_ProxyClient::send_Z_PDU %s", m_client->get_hostname());
391 // delete other info part from PDU before sending to target
392 Z_OtherInformation **oi;
393 get_otherInfoAPDU(apdu, &oi);
396 if (m_client->send_Z_PDU(apdu) < 0)
402 m_client->m_waiting = 1;
405 void Yaz_Proxy::connectNotify()
409 void Yaz_Proxy::shutdown()
411 logf (LOG_LOG, "shutdown (client to proxy)");
412 // only keep if keep_alive flag and cookie is set...
413 if (m_keepalive && m_client && m_client->m_cookie[0])
415 // Tell client (if any) that no server connection is there..
416 m_client->m_server = 0;
425 void Yaz_ProxyClient::shutdown()
427 logf (LOG_LOG, "shutdown (proxy to server) %s", get_hostname());
432 void Yaz_Proxy::failNotify()
434 logf (LOG_LOG, "connection closed by client");
438 void Yaz_ProxyClient::failNotify()
440 logf (LOG_LOG, "Yaz_ProxyClient connection closed by %s", get_hostname());
444 void Yaz_ProxyClient::connectNotify()
446 logf (LOG_LOG, "Yaz_ProxyClient connection accept by %s", get_hostname());
449 IYaz_PDU_Observer *Yaz_ProxyClient::clone(IYaz_PDU_Observable
452 return new Yaz_ProxyClient(the_PDU_Observable);
455 Yaz_ProxyClient::~Yaz_ProxyClient()
461 m_next->m_prev = m_prev;
466 void Yaz_Proxy::timeoutNotify()
468 logf (LOG_LOG, "timeout (client to proxy)");
472 void Yaz_ProxyClient::timeoutNotify()
474 logf (LOG_LOG, "timeout (proxy to target) %s", get_hostname());
478 Yaz_ProxyClient::Yaz_ProxyClient(IYaz_PDU_Observable *the_PDU_Observable) :
479 Yaz_Z_Assoc (the_PDU_Observable)
486 m_last_resultCount = 0;
491 void Yaz_ProxyClient::recv_Z_PDU(Z_APDU *apdu)
494 logf (LOG_LOG, "Yaz_ProxyClient::recv_Z_PDU %s", get_hostname());
495 if (apdu->which == Z_APDU_searchResponse)
496 m_last_resultCount = *apdu->u.searchResponse->resultCount;
497 if (apdu->which == Z_APDU_presentResponse && m_sr_transform)
500 Z_PresentResponse *pr = apdu->u.presentResponse;
501 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
502 Z_SearchResponse *sr = new_apdu->u.searchResponse;
503 sr->referenceId = pr->referenceId;
504 *sr->resultCount = m_last_resultCount;
505 sr->records = pr->records;
506 sr->nextResultSetPosition = pr->nextResultSetPosition;
507 sr->numberOfRecordsReturned = pr->numberOfRecordsReturned;
511 set_otherInformationString (apdu, VAL_COOKIE, 1, m_cookie);
514 logf (LOG_LOG, "Yaz_Proxy::send_Z_PDU");
515 m_server->send_Z_PDU(apdu);