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.15 2000-08-31 14:41:55 adam
8 * Proxy no longer generates cookies (it's up to the client). Proxy
9 * re-opens if target new op is started before previous operation finishes.
11 * Revision 1.14 2000/08/10 08:42:42 adam
12 * Fixes for {set,get}_APDU_log.
14 * Revision 1.13 2000/08/07 14:19:59 adam
15 * Fixed serious bug regarding timeouts. Improved logging for proxy.
17 * Revision 1.12 2000/07/04 13:48:49 adam
18 * Implemented upper-limit on proxy-to-target sessions.
20 * Revision 1.11 1999/12/06 13:52:45 adam
21 * Modified for new location of YAZ header files. Experimental threaded
24 * Revision 1.10 1999/11/10 10:02:34 adam
27 * Revision 1.9 1999/09/13 12:53:44 adam
28 * Proxy removes OtherInfo Proxy Address and Session ID. Other
29 * Otherinfo remains untouched.
31 * Revision 1.8 1999/05/04 10:53:00 adam
32 * Changed the way the PROXY behaves when lost cookie is received.
34 * Revision 1.7 1999/04/28 13:31:17 adam
35 * Better result set optimisation for proxy.
37 * Revision 1.6 1999/04/27 07:52:13 adam
38 * Improved proxy; added query match for result set re-use.
40 * Revision 1.5 1999/04/21 12:09:01 adam
41 * Many improvements. Modified to proxy server to work with "sessions"
44 * Revision 1.4 1999/04/20 10:30:05 adam
45 * Implemented various stuff for client and proxy. Updated calls
46 * to ODR to reflect new name parameter.
48 * Revision 1.3 1999/04/09 11:46:57 adam
49 * Added object Yaz_Z_Assoc. Much more functional client.
51 * Revision 1.2 1999/01/28 13:08:46 adam
52 * Yaz_PDU_Assoc better encapsulated. Memory leak fix in
53 * yaz-socket-manager.cc.
55 * Revision 1.1.1.1 1999/01/28 09:41:07 adam
56 * First implementation of YAZ++.
64 #include <yaz-proxy.h>
66 Yaz_Proxy::Yaz_Proxy(IYaz_PDU_Observable *the_PDU_Observable) :
67 Yaz_Z_Assoc(the_PDU_Observable)
69 m_PDU_Observable = the_PDU_Observable;
80 Yaz_Proxy::~Yaz_Proxy()
82 xfree (m_proxyTarget);
85 void Yaz_Proxy::set_proxyTarget(const char *target)
87 xfree (m_proxyTarget);
90 m_proxyTarget = (char *) xstrdup (target);
93 IYaz_PDU_Observer *Yaz_Proxy::clone(IYaz_PDU_Observable
96 Yaz_Proxy *new_proxy = new Yaz_Proxy(the_PDU_Observable);
97 new_proxy->m_parent = this;
98 new_proxy->timeout(120);
99 new_proxy->set_proxyTarget(m_proxyTarget);
100 new_proxy->set_APDU_log(get_APDU_log());
104 char *Yaz_Proxy::get_cookie(Z_OtherInformation **otherInfo)
107 Z_OtherInformationUnit *oi;
109 ent.proto = PROTO_Z3950;
110 ent.oclass = CLASS_USERINFO;
111 ent.value = (oid_value) VAL_COOKIE;
112 assert (oid_ent_to_oid (&ent, oid));
114 if (oid_ent_to_oid (&ent, oid) &&
115 (oi = update_otherInformation(otherInfo, 0, oid, 1, 1)) &&
116 oi->which == Z_OtherInfo_characterInfo)
117 return oi->information.characterInfo;
121 char *Yaz_Proxy::get_proxy(Z_OtherInformation **otherInfo)
124 Z_OtherInformationUnit *oi;
126 ent.proto = PROTO_Z3950;
127 ent.oclass = CLASS_USERINFO;
128 ent.value = (oid_value) VAL_PROXY;
129 if (oid_ent_to_oid (&ent, oid) &&
130 (oi = update_otherInformation(otherInfo, 0, oid, 1, 1)) &&
131 oi->which == Z_OtherInfo_characterInfo)
132 return oi->information.characterInfo;
136 Yaz_ProxyClient *Yaz_Proxy::get_client(Z_APDU *apdu)
139 Yaz_Proxy *parent = m_parent;
140 Z_OtherInformation **oi;
141 Yaz_ProxyClient *c = m_client;
143 get_otherInfoAPDU(apdu, &oi);
144 char *cookie = get_cookie(oi);
145 logf (LOG_LOG, "Yaz_Proxy::get_client cookie=%s", cookie ? cookie :
148 const char *proxy_host = get_proxy(oi);
150 set_proxyTarget(proxy_host);
151 logf (LOG_LOG, "proxy_host = %s", m_proxyTarget ? m_proxyTarget:"none");
153 // no target specified at all?
159 logf (LOG_LOG, "lookup of clients cookie=%s target=%s",
160 cookie, m_proxyTarget);
161 Yaz_ProxyClient *cc = 0;
163 for (c = parent->m_clientPool; c; c = c->m_next)
165 logf (LOG_LOG, " found client cookie = %s target=%s",
166 c->m_cookie, c->get_hostname());
168 assert (*c->m_prev == c);
169 if (!strcmp(cookie,c->m_cookie) &&
170 !strcmp(m_proxyTarget, c->get_hostname()))
172 logf (LOG_LOG, "found!");
181 logf (LOG_LOG, "reopen target=%s", c->get_hostname());
183 c->client(m_proxyTarget);
186 delete c->m_last_query;
188 c->m_last_resultCount = 0;
189 c->m_sr_transform = 0;
193 c->m_seqno = parent->m_seqno;
200 if (apdu->which != Z_APDU_initRequest)
202 logf (LOG_LOG, "no first INIT!");
205 logf (LOG_LOG, "got InitRequest");
207 // go through list of clients - and find the lowest/oldest one.
208 Yaz_ProxyClient *c_min = 0;
210 int no_of_clients = 0;
211 for (c = parent->m_clientPool; c; c = c->m_next)
214 logf (LOG_LOG, "found seqno = %d", c->m_seqno);
215 if (min_seq < 0 || c->m_seqno < min_seq)
217 min_seq = c->m_seqno;
221 if (no_of_clients >= parent->m_max_clients)
224 logf (LOG_LOG, "Yaz_Proxy::get_client re-using session %d",
232 logf (LOG_LOG, "Yaz_Proxy::get_client making session %d",
234 c = new Yaz_ProxyClient(m_PDU_Observable->clone());
235 c->m_next = parent->m_clientPool;
237 c->m_next->m_prev = &c->m_next;
238 parent->m_clientPool = c;
239 c->m_prev = &parent->m_clientPool;
242 strcpy (c->m_cookie, cookie);
244 c->m_cookie[0] = '\0';
245 logf (LOG_LOG, "Yaz_Proxy::get_client connect to %s", m_proxyTarget);
246 c->m_seqno = parent->m_seqno;
247 c->client(m_proxyTarget);
250 delete c->m_last_query;
252 c->m_last_resultCount = 0;
253 c->m_sr_transform = 0;
262 Z_APDU *Yaz_Proxy::result_set_optimize(Z_APDU *apdu)
264 if (apdu->which != Z_APDU_searchRequest)
266 Z_SearchRequest *sr = apdu->u.searchRequest;
267 Yaz_Z_Query *this_query = new Yaz_Z_Query;
269 this_query->set_Z_Query(sr->query);
271 if (m_client->m_last_query &&
272 m_client->m_last_query->match(this_query))
275 if (m_client->m_last_resultCount > *sr->smallSetUpperBound &&
276 m_client->m_last_resultCount < *sr->largeSetLowerBound)
279 logf (LOG_LOG, "Yaz_Proxy::result_set_optimize medium set");
280 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentRequest);
281 Z_PresentRequest *pr = new_apdu->u.presentRequest;
282 pr->referenceId = sr->referenceId;
283 pr->resultSetId = sr->resultSetName;
284 pr->preferredRecordSyntax = sr->preferredRecordSyntax;
285 *pr->numberOfRecordsRequested = *sr->mediumSetPresentNumber;
286 if (sr->mediumSetElementSetNames)
288 pr->recordComposition = (Z_RecordComposition *)
289 odr_malloc(odr_encode(), sizeof(Z_RecordComposition));
290 pr->recordComposition->which = Z_RecordComp_simple;
291 pr->recordComposition->u.simple = sr->mediumSetElementSetNames;
293 m_client->m_sr_transform = 1;
296 else if (m_client->m_last_resultCount > *sr->largeSetLowerBound ||
297 m_client->m_last_resultCount == 0)
300 logf (LOG_LOG, "Yaz_Proxy::result_set_optimize large set");
301 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
302 new_apdu->u.searchResponse->referenceId = sr->referenceId;
303 new_apdu->u.searchResponse->resultCount =
304 &m_client->m_last_resultCount;
305 send_Z_PDU(new_apdu);
311 logf (LOG_LOG, "Yaz_Proxy::result_set_optimize small set");
312 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentRequest);
313 Z_PresentRequest *pr = new_apdu->u.presentRequest;
314 pr->referenceId = sr->referenceId;
315 pr->resultSetId = sr->resultSetName;
316 pr->preferredRecordSyntax = sr->preferredRecordSyntax;
317 *pr->numberOfRecordsRequested = m_client->m_last_resultCount;
318 if (sr->smallSetElementSetNames)
320 pr->recordComposition = (Z_RecordComposition *)
321 odr_malloc(odr_encode(), sizeof(Z_RecordComposition));
322 pr->recordComposition->which = Z_RecordComp_simple;
323 pr->recordComposition->u.simple = sr->smallSetElementSetNames;
325 m_client->m_sr_transform = 1;
331 logf (LOG_LOG, "Yaz_Proxy::result_set_optimize new set");
332 delete m_client->m_last_query;
333 m_client->m_last_query = this_query;
338 void Yaz_Proxy::recv_Z_PDU(Z_APDU *apdu)
340 logf (LOG_LOG, "Yaz_Proxy::recv_Z_PDU");
341 // Determine our client.
342 m_client = get_client(apdu);
348 m_client->m_server = this;
350 Z_OtherInformation **oi;
351 get_otherInfoAPDU(apdu, &oi);
355 if (apdu->which == Z_APDU_initRequest)
357 if (m_client->m_init_flag)
359 Z_APDU *apdu = create_Z_PDU(Z_APDU_initResponse);
360 if (m_client->m_cookie)
361 set_otherInformationString(apdu, VAL_COOKIE, 1,
366 m_client->m_init_flag = 1;
368 apdu = result_set_optimize(apdu);
372 logf (LOG_LOG, "Yaz_ProxyClient::send_Z_PDU %s", m_client->get_hostname());
373 if (m_client->send_Z_PDU(apdu) < 0)
379 m_client->m_waiting = 1;
382 void Yaz_Proxy::connectNotify()
386 void Yaz_Proxy::shutdown()
388 logf (LOG_LOG, "shutdown (client to proxy)");
389 // only keep if keep_alive flag and cookie is set...
390 if (m_keepalive && m_client && m_client->m_cookie[0])
392 // Tell client (if any) that no server connection is there..
393 m_client->m_server = 0;
402 void Yaz_ProxyClient::shutdown()
404 logf (LOG_LOG, "shutdown (proxy to server) %s", get_hostname());
409 void Yaz_Proxy::failNotify()
411 logf (LOG_LOG, "connection closed by client");
415 void Yaz_ProxyClient::failNotify()
417 logf (LOG_LOG, "Yaz_ProxyClient connection closed by %s", get_hostname());
421 void Yaz_ProxyClient::connectNotify()
423 logf (LOG_LOG, "Yaz_ProxyClient connection accept by %s", get_hostname());
426 IYaz_PDU_Observer *Yaz_ProxyClient::clone(IYaz_PDU_Observable
429 return new Yaz_ProxyClient(the_PDU_Observable);
432 Yaz_ProxyClient::~Yaz_ProxyClient()
438 m_next->m_prev = m_prev;
443 void Yaz_Proxy::timeoutNotify()
445 logf (LOG_LOG, "timeout (client to proxy)");
449 void Yaz_ProxyClient::timeoutNotify()
451 logf (LOG_LOG, "timeout (proxy to target) %s", get_hostname());
455 Yaz_ProxyClient::Yaz_ProxyClient(IYaz_PDU_Observable *the_PDU_Observable) :
456 Yaz_Z_Assoc (the_PDU_Observable)
463 m_last_resultCount = 0;
468 void Yaz_ProxyClient::recv_Z_PDU(Z_APDU *apdu)
471 logf (LOG_LOG, "Yaz_ProxyClient::recv_Z_PDU %s", get_hostname());
472 if (apdu->which == Z_APDU_searchResponse)
473 m_last_resultCount = *apdu->u.searchResponse->resultCount;
474 if (apdu->which == Z_APDU_presentResponse && m_sr_transform)
477 Z_PresentResponse *pr = apdu->u.presentResponse;
478 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
479 Z_SearchResponse *sr = new_apdu->u.searchResponse;
480 sr->referenceId = pr->referenceId;
481 *sr->resultCount = m_last_resultCount;
482 sr->records = pr->records;
483 sr->nextResultSetPosition = pr->nextResultSetPosition;
484 sr->numberOfRecordsReturned = pr->numberOfRecordsReturned;
488 set_otherInformationString (apdu, VAL_COOKIE, 1, m_cookie);
491 logf (LOG_LOG, "Yaz_Proxy::send_Z_PDU");
492 m_server->send_Z_PDU(apdu);