2 * Copyright (c) 1998-2001, Index Data.
3 * See the file LICENSE for details.
5 * $Id: yaz-proxy.cpp,v 1.28 2001-11-04 22:36:21 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;
28 Yaz_Proxy::~Yaz_Proxy()
30 xfree (m_proxyTarget);
33 void Yaz_Proxy::set_proxyTarget(const char *target)
35 xfree (m_proxyTarget);
38 m_proxyTarget = (char *) xstrdup (target);
41 IYaz_PDU_Observer *Yaz_Proxy::sessionNotify(IYaz_PDU_Observable
42 *the_PDU_Observable, int fd)
44 Yaz_Proxy *new_proxy = new Yaz_Proxy(the_PDU_Observable);
45 new_proxy->m_parent = this;
46 new_proxy->timeout(500);
47 new_proxy->set_proxyTarget(m_proxyTarget);
48 new_proxy->set_APDU_log(get_APDU_log());
52 char *Yaz_Proxy::get_cookie(Z_OtherInformation **otherInfo)
55 Z_OtherInformationUnit *oi;
57 ent.proto = PROTO_Z3950;
58 ent.oclass = CLASS_USERINFO;
59 ent.value = (oid_value) VAL_COOKIE;
60 assert (oid_ent_to_oid (&ent, oid));
62 if (oid_ent_to_oid (&ent, oid) &&
63 (oi = update_otherInformation(otherInfo, 0, oid, 1, 1)) &&
64 oi->which == Z_OtherInfo_characterInfo)
65 return oi->information.characterInfo;
69 char *Yaz_Proxy::get_proxy(Z_OtherInformation **otherInfo)
72 Z_OtherInformationUnit *oi;
74 ent.proto = PROTO_Z3950;
75 ent.oclass = CLASS_USERINFO;
76 ent.value = (oid_value) VAL_PROXY;
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 Yaz_ProxyClient *Yaz_Proxy::get_client(Z_APDU *apdu)
87 Yaz_Proxy *parent = m_parent;
88 Z_OtherInformation **oi;
89 Yaz_ProxyClient *c = m_client;
91 get_otherInfoAPDU(apdu, &oi);
92 char *cookie = get_cookie(oi);
93 yaz_log (LOG_LOG, "Yaz_Proxy::get_client cookie=%s", cookie ? cookie :
96 const char *proxy_host = get_proxy(oi);
98 set_proxyTarget(proxy_host);
99 yaz_log (LOG_LOG, "proxy_host = %s", m_proxyTarget ? m_proxyTarget:"none");
101 // no target specified at all?
105 if (cookie && *cookie)
107 yaz_log (LOG_LOG, "lookup of clients cookie=%s target=%s",
108 cookie, m_proxyTarget);
109 Yaz_ProxyClient *cc = 0;
111 for (c = parent->m_clientPool; c; c = c->m_next)
113 yaz_log (LOG_LOG, " found client cookie = %s target=%s seqno=%d",
114 c->m_cookie, c->get_hostname(), c->m_seqno);
116 assert (*c->m_prev == c);
117 if (!strcmp(cookie,c->m_cookie) &&
118 !strcmp(m_proxyTarget, c->get_hostname()))
120 yaz_log (LOG_LOG, "found!");
128 // The following handles "cancel"
129 // If connection is busy (waiting for PDU) and
130 // we have an initRequest we can safely do re-open
131 if (c->m_waiting && apdu->which == Z_APDU_initRequest)
133 yaz_log (LOG_LOG, "reopen target=%s", c->get_hostname());
135 c->client(m_proxyTarget);
138 delete c->m_last_query;
140 c->m_last_resultCount = 0;
141 c->m_sr_transform = 0;
145 c->m_seqno = parent->m_seqno;
146 if (c->m_server && c->m_server != this)
147 c->m_server->m_client = 0;
149 c->m_seqno = parent->m_seqno;
151 yaz_log (LOG_LOG, "get_client 1 %p %p", this, c);
157 if (apdu->which != Z_APDU_initRequest)
159 yaz_log (LOG_LOG, "no first INIT!");
162 yaz_log (LOG_LOG, "got InitRequest");
164 // go through list of clients - and find the lowest/oldest one.
165 Yaz_ProxyClient *c_min = 0;
167 int no_of_clients = 0;
168 for (c = parent->m_clientPool; c; c = c->m_next)
171 if (min_seq < 0 || c->m_seqno < min_seq)
173 min_seq = c->m_seqno;
177 if (no_of_clients >= parent->m_max_clients)
180 if (c->m_waiting || strcmp(m_proxyTarget, c->get_hostname()))
182 yaz_log (LOG_LOG, "Yaz_Proxy::get_client re-init session %d",
184 if (c->m_server && c->m_server != this)
191 "Yaz_Proxy::get_client re-use session %d to %d",
192 c->m_seqno, parent->m_seqno);
194 strcpy (c->m_cookie, cookie);
196 c->m_cookie[0] = '\0';
197 c->m_seqno = parent->m_seqno;
198 if (c->m_server && c->m_server != this)
200 c->m_server->m_client = 0;
204 yaz_log (LOG_LOG, "get_client 2 %p %p", this, c);
210 yaz_log (LOG_LOG, "Yaz_Proxy::get_client making session %d",
212 c = new Yaz_ProxyClient(m_PDU_Observable->clone());
213 c->m_next = parent->m_clientPool;
215 c->m_next->m_prev = &c->m_next;
216 parent->m_clientPool = c;
217 c->m_prev = &parent->m_clientPool;
220 strcpy (c->m_cookie, cookie);
222 c->m_cookie[0] = '\0';
223 yaz_log (LOG_LOG, "Yaz_Proxy::get_client connect to %s",
225 c->m_seqno = parent->m_seqno;
226 c->client(m_proxyTarget);
229 delete c->m_last_query;
231 c->m_last_resultCount = 0;
232 c->m_sr_transform = 0;
238 yaz_log (LOG_LOG, "get_client 3 %p %p", this, c);
242 Z_APDU *Yaz_Proxy::result_set_optimize(Z_APDU *apdu)
244 if (apdu->which != Z_APDU_searchRequest)
246 Z_SearchRequest *sr = apdu->u.searchRequest;
247 Yaz_Z_Query *this_query = new Yaz_Z_Query;
249 this_query->set_Z_Query(sr->query);
251 if (m_client->m_last_query &&
252 m_client->m_last_query->match(this_query))
255 if (m_client->m_last_resultCount > *sr->smallSetUpperBound &&
256 m_client->m_last_resultCount < *sr->largeSetLowerBound)
259 yaz_log (LOG_LOG, "Yaz_Proxy::result_set_optimize medium set");
260 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentRequest);
261 Z_PresentRequest *pr = new_apdu->u.presentRequest;
262 pr->referenceId = sr->referenceId;
263 pr->resultSetId = sr->resultSetName;
264 pr->preferredRecordSyntax = sr->preferredRecordSyntax;
265 *pr->numberOfRecordsRequested = *sr->mediumSetPresentNumber;
266 if (sr->mediumSetElementSetNames)
268 pr->recordComposition = (Z_RecordComposition *)
269 odr_malloc(odr_encode(), sizeof(Z_RecordComposition));
270 pr->recordComposition->which = Z_RecordComp_simple;
271 pr->recordComposition->u.simple = sr->mediumSetElementSetNames;
273 m_client->m_sr_transform = 1;
276 else if (m_client->m_last_resultCount >= *sr->largeSetLowerBound ||
277 m_client->m_last_resultCount == 0)
280 yaz_log (LOG_LOG, "Yaz_Proxy::result_set_optimize large set");
281 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
282 new_apdu->u.searchResponse->referenceId = sr->referenceId;
283 new_apdu->u.searchResponse->resultCount =
284 &m_client->m_last_resultCount;
285 send_Z_PDU(new_apdu);
291 yaz_log (LOG_LOG, "Yaz_Proxy::result_set_optimize small set");
292 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentRequest);
293 Z_PresentRequest *pr = new_apdu->u.presentRequest;
294 pr->referenceId = sr->referenceId;
295 pr->resultSetId = sr->resultSetName;
296 pr->preferredRecordSyntax = sr->preferredRecordSyntax;
297 *pr->numberOfRecordsRequested = m_client->m_last_resultCount;
298 if (sr->smallSetElementSetNames)
300 pr->recordComposition = (Z_RecordComposition *)
301 odr_malloc(odr_encode(), sizeof(Z_RecordComposition));
302 pr->recordComposition->which = Z_RecordComp_simple;
303 pr->recordComposition->u.simple = sr->smallSetElementSetNames;
305 m_client->m_sr_transform = 1;
311 yaz_log (LOG_LOG, "Yaz_Proxy::result_set_optimize new set");
312 delete m_client->m_last_query;
313 m_client->m_last_query = this_query;
318 void Yaz_Proxy::recv_Z_PDU(Z_APDU *apdu)
320 yaz_log (LOG_LOG, "Yaz_Proxy::recv_Z_PDU");
321 // Determine our client.
322 m_client = get_client(apdu);
328 m_client->m_server = this;
330 if (apdu->which == Z_APDU_initRequest)
332 if (m_client->m_init_flag)
334 Z_APDU *apdu = create_Z_PDU(Z_APDU_initResponse);
335 if (m_client->m_cookie)
336 set_otherInformationString(apdu, VAL_COOKIE, 1,
341 m_client->m_init_flag = 1;
343 apdu = result_set_optimize(apdu);
347 yaz_log (LOG_LOG, "Yaz_ProxyClient::send_Z_PDU %s",
348 m_client->get_hostname());
350 // delete other info part from PDU before sending to target
351 Z_OtherInformation **oi;
352 get_otherInfoAPDU(apdu, &oi);
356 if (m_client->send_Z_PDU(apdu) < 0)
363 m_client->m_waiting = 1;
366 void Yaz_Proxy::connectNotify()
370 void Yaz_Proxy::shutdown()
372 yaz_log (LOG_LOG, "shutdown (client to proxy)");
373 // only keep if keep_alive flag and cookie is set...
374 if (m_keepalive && m_client && m_client->m_cookie[0])
376 yaz_log (LOG_LOG, "shutdown - keepalive this=%p, m_server=%p",
377 this, m_client->m_server);
378 if (m_client->m_waiting == 2)
380 // Tell client (if any) that no server connection is there..
381 m_client->m_server = 0;
385 yaz_log (LOG_LOG, "deleting %p %p", this, m_client);
386 if (m_client->m_waiting == 2)
392 yaz_log (LOG_LOG, "abort %p", this);
398 void Yaz_ProxyClient::shutdown()
400 yaz_log (LOG_LOG, "shutdown (proxy to server) %s", get_hostname());
405 void Yaz_Proxy::failNotify()
407 yaz_log (LOG_LOG, "connection closed by client");
411 void Yaz_ProxyClient::failNotify()
413 yaz_log (LOG_LOG, "Yaz_ProxyClient connection closed by %s", get_hostname());
417 void Yaz_ProxyClient::connectNotify()
419 yaz_log (LOG_LOG, "Yaz_ProxyClient connection accepted by %s",
424 IYaz_PDU_Observer *Yaz_ProxyClient::sessionNotify(IYaz_PDU_Observable
425 *the_PDU_Observable, int fd)
427 return new Yaz_ProxyClient(the_PDU_Observable);
430 Yaz_ProxyClient::~Yaz_ProxyClient()
435 m_next->m_prev = m_prev;
436 m_waiting = 2; // for debugging purposes only.
440 void Yaz_Proxy::timeoutNotify()
442 yaz_log (LOG_LOG, "timeout (client to proxy)");
446 void Yaz_ProxyClient::timeoutNotify()
448 yaz_log (LOG_LOG, "timeout (proxy to target) %s", get_hostname());
452 Yaz_ProxyClient::Yaz_ProxyClient(IYaz_PDU_Observable *the_PDU_Observable) :
453 Yaz_Z_Assoc (the_PDU_Observable)
460 m_last_resultCount = 0;
465 void Yaz_ProxyClient::recv_Z_PDU(Z_APDU *apdu)
468 yaz_log (LOG_LOG, "Yaz_ProxyClient::recv_Z_PDU %s", get_hostname());
469 if (apdu->which == Z_APDU_searchResponse)
471 m_last_resultCount = *apdu->u.searchResponse->resultCount;
472 int status = *apdu->u.searchResponse->searchStatus;
474 apdu->u.searchResponse->records &&
475 apdu->u.searchResponse->records->which != Z_Records_DBOSD))
481 if (apdu->which == Z_APDU_presentResponse && m_sr_transform)
484 Z_PresentResponse *pr = apdu->u.presentResponse;
485 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
486 Z_SearchResponse *sr = new_apdu->u.searchResponse;
487 sr->referenceId = pr->referenceId;
488 *sr->resultCount = m_last_resultCount;
489 sr->records = pr->records;
490 sr->nextResultSetPosition = pr->nextResultSetPosition;
491 sr->numberOfRecordsReturned = pr->numberOfRecordsReturned;
495 set_otherInformationString (apdu, VAL_COOKIE, 1, m_cookie);
498 yaz_log (LOG_LOG, "Yaz_Proxy::send_Z_PDU");
499 m_server->send_Z_PDU(apdu);