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.6 1999-04-27 07:52:13 adam
8 * Improved proxy; added query match for result set re-use.
10 * Revision 1.5 1999/04/21 12:09:01 adam
11 * Many improvements. Modified to proxy server to work with "sessions"
14 * Revision 1.4 1999/04/20 10:30:05 adam
15 * Implemented various stuff for client and proxy. Updated calls
16 * to ODR to reflect new name parameter.
18 * Revision 1.3 1999/04/09 11:46:57 adam
19 * Added object Yaz_Z_Assoc. Much more functional client.
21 * Revision 1.2 1999/01/28 13:08:46 adam
22 * Yaz_PDU_Assoc better encapsulated. Memory leak fix in
23 * yaz-socket-manager.cc.
25 * Revision 1.1.1.1 1999/01/28 09:41:07 adam
26 * First implementation of YAZ++.
34 #include <yaz-proxy.h>
36 Yaz_Proxy::Yaz_Proxy(IYaz_PDU_Observable *the_PDU_Observable) :
37 Yaz_Z_Assoc(the_PDU_Observable)
39 m_PDU_Observable = the_PDU_Observable;
47 Yaz_Proxy::~Yaz_Proxy()
51 IYaz_PDU_Observer *Yaz_Proxy::clone(IYaz_PDU_Observable
54 Yaz_Proxy *new_proxy = new Yaz_Proxy(the_PDU_Observable);
55 new_proxy->m_parent = this;
56 new_proxy->timeout(120);
60 char *Yaz_Proxy::get_cookie(Z_OtherInformation **otherInfo)
63 Z_OtherInformationUnit *oi;
65 ent.proto = PROTO_Z3950;
66 ent.oclass = CLASS_USERINFO;
67 ent.value = (oid_value) VAL_COOKIE;
68 assert (oid_ent_to_oid (&ent, oid));
70 if (oid_ent_to_oid (&ent, oid) &&
71 (oi = update_otherInformation(otherInfo, 0, oid, 1)) &&
72 oi->which == Z_OtherInfo_characterInfo)
73 return oi->information.characterInfo;
77 char *Yaz_Proxy::get_proxy(Z_OtherInformation **otherInfo)
80 Z_OtherInformationUnit *oi;
82 ent.proto = PROTO_Z3950;
83 ent.oclass = CLASS_USERINFO;
84 ent.value = (oid_value) VAL_PROXY;
85 if (oid_ent_to_oid (&ent, oid) &&
86 (oi = update_otherInformation(otherInfo, 0, oid, 1)) &&
87 oi->which == Z_OtherInfo_characterInfo)
88 return oi->information.characterInfo;
92 Yaz_ProxyClient *Yaz_Proxy::get_client(Z_APDU *apdu)
95 Yaz_Proxy *parent = m_parent;
96 Z_OtherInformation **oi;
97 Yaz_ProxyClient *c = m_client;
99 get_otherInfoAPDU(apdu, &oi);
100 char *cookie = get_cookie(oi);
101 logf (LOG_LOG, "Yaz_Proxy::get_client cookie=%s", cookie ? cookie :
105 for (c = parent->m_clientPool; c; c = c->m_next)
108 assert (*c->m_prev == c);
109 if (!strcmp(cookie,c->m_cookie))
111 logf (LOG_LOG, "Yaz_Proxy::get_client cached");
118 logf (LOG_LOG, "Yaz_Proxy::get_client creating new");
119 c = new Yaz_ProxyClient(m_PDU_Observable->clone());
120 c->m_next = parent->m_clientPool;
122 c->m_next->m_prev = &c->m_next;
123 parent->m_clientPool = c;
124 c->m_prev = &parent->m_clientPool;
126 sprintf (c->m_cookie, "%d", parent->m_seqno);
129 if (apdu->which == Z_APDU_initRequest)
131 logf (LOG_LOG, "got InitRequest");
133 char *proxy_host = get_proxy(&apdu->u.initRequest->otherInfo);
135 c->client(proxy_host);
137 c->client("localhost:9999");
144 Z_APDU *Yaz_Proxy::result_set_optimize(Z_APDU *apdu)
146 if (apdu->which != Z_APDU_searchRequest)
148 Z_SearchRequest *sr = apdu->u.searchRequest;
149 Yaz_Z_Query *this_query = new Yaz_Z_Query;
151 this_query->set_Z_Query(sr->query);
153 if (m_client->m_last_query &&
154 m_client->m_last_query->match(this_query))
157 if (*sr->smallSetUpperBound == 0)
160 logf (LOG_LOG, "query match");
161 new_apdu = create_Z_PDU(Z_APDU_searchResponse);
162 new_apdu->u.searchResponse->referenceId = sr->referenceId;
163 new_apdu->u.searchResponse->resultCount =
164 &m_client->m_last_resultCount;
165 send_Z_PDU(new_apdu);
170 logf (LOG_LOG, "query match (piggy back)");
175 logf (LOG_LOG, "query doesn't match");
176 delete m_client->m_last_query;
177 m_client->m_last_query = this_query;
182 void Yaz_Proxy::recv_Z_PDU(Z_APDU *apdu)
184 logf (LOG_LOG, "Yaz_Proxy::recv_Z_PDU");
185 // Determine our client.
186 m_client = get_client(apdu);
192 m_client->m_server = this;
194 Z_OtherInformation **oi;
195 get_otherInfoAPDU(apdu, &oi);
197 if (apdu->which == Z_APDU_initRequest)
199 if (m_client->m_init_flag)
201 Z_APDU *apdu = create_Z_PDU(Z_APDU_initResponse);
202 if (m_client->m_cookie)
203 set_otherInformationString(apdu, VAL_COOKIE, 1,
208 m_client->m_init_flag = 1;
210 apdu = result_set_optimize(apdu);
214 logf (LOG_LOG, "Yaz_ProxyClient::send_Z_PDU");
215 if (m_client->send_Z_PDU(apdu) < 0)
222 void Yaz_Proxy::failNotify()
224 logf (LOG_LOG, "failNotity server");
227 // Tell client (if any) that no server connection is there..
229 m_client->m_server = 0;
238 void Yaz_ProxyClient::failNotify()
240 logf (LOG_LOG, "failNotity client");
245 IYaz_PDU_Observer *Yaz_ProxyClient::clone(IYaz_PDU_Observable
248 return new Yaz_ProxyClient(the_PDU_Observable);
251 Yaz_ProxyClient::~Yaz_ProxyClient()
257 m_next->m_prev = m_prev;
262 void Yaz_Proxy::timeoutNotify()
267 void Yaz_ProxyClient::timeoutNotify()
272 Yaz_ProxyClient::Yaz_ProxyClient(IYaz_PDU_Observable *the_PDU_Observable) :
273 Yaz_Z_Assoc (the_PDU_Observable)
280 m_last_resultCount = 0;
283 void Yaz_ProxyClient::recv_Z_PDU(Z_APDU *apdu)
285 logf (LOG_LOG, "Yaz_ProxyClient::recv_Z_PDU");
287 set_otherInformationString (apdu, VAL_COOKIE, 1, m_cookie);
288 if (apdu->which == Z_APDU_searchResponse)
289 m_last_resultCount = *apdu->u.searchResponse->resultCount;
292 logf (LOG_LOG, "Yaz_Proxy::send_Z_PDU");
293 m_server->send_Z_PDU(apdu);