+ xfree (m_proxyTarget);
+}
+
+void Yaz_Proxy::set_proxyTarget(const char *target)
+{
+ xfree (m_proxyTarget);
+ m_proxyTarget = 0;
+ if (target)
+ m_proxyTarget = (char *) xstrdup (target);
+}
+
+IYaz_PDU_Observer *Yaz_Proxy::sessionNotify(IYaz_PDU_Observable
+ *the_PDU_Observable, int fd)
+{
+ Yaz_Proxy *new_proxy = new Yaz_Proxy(the_PDU_Observable);
+ new_proxy->m_parent = this;
+ new_proxy->timeout(500);
+ new_proxy->set_proxyTarget(m_proxyTarget);
+ new_proxy->set_APDU_log(get_APDU_log());
+ return new_proxy;
+}
+
+char *Yaz_Proxy::get_cookie(Z_OtherInformation **otherInfo)
+{
+ int oid[OID_SIZE];
+ Z_OtherInformationUnit *oi;
+ struct oident ent;
+ ent.proto = PROTO_Z3950;
+ ent.oclass = CLASS_USERINFO;
+ ent.value = (oid_value) VAL_COOKIE;
+ assert (oid_ent_to_oid (&ent, oid));
+
+ if (oid_ent_to_oid (&ent, oid) &&
+ (oi = update_otherInformation(otherInfo, 0, oid, 1, 1)) &&
+ oi->which == Z_OtherInfo_characterInfo)
+ return oi->information.characterInfo;
+ return 0;
+}
+
+char *Yaz_Proxy::get_proxy(Z_OtherInformation **otherInfo)
+{
+ int oid[OID_SIZE];
+ Z_OtherInformationUnit *oi;
+ struct oident ent;
+ ent.proto = PROTO_Z3950;
+ ent.oclass = CLASS_USERINFO;
+ ent.value = (oid_value) VAL_PROXY;
+ if (oid_ent_to_oid (&ent, oid) &&
+ (oi = update_otherInformation(otherInfo, 0, oid, 1, 1)) &&
+ oi->which == Z_OtherInfo_characterInfo)
+ return oi->information.characterInfo;
+ return 0;
+}
+
+Yaz_ProxyClient *Yaz_Proxy::get_client(Z_APDU *apdu)
+{
+ assert (m_parent);
+ Yaz_Proxy *parent = m_parent;
+ Z_OtherInformation **oi;
+ Yaz_ProxyClient *c = m_client;
+
+ get_otherInfoAPDU(apdu, &oi);
+ char *cookie = get_cookie(oi);
+ logf (LOG_LOG, "Yaz_Proxy::get_client cookie=%s", cookie ? cookie :
+ "null");
+
+ const char *proxy_host = get_proxy(oi);
+ if (proxy_host)
+ set_proxyTarget(proxy_host);
+ logf (LOG_LOG, "proxy_host = %s", m_proxyTarget ? m_proxyTarget:"none");
+
+ // no target specified at all?
+ if (!m_proxyTarget)
+ return 0;
+
+ if (cookie && *cookie)
+ {
+ logf (LOG_LOG, "lookup of clients cookie=%s target=%s",
+ cookie, m_proxyTarget);
+ Yaz_ProxyClient *cc = 0;
+
+ for (c = parent->m_clientPool; c; c = c->m_next)
+ {
+ logf (LOG_LOG, " found client cookie = %s target=%s seqno=%d",
+ c->m_cookie, c->get_hostname(), c->m_seqno);
+ assert (c->m_prev);
+ assert (*c->m_prev == c);
+ if (!strcmp(cookie,c->m_cookie) &&
+ !strcmp(m_proxyTarget, c->get_hostname()))
+ {
+ logf (LOG_LOG, "found!");
+ cc = c;
+ }
+ }
+ if (cc)
+ {
+ // found it in cache
+ c = cc;
+ // The following handles "cancel"
+ // If connection is busy (waiting for PDU) and
+ // we have an initRequest we can safely do re-open
+ if (c->m_waiting && apdu->which == Z_APDU_initRequest)
+ {
+ logf (LOG_LOG, "reopen target=%s", c->get_hostname());
+ c->close();
+ c->client(m_proxyTarget);
+ c->m_init_flag = 0;
+
+ delete c->m_last_query;
+ c->m_last_query = 0;
+ c->m_last_resultCount = 0;
+ c->m_sr_transform = 0;
+ c->m_waiting = 0;
+ c->timeout(600);
+ }
+ c->m_seqno = parent->m_seqno;
+ if (c->m_server && c->m_server != this)
+ c->m_server->m_client = 0;
+ c->m_server = this;
+ c->m_seqno = parent->m_seqno;
+ (parent->m_seqno)++;
+ yaz_log (LOG_LOG, "get_client 1 %p %p", this, c);
+ return c;
+ }
+ }
+ if (!m_client)
+ {
+ if (apdu->which != Z_APDU_initRequest)
+ {
+ logf (LOG_LOG, "no first INIT!");
+ return 0;
+ }
+ logf (LOG_LOG, "got InitRequest");
+
+ // go through list of clients - and find the lowest/oldest one.
+ Yaz_ProxyClient *c_min = 0;
+ int min_seq = -1;
+ int no_of_clients = 0;
+ for (c = parent->m_clientPool; c; c = c->m_next)
+ {
+ no_of_clients++;
+ if (min_seq < 0 || c->m_seqno < min_seq)
+ {
+ min_seq = c->m_seqno;
+ c_min = c;
+ }
+ }
+ if (no_of_clients >= parent->m_max_clients)
+ {
+ c = c_min;
+ if (c->m_waiting || strcmp(m_proxyTarget, c->get_hostname()))
+ {
+ logf (LOG_LOG, "Yaz_Proxy::get_client re-init session %d",
+ c->m_seqno);
+ if (c->m_server && c->m_server != this)
+ delete c->m_server;
+ c->m_server = 0;
+ }
+ else
+ {
+ logf (LOG_LOG, "Yaz_Proxy::get_client re-use session %d to %d",
+ c->m_seqno, parent->m_seqno);
+ if (cookie)
+ strcpy (c->m_cookie, cookie);
+ else
+ c->m_cookie[0] = '\0';
+ c->m_seqno = parent->m_seqno;
+ if (c->m_server && c->m_server != this)
+ {
+ c->m_server->m_client = 0;
+ delete c->m_server;
+ }
+ (parent->m_seqno)++;
+ yaz_log (LOG_LOG, "get_client 2 %p %p", this, c);
+ return c;
+ }
+ }
+ else
+ {
+ logf (LOG_LOG, "Yaz_Proxy::get_client making session %d",
+ parent->m_seqno);
+ c = new Yaz_ProxyClient(m_PDU_Observable->clone());
+ c->m_next = parent->m_clientPool;
+ if (c->m_next)
+ c->m_next->m_prev = &c->m_next;
+ parent->m_clientPool = c;
+ c->m_prev = &parent->m_clientPool;
+ }
+ if (cookie)
+ strcpy (c->m_cookie, cookie);
+ else
+ c->m_cookie[0] = '\0';
+ logf (LOG_LOG, "Yaz_Proxy::get_client connect to %s", m_proxyTarget);
+ c->m_seqno = parent->m_seqno;
+ c->client(m_proxyTarget);
+ c->m_init_flag = 0;
+
+ delete c->m_last_query;
+ c->m_last_query = 0;
+ c->m_last_resultCount = 0;
+ c->m_sr_transform = 0;
+ c->m_waiting = 0;
+ c->timeout(10);
+
+ (parent->m_seqno)++;
+ }
+ yaz_log (LOG_LOG, "get_client 3 %p %p", this, c);
+ return c;