2 * Copyright (c) 1998-2001, Index Data.
3 * See the file LICENSE for details.
5 * $Id: yaz-proxy.cpp,v 1.32 2002-08-28 13:01:41 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);
62 yaz_log (LOG_LOG, "New session p=%p", new_proxy);
66 char *Yaz_Proxy::get_cookie(Z_OtherInformation **otherInfo)
69 Z_OtherInformationUnit *oi;
71 ent.proto = PROTO_Z3950;
72 ent.oclass = CLASS_USERINFO;
73 ent.value = (oid_value) VAL_COOKIE;
74 assert (oid_ent_to_oid (&ent, oid));
76 if (oid_ent_to_oid (&ent, oid) &&
77 (oi = update_otherInformation(otherInfo, 0, oid, 1, 1)) &&
78 oi->which == Z_OtherInfo_characterInfo)
79 return oi->information.characterInfo;
83 char *Yaz_Proxy::get_proxy(Z_OtherInformation **otherInfo)
86 Z_OtherInformationUnit *oi;
88 ent.proto = PROTO_Z3950;
89 ent.oclass = CLASS_USERINFO;
90 ent.value = (oid_value) VAL_PROXY;
91 if (oid_ent_to_oid (&ent, oid) &&
92 (oi = update_otherInformation(otherInfo, 0, oid, 1, 1)) &&
93 oi->which == Z_OtherInfo_characterInfo)
94 return oi->information.characterInfo;
98 Yaz_ProxyClient *Yaz_Proxy::get_client(Z_APDU *apdu)
101 Yaz_Proxy *parent = m_parent;
102 Z_OtherInformation **oi;
103 Yaz_ProxyClient *c = m_client;
105 get_otherInfoAPDU(apdu, &oi);
106 char *cookie = get_cookie(oi);
108 const char *proxy_host = get_proxy(oi);
110 set_proxy_target(proxy_host);
112 // no target specified at all?
116 if (!strcmp(m_proxyTarget, "stop"))
118 if (cookie && *cookie)
120 Yaz_ProxyClient *cc = 0;
122 for (c = parent->m_clientPool; c; c = c->m_next)
125 assert (*c->m_prev == c);
126 if (!strcmp(cookie,c->m_cookie) &&
127 !strcmp(m_proxyTarget, c->get_hostname()))
136 // The following handles "cancel"
137 // If connection is busy (waiting for PDU) and
138 // we have an initRequest we can safely do re-open
139 if (c->m_waiting && apdu->which == Z_APDU_initRequest)
141 yaz_log (LOG_LOG, "reopen target=%s", c->get_hostname());
143 c->client(m_proxyTarget);
147 c->m_last_resultCount = 0;
148 c->m_sr_transform = 0;
152 c->m_seqno = parent->m_seqno;
153 if (c->m_server && c->m_server != this)
154 c->m_server->m_client = 0;
156 c->m_seqno = parent->m_seqno;
158 yaz_log (LOG_DEBUG, "get_client 1 %p %p", this, c);
164 if (apdu->which != Z_APDU_initRequest)
166 yaz_log (LOG_LOG, "no first INIT!");
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 yaz_log (LOG_LOG, "Existing sessions");
191 for (c = parent->m_clientPool; c; c = c->m_next)
193 yaz_log (LOG_LOG, " Session %-3d wait=%d %s", c->m_seqno,
194 c->m_waiting, c->get_hostname());
196 if (min_seq < 0 || c->m_seqno < min_seq)
198 min_seq = c->m_seqno;
202 if (no_of_clients >= parent->m_max_clients)
205 if (c->m_waiting || strcmp(m_proxyTarget, c->get_hostname()))
207 yaz_log (LOG_LOG, "Replace session %d",
209 if (c->m_server && c->m_server != this)
215 yaz_log (LOG_LOG, "Reuse session %d to %d",
216 c->m_seqno, parent->m_seqno);
218 strcpy (c->m_cookie, cookie);
220 c->m_cookie[0] = '\0';
221 c->m_seqno = parent->m_seqno;
222 if (c->m_server && c->m_server != this)
224 c->m_server->m_client = 0;
228 yaz_log (LOG_DEBUG, "get_client 2 %p %p", this, c);
234 yaz_log (LOG_LOG, "Making session %d %s", parent->m_seqno,
236 c = new Yaz_ProxyClient(m_PDU_Observable->clone());
237 c->m_next = parent->m_clientPool;
239 c->m_next->m_prev = &c->m_next;
240 parent->m_clientPool = c;
241 c->m_prev = &parent->m_clientPool;
244 strcpy (c->m_cookie, cookie);
246 c->m_cookie[0] = '\0';
247 yaz_log (LOG_LOG, "Connecting to %s", m_proxyTarget);
248 c->m_seqno = parent->m_seqno;
249 c->client(m_proxyTarget);
251 c->m_last_resultCount = 0;
253 c->m_sr_transform = 0;
259 yaz_log (LOG_DEBUG, "get_client 3 %p %p", this, c);
263 Z_APDU *Yaz_Proxy::result_set_optimize(Z_APDU *apdu)
265 if (apdu->which != Z_APDU_searchRequest)
267 if (*m_parent->m_optimize != '1')
269 Z_SearchRequest *sr = apdu->u.searchRequest;
270 Yaz_Z_Query *this_query = new Yaz_Z_Query;
271 Yaz_Z_Databases this_databases;
273 this_databases.set(sr->num_databaseNames, (const char **)
276 this_query->set_Z_Query(sr->query);
278 if (m_client->m_last_ok && m_client->m_last_query &&
279 m_client->m_last_query->match(this_query) &&
280 m_client->m_last_databases.match(this_databases))
283 if (m_client->m_last_resultCount > *sr->smallSetUpperBound &&
284 m_client->m_last_resultCount < *sr->largeSetLowerBound)
287 // send present request (medium size)
288 yaz_log (LOG_LOG, "Optimizing search for medium set");
289 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentRequest);
290 Z_PresentRequest *pr = new_apdu->u.presentRequest;
291 pr->referenceId = sr->referenceId;
292 pr->resultSetId = sr->resultSetName;
293 pr->preferredRecordSyntax = sr->preferredRecordSyntax;
294 *pr->numberOfRecordsRequested = *sr->mediumSetPresentNumber;
295 if (sr->mediumSetElementSetNames)
297 pr->recordComposition = (Z_RecordComposition *)
298 odr_malloc(odr_encode(), sizeof(Z_RecordComposition));
299 pr->recordComposition->which = Z_RecordComp_simple;
300 pr->recordComposition->u.simple = sr->mediumSetElementSetNames;
302 m_client->m_sr_transform = 1;
305 else if (m_client->m_last_resultCount >= *sr->largeSetLowerBound ||
306 m_client->m_last_resultCount == 0)
308 // large set. Return pseudo-search response immediately
309 yaz_log (LOG_LOG, "Optimizing search for large set");
310 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
311 new_apdu->u.searchResponse->referenceId = sr->referenceId;
312 new_apdu->u.searchResponse->resultCount =
313 &m_client->m_last_resultCount;
314 send_Z_PDU(new_apdu);
320 // send a present request (small set)
321 yaz_log (LOG_LOG, "Optimizing search for small set");
322 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentRequest);
323 Z_PresentRequest *pr = new_apdu->u.presentRequest;
324 pr->referenceId = sr->referenceId;
325 pr->resultSetId = sr->resultSetName;
326 pr->preferredRecordSyntax = sr->preferredRecordSyntax;
327 *pr->numberOfRecordsRequested = m_client->m_last_resultCount;
328 if (sr->smallSetElementSetNames)
330 pr->recordComposition = (Z_RecordComposition *)
331 odr_malloc(odr_encode(), sizeof(Z_RecordComposition));
332 pr->recordComposition->which = Z_RecordComp_simple;
333 pr->recordComposition->u.simple = sr->smallSetElementSetNames;
335 m_client->m_sr_transform = 1;
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 static const char *apdu_name(Z_APDU *apdu)
354 case Z_APDU_initRequest:
355 return "initRequest";
356 case Z_APDU_initResponse:
357 return "initResponse";
358 case Z_APDU_searchRequest:
359 return "searchRequest";
360 case Z_APDU_searchResponse:
361 return "searchResponse";
362 case Z_APDU_presentRequest:
363 return "presentRequest";
364 case Z_APDU_presentResponse:
365 return "presentResponse";
370 void Yaz_Proxy::recv_Z_PDU(Z_APDU *apdu)
372 yaz_log (LOG_LOG, "Receiving %s from client", apdu_name(apdu));
373 // Determine our client.
374 m_client = get_client(apdu);
380 m_client->m_server = this;
382 if (apdu->which == Z_APDU_initRequest)
384 if (m_client->m_init_flag)
386 Z_APDU *apdu = create_Z_PDU(Z_APDU_initResponse);
387 if (m_client->m_cookie)
388 set_otherInformationString(apdu, VAL_COOKIE, 1,
393 m_client->m_init_flag = 1;
395 apdu = result_set_optimize(apdu);
399 yaz_log (LOG_LOG, "Sending %s to %s",
400 apdu_name(apdu), m_client->get_hostname());
402 // delete other info part from PDU before sending to target
403 Z_OtherInformation **oi;
404 get_otherInfoAPDU(apdu, &oi);
408 if (m_client->send_Z_PDU(apdu) < 0)
415 m_client->m_waiting = 1;
418 void Yaz_Proxy::connectNotify()
422 void Yaz_Proxy::shutdown()
424 // only keep if keep_alive flag and cookie is set...
425 if (m_keepalive && m_client && m_client->m_cookie[0])
427 if (m_client->m_waiting == 2)
429 // Tell client (if any) that no server connection is there..
430 m_client->m_server = 0;
431 yaz_log (LOG_LOG, "shutdown (client to proxy) keepalive %s", m_client->get_hostname());
435 if (m_client->m_waiting == 2)
437 yaz_log (LOG_LOG, "shutdown (client to proxy) close %s", m_client->get_hostname());
442 yaz_log (LOG_LOG, "shutdown (client to proxy) bad state");
447 yaz_log (LOG_LOG, "shutdown (client to proxy)");
452 void Yaz_ProxyClient::shutdown()
454 yaz_log (LOG_LOG, "shutdown (proxy to server) %s", get_hostname());
459 void Yaz_Proxy::failNotify()
461 yaz_log (LOG_LOG, "Yaz_Proxy connection closed by client");
465 void Yaz_ProxyClient::failNotify()
467 yaz_log (LOG_LOG, "Yaz_ProxyClient connection closed by %s", get_hostname());
471 void Yaz_ProxyClient::connectNotify()
473 yaz_log (LOG_LOG, "Connection accepted by %s", get_hostname());
477 IYaz_PDU_Observer *Yaz_ProxyClient::sessionNotify(IYaz_PDU_Observable
478 *the_PDU_Observable, int fd)
480 return new Yaz_ProxyClient(the_PDU_Observable);
483 Yaz_ProxyClient::~Yaz_ProxyClient()
488 m_next->m_prev = m_prev;
489 m_waiting = 2; // for debugging purposes only.
493 void Yaz_Proxy::timeoutNotify()
495 yaz_log (LOG_LOG, "timeout (client to proxy)");
499 void Yaz_ProxyClient::timeoutNotify()
501 yaz_log (LOG_LOG, "timeout (proxy to target) %s", get_hostname());
505 Yaz_ProxyClient::Yaz_ProxyClient(IYaz_PDU_Observable *the_PDU_Observable) :
506 Yaz_Z_Assoc (the_PDU_Observable)
513 m_last_resultCount = 0;
519 const char *Yaz_Proxy::option(const char *name, const char *value)
521 if (!strcmp (name, "optimize")) {
524 m_optimize = xstrdup (value);
531 void Yaz_ProxyClient::recv_Z_PDU(Z_APDU *apdu)
534 yaz_log (LOG_LOG, "Receiving %s from %s", apdu_name(apdu),
536 if (apdu->which == Z_APDU_searchResponse)
538 m_last_resultCount = *apdu->u.searchResponse->resultCount;
539 int status = *apdu->u.searchResponse->searchStatus;
541 (!apdu->u.searchResponse->records ||
542 apdu->u.searchResponse->records->which == Z_Records_DBOSD))
545 if (apdu->which == Z_APDU_presentResponse && m_sr_transform)
548 Z_PresentResponse *pr = apdu->u.presentResponse;
549 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
550 Z_SearchResponse *sr = new_apdu->u.searchResponse;
551 sr->referenceId = pr->referenceId;
552 *sr->resultCount = m_last_resultCount;
553 sr->records = pr->records;
554 sr->nextResultSetPosition = pr->nextResultSetPosition;
555 sr->numberOfRecordsReturned = pr->numberOfRecordsReturned;
558 if (m_cookie && *m_cookie)
559 set_otherInformationString (apdu, VAL_COOKIE, 1, m_cookie);
562 yaz_log (LOG_LOG, "Sending %s to client", apdu_name(apdu));
563 m_server->send_Z_PDU(apdu);