2 * Copyright (c) 1998-2001, Index Data.
3 * See the file LICENSE for details.
5 * $Id: yaz-proxy.cpp,v 1.31 2002-01-14 12:01:28 adam Exp $
12 #include <yaz++/yaz-proxy.h>
14 Yaz_Proxy::Yaz_Proxy(IYaz_PDU_Observable *the_PDU_Observable) :
15 Yaz_Z_Assoc(the_PDU_Observable)
17 m_PDU_Observable = the_PDU_Observable;
24 m_proxy_authentication = 0;
27 m_optimize = xstrdup ("1");
30 Yaz_Proxy::~Yaz_Proxy()
32 xfree (m_proxyTarget);
33 xfree (m_proxy_authentication);
37 void Yaz_Proxy::set_proxy_target(const char *target)
39 xfree (m_proxyTarget);
42 m_proxyTarget = (char *) xstrdup (target);
45 void Yaz_Proxy::set_proxy_authentication (const char *auth)
47 xfree (m_proxy_authentication);
48 m_proxy_authentication = 0;
50 m_proxy_authentication = (char *) xstrdup (auth);
53 IYaz_PDU_Observer *Yaz_Proxy::sessionNotify(IYaz_PDU_Observable
54 *the_PDU_Observable, int fd)
56 Yaz_Proxy *new_proxy = new Yaz_Proxy(the_PDU_Observable);
57 new_proxy->m_parent = this;
58 new_proxy->timeout(500);
59 new_proxy->set_proxy_target(m_proxyTarget);
60 new_proxy->set_APDU_log(get_APDU_log());
61 new_proxy->set_proxy_authentication(m_proxy_authentication);
65 char *Yaz_Proxy::get_cookie(Z_OtherInformation **otherInfo)
68 Z_OtherInformationUnit *oi;
70 ent.proto = PROTO_Z3950;
71 ent.oclass = CLASS_USERINFO;
72 ent.value = (oid_value) VAL_COOKIE;
73 assert (oid_ent_to_oid (&ent, oid));
75 if (oid_ent_to_oid (&ent, oid) &&
76 (oi = update_otherInformation(otherInfo, 0, oid, 1, 1)) &&
77 oi->which == Z_OtherInfo_characterInfo)
78 return oi->information.characterInfo;
82 char *Yaz_Proxy::get_proxy(Z_OtherInformation **otherInfo)
85 Z_OtherInformationUnit *oi;
87 ent.proto = PROTO_Z3950;
88 ent.oclass = CLASS_USERINFO;
89 ent.value = (oid_value) VAL_PROXY;
90 if (oid_ent_to_oid (&ent, oid) &&
91 (oi = update_otherInformation(otherInfo, 0, oid, 1, 1)) &&
92 oi->which == Z_OtherInfo_characterInfo)
93 return oi->information.characterInfo;
97 Yaz_ProxyClient *Yaz_Proxy::get_client(Z_APDU *apdu)
100 Yaz_Proxy *parent = m_parent;
101 Z_OtherInformation **oi;
102 Yaz_ProxyClient *c = m_client;
104 get_otherInfoAPDU(apdu, &oi);
105 char *cookie = get_cookie(oi);
107 const char *proxy_host = get_proxy(oi);
109 set_proxy_target(proxy_host);
111 // no target specified at all?
115 if (!strcmp(m_proxyTarget, "stop"))
117 if (cookie && *cookie)
119 Yaz_ProxyClient *cc = 0;
121 for (c = parent->m_clientPool; c; c = c->m_next)
124 assert (*c->m_prev == c);
125 if (!strcmp(cookie,c->m_cookie) &&
126 !strcmp(m_proxyTarget, c->get_hostname()))
135 // The following handles "cancel"
136 // If connection is busy (waiting for PDU) and
137 // we have an initRequest we can safely do re-open
138 if (c->m_waiting && apdu->which == Z_APDU_initRequest)
140 yaz_log (LOG_LOG, "reopen target=%s", c->get_hostname());
142 c->client(m_proxyTarget);
146 c->m_last_resultCount = 0;
147 c->m_sr_transform = 0;
151 c->m_seqno = parent->m_seqno;
152 if (c->m_server && c->m_server != this)
153 c->m_server->m_client = 0;
155 c->m_seqno = parent->m_seqno;
157 yaz_log (LOG_DEBUG, "get_client 1 %p %p", this, c);
163 if (apdu->which != Z_APDU_initRequest)
165 yaz_log (LOG_LOG, "no first INIT!");
168 yaz_log (LOG_LOG, "got InitRequest");
169 Z_InitRequest *initRequest = apdu->u.initRequest;
171 if (!initRequest->idAuthentication)
173 if (m_proxy_authentication)
175 initRequest->idAuthentication =
176 (Z_IdAuthentication *)
177 odr_malloc (odr_encode(),
178 sizeof(*initRequest->idAuthentication));
179 initRequest->idAuthentication->which =
180 Z_IdAuthentication_open;
181 initRequest->idAuthentication->u.open =
182 odr_strdup (odr_encode(), m_proxy_authentication);
186 // go through list of clients - and find the lowest/oldest one.
187 Yaz_ProxyClient *c_min = 0;
189 int no_of_clients = 0;
190 for (c = parent->m_clientPool; c; c = c->m_next)
193 if (min_seq < 0 || c->m_seqno < min_seq)
195 min_seq = c->m_seqno;
199 if (no_of_clients >= parent->m_max_clients)
202 if (c->m_waiting || strcmp(m_proxyTarget, c->get_hostname()))
204 yaz_log (LOG_LOG, "Yaz_Proxy::get_client re-init session %d",
206 if (c->m_server && c->m_server != this)
213 "Yaz_Proxy::get_client re-use session %d to %d",
214 c->m_seqno, parent->m_seqno);
216 strcpy (c->m_cookie, cookie);
218 c->m_cookie[0] = '\0';
219 c->m_seqno = parent->m_seqno;
220 if (c->m_server && c->m_server != this)
222 c->m_server->m_client = 0;
226 yaz_log (LOG_DEBUG, "get_client 2 %p %p", this, c);
232 yaz_log (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 yaz_log (LOG_LOG, "Yaz_Proxy::get_client connect to %s",
247 c->m_seqno = parent->m_seqno;
248 c->client(m_proxyTarget);
250 c->m_last_resultCount = 0;
252 c->m_sr_transform = 0;
258 yaz_log (LOG_DEBUG, "get_client 3 %p %p", this, c);
262 Z_APDU *Yaz_Proxy::result_set_optimize(Z_APDU *apdu)
264 if (apdu->which != Z_APDU_searchRequest)
266 if (*m_parent->m_optimize != '1')
268 Z_SearchRequest *sr = apdu->u.searchRequest;
269 Yaz_Z_Query *this_query = new Yaz_Z_Query;
270 Yaz_Z_Databases this_databases;
272 this_databases.set(sr->num_databaseNames, (const char **)
275 this_query->set_Z_Query(sr->query);
277 if (m_client->m_last_ok && m_client->m_last_query &&
278 m_client->m_last_query->match(this_query) &&
279 m_client->m_last_databases.match(this_databases))
282 if (m_client->m_last_resultCount > *sr->smallSetUpperBound &&
283 m_client->m_last_resultCount < *sr->largeSetLowerBound)
286 // send present request (medium size)
287 yaz_log (LOG_LOG, "Yaz_Proxy::result_set_optimize medium set");
288 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentRequest);
289 Z_PresentRequest *pr = new_apdu->u.presentRequest;
290 pr->referenceId = sr->referenceId;
291 pr->resultSetId = sr->resultSetName;
292 pr->preferredRecordSyntax = sr->preferredRecordSyntax;
293 *pr->numberOfRecordsRequested = *sr->mediumSetPresentNumber;
294 if (sr->mediumSetElementSetNames)
296 pr->recordComposition = (Z_RecordComposition *)
297 odr_malloc(odr_encode(), sizeof(Z_RecordComposition));
298 pr->recordComposition->which = Z_RecordComp_simple;
299 pr->recordComposition->u.simple = sr->mediumSetElementSetNames;
301 m_client->m_sr_transform = 1;
304 else if (m_client->m_last_resultCount >= *sr->largeSetLowerBound ||
305 m_client->m_last_resultCount == 0)
307 // large set. Return pseudo-search response immediately
308 yaz_log (LOG_LOG, "Yaz_Proxy::result_set_optimize large set");
309 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
310 new_apdu->u.searchResponse->referenceId = sr->referenceId;
311 new_apdu->u.searchResponse->resultCount =
312 &m_client->m_last_resultCount;
313 send_Z_PDU(new_apdu);
319 // send a present request (small set)
320 yaz_log (LOG_LOG, "Yaz_Proxy::result_set_optimize small set");
321 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentRequest);
322 Z_PresentRequest *pr = new_apdu->u.presentRequest;
323 pr->referenceId = sr->referenceId;
324 pr->resultSetId = sr->resultSetName;
325 pr->preferredRecordSyntax = sr->preferredRecordSyntax;
326 *pr->numberOfRecordsRequested = m_client->m_last_resultCount;
327 if (sr->smallSetElementSetNames)
329 pr->recordComposition = (Z_RecordComposition *)
330 odr_malloc(odr_encode(), sizeof(Z_RecordComposition));
331 pr->recordComposition->which = Z_RecordComp_simple;
332 pr->recordComposition->u.simple = sr->smallSetElementSetNames;
334 m_client->m_sr_transform = 1;
340 yaz_log (LOG_LOG, "Yaz_Proxy::result_set_optimize new set");
341 delete m_client->m_last_query;
342 m_client->m_last_query = this_query;
343 m_client->m_last_ok = 0;
344 m_client->m_last_databases.set(sr->num_databaseNames,
345 (const char **) sr->databaseNames);
350 void Yaz_Proxy::recv_Z_PDU(Z_APDU *apdu)
352 yaz_log (LOG_LOG, "Yaz_Proxy::recv_Z_PDU");
353 // Determine our client.
354 m_client = get_client(apdu);
360 m_client->m_server = this;
362 if (apdu->which == Z_APDU_initRequest)
364 if (m_client->m_init_flag)
366 Z_APDU *apdu = create_Z_PDU(Z_APDU_initResponse);
367 if (m_client->m_cookie)
368 set_otherInformationString(apdu, VAL_COOKIE, 1,
373 m_client->m_init_flag = 1;
375 apdu = result_set_optimize(apdu);
379 yaz_log (LOG_LOG, "Yaz_ProxyClient::send_Z_PDU %s",
380 m_client->get_hostname());
382 // delete other info part from PDU before sending to target
383 Z_OtherInformation **oi;
384 get_otherInfoAPDU(apdu, &oi);
388 if (m_client->send_Z_PDU(apdu) < 0)
395 m_client->m_waiting = 1;
398 void Yaz_Proxy::connectNotify()
402 void Yaz_Proxy::shutdown()
404 // only keep if keep_alive flag and cookie is set...
405 if (m_keepalive && m_client && m_client->m_cookie[0])
407 if (m_client->m_waiting == 2)
409 // Tell client (if any) that no server connection is there..
410 m_client->m_server = 0;
411 yaz_log (LOG_LOG, "shutdown (client to proxy) keepalive %s", m_client->get_hostname());
415 if (m_client->m_waiting == 2)
417 yaz_log (LOG_LOG, "shutdown (client to proxy) close %s", m_client->get_hostname());
422 yaz_log (LOG_LOG, "shutdown (client to proxy) bad state");
427 yaz_log (LOG_LOG, "shutdown (client to proxy)");
432 void Yaz_ProxyClient::shutdown()
434 yaz_log (LOG_LOG, "shutdown (proxy to server) %s", get_hostname());
439 void Yaz_Proxy::failNotify()
441 yaz_log (LOG_LOG, "Yaz_Proxy connection closed by client");
445 void Yaz_ProxyClient::failNotify()
447 yaz_log (LOG_LOG, "Yaz_ProxyClient connection closed by %s", get_hostname());
451 void Yaz_ProxyClient::connectNotify()
453 yaz_log (LOG_LOG, "Yaz_ProxyClient connection accepted by %s",
458 IYaz_PDU_Observer *Yaz_ProxyClient::sessionNotify(IYaz_PDU_Observable
459 *the_PDU_Observable, int fd)
461 return new Yaz_ProxyClient(the_PDU_Observable);
464 Yaz_ProxyClient::~Yaz_ProxyClient()
469 m_next->m_prev = m_prev;
470 m_waiting = 2; // for debugging purposes only.
474 void Yaz_Proxy::timeoutNotify()
476 yaz_log (LOG_LOG, "timeout (client to proxy)");
480 void Yaz_ProxyClient::timeoutNotify()
482 yaz_log (LOG_LOG, "timeout (proxy to target) %s", get_hostname());
486 Yaz_ProxyClient::Yaz_ProxyClient(IYaz_PDU_Observable *the_PDU_Observable) :
487 Yaz_Z_Assoc (the_PDU_Observable)
494 m_last_resultCount = 0;
500 const char *Yaz_Proxy::option(const char *name, const char *value)
502 if (!strcmp (name, "optimize")) {
505 m_optimize = xstrdup (value);
512 void Yaz_ProxyClient::recv_Z_PDU(Z_APDU *apdu)
515 yaz_log (LOG_LOG, "Yaz_ProxyClient::recv_Z_PDU %s", get_hostname());
516 if (apdu->which == Z_APDU_searchResponse)
518 m_last_resultCount = *apdu->u.searchResponse->resultCount;
519 int status = *apdu->u.searchResponse->searchStatus;
521 (!apdu->u.searchResponse->records ||
522 apdu->u.searchResponse->records->which == Z_Records_DBOSD))
525 if (apdu->which == Z_APDU_presentResponse && m_sr_transform)
528 Z_PresentResponse *pr = apdu->u.presentResponse;
529 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
530 Z_SearchResponse *sr = new_apdu->u.searchResponse;
531 sr->referenceId = pr->referenceId;
532 *sr->resultCount = m_last_resultCount;
533 sr->records = pr->records;
534 sr->nextResultSetPosition = pr->nextResultSetPosition;
535 sr->numberOfRecordsReturned = pr->numberOfRecordsReturned;
538 if (m_cookie && *m_cookie)
539 set_otherInformationString (apdu, VAL_COOKIE, 1, m_cookie);
542 yaz_log (LOG_LOG, "Yaz_Proxy::send_Z_PDU");
543 m_server->send_Z_PDU(apdu);