2 * Copyright (c) 1998-2001, Index Data.
3 * See the file LICENSE for details.
5 * $Log: yaz-proxy.cpp,v $
6 * Revision 1.26 2001-08-13 16:39:12 adam
7 * PDU_Assoc keeps track of children. Using yaz_log instead of logf.
9 * Revision 1.25 2001/04/25 18:59:30 adam
10 * Added referenceId handling for server.
12 * Revision 1.24 2001/04/10 10:48:08 adam
13 * Fixed problem where proxy could cash bad result sets.
15 * Revision 1.23 2001/03/26 14:43:49 adam
16 * New threaded PDU association.
18 * Revision 1.22 2000/11/20 11:27:33 adam
19 * Fixes for connect operation (timeout and notify fix).
21 * Revision 1.21 2000/11/01 14:22:59 adam
22 * Added fd parameter for method IYaz_PDU_Observer::clone.
24 * Revision 1.20 2000/10/24 12:29:57 adam
25 * Fixed bug in proxy where a Yaz_ProxyClient could be owned by
26 * two Yaz_Proxy's (fatal).
28 * Revision 1.19 2000/10/11 11:58:16 adam
29 * Moved header files to include/yaz++. Switched to libtool and automake.
30 * Configure script creates yaz++-config script.
32 * Revision 1.18 2000/09/08 10:23:42 adam
33 * Added skeleton of yaz-z-server.
35 * Revision 1.17 2000/09/05 13:57:28 adam
36 * Fixed get_otherInfoAPDU to return otherInfo for extended services.
38 * Revision 1.16 2000/09/04 08:29:22 adam
39 * Fixed memory leak(s). Added re-use of associations, rather than
40 * re-init, when maximum number of targets are in use.
42 * Revision 1.15 2000/08/31 14:41:55 adam
43 * Proxy no longer generates cookies (it's up to the client). Proxy
44 * re-opens if target new op is started before previous operation finishes.
46 * Revision 1.14 2000/08/10 08:42:42 adam
47 * Fixes for {set,get}_APDU_log.
49 * Revision 1.13 2000/08/07 14:19:59 adam
50 * Fixed serious bug regarding timeouts. Improved logging for proxy.
52 * Revision 1.12 2000/07/04 13:48:49 adam
53 * Implemented upper-limit on proxy-to-target sessions.
55 * Revision 1.11 1999/12/06 13:52:45 adam
56 * Modified for new location of YAZ header files. Experimental threaded
59 * Revision 1.10 1999/11/10 10:02:34 adam
62 * Revision 1.9 1999/09/13 12:53:44 adam
63 * Proxy removes OtherInfo Proxy Address and Session ID. Other
64 * Otherinfo remains untouched.
66 * Revision 1.8 1999/05/04 10:53:00 adam
67 * Changed the way the PROXY behaves when lost cookie is received.
69 * Revision 1.7 1999/04/28 13:31:17 adam
70 * Better result set optimisation for proxy.
72 * Revision 1.6 1999/04/27 07:52:13 adam
73 * Improved proxy; added query match for result set re-use.
75 * Revision 1.5 1999/04/21 12:09:01 adam
76 * Many improvements. Modified to proxy server to work with "sessions"
79 * Revision 1.4 1999/04/20 10:30:05 adam
80 * Implemented various stuff for client and proxy. Updated calls
81 * to ODR to reflect new name parameter.
83 * Revision 1.3 1999/04/09 11:46:57 adam
84 * Added object Yaz_Z_Assoc. Much more functional client.
86 * Revision 1.2 1999/01/28 13:08:46 adam
87 * Yaz_PDU_Assoc better encapsulated. Memory leak fix in
88 * yaz-socket-manager.cc.
90 * Revision 1.1.1.1 1999/01/28 09:41:07 adam
91 * First implementation of YAZ++.
99 #include <yaz++/yaz-proxy.h>
101 Yaz_Proxy::Yaz_Proxy(IYaz_PDU_Observable *the_PDU_Observable) :
102 Yaz_Z_Assoc(the_PDU_Observable)
104 m_PDU_Observable = the_PDU_Observable;
115 Yaz_Proxy::~Yaz_Proxy()
117 xfree (m_proxyTarget);
120 void Yaz_Proxy::set_proxyTarget(const char *target)
122 xfree (m_proxyTarget);
125 m_proxyTarget = (char *) xstrdup (target);
128 IYaz_PDU_Observer *Yaz_Proxy::sessionNotify(IYaz_PDU_Observable
129 *the_PDU_Observable, int fd)
131 Yaz_Proxy *new_proxy = new Yaz_Proxy(the_PDU_Observable);
132 new_proxy->m_parent = this;
133 new_proxy->timeout(500);
134 new_proxy->set_proxyTarget(m_proxyTarget);
135 new_proxy->set_APDU_log(get_APDU_log());
139 char *Yaz_Proxy::get_cookie(Z_OtherInformation **otherInfo)
142 Z_OtherInformationUnit *oi;
144 ent.proto = PROTO_Z3950;
145 ent.oclass = CLASS_USERINFO;
146 ent.value = (oid_value) VAL_COOKIE;
147 assert (oid_ent_to_oid (&ent, oid));
149 if (oid_ent_to_oid (&ent, oid) &&
150 (oi = update_otherInformation(otherInfo, 0, oid, 1, 1)) &&
151 oi->which == Z_OtherInfo_characterInfo)
152 return oi->information.characterInfo;
156 char *Yaz_Proxy::get_proxy(Z_OtherInformation **otherInfo)
159 Z_OtherInformationUnit *oi;
161 ent.proto = PROTO_Z3950;
162 ent.oclass = CLASS_USERINFO;
163 ent.value = (oid_value) VAL_PROXY;
164 if (oid_ent_to_oid (&ent, oid) &&
165 (oi = update_otherInformation(otherInfo, 0, oid, 1, 1)) &&
166 oi->which == Z_OtherInfo_characterInfo)
167 return oi->information.characterInfo;
171 Yaz_ProxyClient *Yaz_Proxy::get_client(Z_APDU *apdu)
174 Yaz_Proxy *parent = m_parent;
175 Z_OtherInformation **oi;
176 Yaz_ProxyClient *c = m_client;
178 get_otherInfoAPDU(apdu, &oi);
179 char *cookie = get_cookie(oi);
180 yaz_log (LOG_LOG, "Yaz_Proxy::get_client cookie=%s", cookie ? cookie :
183 const char *proxy_host = get_proxy(oi);
185 set_proxyTarget(proxy_host);
186 yaz_log (LOG_LOG, "proxy_host = %s", m_proxyTarget ? m_proxyTarget:"none");
188 // no target specified at all?
192 if (cookie && *cookie)
194 yaz_log (LOG_LOG, "lookup of clients cookie=%s target=%s",
195 cookie, m_proxyTarget);
196 Yaz_ProxyClient *cc = 0;
198 for (c = parent->m_clientPool; c; c = c->m_next)
200 yaz_log (LOG_LOG, " found client cookie = %s target=%s seqno=%d",
201 c->m_cookie, c->get_hostname(), c->m_seqno);
203 assert (*c->m_prev == c);
204 if (!strcmp(cookie,c->m_cookie) &&
205 !strcmp(m_proxyTarget, c->get_hostname()))
207 yaz_log (LOG_LOG, "found!");
215 // The following handles "cancel"
216 // If connection is busy (waiting for PDU) and
217 // we have an initRequest we can safely do re-open
218 if (c->m_waiting && apdu->which == Z_APDU_initRequest)
220 yaz_log (LOG_LOG, "reopen target=%s", c->get_hostname());
222 c->client(m_proxyTarget);
225 delete c->m_last_query;
227 c->m_last_resultCount = 0;
228 c->m_sr_transform = 0;
232 c->m_seqno = parent->m_seqno;
233 if (c->m_server && c->m_server != this)
234 c->m_server->m_client = 0;
236 c->m_seqno = parent->m_seqno;
238 yaz_log (LOG_LOG, "get_client 1 %p %p", this, c);
244 if (apdu->which != Z_APDU_initRequest)
246 yaz_log (LOG_LOG, "no first INIT!");
249 yaz_log (LOG_LOG, "got InitRequest");
251 // go through list of clients - and find the lowest/oldest one.
252 Yaz_ProxyClient *c_min = 0;
254 int no_of_clients = 0;
255 for (c = parent->m_clientPool; c; c = c->m_next)
258 if (min_seq < 0 || c->m_seqno < min_seq)
260 min_seq = c->m_seqno;
264 if (no_of_clients >= parent->m_max_clients)
267 if (c->m_waiting || strcmp(m_proxyTarget, c->get_hostname()))
269 yaz_log (LOG_LOG, "Yaz_Proxy::get_client re-init session %d",
271 if (c->m_server && c->m_server != this)
278 "Yaz_Proxy::get_client re-use session %d to %d",
279 c->m_seqno, parent->m_seqno);
281 strcpy (c->m_cookie, cookie);
283 c->m_cookie[0] = '\0';
284 c->m_seqno = parent->m_seqno;
285 if (c->m_server && c->m_server != this)
287 c->m_server->m_client = 0;
291 yaz_log (LOG_LOG, "get_client 2 %p %p", this, c);
297 yaz_log (LOG_LOG, "Yaz_Proxy::get_client making session %d",
299 c = new Yaz_ProxyClient(m_PDU_Observable->clone());
300 c->m_next = parent->m_clientPool;
302 c->m_next->m_prev = &c->m_next;
303 parent->m_clientPool = c;
304 c->m_prev = &parent->m_clientPool;
307 strcpy (c->m_cookie, cookie);
309 c->m_cookie[0] = '\0';
310 yaz_log (LOG_LOG, "Yaz_Proxy::get_client connect to %s",
312 c->m_seqno = parent->m_seqno;
313 c->client(m_proxyTarget);
316 delete c->m_last_query;
318 c->m_last_resultCount = 0;
319 c->m_sr_transform = 0;
325 yaz_log (LOG_LOG, "get_client 3 %p %p", this, c);
329 Z_APDU *Yaz_Proxy::result_set_optimize(Z_APDU *apdu)
331 if (apdu->which != Z_APDU_searchRequest)
333 Z_SearchRequest *sr = apdu->u.searchRequest;
334 Yaz_Z_Query *this_query = new Yaz_Z_Query;
336 this_query->set_Z_Query(sr->query);
338 if (m_client->m_last_query &&
339 m_client->m_last_query->match(this_query))
342 if (m_client->m_last_resultCount > *sr->smallSetUpperBound &&
343 m_client->m_last_resultCount < *sr->largeSetLowerBound)
346 yaz_log (LOG_LOG, "Yaz_Proxy::result_set_optimize medium set");
347 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentRequest);
348 Z_PresentRequest *pr = new_apdu->u.presentRequest;
349 pr->referenceId = sr->referenceId;
350 pr->resultSetId = sr->resultSetName;
351 pr->preferredRecordSyntax = sr->preferredRecordSyntax;
352 *pr->numberOfRecordsRequested = *sr->mediumSetPresentNumber;
353 if (sr->mediumSetElementSetNames)
355 pr->recordComposition = (Z_RecordComposition *)
356 odr_malloc(odr_encode(), sizeof(Z_RecordComposition));
357 pr->recordComposition->which = Z_RecordComp_simple;
358 pr->recordComposition->u.simple = sr->mediumSetElementSetNames;
360 m_client->m_sr_transform = 1;
363 else if (m_client->m_last_resultCount > *sr->largeSetLowerBound ||
364 m_client->m_last_resultCount == 0)
367 yaz_log (LOG_LOG, "Yaz_Proxy::result_set_optimize large set");
368 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
369 new_apdu->u.searchResponse->referenceId = sr->referenceId;
370 new_apdu->u.searchResponse->resultCount =
371 &m_client->m_last_resultCount;
372 send_Z_PDU(new_apdu);
378 yaz_log (LOG_LOG, "Yaz_Proxy::result_set_optimize small set");
379 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentRequest);
380 Z_PresentRequest *pr = new_apdu->u.presentRequest;
381 pr->referenceId = sr->referenceId;
382 pr->resultSetId = sr->resultSetName;
383 pr->preferredRecordSyntax = sr->preferredRecordSyntax;
384 *pr->numberOfRecordsRequested = m_client->m_last_resultCount;
385 if (sr->smallSetElementSetNames)
387 pr->recordComposition = (Z_RecordComposition *)
388 odr_malloc(odr_encode(), sizeof(Z_RecordComposition));
389 pr->recordComposition->which = Z_RecordComp_simple;
390 pr->recordComposition->u.simple = sr->smallSetElementSetNames;
392 m_client->m_sr_transform = 1;
398 yaz_log (LOG_LOG, "Yaz_Proxy::result_set_optimize new set");
399 delete m_client->m_last_query;
400 m_client->m_last_query = this_query;
405 void Yaz_Proxy::recv_Z_PDU(Z_APDU *apdu)
407 yaz_log (LOG_LOG, "Yaz_Proxy::recv_Z_PDU");
408 // Determine our client.
409 m_client = get_client(apdu);
415 m_client->m_server = this;
417 if (apdu->which == Z_APDU_initRequest)
419 if (m_client->m_init_flag)
421 Z_APDU *apdu = create_Z_PDU(Z_APDU_initResponse);
422 if (m_client->m_cookie)
423 set_otherInformationString(apdu, VAL_COOKIE, 1,
428 m_client->m_init_flag = 1;
430 apdu = result_set_optimize(apdu);
434 yaz_log (LOG_LOG, "Yaz_ProxyClient::send_Z_PDU %s",
435 m_client->get_hostname());
437 // delete other info part from PDU before sending to target
438 Z_OtherInformation **oi;
439 get_otherInfoAPDU(apdu, &oi);
443 if (m_client->send_Z_PDU(apdu) < 0)
450 m_client->m_waiting = 1;
453 void Yaz_Proxy::connectNotify()
457 void Yaz_Proxy::shutdown()
459 yaz_log (LOG_LOG, "shutdown (client to proxy)");
460 // only keep if keep_alive flag and cookie is set...
461 if (m_keepalive && m_client && m_client->m_cookie[0])
463 if (m_client->m_waiting == 2)
465 // Tell client (if any) that no server connection is there..
466 m_client->m_server = 0;
470 yaz_log (LOG_LOG, "deleting %p %p", this, m_client);
471 if (m_client->m_waiting == 2)
482 void Yaz_ProxyClient::shutdown()
484 yaz_log (LOG_LOG, "shutdown (proxy to server) %s", get_hostname());
489 void Yaz_Proxy::failNotify()
491 yaz_log (LOG_LOG, "connection closed by client");
495 void Yaz_ProxyClient::failNotify()
497 yaz_log (LOG_LOG, "Yaz_ProxyClient connection closed by %s", get_hostname());
501 void Yaz_ProxyClient::connectNotify()
503 yaz_log (LOG_LOG, "Yaz_ProxyClient connection accepted by %s",
508 IYaz_PDU_Observer *Yaz_ProxyClient::sessionNotify(IYaz_PDU_Observable
509 *the_PDU_Observable, int fd)
511 return new Yaz_ProxyClient(the_PDU_Observable);
514 Yaz_ProxyClient::~Yaz_ProxyClient()
519 m_next->m_prev = m_prev;
520 m_waiting = 2; // for debugging purposes only.
524 void Yaz_Proxy::timeoutNotify()
526 yaz_log (LOG_LOG, "timeout (client to proxy)");
530 void Yaz_ProxyClient::timeoutNotify()
532 yaz_log (LOG_LOG, "timeout (proxy to target) %s", get_hostname());
536 Yaz_ProxyClient::Yaz_ProxyClient(IYaz_PDU_Observable *the_PDU_Observable) :
537 Yaz_Z_Assoc (the_PDU_Observable)
544 m_last_resultCount = 0;
549 void Yaz_ProxyClient::recv_Z_PDU(Z_APDU *apdu)
552 yaz_log (LOG_LOG, "Yaz_ProxyClient::recv_Z_PDU %s", get_hostname());
553 if (apdu->which == Z_APDU_searchResponse)
555 m_last_resultCount = *apdu->u.searchResponse->resultCount;
556 int status = *apdu->u.searchResponse->searchStatus;
558 apdu->u.searchResponse->records &&
559 apdu->u.searchResponse->records->which != Z_Records_DBOSD))
565 if (apdu->which == Z_APDU_presentResponse && m_sr_transform)
568 Z_PresentResponse *pr = apdu->u.presentResponse;
569 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
570 Z_SearchResponse *sr = new_apdu->u.searchResponse;
571 sr->referenceId = pr->referenceId;
572 *sr->resultCount = m_last_resultCount;
573 sr->records = pr->records;
574 sr->nextResultSetPosition = pr->nextResultSetPosition;
575 sr->numberOfRecordsReturned = pr->numberOfRecordsReturned;
579 set_otherInformationString (apdu, VAL_COOKIE, 1, m_cookie);
582 yaz_log (LOG_LOG, "Yaz_Proxy::send_Z_PDU");
583 m_server->send_Z_PDU(apdu);