2 * Copyright (c) 1998-2001, Index Data.
3 * See the file LICENSE for details.
5 * $Id: yaz-proxy.cpp,v 1.37 2002-10-23 10:15:18 adam Exp $
12 #include <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;
28 m_optimize = xstrdup ("1");
31 Yaz_Proxy::~Yaz_Proxy()
33 xfree (m_proxyTarget);
34 xfree (m_proxy_authentication);
38 void Yaz_Proxy::set_proxy_target(const char *target)
40 xfree (m_proxyTarget);
43 m_proxyTarget = (char *) xstrdup (target);
46 void Yaz_Proxy::set_proxy_authentication (const char *auth)
48 xfree (m_proxy_authentication);
49 m_proxy_authentication = 0;
51 m_proxy_authentication = (char *) xstrdup (auth);
54 IYaz_PDU_Observer *Yaz_Proxy::sessionNotify(IYaz_PDU_Observable
55 *the_PDU_Observable, int fd)
57 Yaz_Proxy *new_proxy = new Yaz_Proxy(the_PDU_Observable);
58 new_proxy->m_parent = this;
59 new_proxy->timeout(m_idletime);
60 new_proxy->set_proxy_target(m_proxyTarget);
61 new_proxy->set_APDU_log(get_APDU_log());
62 new_proxy->set_proxy_authentication(m_proxy_authentication);
63 yaz_log (LOG_LOG, "New session p=%p", new_proxy);
67 char *Yaz_Proxy::get_cookie(Z_OtherInformation **otherInfo)
70 Z_OtherInformationUnit *oi;
72 ent.proto = PROTO_Z3950;
73 ent.oclass = CLASS_USERINFO;
74 ent.value = (oid_value) VAL_COOKIE;
75 assert (oid_ent_to_oid (&ent, oid));
77 if (oid_ent_to_oid (&ent, oid) &&
78 (oi = update_otherInformation(otherInfo, 0, oid, 1, 1)) &&
79 oi->which == Z_OtherInfo_characterInfo)
80 return oi->information.characterInfo;
84 char *Yaz_Proxy::get_proxy(Z_OtherInformation **otherInfo)
87 Z_OtherInformationUnit *oi;
89 ent.proto = PROTO_Z3950;
90 ent.oclass = CLASS_USERINFO;
91 ent.value = (oid_value) VAL_PROXY;
92 if (oid_ent_to_oid (&ent, oid) &&
93 (oi = update_otherInformation(otherInfo, 0, oid, 1, 1)) &&
94 oi->which == Z_OtherInfo_characterInfo)
95 return oi->information.characterInfo;
99 Yaz_ProxyClient *Yaz_Proxy::get_client(Z_APDU *apdu)
102 Yaz_Proxy *parent = m_parent;
103 Z_OtherInformation **oi;
104 Yaz_ProxyClient *c = m_client;
106 get_otherInfoAPDU(apdu, &oi);
107 char *cookie = get_cookie(oi);
109 const char *proxy_host = get_proxy(oi);
111 set_proxy_target(proxy_host);
113 // no target specified at all?
117 if (!strcmp(m_proxyTarget, "stop"))
119 if (cookie && *cookie)
121 Yaz_ProxyClient *cc = 0;
123 for (c = parent->m_clientPool; c; c = c->m_next)
126 assert (*c->m_prev == c);
127 if (!strcmp(cookie,c->m_cookie) &&
128 !strcmp(m_proxyTarget, c->get_hostname()))
137 // The following handles "cancel"
138 // If connection is busy (waiting for PDU) and
139 // we have an initRequest we can safely do re-open
140 if (c->m_waiting && apdu->which == Z_APDU_initRequest)
142 yaz_log (LOG_LOG, "reopen target=%s", c->get_hostname());
144 c->client(m_proxyTarget);
148 c->m_last_resultCount = 0;
149 c->m_sr_transform = 0;
151 c->timeout(m_idletime);
153 c->m_seqno = parent->m_seqno;
154 if (c->m_server && c->m_server != this)
155 c->m_server->m_client = 0;
157 c->m_seqno = parent->m_seqno;
159 yaz_log (LOG_DEBUG, "get_client 1 %p %p", this, c);
165 if (apdu->which != Z_APDU_initRequest)
167 yaz_log (LOG_LOG, "no first INIT!");
170 Z_InitRequest *initRequest = apdu->u.initRequest;
172 if (!initRequest->idAuthentication)
174 if (m_proxy_authentication)
176 initRequest->idAuthentication =
177 (Z_IdAuthentication *)
178 odr_malloc (odr_encode(),
179 sizeof(*initRequest->idAuthentication));
180 initRequest->idAuthentication->which =
181 Z_IdAuthentication_open;
182 initRequest->idAuthentication->u.open =
183 odr_strdup (odr_encode(), m_proxy_authentication);
187 // go through list of clients - and find the lowest/oldest one.
188 Yaz_ProxyClient *c_min = 0;
190 int no_of_clients = 0;
191 yaz_log (LOG_LOG, "Existing sessions");
192 for (c = parent->m_clientPool; c; c = c->m_next)
194 yaz_log (LOG_LOG, " Session %-3d wait=%d %s", c->m_seqno,
195 c->m_waiting, c->get_hostname());
197 if (min_seq < 0 || c->m_seqno < min_seq)
199 min_seq = c->m_seqno;
203 if (no_of_clients >= parent->m_max_clients)
206 if (c->m_waiting || strcmp(m_proxyTarget, c->get_hostname()))
208 yaz_log (LOG_LOG, "Replace session %d",
210 if (c->m_server && c->m_server != this)
216 yaz_log (LOG_LOG, "Reuse session %d to %d",
217 c->m_seqno, parent->m_seqno);
219 strcpy (c->m_cookie, cookie);
221 c->m_cookie[0] = '\0';
222 c->m_seqno = parent->m_seqno;
223 if (c->m_server && c->m_server != this)
225 c->m_server->m_client = 0;
229 yaz_log (LOG_DEBUG, "get_client 2 %p %p", this, c);
235 yaz_log (LOG_LOG, "Making session %d %s", parent->m_seqno,
237 c = new Yaz_ProxyClient(m_PDU_Observable->clone());
238 c->m_next = parent->m_clientPool;
240 c->m_next->m_prev = &c->m_next;
241 parent->m_clientPool = c;
242 c->m_prev = &parent->m_clientPool;
245 strcpy (c->m_cookie, cookie);
247 c->m_cookie[0] = '\0';
248 yaz_log (LOG_LOG, "Connecting to %s", m_proxyTarget);
249 c->m_seqno = parent->m_seqno;
250 c->client(m_proxyTarget);
252 c->m_last_resultCount = 0;
254 c->m_sr_transform = 0;
260 yaz_log (LOG_DEBUG, "get_client 3 %p %p", this, c);
264 Z_APDU *Yaz_Proxy::result_set_optimize(Z_APDU *apdu)
266 if (apdu->which != Z_APDU_searchRequest)
268 if (*m_parent->m_optimize != '1')
270 Z_SearchRequest *sr = apdu->u.searchRequest;
271 Yaz_Z_Query *this_query = new Yaz_Z_Query;
272 Yaz_Z_Databases this_databases;
274 this_databases.set(sr->num_databaseNames, (const char **)
277 this_query->set_Z_Query(sr->query);
279 if (m_client->m_last_ok && m_client->m_last_query &&
280 m_client->m_last_query->match(this_query) &&
281 !strcmp(m_client->m_last_resultSetId, sr->resultSetName) &&
282 m_client->m_last_databases.match(this_databases))
285 if (m_client->m_last_resultCount > *sr->smallSetUpperBound &&
286 m_client->m_last_resultCount < *sr->largeSetLowerBound)
289 // send present request (medium size)
290 yaz_log (LOG_LOG, "Optimizing search for medium set");
291 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentRequest);
292 Z_PresentRequest *pr = new_apdu->u.presentRequest;
293 pr->referenceId = sr->referenceId;
294 pr->resultSetId = sr->resultSetName;
295 pr->preferredRecordSyntax = sr->preferredRecordSyntax;
296 *pr->numberOfRecordsRequested = *sr->mediumSetPresentNumber;
297 if (sr->mediumSetElementSetNames)
299 pr->recordComposition = (Z_RecordComposition *)
300 odr_malloc(odr_encode(), sizeof(Z_RecordComposition));
301 pr->recordComposition->which = Z_RecordComp_simple;
302 pr->recordComposition->u.simple = sr->mediumSetElementSetNames;
304 m_client->m_sr_transform = 1;
307 else if (m_client->m_last_resultCount >= *sr->largeSetLowerBound ||
308 m_client->m_last_resultCount <= 0)
310 // large set. Return pseudo-search response immediately
311 yaz_log (LOG_LOG, "Optimizing search for large set");
312 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
313 new_apdu->u.searchResponse->referenceId = sr->referenceId;
314 new_apdu->u.searchResponse->resultCount =
315 &m_client->m_last_resultCount;
316 send_Z_PDU(new_apdu);
322 // send a present request (small set)
323 yaz_log (LOG_LOG, "Optimizing search for small set");
324 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentRequest);
325 Z_PresentRequest *pr = new_apdu->u.presentRequest;
326 pr->referenceId = sr->referenceId;
327 pr->resultSetId = sr->resultSetName;
328 pr->preferredRecordSyntax = sr->preferredRecordSyntax;
329 *pr->numberOfRecordsRequested = m_client->m_last_resultCount;
330 if (sr->smallSetElementSetNames)
332 pr->recordComposition = (Z_RecordComposition *)
333 odr_malloc(odr_encode(), sizeof(Z_RecordComposition));
334 pr->recordComposition->which = Z_RecordComp_simple;
335 pr->recordComposition->u.simple = sr->smallSetElementSetNames;
337 m_client->m_sr_transform = 1;
343 delete m_client->m_last_query;
344 m_client->m_last_query = this_query;
345 m_client->m_last_ok = 0;
347 xfree (m_client->m_last_resultSetId);
348 m_client->m_last_resultSetId = xstrdup (sr->resultSetName);
350 m_client->m_last_databases.set(sr->num_databaseNames,
351 (const char **) sr->databaseNames);
356 static const char *apdu_name(Z_APDU *apdu)
360 case Z_APDU_initRequest:
361 return "initRequest";
362 case Z_APDU_initResponse:
363 return "initResponse";
364 case Z_APDU_searchRequest:
365 return "searchRequest";
366 case Z_APDU_searchResponse:
367 return "searchResponse";
368 case Z_APDU_presentRequest:
369 return "presentRequest";
370 case Z_APDU_presentResponse:
371 return "presentResponse";
376 void Yaz_Proxy::recv_Z_PDU(Z_APDU *apdu)
378 yaz_log (LOG_LOG, "Receiving %s from client", apdu_name(apdu));
379 // Determine our client.
380 m_client = get_client(apdu);
386 m_client->m_server = this;
388 if (apdu->which == Z_APDU_initRequest)
390 if (m_client->m_init_flag)
392 Z_APDU *apdu = m_client->m_initResponse;
393 apdu->u.initResponse->otherInfo = 0;
394 if (m_client->m_cookie)
395 set_otherInformationString(apdu, VAL_COOKIE, 1,
400 m_client->m_init_flag = 1;
402 apdu = result_set_optimize(apdu);
406 yaz_log (LOG_LOG, "Sending %s to %s",
407 apdu_name(apdu), m_client->get_hostname());
409 // delete other info part from PDU before sending to target
410 Z_OtherInformation **oi;
411 get_otherInfoAPDU(apdu, &oi);
415 if (m_client->send_Z_PDU(apdu) < 0)
422 m_client->m_waiting = 1;
425 void Yaz_Proxy::connectNotify()
429 void Yaz_Proxy::shutdown()
431 // only keep if keep_alive flag and cookie is set...
432 if (m_keepalive && m_client && m_client->m_cookie[0])
434 yaz_log (LOG_LOG, "shutdown (client to proxy) keepalive %s",
435 m_client->get_hostname());
436 assert (m_client->m_waiting != 2);
437 // Tell client (if any) that no server connection is there..
438 m_client->m_server = 0;
443 yaz_log (LOG_LOG, "shutdown (client to proxy) close %s",
444 m_client->get_hostname());
445 assert (m_client->m_waiting != 2);
450 yaz_log (LOG_LOG, "shutdown (client to proxy) bad state");
455 yaz_log (LOG_LOG, "shutdown (client to proxy)");
460 void Yaz_ProxyClient::shutdown()
462 yaz_log (LOG_LOG, "shutdown (proxy to server) %s", get_hostname());
467 void Yaz_Proxy::failNotify()
469 yaz_log (LOG_LOG, "Yaz_Proxy connection closed by client");
473 void Yaz_ProxyClient::failNotify()
475 yaz_log (LOG_LOG, "Yaz_ProxyClient connection closed by %s", get_hostname());
479 void Yaz_ProxyClient::connectNotify()
481 yaz_log (LOG_LOG, "Connection accepted by %s", get_hostname());
485 IYaz_PDU_Observer *Yaz_ProxyClient::sessionNotify(IYaz_PDU_Observable
486 *the_PDU_Observable, int fd)
488 return new Yaz_ProxyClient(the_PDU_Observable);
491 Yaz_ProxyClient::~Yaz_ProxyClient()
496 m_next->m_prev = m_prev;
497 m_waiting = 2; // for debugging purposes only.
498 odr_destroy(m_init_odr);
500 xfree (m_last_resultSetId);
503 void Yaz_Proxy::timeoutNotify()
505 yaz_log (LOG_LOG, "timeout (client to proxy)");
509 void Yaz_ProxyClient::timeoutNotify()
511 yaz_log (LOG_LOG, "timeout (proxy to target) %s", get_hostname());
515 Yaz_ProxyClient::Yaz_ProxyClient(IYaz_PDU_Observable *the_PDU_Observable) :
516 Yaz_Z_Assoc (the_PDU_Observable)
523 m_last_resultSetId = 0;
524 m_last_resultCount = 0;
528 m_init_odr = odr_createmem (ODR_DECODE);
532 const char *Yaz_Proxy::option(const char *name, const char *value)
534 if (!strcmp (name, "optimize")) {
537 m_optimize = xstrdup (value);
544 void Yaz_ProxyClient::recv_Z_PDU(Z_APDU *apdu)
547 yaz_log (LOG_LOG, "Receiving %s from %s", apdu_name(apdu),
549 if (apdu->which == Z_APDU_initResponse)
551 NMEM nmem = odr_extract_mem (odr_decode());
552 odr_reset (m_init_odr);
553 nmem_transfer (m_init_odr->mem, nmem);
554 m_initResponse = apdu;
557 if (apdu->which == Z_APDU_searchResponse)
559 m_last_resultCount = *apdu->u.searchResponse->resultCount;
560 int status = *apdu->u.searchResponse->searchStatus;
562 (!apdu->u.searchResponse->records ||
563 apdu->u.searchResponse->records->which == Z_Records_DBOSD))
566 if (apdu->which == Z_APDU_presentResponse && m_sr_transform)
569 Z_PresentResponse *pr = apdu->u.presentResponse;
570 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
571 Z_SearchResponse *sr = new_apdu->u.searchResponse;
572 sr->referenceId = pr->referenceId;
573 *sr->resultCount = m_last_resultCount;
574 sr->records = pr->records;
575 sr->nextResultSetPosition = pr->nextResultSetPosition;
576 sr->numberOfRecordsReturned = pr->numberOfRecordsReturned;
579 if (m_cookie && *m_cookie)
580 set_otherInformationString (apdu, VAL_COOKIE, 1, m_cookie);
583 yaz_log (LOG_LOG, "Sending %s to client", apdu_name(apdu));
584 m_server->send_Z_PDU(apdu);