- yp2::odr odr;
- Z_APDU *apdu = zget_APDU(odr, Z_APDU_initResponse);
-
- apdu->u.initResponse->userInformationField =
- zget_init_diagnostics(odr,
- YAZ_BIB1_INIT_NEGOTIATION_OPTION_REQUIRED,
- "Virtual host not given");
- package.response() = apdu;
-
- package.session().close();
- return 0;
+ target = m_default_target;
+ std::list<std::string> vhosts;
+ mp::util::remove_vhost_otherinfo(&apdu->u.initRequest->otherInfo,
+ vhosts);
+ size_t no_vhosts = vhosts.size();
+ if (no_vhosts == 1)
+ {
+ std::list<std::string>::const_iterator v_it = vhosts.begin();
+ target = *v_it;
+ }
+ else if (no_vhosts == 0)
+ {
+ if (!target.length())
+ {
+ // no default target. So we don't know where to connect
+ mp::odr odr;
+ package.response() = odr.create_initResponse(
+ apdu,
+ YAZ_BIB1_INIT_NEGOTIATION_OPTION_REQUIRED,
+ "z3950_client: No vhost given");
+
+ package.session().close();
+ return 0;
+ }
+ }
+ else if (no_vhosts > 1)
+ {
+ mp::odr odr;
+ package.response() = odr.create_initResponse(
+ apdu,
+ YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP,
+ "z3950_client: Can not cope with multiple vhosts");
+ package.session().close();
+ return 0;
+ }
+ }
+
+ // see if we have reached max number of clients (max-sockets)
+
+ while (max_sockets)
+ {
+ int no_not_in_use = 0;
+ int number = 0;
+ it = m_clients.begin();
+ for (; it != m_clients.end(); it++)
+ {
+ yf::Z3950Client::Assoc *as = it->second;
+ if (!strcmp(as->m_host.c_str(), target.c_str()))
+ {
+ number++;
+ if (!as->m_in_use)
+ no_not_in_use++;
+ }
+ }
+ yaz_log(YLOG_LOG, "Found %d/%d connections for %s", number, max_sockets,
+ target.c_str());
+ if (number < max_sockets)
+ break;
+ if (no_not_in_use == 0) // all in use..
+ {
+ mp::odr odr;
+
+ package.response() = odr.create_initResponse(
+ apdu, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
+ "z3950_client: max sessions");
+ package.session().close();
+ return 0;
+ }
+ boost::xtime xt;
+ xtime_get(&xt,
+#if BOOST_VERSION >= 105000
+ boost::TIME_UTC_
+#else
+ boost::TIME_UTC
+#endif
+ );
+
+ xt.sec += 15;
+ if (!m_cond_session_ready.timed_wait(lock, xt))
+ {
+ mp::odr odr;
+
+ package.response() = odr.create_initResponse(
+ apdu, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
+ "z3950_client: max sessions");
+ package.session().close();
+ return 0;
+ }