-/* $Id: filter_multi.cpp,v 1.2 2006-01-16 01:10:19 adam Exp $
+/* $Id: filter_multi.cpp,v 1.3 2006-01-16 15:51:56 adam Exp $
Copyright (c) 2005, Index Data.
%LICENSE%
{
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;
- std::map<std::string, Map>::const_iterator it;
- it = m_p->m_maps.find(std::string(vhost));
- if (it == m_p->m_maps.end())
+ int no_targets = 0;
+ while (true)
+ {
+ const char *vhost_cstr =
+ yaz_oi_get_string_oidval(&req->otherInfo, VAL_PROXY, no_targets+1,
+ 0);
+ if (!vhost_cstr)
+ break;
+ no_targets++;
+ if (no_targets > 1000)
+ return;
+ targets.push_back(vhost_cstr);
+ }
+ if (no_targets < 2)
{
- // 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 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
// 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)
*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);
}
-/* $Id: filter_virt_db.cpp,v 1.25 2006-01-16 01:10:19 adam Exp $
+/* $Id: filter_virt_db.cpp,v 1.26 2006-01-16 15:51:56 adam Exp $
Copyright (c) 2005, Index Data.
%LICENSE%
std::string m_setname;
};
struct Virt_db::Map {
- Map(std::string vhost, std::string route);
+ Map(std::list<std::string> targets, std::string route);
Map();
- std::string m_vhost;
+ std::list<std::string> m_targets;
std::string m_route;
};
struct Virt_db::Backend {
yp2::Session m_backend_session;
+#if 0
std::string m_backend_database;
- std::string m_frontend_database;
- std::string m_vhost;
+#endif
+ std::list<std::string> m_frontend_databases;
+ std::list<std::string> m_targets;
std::string m_route;
bool m_named_result_sets;
int m_number_of_sets;
void close(Package &package);
typedef std::map<std::string,Virt_db::Set>::iterator Sets_it;
- BackendPtr lookup_backend_from_database(std::string database);
- BackendPtr create_backend_from_database(std::string database);
+ BackendPtr lookup_backend_from_databases(
+ std::list<std::string> databases);
+ BackendPtr create_backend_from_databases(
+ std::list<std::string> databases);
- BackendPtr init_backend(std::string database, Package &package,
+ BackendPtr init_backend(std::list<std::string> database,
+ Package &package,
int &error_code, std::string &addinfo);
Rep *m_p;
};
using namespace yp2;
-yf::Virt_db::BackendPtr yf::Virt_db::Frontend::lookup_backend_from_database(
- std::string database)
+yf::Virt_db::BackendPtr yf::Virt_db::Frontend::lookup_backend_from_databases(
+ std::list<std::string> databases)
{
std::list<BackendPtr>::const_iterator map_it;
map_it = m_backend_list.begin();
for (; map_it != m_backend_list.end(); map_it++)
- if ((*map_it)->m_frontend_database == database)
+ if ((*map_it)->m_frontend_databases == databases)
return *map_it;
BackendPtr null;
return null;
}
-yf::Virt_db::BackendPtr yf::Virt_db::Frontend::create_backend_from_database(
- std::string database)
+yf::Virt_db::BackendPtr yf::Virt_db::Frontend::create_backend_from_databases(
+ std::list<std::string> databases)
{
- std::map<std::string, Virt_db::Map>::iterator map_it;
- map_it = m_p->m_maps.find(database);
- if (map_it == m_p->m_maps.end())
- {
- BackendPtr ptr;
- return ptr;
- }
BackendPtr b(new Backend);
+ std::list<std::string>::const_iterator db_it = databases.begin();
b->m_number_of_sets = 0;
- b->m_frontend_database = database;
+ b->m_frontend_databases = databases;
b->m_named_result_sets = false;
- b->m_route = map_it->second.m_route;
- b->m_vhost = map_it->second.m_vhost;
+
+ std::map<std::string,bool> targets_dedup;
+ for (; db_it != databases.end(); db_it++)
+ {
+ std::map<std::string, Virt_db::Map>::iterator map_it;
+ map_it = m_p->m_maps.find(*db_it);
+ if (map_it == m_p->m_maps.end()) // database not found
+ {
+ BackendPtr ptr;
+ return ptr;
+ }
+ std::list<std::string>::const_iterator t_it =
+ map_it->second.m_targets.begin();
+ for (; t_it != map_it->second.m_targets.end(); t_it++)
+ targets_dedup[*t_it] = true;
+ b->m_route = map_it->second.m_route;
+ }
+ std::map<std::string,bool>::const_iterator tm_it = targets_dedup.begin();
+ for (; tm_it != targets_dedup.end(); tm_it++)
+ b->m_targets.push_back(tm_it->first);
+#if 0
const char *sep = strchr(b->m_vhost.c_str(), '/');
std::string backend_database;
if (sep)
b->m_backend_database = std::string(sep+1);
else
b->m_backend_database = database;
-
+#endif
return b;
}
yf::Virt_db::BackendPtr yf::Virt_db::Frontend::init_backend(
- std::string database, Package &package,
+ std::list<std::string> databases, Package &package,
int &error_code, std::string &addinfo)
{
- BackendPtr b = create_backend_from_database(database);
+ BackendPtr b = create_backend_from_databases(databases);
if (!b)
{
error_code = YAZ_BIB1_DATABASE_UNAVAILABLE;
- addinfo = database;
+ // addinfo = database;
return b;
}
Package init_package(b->m_backend_session, package.origin());
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>::const_iterator t_it = b->m_targets.begin();
+ int cat = 1;
+ for (; t_it != b->m_targets.end(); t_it++, cat++)
+ {
+ yaz_oi_set_string_oidval(&init_apdu->u.initRequest->otherInfo, odr,
+ VAL_PROXY, cat, t_it->c_str());
+ }
Z_InitRequest *req = init_apdu->u.initRequest;
ODR_MASK_SET(req->options, Z_Options_search);
if (init_package.session().is_closed())
{
error_code = YAZ_BIB1_DATABASE_UNAVAILABLE;
- addinfo = database;
+ // addinfo = database;
BackendPtr null;
return null;
}
else
{
error_code = YAZ_BIB1_DATABASE_UNAVAILABLE;
- addinfo = database;
+ // addinfo = database;
BackendPtr null;
return null;
}
std::string resultSetId = req->resultSetName;
yp2::odr odr;
- // only one datatabase for now
- if (req->num_databaseNames != 1)
- { // exactly one database must be specified
- Z_APDU *apdu =
- odr.create_searchResponse(
- apdu_req, YAZ_BIB1_TOO_MANY_DATABASES_SPECIFIED, 0);
- package.response() = apdu;
-
- return;
- }
- std::string database = std::string(req->databaseNames[0]);
+ std::list<std::string> databases;
+ int i;
+ for (i = 0; i<req->num_databaseNames; i++)
+ databases.push_back(req->databaseNames[i]);
BackendPtr b; // null for now
Sets_it sets_it = m_sets.find(req->resultSetName);
for (; map_it != m_backend_list.end(); map_it++)
{
BackendPtr tmp = *map_it;
- if (tmp->m_frontend_database == database)
+ if (tmp->m_frontend_databases == databases)
break;
}
if (map_it != m_backend_list.end())
for (; map_it != m_backend_list.end(); map_it++)
{
BackendPtr tmp = *map_it;
- if (tmp->m_frontend_database == database &&
+ if (tmp->m_frontend_databases == databases &&
(tmp->m_named_result_sets ||
tmp->m_number_of_sets == 0))
break;
{
int error_code;
std::string addinfo;
- b = init_backend(database, package, error_code, addinfo);
+ b = init_backend(databases, package, error_code, addinfo);
if (!b)
{
// did not get a backend (unavailable somehow?)
req->resultSetName = odr_strdup(odr, backend_setname.c_str());
}
+#if 0
const char *backend_database = b->m_backend_database.c_str();
req->databaseNames[0] = odr_strdup(odr, backend_database);
+#endif
*req->replaceIndicator = 1;
{
Z_APDU *apdu =
odr.create_searchResponse(
- apdu_req,
- YAZ_BIB1_DATABASE_UNAVAILABLE, database.c_str());
+ apdu_req, YAZ_BIB1_DATABASE_UNAVAILABLE, 0);
package.response() = apdu;
return;
}
{
}
-yf::Virt_db::Map::Map(std::string vhost, std::string route)
- : m_vhost(vhost), m_route(route)
+yf::Virt_db::Map::Map(std::list<std::string> targets, std::string route)
+ : m_targets(targets), m_route(route)
{
}
std::string vhost;
yp2::odr odr;
- // only one datatabase for now
- if (req->num_databaseNames != 1)
- { // exactly one database must be specified
- Z_APDU *apdu =
- odr.create_scanResponse(
- apdu_req,
- YAZ_BIB1_TOO_MANY_DATABASES_SPECIFIED, 0);
- package.response() = apdu;
- return;
- }
- std::string database = std::string(req->databaseNames[0]);
-
+ std::list<std::string> databases;
+ int i;
+ for (i = 0; i<req->num_databaseNames; i++)
+ databases.push_back(req->databaseNames[i]);
+
BackendPtr b;
// pick up any existing backend with a database match
std::list<BackendPtr>::const_iterator map_it;
for (; map_it != m_backend_list.end(); map_it++)
{
BackendPtr tmp = *map_it;
- if (tmp->m_frontend_database == database)
+ if (tmp->m_frontend_databases == databases)
break;
}
if (map_it != m_backend_list.end())
{
int error_code;
std::string addinfo;
- b = init_backend(database, package, error_code, addinfo);
+ b = init_backend(databases, package, error_code, addinfo);
if (!b)
{
// did not get a backend (unavailable somehow?)
scan_package.copy_filter(package);
+#if 0
const char *backend_database = b->m_backend_database.c_str();
req->databaseNames[0] = odr_strdup(odr, backend_database);
+#endif
scan_package.request() = yazpp_1::GDU(apdu_req);
{
Z_APDU *apdu =
odr.create_scanResponse(
- apdu_req, YAZ_BIB1_DATABASE_UNAVAILABLE, database.c_str());
+ apdu_req, YAZ_BIB1_DATABASE_UNAVAILABLE, 0);
package.response() = apdu;
return;
}
}
-void yf::Virt_db::add_map_db2vhost(std::string db, std::string vhost,
- std::string route)
+void yf::Virt_db::add_map_db2targets(std::string db,
+ std::list<std::string> targets,
+ std::string route)
{
- m_p->m_maps[db] = Virt_db::Map(vhost, route);
+ m_p->m_maps[db] = Virt_db::Map(targets, route);
+}
+
+
+void yf::Virt_db::add_map_db2target(std::string db,
+ std::string target,
+ std::string route)
+{
+ std::list<std::string> targets;
+ targets.push_back(target);
+
+ m_p->m_maps[db] = Virt_db::Map(targets, route);
}
void yf::Virt_db::process(Package &package) const
if (!strcmp((const char *) ptr->name, "virtual"))
{
std::string database;
- std::string target;
+ std::list<std::string> targets;
xmlNode *v_node = ptr->children;
for (; v_node; v_node = v_node->next)
{
if (yp2::xml::is_element_yp2(v_node, "database"))
database = yp2::xml::get_text(v_node);
else if (yp2::xml::is_element_yp2(v_node, "target"))
- target = yp2::xml::get_text(v_node);
+ targets.push_back(yp2::xml::get_text(v_node));
else
throw yp2::filter::FilterException
("Bad element "
);
}
std::string route = yp2::xml::get_route(ptr);
- add_map_db2vhost(database, target, route);
+ add_map_db2targets(database, targets, route);
+#if 0
std::cout << "Add " << database << "->" << target
<< "," << route << "\n";
+#endif
}
else
{