+ BackendPtr b = create_backend_from_databases(databases, error_code,
+ addinfo);
+ if (!b)
+ return b;
+ Package init_package(b->m_backend_session, package.origin());
+ init_package.copy_filter(package);
+
+ mp::odr odr;
+
+ Z_APDU *init_apdu = zget_APDU(odr, Z_APDU_initRequest);
+
+ mp::util::set_vhost_otherinfo(&init_apdu->u.initRequest->otherInfo, odr,
+ b->m_targets);
+ Z_InitRequest *req = init_apdu->u.initRequest;
+
+ // copy stuff from Frontend Init Request
+ Z_GDU *org_gdu = m_init_gdu.get();
+ Z_InitRequest *org_init = org_gdu->u.z3950->u.initRequest;
+
+ req->idAuthentication = org_init->idAuthentication;
+ req->implementationId = org_init->implementationId;
+ req->implementationName = org_init->implementationName;
+ req->implementationVersion = org_init->implementationVersion;
+ *req->preferredMessageSize = *org_init->preferredMessageSize;
+ *req->maximumRecordSize = *org_init->maximumRecordSize;
+
+ ODR_MASK_SET(req->options, Z_Options_search);
+ ODR_MASK_SET(req->options, Z_Options_present);
+ ODR_MASK_SET(req->options, Z_Options_namedResultSets);
+ ODR_MASK_SET(req->options, Z_Options_scan);
+
+ ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_1);
+ ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_2);
+ ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_3);
+
+ init_package.request() = init_apdu;
+
+ init_package.move(b->m_route); // sending init
+
+ Z_GDU *gdu = init_package.response().get();
+ // we hope to get an init response
+ error_code = 0;
+ if (gdu && gdu->which == Z_GDU_Z3950
+ && gdu->u.z3950->which == Z_APDU_initResponse)
+ {
+ Z_InitResponse *res = gdu->u.z3950->u.initResponse;
+ if (ODR_MASK_GET(res->options, Z_Options_namedResultSets))
+ {
+ b->m_named_result_sets = true;
+ }
+ if (*res->result && !init_package.session().is_closed())
+ {
+ m_backend_list.push_back(b);
+ return b;
+ }
+ mp::util::get_init_diagnostics(res, error_code, addinfo);
+ }
+ if (error_code == 0)
+ {
+ std::list<std::string>::const_iterator db_it = databases.begin();
+ error_code = YAZ_BIB1_ACCESS_TO_SPECIFIED_DATABASE_DENIED;
+ if (db_it != databases.end())
+ addinfo = *db_it;
+ }
+ if (!init_package.session().is_closed())
+ {
+ Package close_package(b->m_backend_session, package.origin());
+ close_package.copy_filter(package);
+ close_package.session().close();
+ close_package.move(b->m_route); // closing it
+ }
+ BackendPtr null;
+ return null;
+}
+
+void yf::VirtualDB::Frontend::search(mp::Package &package, Z_APDU *apdu_req)
+{
+ yazpp_1::GDU ngdu(apdu_req);
+ Z_SearchRequest *req = ngdu.get()->u.z3950->u.searchRequest;
+ std::string vhost;
+ std::string resultSetId = req->resultSetName;
+ mp::odr odr;
+
+ std::list<std::string> databases;
+ int i;
+ for (i = 0; i<req->num_databaseNames; i++)
+ databases.push_back(req->databaseNames[i]);
+
+ Sets_it sets_it = m_sets.find(req->resultSetName);
+ if (sets_it != m_sets.end())
+ {
+ // result set already exist
+ // if replace indicator is off: we return diagnostic if
+ // result set already exist.
+ if (*req->replaceIndicator == 0)
+ {
+ Z_APDU *apdu =
+ odr.create_searchResponse(
+ apdu_req,
+ YAZ_BIB1_RESULT_SET_EXISTS_AND_REPLACE_INDICATOR_OFF,
+ 0);
+ package.response() = apdu;
+
+ return;
+ }
+ sets_it->second.m_backend->m_number_of_sets--;
+ }
+ // pick up any existing database with named result sets ..
+ // or one which has no result sets.. yet.
+ BackendPtr b; // null for now
+ std::list<BackendPtr>::const_iterator map_it;
+ map_it = m_backend_list.begin();
+ for (; map_it != m_backend_list.end(); map_it++)
+ {
+ BackendPtr tmp = *map_it;
+ if (tmp->m_frontend_databases == databases &&
+ (tmp->m_named_result_sets ||
+ tmp->m_number_of_sets == 0))
+ {
+ b = *map_it;
+ break;
+ }
+ }
+ if (!b) // no backend yet. Must create a new one
+ {
+ int error_code;
+ std::string addinfo;
+ b = init_backend(databases, package, error_code, addinfo);
+ if (!b)
+ {
+ // did not get a backend (unavailable somehow?)
+
+ Z_APDU *apdu =
+ odr.create_searchResponse(
+ apdu_req, error_code, addinfo.c_str());
+ package.response() = apdu;
+ return;
+ }
+ }
+ m_sets.erase(req->resultSetName);
+ // sending search to backend
+ Package search_package(b->m_backend_session, package.origin());
+
+ search_package.copy_filter(package);
+
+ std::string backend_setname;
+ if (b->m_named_result_sets)
+ {
+ backend_setname = std::string(req->resultSetName);
+ }
+ else
+ {
+ backend_setname = "default";
+ req->resultSetName = odr_strdup(odr, backend_setname.c_str());
+ }
+
+ // pick first targets spec and move the databases from it ..
+ std::list<std::string>::const_iterator t_it = b->m_targets.begin();
+ if (t_it != b->m_targets.end())
+ {
+ mp::util::set_databases_from_zurl(odr, *t_it,
+ &req->num_databaseNames,
+ &req->databaseNames);
+ }
+
+ *req->replaceIndicator = 1;
+
+ search_package.request() = ngdu;
+
+ search_package.move(b->m_route);
+
+ if (search_package.session().is_closed())
+ {
+ package.response() = search_package.response();
+ package.session().close();
+ return;
+ }
+ b->m_number_of_sets++;
+
+ m_sets[resultSetId] = VirtualDB::Set(b, backend_setname);
+ fixup_package(search_package, b);
+ package.response() = search_package.response();
+}
+
+yf::VirtualDB::Frontend::Frontend(Rep *rep)
+{
+ m_p = rep;