+ // 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;
+ if (parent->m_clientPool)
+ 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, "Move session %d to %d %s",
+ c->m_seqno, parent->m_seqno, c->get_hostname());
+ 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;
+ }
+ }
+ else
+ {
+ yaz_log (LOG_LOG, "Making session %d %s", parent->m_seqno,
+ m_proxyTarget);
+ 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';
+ yaz_log (LOG_LOG, "Connecting to %s", m_proxyTarget);
+ c->m_seqno = parent->m_seqno;