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.5 1999-04-21 12:09:01 adam
8 * Many improvements. Modified to proxy server to work with "sessions"
11 * Revision 1.4 1999/04/20 10:30:05 adam
12 * Implemented various stuff for client and proxy. Updated calls
13 * to ODR to reflect new name parameter.
15 * Revision 1.3 1999/04/09 11:46:57 adam
16 * Added object Yaz_Z_Assoc. Much more functional client.
18 * Revision 1.2 1999/01/28 13:08:46 adam
19 * Yaz_PDU_Assoc better encapsulated. Memory leak fix in
20 * yaz-socket-manager.cc.
22 * Revision 1.1.1.1 1999/01/28 09:41:07 adam
23 * First implementation of YAZ++.
31 #include <yaz-proxy.h>
33 Yaz_Proxy::Yaz_Proxy(IYaz_PDU_Observable *the_PDU_Observable) :
34 Yaz_Z_Assoc(the_PDU_Observable)
36 m_PDU_Observable = the_PDU_Observable;
44 Yaz_Proxy::~Yaz_Proxy()
48 IYaz_PDU_Observer *Yaz_Proxy::clone(IYaz_PDU_Observable
51 Yaz_Proxy *new_proxy = new Yaz_Proxy(the_PDU_Observable);
52 new_proxy->m_parent = this;
53 new_proxy->timeout(120);
57 char *Yaz_Proxy::get_cookie(Z_OtherInformation **otherInfo)
60 Z_OtherInformationUnit *oi;
62 ent.proto = PROTO_Z3950;
63 ent.oclass = CLASS_USERINFO;
64 ent.value = (oid_value) VAL_COOKIE;
65 assert (oid_ent_to_oid (&ent, oid));
67 if (oid_ent_to_oid (&ent, oid) &&
68 (oi = update_otherInformation(otherInfo, 0, oid, 1)) &&
69 oi->which == Z_OtherInfo_characterInfo)
70 return oi->information.characterInfo;
74 char *Yaz_Proxy::get_proxy(Z_OtherInformation **otherInfo)
77 Z_OtherInformationUnit *oi;
79 ent.proto = PROTO_Z3950;
80 ent.oclass = CLASS_USERINFO;
81 ent.value = (oid_value) VAL_PROXY;
82 if (oid_ent_to_oid (&ent, oid) &&
83 (oi = update_otherInformation(otherInfo, 0, oid, 1)) &&
84 oi->which == Z_OtherInfo_characterInfo)
85 return oi->information.characterInfo;
89 Yaz_ProxyClient *Yaz_Proxy::get_client(Z_APDU *apdu)
92 Yaz_Proxy *parent = m_parent;
93 Z_OtherInformation **oi;
94 Yaz_ProxyClient *c = m_client;
96 get_otherInfoAPDU(apdu, &oi);
97 char *cookie = get_cookie(oi);
98 logf (LOG_LOG, "Yaz_Proxy::get_client cookie=%s", cookie ? cookie :
102 for (c = parent->m_clientPool; c; c = c->m_next)
105 assert (*c->m_prev == c);
106 if (!strcmp(cookie,c->m_cookie))
108 logf (LOG_LOG, "Yaz_Proxy::get_client cached");
115 logf (LOG_LOG, "Yaz_Proxy::get_client creating new");
116 c = new Yaz_ProxyClient(m_PDU_Observable->clone());
117 c->m_next = parent->m_clientPool;
119 c->m_next->m_prev = &c->m_next;
120 parent->m_clientPool = c;
121 c->m_prev = &parent->m_clientPool;
123 sprintf (c->m_cookie, "%d", parent->m_seqno);
126 if (apdu->which == Z_APDU_initRequest)
128 logf (LOG_LOG, "got InitRequest");
130 char *proxy_host = get_proxy(&apdu->u.initRequest->otherInfo);
132 c->client(proxy_host);
134 c->client("localhost:9999");
141 void Yaz_Proxy::recv_Z_PDU(Z_APDU *apdu)
143 logf (LOG_LOG, "Yaz_Proxy::recv_Z_PDU");
144 // Determine our client.
145 m_client = get_client(apdu);
151 m_client->m_server = this;
153 Z_OtherInformation **oi;
154 get_otherInfoAPDU(apdu, &oi);
156 logf (LOG_LOG, "Yaz_ProxyClient::send_Z_PDU");
157 if (m_client->send_Z_PDU(apdu) < 0)
164 void Yaz_Proxy::failNotify()
166 logf (LOG_LOG, "failNotity server");
169 // Tell client (if any) that not server connection is there..
171 m_client->m_server = 0;
180 void Yaz_ProxyClient::failNotify()
182 logf (LOG_LOG, "failNotity client");
187 IYaz_PDU_Observer *Yaz_ProxyClient::clone(IYaz_PDU_Observable
190 return new Yaz_ProxyClient(the_PDU_Observable);
193 Yaz_ProxyClient::~Yaz_ProxyClient()
199 m_next->m_prev = m_prev;
203 void Yaz_Proxy::timeoutNotify()
208 void Yaz_ProxyClient::timeoutNotify()
213 Yaz_ProxyClient::Yaz_ProxyClient(IYaz_PDU_Observable *the_PDU_Observable) :
214 Yaz_Z_Assoc (the_PDU_Observable)
221 void Yaz_ProxyClient::recv_Z_PDU(Z_APDU *apdu)
223 logf (LOG_LOG, "Yaz_ProxyClient::recv_Z_PDU");
225 set_otherInformationString (apdu, VAL_COOKIE, 1, m_cookie);
228 logf (LOG_LOG, "Yaz_Proxy::send_Z_PDU");
229 m_server->send_Z_PDU(apdu);