- 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");
+ yaz_log (LOG_LOG, "no first INIT!");
+ return 0;
+ }
+ Z_InitRequest *initRequest = apdu->u.initRequest;
+
+ if (!initRequest->idAuthentication)
+ {
+ if (m_proxy_authentication)
+ {
+ initRequest->idAuthentication =
+ (Z_IdAuthentication *)
+ odr_malloc (odr_encode(),
+ sizeof(*initRequest->idAuthentication));
+ initRequest->idAuthentication->which =
+ Z_IdAuthentication_open;
+ initRequest->idAuthentication->u.open =
+ odr_strdup (odr_encode(), m_proxy_authentication);
+ }
+ }
+
+ // 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;
+ yaz_log (LOG_LOG, "Existing sessions");
+ for (c = parent->m_clientPool; c; c = c->m_next)
+ {
+ yaz_log (LOG_LOG, " Session %-3d wait=%d %s", c->m_seqno,
+ c->m_waiting, c->get_hostname());
+ 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()))
+ {
+ yaz_log (LOG_LOG, "Replace session %d",
+ c->m_seqno);
+ if (c->m_server && c->m_server != this)
+ delete c->m_server;
+ c->m_server = 0;
+ }
+ else
+ {
+ yaz_log (LOG_LOG, "Reuse 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_DEBUG, "get_client 2 %p %p", this, c);
+ return c;
+ }