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.8 1999-05-04 10:53:00 adam
8 * Changed the way the PROXY behaves when lost cookie is received.
10 * Revision 1.7 1999/04/28 13:31:17 adam
11 * Better result set optimisation for proxy.
13 * Revision 1.6 1999/04/27 07:52:13 adam
14 * Improved proxy; added query match for result set re-use.
16 * Revision 1.5 1999/04/21 12:09:01 adam
17 * Many improvements. Modified to proxy server to work with "sessions"
20 * Revision 1.4 1999/04/20 10:30:05 adam
21 * Implemented various stuff for client and proxy. Updated calls
22 * to ODR to reflect new name parameter.
24 * Revision 1.3 1999/04/09 11:46:57 adam
25 * Added object Yaz_Z_Assoc. Much more functional client.
27 * Revision 1.2 1999/01/28 13:08:46 adam
28 * Yaz_PDU_Assoc better encapsulated. Memory leak fix in
29 * yaz-socket-manager.cc.
31 * Revision 1.1.1.1 1999/01/28 09:41:07 adam
32 * First implementation of YAZ++.
40 #include <yaz-proxy.h>
42 Yaz_Proxy::Yaz_Proxy(IYaz_PDU_Observable *the_PDU_Observable) :
43 Yaz_Z_Assoc(the_PDU_Observable)
45 m_PDU_Observable = the_PDU_Observable;
53 Yaz_Proxy::~Yaz_Proxy()
57 IYaz_PDU_Observer *Yaz_Proxy::clone(IYaz_PDU_Observable
60 Yaz_Proxy *new_proxy = new Yaz_Proxy(the_PDU_Observable);
61 new_proxy->m_parent = this;
62 new_proxy->timeout(120);
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)) &&
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)) &&
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);
107 logf (LOG_LOG, "Yaz_Proxy::get_client cookie=%s", cookie ? cookie :
111 for (c = parent->m_clientPool; c; c = c->m_next)
114 assert (*c->m_prev == c);
115 if (!strcmp(cookie,c->m_cookie))
117 logf (LOG_LOG, "Yaz_Proxy::get_client cached");
125 logf (LOG_LOG, "Yaz_Proxy::get_client creating new");
126 c = new Yaz_ProxyClient(m_PDU_Observable->clone());
127 c->m_next = parent->m_clientPool;
129 c->m_next->m_prev = &c->m_next;
130 parent->m_clientPool = c;
131 c->m_prev = &parent->m_clientPool;
133 sprintf (c->m_cookie, "%d", parent->m_seqno);
136 if (apdu->which == Z_APDU_initRequest)
138 logf (LOG_LOG, "got InitRequest");
140 char *proxy_host = get_proxy(&apdu->u.initRequest->otherInfo);
142 c->client(proxy_host);
144 c->client("localhost:9999");
151 Z_APDU *Yaz_Proxy::result_set_optimize(Z_APDU *apdu)
153 if (apdu->which != Z_APDU_searchRequest)
155 Z_SearchRequest *sr = apdu->u.searchRequest;
156 Yaz_Z_Query *this_query = new Yaz_Z_Query;
158 this_query->set_Z_Query(sr->query);
160 if (m_client->m_last_query &&
161 m_client->m_last_query->match(this_query))
164 if (m_client->m_last_resultCount > *sr->smallSetUpperBound &&
165 m_client->m_last_resultCount < *sr->largeSetLowerBound)
168 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentRequest);
169 Z_PresentRequest *pr = new_apdu->u.presentRequest;
170 pr->referenceId = sr->referenceId;
171 pr->resultSetId = sr->resultSetName;
172 pr->preferredRecordSyntax = sr->preferredRecordSyntax;
173 *pr->numberOfRecordsRequested = *sr->mediumSetPresentNumber;
174 if (sr->mediumSetElementSetNames)
176 pr->recordComposition = (Z_RecordComposition *)
177 odr_malloc(odr_encode(), sizeof(Z_RecordComposition));
178 pr->recordComposition->which = Z_RecordComp_simple;
179 pr->recordComposition->u.simple = sr->mediumSetElementSetNames;
181 m_client->m_sr_transform = 1;
184 else if (m_client->m_last_resultCount >= *sr->largeSetLowerBound ||
185 m_client->m_last_resultCount == 0)
188 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
189 new_apdu->u.searchResponse->referenceId = sr->referenceId;
190 new_apdu->u.searchResponse->resultCount =
191 &m_client->m_last_resultCount;
192 send_Z_PDU(new_apdu);
198 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentRequest);
199 Z_PresentRequest *pr = new_apdu->u.presentRequest;
200 pr->referenceId = sr->referenceId;
201 pr->resultSetId = sr->resultSetName;
202 pr->preferredRecordSyntax = sr->preferredRecordSyntax;
203 *pr->numberOfRecordsRequested = m_client->m_last_resultCount;
204 if (sr->smallSetElementSetNames)
206 pr->recordComposition = (Z_RecordComposition *)
207 odr_malloc(odr_encode(), sizeof(Z_RecordComposition));
208 pr->recordComposition->which = Z_RecordComp_simple;
209 pr->recordComposition->u.simple = sr->smallSetElementSetNames;
211 m_client->m_sr_transform = 1;
217 logf (LOG_LOG, "query doesn't match");
218 delete m_client->m_last_query;
219 m_client->m_last_query = this_query;
224 void Yaz_Proxy::recv_Z_PDU(Z_APDU *apdu)
226 logf (LOG_LOG, "Yaz_Proxy::recv_Z_PDU");
227 // Determine our client.
228 m_client = get_client(apdu);
234 m_client->m_server = this;
236 Z_OtherInformation **oi;
237 get_otherInfoAPDU(apdu, &oi);
239 if (apdu->which == Z_APDU_initRequest)
241 if (m_client->m_init_flag)
243 Z_APDU *apdu = create_Z_PDU(Z_APDU_initResponse);
244 if (m_client->m_cookie)
245 set_otherInformationString(apdu, VAL_COOKIE, 1,
250 m_client->m_init_flag = 1;
252 apdu = result_set_optimize(apdu);
256 logf (LOG_LOG, "Yaz_ProxyClient::send_Z_PDU");
257 if (m_client->send_Z_PDU(apdu) < 0)
264 void Yaz_Proxy::failNotify()
266 logf (LOG_LOG, "failNotity server");
269 // Tell client (if any) that no server connection is there..
271 m_client->m_server = 0;
280 void Yaz_ProxyClient::failNotify()
282 logf (LOG_LOG, "failNotity client");
287 IYaz_PDU_Observer *Yaz_ProxyClient::clone(IYaz_PDU_Observable
290 return new Yaz_ProxyClient(the_PDU_Observable);
293 Yaz_ProxyClient::~Yaz_ProxyClient()
299 m_next->m_prev = m_prev;
304 void Yaz_Proxy::timeoutNotify()
309 void Yaz_ProxyClient::timeoutNotify()
314 Yaz_ProxyClient::Yaz_ProxyClient(IYaz_PDU_Observable *the_PDU_Observable) :
315 Yaz_Z_Assoc (the_PDU_Observable)
322 m_last_resultCount = 0;
326 void Yaz_ProxyClient::recv_Z_PDU(Z_APDU *apdu)
328 logf (LOG_LOG, "Yaz_ProxyClient::recv_Z_PDU");
329 if (apdu->which == Z_APDU_searchResponse)
330 m_last_resultCount = *apdu->u.searchResponse->resultCount;
331 if (apdu->which == Z_APDU_presentResponse && m_sr_transform)
334 Z_PresentResponse *pr = apdu->u.presentResponse;
335 Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
336 Z_SearchResponse *sr = new_apdu->u.searchResponse;
337 sr->referenceId = pr->referenceId;
338 *sr->resultCount = m_last_resultCount;
339 sr->records = pr->records;
340 sr->nextResultSetPosition = pr->nextResultSetPosition;
341 sr->numberOfRecordsReturned = pr->numberOfRecordsReturned;
345 set_otherInformationString (apdu, VAL_COOKIE, 1, m_cookie);
348 logf (LOG_LOG, "Yaz_Proxy::send_Z_PDU");
349 m_server->send_Z_PDU(apdu);