+ Yaz_Proxy *new_proxy = new Yaz_Proxy(the_PDU_Observable);
+ new_proxy->m_parent = this;
+ new_proxy->timeout(120);
+ new_proxy->set_proxyTarget(m_proxyTarget);
+ 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");
+ if (cookie)
+ {
+ for (c = parent->m_clientPool; c; c = c->m_next)
+ {
+ assert (c->m_prev);
+ assert (*c->m_prev == c);
+ if (!strcmp(cookie,c->m_cookie))
+ {
+ logf (LOG_LOG, "Yaz_Proxy::get_client found cached target");
+ c->m_seqno = parent->m_seqno;
+ return c;
+ }
+ }
+ }
+ if (!m_client)
+ {
+ if (apdu->which == Z_APDU_initRequest)
+ {
+ logf (LOG_LOG, "got InitRequest");
+
+ const char *proxy_host =
+ get_proxy(&apdu->u.initRequest->otherInfo);
+ if (proxy_host)
+ set_proxyTarget(proxy_host);
+ logf (LOG_LOG, "proxy_host = %s", m_proxyTarget ?
+ m_proxyTarget:"none");
+ }
+ else
+ {
+ logf (LOG_LOG, "no first INIT!");
+ return 0;
+ }
+ if (!m_proxyTarget)
+ return 0;
+
+ 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++;
+ logf (LOG_LOG, "found seqno = %d", c->m_seqno);
+ 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;
+ logf (LOG_LOG, "Yaz_Proxy::get_client re-using session %d",
+ c->m_seqno);
+ if (c->m_server)
+ delete c->m_server;
+ c->m_server = 0;
+ }
+ else
+ {
+ logf (LOG_LOG, "Yaz_Proxy::get_client making new session");
+ 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;
+ }
+ sprintf (c->m_cookie, "%d", parent->m_seqno);
+ logf (LOG_LOG, "Yaz_Proxy::get_client new session %s", c->m_cookie);
+ 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->timeout(600);
+
+ (parent->m_seqno)++;
+ }
+ return c;
+}
+
+Z_APDU *Yaz_Proxy::result_set_optimize(Z_APDU *apdu)
+{
+ if (apdu->which != Z_APDU_searchRequest)
+ return apdu;
+ Z_SearchRequest *sr = apdu->u.searchRequest;
+ Yaz_Z_Query *this_query = new Yaz_Z_Query;
+
+ this_query->set_Z_Query(sr->query);
+
+ if (m_client->m_last_query &&
+ m_client->m_last_query->match(this_query))
+ {
+ delete this_query;
+ if (m_client->m_last_resultCount > *sr->smallSetUpperBound &&
+ m_client->m_last_resultCount < *sr->largeSetLowerBound)
+ {
+ // medium Set
+ Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentRequest);
+ Z_PresentRequest *pr = new_apdu->u.presentRequest;
+ pr->referenceId = sr->referenceId;
+ pr->resultSetId = sr->resultSetName;
+ pr->preferredRecordSyntax = sr->preferredRecordSyntax;
+ *pr->numberOfRecordsRequested = *sr->mediumSetPresentNumber;
+ if (sr->mediumSetElementSetNames)
+ {
+ pr->recordComposition = (Z_RecordComposition *)
+ odr_malloc(odr_encode(), sizeof(Z_RecordComposition));
+ pr->recordComposition->which = Z_RecordComp_simple;
+ pr->recordComposition->u.simple = sr->mediumSetElementSetNames;
+ }
+ m_client->m_sr_transform = 1;
+ return new_apdu;
+ }
+ else if (m_client->m_last_resultCount >= *sr->largeSetLowerBound ||
+ m_client->m_last_resultCount == 0)
+ {
+ // large set
+ Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
+ new_apdu->u.searchResponse->referenceId = sr->referenceId;
+ new_apdu->u.searchResponse->resultCount =
+ &m_client->m_last_resultCount;
+ send_Z_PDU(new_apdu);
+ return 0;
+ }
+ else
+ {
+ // small set
+ Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentRequest);
+ Z_PresentRequest *pr = new_apdu->u.presentRequest;
+ pr->referenceId = sr->referenceId;
+ pr->resultSetId = sr->resultSetName;
+ pr->preferredRecordSyntax = sr->preferredRecordSyntax;
+ *pr->numberOfRecordsRequested = m_client->m_last_resultCount;
+ if (sr->smallSetElementSetNames)
+ {
+ pr->recordComposition = (Z_RecordComposition *)
+ odr_malloc(odr_encode(), sizeof(Z_RecordComposition));
+ pr->recordComposition->which = Z_RecordComp_simple;
+ pr->recordComposition->u.simple = sr->smallSetElementSetNames;
+ }
+ m_client->m_sr_transform = 1;
+ return new_apdu;
+ }
+ }
+ else
+ {
+ logf (LOG_LOG, "query doesn't match");
+ delete m_client->m_last_query;
+ m_client->m_last_query = this_query;
+ }
+ return apdu;