-/* $Id: filter_multi.cpp,v 1.2 2006-01-16 01:10:19 adam Exp $
+/* $Id: filter_multi.cpp,v 1.8 2006-01-18 10:57:27 adam Exp $
Copyright (c) 2005, Index Data.
%LICENSE%
struct Multi::Frontend {
Frontend(Rep *rep);
~Frontend();
- yp2::Session m_session;
bool m_is_multi;
bool m_in_use;
std::list<BackendPtr> m_backend_list;
void close(Package &package);
void search(Package &package, Z_APDU *apdu);
void present(Package &package, Z_APDU *apdu);
+ void scan(Package &package, Z_APDU *apdu);
Rep *m_p;
};
struct Multi::Map {
private:
boost::mutex m_sessions_mutex;
std::map<std::string, Multi::Map>m_maps;
-
+ std::map<std::string,std::string> m_target_route;
boost::mutex m_mutex;
boost::condition m_cond_session_ready;
std::map<yp2::Session, FrontendPtr> m_clients;
{
Z_InitRequest *req = gdu->u.z3950->u.initRequest;
- // empty or non-existang vhost is the same..
- const char *vhost_cstr =
- yaz_oi_get_string_oidval(&req->otherInfo, VAL_PROXY, 1, 0);
- std::string vhost;
- if (vhost_cstr)
- vhost = std::string(vhost_cstr);
+ std::list<std::string> targets;
+
+ yp2::util::get_vhost_otherinfo(&req->otherInfo, false, targets);
- std::map<std::string, Map>::const_iterator it;
- it = m_p->m_maps.find(std::string(vhost));
- if (it == m_p->m_maps.end())
+ if (targets.size() < 1)
{
- // might return diagnostics if no match
package.move();
return;
}
- std::list<std::string>::const_iterator hit = it->second.m_hosts.begin();
- for (; hit != it->second.m_hosts.end(); hit++)
+
+ std::list<std::string>::const_iterator t_it = targets.begin();
+ for (; t_it != targets.end(); t_it++)
{
Session s;
Backend *b = new Backend;
- b->m_vhost = *hit;
- b->m_route = it->second.m_route;
+ b->m_vhost = *t_it;
+
+ b->m_route = m_p->m_target_route[*t_it];
+ // b->m_route unset
b->m_package = PackagePtr(new Package(s, package.origin()));
m_backend_list.push_back(BackendPtr(b));
}
- // we're going to deal with this for sure..
-
m_is_multi = true;
// create init request
BackendPtr b = *bit;
Z_APDU *init_apdu = zget_APDU(odr, Z_APDU_initRequest);
- yaz_oi_set_string_oidval(&init_apdu->u.initRequest->otherInfo, odr,
- VAL_PROXY, 1, b->m_vhost.c_str());
-
+ std::list<std::string>vhost_one;
+ vhost_one.push_back(b->m_vhost);
+ yp2::util::set_vhost_otherinfo(&init_apdu->u.initRequest->otherInfo,
+ odr, vhost_one);
+
Z_InitRequest *req = init_apdu->u.initRequest;
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);
// create the frontend init response based on each backend init response
yp2::odr odr;
- int i;
-
Z_APDU *f_apdu = odr.create_initResponse(gdu->u.z3950, 0, 0);
Z_InitResponse *f_resp = f_apdu->u.initResponse;
if (gdu && gdu->which == Z_GDU_Z3950 && gdu->u.z3950->which ==
Z_APDU_initResponse)
{
+ int i;
Z_APDU *b_apdu = gdu->u.z3950;
Z_InitResponse *b_resp = b_apdu->u.initResponse;
// create search request
Z_SearchRequest *req = apdu_req->u.searchRequest;
- // deal with piggy back (for now disable)
+ // save these for later
+ int smallSetUpperBound = *req->smallSetUpperBound;
+ int largeSetLowerBound = *req->largeSetLowerBound;
+ int mediumSetPresentNumber = *req->mediumSetPresentNumber;
+
+ // they are altered now - to disable piggyback
*req->smallSetUpperBound = 0;
*req->largeSetLowerBound = 1;
*req->mediumSetPresentNumber = 1;
+ int default_num_db = req->num_databaseNames;
+ char **default_db = req->databaseNames;
+
std::list<BackendPtr>::const_iterator bit;
for (bit = m_backend_list.begin(); bit != m_backend_list.end(); bit++)
{
PackagePtr p = (*bit)->m_package;
- // we don't modify database name yet!
-
+ yp2::odr odr;
+
+ if (!yp2::util::set_databases_from_zurl(odr, (*bit)->m_vhost,
+ &req->num_databaseNames,
+ &req->databaseNames))
+ {
+ req->num_databaseNames = default_num_db;
+ req->databaseNames = default_db;
+ }
p->request() = apdu_req;
p->copy_filter(package);
}
// look at each response
FrontendSet resultSet(std::string(req->resultSetName));
- int total_count = 0;
+ int result_set_size = 0;
Z_Records *z_records_diag = 0; // no diagnostics (yet)
for (bit = m_backend_list.begin(); bit != m_backend_list.end(); bit++)
{
BackendSet backendSet;
backendSet.m_backend = *bit;
backendSet.m_count = *b_resp->resultCount;
- total_count += *b_resp->resultCount;
+ result_set_size += *b_resp->resultCount;
resultSet.m_backend_sets.push_back(backendSet);
}
else
Z_APDU *f_apdu = odr.create_searchResponse(apdu_req, 0, 0);
Z_SearchResponse *f_resp = f_apdu->u.searchResponse;
+ *f_resp->resultCount = result_set_size;
if (z_records_diag)
{
// search error
f_resp->records = z_records_diag;
+ package.response() = f_apdu;
+ return;
}
- else
- { // assume OK
- m_sets[resultSet.m_setname] = resultSet;
+ // assume OK
+ m_sets[resultSet.m_setname] = resultSet;
+
+ int number;
+ yp2::util::piggyback(smallSetUpperBound,
+ largeSetLowerBound,
+ mediumSetPresentNumber,
+ result_set_size,
+ number);
+ Package pp(package.session(), package.origin());
+ if (number > 0)
+ {
+ pp.copy_filter(package);
+ Z_APDU *p_apdu = zget_APDU(odr, Z_APDU_presentRequest);
+ Z_PresentRequest *p_req = p_apdu->u.presentRequest;
+ p_req->preferredRecordSyntax = req->preferredRecordSyntax;
+ p_req->resultSetId = req->resultSetName;
+ *p_req->resultSetStartPoint = 1;
+ *p_req->numberOfRecordsRequested = number;
+ pp.request() = p_apdu;
+ present(pp, p_apdu);
+
+ if (pp.session().is_closed())
+ package.session().close();
+
+ Z_GDU *gdu = pp.response().get();
+ if (gdu && gdu->which == Z_GDU_Z3950 && gdu->u.z3950->which ==
+ Z_APDU_presentResponse)
+ {
+ Z_PresentResponse *p_res = gdu->u.z3950->u.presentResponse;
+ f_resp->records = p_res->records;
+ *f_resp->numberOfRecordsReturned =
+ *p_res->numberOfRecordsReturned;
+ *f_resp->nextResultSetPosition =
+ *p_res->nextResultSetPosition;
+ }
+ else
+ {
+ package.response() = pp.response();
+ return;
+ }
}
- *f_resp->resultCount = total_count;
-
- package.response() = f_apdu;
+ package.response() = f_apdu; // in this scope because of p
}
void yf::Multi::Frontend::present(Package &package, Z_APDU *apdu_req)
package.response() = f_apdu;
}
+void yf::Multi::Frontend::scan(Package &package, Z_APDU *apdu_req)
+{
+ if (m_backend_list.size() > 1)
+ {
+ yp2::odr odr;
+ Z_APDU *f_apdu =
+ odr.create_scanResponse(
+ apdu_req, YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP, 0);
+ package.response() = f_apdu;
+ return;
+ }
+ Z_ScanRequest *req = apdu_req->u.scanRequest;
+
+ int default_num_db = req->num_databaseNames;
+ char **default_db = req->databaseNames;
+
+ std::list<BackendPtr>::const_iterator bit;
+ for (bit = m_backend_list.begin(); bit != m_backend_list.end(); bit++)
+ {
+ PackagePtr p = (*bit)->m_package;
+ yp2::odr odr;
+
+ if (!yp2::util::set_databases_from_zurl(odr, (*bit)->m_vhost,
+ &req->num_databaseNames,
+ &req->databaseNames))
+ {
+ req->num_databaseNames = default_num_db;
+ req->databaseNames = default_db;
+ }
+ p->request() = apdu_req;
+ p->copy_filter(package);
+ }
+ multi_move(m_backend_list);
+
+ for (bit = m_backend_list.begin(); bit != m_backend_list.end(); bit++)
+ {
+ PackagePtr p = (*bit)->m_package;
+
+ if (p->session().is_closed()) // if any backend closes, close frontend
+ package.session().close();
+
+ Z_GDU *gdu = p->response().get();
+ if (gdu && gdu->which == Z_GDU_Z3950 && gdu->u.z3950->which ==
+ Z_APDU_scanResponse)
+ {
+ package.response() = p->response();
+ break;
+ }
+ else
+ {
+ // if any target does not return scan response - return that
+ package.response() = p->response();
+ return;
+ }
+ }
+}
+
void yf::Multi::process(Package &package) const
{
FrontendPtr f = m_p->get_frontend(package);
{
f->present(package, apdu);
}
+ else if (apdu->which == Z_APDU_scanRequest)
+ {
+ f->scan(package, apdu);
+ }
else
{
yp2::odr odr;
{
if (ptr->type != XML_ELEMENT_NODE)
continue;
- if (!strcmp((const char *) ptr->name, "virtual"))
+ if (!strcmp((const char *) ptr->name, "target"))
+ {
+ std::string route = yp2::xml::get_route(ptr);
+ std::string target = yp2::xml::get_text(ptr);
+ std::cout << "route=" << route << " target=" << target << "\n";
+ m_p->m_target_route[target] = route;
+ }
+ else if (!strcmp((const char *) ptr->name, "virtual"))
{
std::list<std::string> targets;
std::string vhost;