2 * Copyright (c) 1998-2001, Index Data.
3 * See the file LICENSE for details.
5 * $Log: yaz-proxy.cpp,v $
6 * Revision 1.27 2001-11-02 13:06:29 adam
7 * Fixed bug in result_set_optimize where LSLB == resultCount.
9 * Revision 1.26 2001/08/13 16:39:12 adam
10 * PDU_Assoc keeps track of children. Using yaz_log instead of logf.
12 * Revision 1.25 2001/04/25 18:59:30 adam
13 * Added referenceId handling for server.
15 * Revision 1.24 2001/04/10 10:48:08 adam
16 * Fixed problem where proxy could cash bad result sets.
18 * Revision 1.23 2001/03/26 14:43:49 adam
19 * New threaded PDU association.
21 * Revision 1.22 2000/11/20 11:27:33 adam
22 * Fixes for connect operation (timeout and notify fix).
24 * Revision 1.21 2000/11/01 14:22:59 adam
25 * Added fd parameter for method IYaz_PDU_Observer::clone.
27 * Revision 1.20 2000/10/24 12:29:57 adam
28 * Fixed bug in proxy where a Yaz_ProxyClient could be owned by
29 * two Yaz_Proxy's (fatal).
31 * Revision 1.19 2000/10/11 11:58:16 adam
32 * Moved header files to include/yaz++. Switched to libtool and automake.
33 * Configure script creates yaz++-config script.
35 * Revision 1.18 2000/09/08 10:23:42 adam
36 * Added skeleton of yaz-z-server.
38 * Revision 1.17 2000/09/05 13:57:28 adam
39 * Fixed get_otherInfoAPDU to return otherInfo for extended services.
41 * Revision 1.16 2000/09/04 08:29:22 adam
42 * Fixed memory leak(s). Added re-use of associations, rather than
43 * re-init, when maximum number of targets are in use.
45 * Revision 1.15 2000/08/31 14:41:55 adam
46 * Proxy no longer generates cookies (it's up to the client). Proxy
47 * re-opens if target new op is started before previous operation finishes.
49 * Revision 1.14 2000/08/10 08:42:42 adam
50 * Fixes for {set,get}_APDU_log.
52 * Revision 1.13 2000/08/07 14:19:59 adam
53 * Fixed serious bug regarding timeouts. Improved logging for proxy.
55 * Revision 1.12 2000/07/04 13:48:49 adam
56 * Implemented upper-limit on proxy-to-target sessions.
58 * Revision 1.11 1999/12/06 13:52:45 adam
59 * Modified for new location of YAZ header files. Experimental threaded
62 * Revision 1.10 1999/11/10 10:02:34 adam
65 * Revision 1.9 1999/09/13 12:53:44 adam
66 * Proxy removes OtherInfo Proxy Address and Session ID. Other
67 * Otherinfo remains untouched.
69 * Revision 1.8 1999/05/04 10:53:00 adam
70 * Changed the way the PROXY behaves when lost cookie is received.
72 * Revision 1.7 1999/04/28 13:31:17 adam
73 * Better result set optimisation for proxy.
75 * Revision 1.6 1999/04/27 07:52:13 adam
76 * Improved proxy; added query match for result set re-use.
78 * Revision 1.5 1999/04/21 12:09:01 adam
79 * Many improvements. Modified to proxy server to work with "sessions"
82 * Revision 1.4 1999/04/20 10:30:05 adam
83 * Implemented various stuff for client and proxy. Updated calls
84 * to ODR to reflect new name parameter.
86 * Revision 1.3 1999/04/09 11:46:57 adam
87 * Added object Yaz_Z_Assoc. Much more functional client.
89 * Revision 1.2 1999/01/28 13:08:46 adam
90 * Yaz_PDU_Assoc better encapsulated. Memory leak fix in
91 * yaz-socket-manager.cc.
93 * Revision 1.1.1.1 1999/01/28 09:41:07 adam
94 * First implementation of YAZ++.
102 #include <yaz++/yaz-proxy.h>
104 Yaz_Proxy::Yaz_Proxy(IYaz_PDU_Observable *the_PDU_Observable) :
105 Yaz_Z_Assoc(the_PDU_Observable)
107 m_PDU_Observable = the_PDU_Observable;
118 Yaz_Proxy::~Yaz_Proxy()
120 xfree (m_proxyTarget);
123 void Yaz_Proxy::set_proxyTarget(const char *target)
125 xfree (m_proxyTarget);
128 m_proxyTarget = (char *) xstrdup (target);
131 IYaz_PDU_Observer *Yaz_Proxy::sessionNotify(IYaz_PDU_Observable
132 *the_PDU_Observable, int fd)
134 Yaz_Proxy *new_proxy = new Yaz_Proxy(the_PDU_Observable);
135 new_proxy->m_parent = this;
136 new_proxy->timeout(500);
137 new_proxy->set_proxyTarget(m_proxyTarget);
138 new_proxy->set_APDU_log(get_APDU_log());
142 char *Yaz_Proxy::get_cookie(Z_OtherInformation **otherInfo)
145 Z_OtherInformationUnit *oi;
147 ent.proto = PROTO_Z3950;
148 ent.oclass = CLASS_USERINFO;
149 ent.value = (oid_value) VAL_COOKIE;
150 assert (oid_ent_to_oid (&ent, oid));
152 if (oid_ent_to_oid (&ent, oid) &&
153 (oi = update_otherInformation(otherInfo, 0, oid, 1, 1)) &&
154 oi->which == Z_OtherInfo_characterInfo)
155 return oi->information.characterInfo;
159 char *Yaz_Proxy::get_proxy(Z_OtherInformation **otherInfo)
162 Z_OtherInformationUnit *oi;
164 ent.proto = PROTO_Z3950;
165 ent.oclass = CLASS_USERINFO;
166 ent.value = (oid_value) VAL_PROXY;
167 if (oid_ent_to_oid (&ent, oid) &&
168 (oi = update_otherInformation(otherInfo, 0, oid, 1, 1)) &&
169 oi->which == Z_OtherInfo_characterInfo)
170 return oi->information.characterInfo;
174 Yaz_ProxyClient *Yaz_Proxy::get_client(Z_APDU *apdu)
177 Yaz_Proxy *parent = m_parent;
178 Z_OtherInformation **oi;
179 Yaz_ProxyClient *c = m_client;
181 get_otherInfoAPDU(apdu, &oi);
182 char *cookie = get_cookie(oi);
183 yaz_log (LOG_LOG, "Yaz_Proxy::get_client cookie=%s", cookie ? cookie :
186 const char *proxy_host = get_proxy(oi);
188 set_proxyTarget(proxy_host);
189 yaz_log (LOG_LOG, "proxy_host = %s", m_proxyTarget ? m_proxyTarget:"none");
191 // no target specified at all?
195 if (cookie && *cookie)
197 yaz_log (LOG_LOG, "lookup of clients cookie=%s target=%s",
198 cookie, m_proxyTarget);
199 Yaz_ProxyClient *cc = 0;
201 for (c = parent->m_clientPool; c; c = c->m_next)
203 yaz_log (LOG_LOG, " found client cookie = %s target=%s seqno=%d",
204 c->m_cookie, c->get_hostname(), c->m_seqno);
206 assert (*c->m_prev == c);
207 if (!strcmp(cookie,c->m_cookie) &&
208 !strcmp(m_proxyTarget, c->get_hostname()))
210 yaz_log (LOG_LOG, "found!");
218 // The following handles "cancel"
219 // If connection is busy (waiting for PDU) and
220 // we have an initRequest we can safely do re-open
221 if (c->m_waiting && apdu->which == Z_APDU_initRequest)
223 yaz_log (LOG_LOG, "reopen target=%s", c->get_hostname());
225 c->client(m_proxyTarget);
228 delete c->m_last_query;
230 c->m_last_resultCount = 0;
231 c->m_sr_transform = 0;
235 c->m_seqno = parent->m_seqno;
236 if (c->m_server && c->m_server != this)
237 c->m_server->m_client = 0;
239 c->m_seqno = parent->m_seqno;
241 yaz_log (LOG_LOG, "get_client 1 %p %p", this, c);
247 if (apdu->which != Z_APDU_initRequest)
249 yaz_log (LOG_LOG, "no first INIT!");
252 yaz_log (LOG_LOG, "got InitRequest");
254 // go through list of clients - and find the lowest/oldest one.
255 Yaz_ProxyClient *c_min = 0;
257 int no_of_clients = 0;
258 for (c = parent->m_clientPool; c; c = c->m_next)
261 if (min_seq < 0 || c->m_seqno < min_seq)
263 min_seq = c->m_seqno;
267 if (no_of_clients >= parent->m_max_clients)
270 if (c->m_waiting || strcmp(m_proxyTarget, c->get_hostname()))
272 yaz_log (LOG_LOG, "Yaz_Proxy::get_client re-init session %d",
274 if (c->m_server && c->m_server != this)
281 "Yaz_Proxy::get_client re-use session %d to %d",
282 c->m_seqno, parent->m_seqno);
284 strcpy (c->m_cookie, cookie);
286 c->m_cookie[0] = '\0';
287 c->m_seqno = parent->m_seqno;
288 if (c->m_server && c->m_server != this)
290 c->m_server->m_client = 0;
294 yaz_log (LOG_LOG, "get_client 2 %p %p", this, c);
300 yaz_log (LOG_LOG, "Yaz_Proxy::get_client making session %d",
302 c = new Yaz_ProxyClient(m_PDU_Observable->clone());
303 c->m_next = parent->m_clientPool;
305 c->m_next->m_prev = &c->m_next;
306 parent->m_clientPool = c;
307 c->m_prev = &parent->m_clientPool;
310 strcpy (c->m_cookie, cookie);
312 c->m_cookie[0] = '\0';
313 yaz_log (LOG_LOG, "Yaz_Proxy::get_client connect to %s",
315 c->m_seqno = parent->m_seqno;
316 c->client(m_proxyTarget);
319 delete c->m_last_query;
321 c->m_last_resultCount = 0;
322 c->m_sr_transform = 0;
328 yaz_log (LOG_LOG, "get_client 3 %p %p", this, c);
332 Z_APDU *Yaz_Proxy::result_set_optimize(Z_APDU *apdu)
334 if (apdu->which != Z_APDU_searchRequest)
336 Z_SearchRequest *sr = apdu->u.searchRequest;
337 Yaz_Z_Query *this_query = new Yaz_Z_Query;
339 this_query->set_Z_Query(sr->query);
341 if (m_client->m_last_query &&
342 m_client->m_last_query->match(this_query))
345 if (m_client->m_last_resultCount > *sr->smallSetUpperBound &&
346 m_client->m_last_resultCount < *sr->largeSetLowerBound)
349 yaz_log (LOG_LOG, "Yaz_Proxy::result_set_optimize medium set");
350 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentRequest);
351 Z_PresentRequest *pr = new_apdu->u.presentRequest;
352 pr->referenceId = sr->referenceId;
353 pr->resultSetId = sr->resultSetName;
354 pr->preferredRecordSyntax = sr->preferredRecordSyntax;
355 *pr->numberOfRecordsRequested = *sr->mediumSetPresentNumber;
356 if (sr->mediumSetElementSetNames)
358 pr->recordComposition = (Z_RecordComposition *)
359 odr_malloc(odr_encode(), sizeof(Z_RecordComposition));
360 pr->recordComposition->which = Z_RecordComp_simple;
361 pr->recordComposition->u.simple = sr->mediumSetElementSetNames;
363 m_client->m_sr_transform = 1;
366 else if (m_client->m_last_resultCount >= *sr->largeSetLowerBound ||
367 m_client->m_last_resultCount == 0)
370 yaz_log (LOG_LOG, "Yaz_Proxy::result_set_optimize large set");
371 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
372 new_apdu->u.searchResponse->referenceId = sr->referenceId;
373 new_apdu->u.searchResponse->resultCount =
374 &m_client->m_last_resultCount;
375 send_Z_PDU(new_apdu);
381 yaz_log (LOG_LOG, "Yaz_Proxy::result_set_optimize small set");
382 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentRequest);
383 Z_PresentRequest *pr = new_apdu->u.presentRequest;
384 pr->referenceId = sr->referenceId;
385 pr->resultSetId = sr->resultSetName;
386 pr->preferredRecordSyntax = sr->preferredRecordSyntax;
387 *pr->numberOfRecordsRequested = m_client->m_last_resultCount;
388 if (sr->smallSetElementSetNames)
390 pr->recordComposition = (Z_RecordComposition *)
391 odr_malloc(odr_encode(), sizeof(Z_RecordComposition));
392 pr->recordComposition->which = Z_RecordComp_simple;
393 pr->recordComposition->u.simple = sr->smallSetElementSetNames;
395 m_client->m_sr_transform = 1;
401 yaz_log (LOG_LOG, "Yaz_Proxy::result_set_optimize new set");
402 delete m_client->m_last_query;
403 m_client->m_last_query = this_query;
408 void Yaz_Proxy::recv_Z_PDU(Z_APDU *apdu)
410 yaz_log (LOG_LOG, "Yaz_Proxy::recv_Z_PDU");
411 // Determine our client.
412 m_client = get_client(apdu);
418 m_client->m_server = this;
420 if (apdu->which == Z_APDU_initRequest)
422 if (m_client->m_init_flag)
424 Z_APDU *apdu = create_Z_PDU(Z_APDU_initResponse);
425 if (m_client->m_cookie)
426 set_otherInformationString(apdu, VAL_COOKIE, 1,
431 m_client->m_init_flag = 1;
433 apdu = result_set_optimize(apdu);
437 yaz_log (LOG_LOG, "Yaz_ProxyClient::send_Z_PDU %s",
438 m_client->get_hostname());
440 // delete other info part from PDU before sending to target
441 Z_OtherInformation **oi;
442 get_otherInfoAPDU(apdu, &oi);
446 if (m_client->send_Z_PDU(apdu) < 0)
453 m_client->m_waiting = 1;
456 void Yaz_Proxy::connectNotify()
460 void Yaz_Proxy::shutdown()
462 yaz_log (LOG_LOG, "shutdown (client to proxy)");
463 // only keep if keep_alive flag and cookie is set...
464 if (m_keepalive && m_client && m_client->m_cookie[0])
466 yaz_log (LOG_LOG, "shutdown - keepalive this=%p, m_server=%p",
467 this, m_client->m_server);
468 if (m_client->m_waiting == 2)
470 // Tell client (if any) that no server connection is there..
471 m_client->m_server = 0;
475 yaz_log (LOG_LOG, "deleting %p %p", this, m_client);
476 if (m_client->m_waiting == 2)
482 yaz_log (LOG_LOG, "abort %p", this);
488 void Yaz_ProxyClient::shutdown()
490 yaz_log (LOG_LOG, "shutdown (proxy to server) %s", get_hostname());
495 void Yaz_Proxy::failNotify()
497 yaz_log (LOG_LOG, "connection closed by client");
501 void Yaz_ProxyClient::failNotify()
503 yaz_log (LOG_LOG, "Yaz_ProxyClient connection closed by %s", get_hostname());
507 void Yaz_ProxyClient::connectNotify()
509 yaz_log (LOG_LOG, "Yaz_ProxyClient connection accepted by %s",
514 IYaz_PDU_Observer *Yaz_ProxyClient::sessionNotify(IYaz_PDU_Observable
515 *the_PDU_Observable, int fd)
517 return new Yaz_ProxyClient(the_PDU_Observable);
520 Yaz_ProxyClient::~Yaz_ProxyClient()
525 m_next->m_prev = m_prev;
526 m_waiting = 2; // for debugging purposes only.
530 void Yaz_Proxy::timeoutNotify()
532 yaz_log (LOG_LOG, "timeout (client to proxy)");
536 void Yaz_ProxyClient::timeoutNotify()
538 yaz_log (LOG_LOG, "timeout (proxy to target) %s", get_hostname());
542 Yaz_ProxyClient::Yaz_ProxyClient(IYaz_PDU_Observable *the_PDU_Observable) :
543 Yaz_Z_Assoc (the_PDU_Observable)
550 m_last_resultCount = 0;
555 void Yaz_ProxyClient::recv_Z_PDU(Z_APDU *apdu)
558 yaz_log (LOG_LOG, "Yaz_ProxyClient::recv_Z_PDU %s", get_hostname());
559 if (apdu->which == Z_APDU_searchResponse)
561 m_last_resultCount = *apdu->u.searchResponse->resultCount;
562 int status = *apdu->u.searchResponse->searchStatus;
564 apdu->u.searchResponse->records &&
565 apdu->u.searchResponse->records->which != Z_Records_DBOSD))
571 if (apdu->which == Z_APDU_presentResponse && m_sr_transform)
574 Z_PresentResponse *pr = apdu->u.presentResponse;
575 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
576 Z_SearchResponse *sr = new_apdu->u.searchResponse;
577 sr->referenceId = pr->referenceId;
578 *sr->resultCount = m_last_resultCount;
579 sr->records = pr->records;
580 sr->nextResultSetPosition = pr->nextResultSetPosition;
581 sr->numberOfRecordsReturned = pr->numberOfRecordsReturned;
585 set_otherInformationString (apdu, VAL_COOKIE, 1, m_cookie);
588 yaz_log (LOG_LOG, "Yaz_Proxy::send_Z_PDU");
589 m_server->send_Z_PDU(apdu);