2 * Copyright (c) 1998-1999, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
6 * $Log: yaz-proxy.cpp,v $
7 * Revision 1.12 2000-07-04 13:48:49 adam
8 * Implemented upper-limit on proxy-to-target sessions.
10 * Revision 1.11 1999/12/06 13:52:45 adam
11 * Modified for new location of YAZ header files. Experimental threaded
14 * Revision 1.10 1999/11/10 10:02:34 adam
17 * Revision 1.9 1999/09/13 12:53:44 adam
18 * Proxy removes OtherInfo Proxy Address and Session ID. Other
19 * Otherinfo remains untouched.
21 * Revision 1.8 1999/05/04 10:53:00 adam
22 * Changed the way the PROXY behaves when lost cookie is received.
24 * Revision 1.7 1999/04/28 13:31:17 adam
25 * Better result set optimisation for proxy.
27 * Revision 1.6 1999/04/27 07:52:13 adam
28 * Improved proxy; added query match for result set re-use.
30 * Revision 1.5 1999/04/21 12:09:01 adam
31 * Many improvements. Modified to proxy server to work with "sessions"
34 * Revision 1.4 1999/04/20 10:30:05 adam
35 * Implemented various stuff for client and proxy. Updated calls
36 * to ODR to reflect new name parameter.
38 * Revision 1.3 1999/04/09 11:46:57 adam
39 * Added object Yaz_Z_Assoc. Much more functional client.
41 * Revision 1.2 1999/01/28 13:08:46 adam
42 * Yaz_PDU_Assoc better encapsulated. Memory leak fix in
43 * yaz-socket-manager.cc.
45 * Revision 1.1.1.1 1999/01/28 09:41:07 adam
46 * First implementation of YAZ++.
53 #include <yaz-proxy.h>
55 Yaz_Proxy::Yaz_Proxy(IYaz_PDU_Observable *the_PDU_Observable) :
56 Yaz_Z_Assoc(the_PDU_Observable)
58 m_PDU_Observable = the_PDU_Observable;
68 Yaz_Proxy::~Yaz_Proxy()
70 xfree (m_proxyTarget);
74 void Yaz_Proxy::set_proxyTarget(const char *target)
76 xfree (m_proxyTarget);
79 m_proxyTarget = (char *) xstrdup (target);
82 IYaz_PDU_Observer *Yaz_Proxy::clone(IYaz_PDU_Observable
85 Yaz_Proxy *new_proxy = new Yaz_Proxy(the_PDU_Observable);
86 new_proxy->m_parent = this;
87 new_proxy->timeout(120);
88 new_proxy->set_proxyTarget(m_proxyTarget);
92 char *Yaz_Proxy::get_cookie(Z_OtherInformation **otherInfo)
95 Z_OtherInformationUnit *oi;
97 ent.proto = PROTO_Z3950;
98 ent.oclass = CLASS_USERINFO;
99 ent.value = (oid_value) VAL_COOKIE;
100 assert (oid_ent_to_oid (&ent, oid));
102 if (oid_ent_to_oid (&ent, oid) &&
103 (oi = update_otherInformation(otherInfo, 0, oid, 1, 1)) &&
104 oi->which == Z_OtherInfo_characterInfo)
105 return oi->information.characterInfo;
109 char *Yaz_Proxy::get_proxy(Z_OtherInformation **otherInfo)
112 Z_OtherInformationUnit *oi;
114 ent.proto = PROTO_Z3950;
115 ent.oclass = CLASS_USERINFO;
116 ent.value = (oid_value) VAL_PROXY;
117 if (oid_ent_to_oid (&ent, oid) &&
118 (oi = update_otherInformation(otherInfo, 0, oid, 1, 1)) &&
119 oi->which == Z_OtherInfo_characterInfo)
120 return oi->information.characterInfo;
124 Yaz_ProxyClient *Yaz_Proxy::get_client(Z_APDU *apdu)
127 Yaz_Proxy *parent = m_parent;
128 Z_OtherInformation **oi;
129 Yaz_ProxyClient *c = m_client;
131 get_otherInfoAPDU(apdu, &oi);
132 char *cookie = get_cookie(oi);
133 logf (LOG_LOG, "Yaz_Proxy::get_client cookie=%s", cookie ? cookie :
137 for (c = parent->m_clientPool; c; c = c->m_next)
140 assert (*c->m_prev == c);
141 if (!strcmp(cookie,c->m_cookie))
143 logf (LOG_LOG, "Yaz_Proxy::get_client found cached target");
144 c->m_seqno = parent->m_seqno;
151 if (apdu->which == Z_APDU_initRequest)
153 logf (LOG_LOG, "got InitRequest");
155 const char *proxy_host =
156 get_proxy(&apdu->u.initRequest->otherInfo);
158 set_proxyTarget(proxy_host);
159 logf (LOG_LOG, "proxy_host = %s", m_proxyTarget ?
160 m_proxyTarget:"none");
164 logf (LOG_LOG, "no first INIT!");
170 Yaz_ProxyClient *c_min = 0;
172 int no_of_clients = 0;
173 for (c = parent->m_clientPool; c; c = c->m_next)
176 logf (LOG_LOG, "found seqno = %d", c->m_seqno);
177 if (min_seq < 0 || c->m_seqno < min_seq)
179 min_seq = c->m_seqno;
183 if (no_of_clients >= parent->m_max_clients)
186 logf (LOG_LOG, "Yaz_Proxy::get_client re-using session %d",
194 logf (LOG_LOG, "Yaz_Proxy::get_client making new session");
195 c = new Yaz_ProxyClient(m_PDU_Observable->clone());
196 c->m_next = parent->m_clientPool;
198 c->m_next->m_prev = &c->m_next;
199 parent->m_clientPool = c;
200 c->m_prev = &parent->m_clientPool;
202 sprintf (c->m_cookie, "%d", parent->m_seqno);
203 logf (LOG_LOG, "Yaz_Proxy::get_client new session %s", c->m_cookie);
204 c->m_seqno = parent->m_seqno;
205 c->client(m_proxyTarget);
208 delete c->m_last_query;
210 c->m_last_resultCount = 0;
211 c->m_sr_transform = 0;
220 Z_APDU *Yaz_Proxy::result_set_optimize(Z_APDU *apdu)
222 if (apdu->which != Z_APDU_searchRequest)
224 Z_SearchRequest *sr = apdu->u.searchRequest;
225 Yaz_Z_Query *this_query = new Yaz_Z_Query;
227 this_query->set_Z_Query(sr->query);
229 if (m_client->m_last_query &&
230 m_client->m_last_query->match(this_query))
233 if (m_client->m_last_resultCount > *sr->smallSetUpperBound &&
234 m_client->m_last_resultCount < *sr->largeSetLowerBound)
237 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentRequest);
238 Z_PresentRequest *pr = new_apdu->u.presentRequest;
239 pr->referenceId = sr->referenceId;
240 pr->resultSetId = sr->resultSetName;
241 pr->preferredRecordSyntax = sr->preferredRecordSyntax;
242 *pr->numberOfRecordsRequested = *sr->mediumSetPresentNumber;
243 if (sr->mediumSetElementSetNames)
245 pr->recordComposition = (Z_RecordComposition *)
246 odr_malloc(odr_encode(), sizeof(Z_RecordComposition));
247 pr->recordComposition->which = Z_RecordComp_simple;
248 pr->recordComposition->u.simple = sr->mediumSetElementSetNames;
250 m_client->m_sr_transform = 1;
253 else if (m_client->m_last_resultCount >= *sr->largeSetLowerBound ||
254 m_client->m_last_resultCount == 0)
257 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
258 new_apdu->u.searchResponse->referenceId = sr->referenceId;
259 new_apdu->u.searchResponse->resultCount =
260 &m_client->m_last_resultCount;
261 send_Z_PDU(new_apdu);
267 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentRequest);
268 Z_PresentRequest *pr = new_apdu->u.presentRequest;
269 pr->referenceId = sr->referenceId;
270 pr->resultSetId = sr->resultSetName;
271 pr->preferredRecordSyntax = sr->preferredRecordSyntax;
272 *pr->numberOfRecordsRequested = m_client->m_last_resultCount;
273 if (sr->smallSetElementSetNames)
275 pr->recordComposition = (Z_RecordComposition *)
276 odr_malloc(odr_encode(), sizeof(Z_RecordComposition));
277 pr->recordComposition->which = Z_RecordComp_simple;
278 pr->recordComposition->u.simple = sr->smallSetElementSetNames;
280 m_client->m_sr_transform = 1;
286 logf (LOG_LOG, "query doesn't match");
287 delete m_client->m_last_query;
288 m_client->m_last_query = this_query;
293 void Yaz_Proxy::recv_Z_PDU(Z_APDU *apdu)
295 logf (LOG_LOG, "Yaz_Proxy::recv_Z_PDU");
296 // Determine our client.
297 m_client = get_client(apdu);
303 m_client->m_server = this;
306 Z_OtherInformation **oi;
307 get_otherInfoAPDU(apdu, &oi);
311 if (apdu->which == Z_APDU_initRequest)
313 if (m_client->m_init_flag)
315 Z_APDU *apdu = create_Z_PDU(Z_APDU_initResponse);
316 if (m_client->m_cookie)
317 set_otherInformationString(apdu, VAL_COOKIE, 1,
322 m_client->m_init_flag = 1;
324 apdu = result_set_optimize(apdu);
328 logf (LOG_LOG, "Yaz_ProxyClient::send_Z_PDU");
329 if (m_client->send_Z_PDU(apdu) < 0)
336 void Yaz_Proxy::connectNotify()
340 void Yaz_Proxy::shutdown()
342 logf (LOG_LOG, "shutdown (client to proxy)");
345 // Tell client (if any) that no server connection is there..
347 m_client->m_server = 0;
356 void Yaz_ProxyClient::shutdown()
358 logf (LOG_LOG, "shutdown (proxy to server)");
363 void Yaz_Proxy::failNotify()
365 logf (LOG_LOG, "connection closed by client");
369 void Yaz_ProxyClient::failNotify()
371 logf (LOG_LOG, "connection closed by target");
375 void Yaz_ProxyClient::connectNotify()
377 logf (LOG_LOG, "connection accepted by target");
380 IYaz_PDU_Observer *Yaz_ProxyClient::clone(IYaz_PDU_Observable
383 return new Yaz_ProxyClient(the_PDU_Observable);
386 Yaz_ProxyClient::~Yaz_ProxyClient()
392 m_next->m_prev = m_prev;
397 void Yaz_Proxy::timeoutNotify()
399 logf (LOG_LOG, "timeout (client to proxy)");
403 void Yaz_ProxyClient::timeoutNotify()
405 logf (LOG_LOG, "timeout (proxy to target)");
409 Yaz_ProxyClient::Yaz_ProxyClient(IYaz_PDU_Observable *the_PDU_Observable) :
410 Yaz_Z_Assoc (the_PDU_Observable)
417 m_last_resultCount = 0;
421 void Yaz_ProxyClient::recv_Z_PDU(Z_APDU *apdu)
423 logf (LOG_LOG, "Yaz_ProxyClient::recv_Z_PDU");
424 if (apdu->which == Z_APDU_searchResponse)
425 m_last_resultCount = *apdu->u.searchResponse->resultCount;
426 if (apdu->which == Z_APDU_presentResponse && m_sr_transform)
429 Z_PresentResponse *pr = apdu->u.presentResponse;
430 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
431 Z_SearchResponse *sr = new_apdu->u.searchResponse;
432 sr->referenceId = pr->referenceId;
433 *sr->resultCount = m_last_resultCount;
434 sr->records = pr->records;
435 sr->nextResultSetPosition = pr->nextResultSetPosition;
436 sr->numberOfRecordsReturned = pr->numberOfRecordsReturned;
440 set_otherInformationString (apdu, VAL_COOKIE, 1, m_cookie);
443 logf (LOG_LOG, "Yaz_Proxy::send_Z_PDU");
444 m_server->send_Z_PDU(apdu);