-/* $Id: filter_virt_db.cpp,v 1.22 2006-01-12 14:45:04 adam Exp $
+/* $Id: filter_virt_db.cpp,v 1.23 2006-01-13 15:09:35 adam Exp $
Copyright (c) 2005, Index Data.
%LICENSE%
#include <boost/thread/mutex.hpp>
#include <boost/thread/condition.hpp>
+#include <boost/shared_ptr.hpp>
#include "util.hpp"
#include "filter_virt_db.hpp"
namespace yp2 {
namespace filter {
+
struct Virt_db::Set {
- Set(yp2::Session &id, std::string setname,
- std::string vhost, std::string route,
- bool named_result_sets);
+ Set(BackendPtr b, std::string setname);
Set();
~Set();
- yp2::Session m_backend_session;
- std::string m_backend_setname;
- std::string m_vhost;
- std::string m_route;
- bool m_named_result_sets;
+ BackendPtr m_backend;
+ std::string m_setname;
};
struct Virt_db::Map {
Map(std::string vhost, std::string route);
std::string m_vhost;
std::string m_route;
};
+ struct Virt_db::Backend {
+ yp2::Session m_backend_session;
+ std::string m_backend_database;
+ std::string m_frontend_database;
+ std::string m_vhost;
+ std::string m_route;
+ bool m_named_result_sets;
+ int m_number_of_sets;
+ };
struct Virt_db::Frontend {
- Frontend();
+ Frontend(Rep *rep);
~Frontend();
yp2::Session m_session;
bool m_is_virtual;
bool m_in_use;
+ std::list<BackendPtr> m_backend_list;
std::map<std::string,Virt_db::Set> m_sets;
- void search(Package &package, Z_APDU *apdu,
- const std::map<std::string, Virt_db::Map> &maps);
+
+ void search(Package &package, Z_APDU *apdu);
void present(Package &package, Z_APDU *apdu);
+ void scan(Package &package, Z_APDU *apdu);
+
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 init_backend(std::string database, Package &package,
+ int &error_code, std::string &addinfo);
+ Rep *m_p;
};
class Virt_db::Rep {
friend class Virt_db;
+ friend class Frontend;
Frontend *get_frontend(Package &package);
void release_frontend(Package &package);
using namespace yp2;
-yf::Virt_db::Frontend::Frontend()
+yf::Virt_db::BackendPtr yf::Virt_db::Frontend::lookup_backend_from_database(
+ std::string database)
+{
+ 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)
+ return *map_it;
+ BackendPtr null;
+ return null;
+}
+
+yf::Virt_db::BackendPtr yf::Virt_db::Frontend::create_backend_from_database(
+ std::string database)
+{
+ 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);
+
+ b->m_number_of_sets = 0;
+ b->m_frontend_database = database;
+ b->m_named_result_sets = false;
+ b->m_route = map_it->second.m_route;
+ b->m_vhost = map_it->second.m_vhost;
+ 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;
+
+ return b;
+}
+
+yf::Virt_db::BackendPtr yf::Virt_db::Frontend::init_backend(
+ std::string database, Package &package,
+ int &error_code, std::string &addinfo)
+{
+ BackendPtr b = create_backend_from_database(database);
+ if (!b)
+ {
+ error_code = YAZ_BIB1_DATABASE_UNAVAILABLE;
+ addinfo = database;
+ return b;
+ }
+ Package init_package(b->m_backend_session, package.origin());
+ init_package.copy_filter(package);
+
+ yp2::odr odr;
+
+ 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());
+
+ 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);
+ ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_3);
+
+ init_package.request() = init_apdu;
+
+ init_package.move(b->m_route); // sending init
+
+ if (init_package.session().is_closed())
+ {
+ error_code = YAZ_BIB1_DATABASE_UNAVAILABLE;
+ addinfo = database;
+ BackendPtr null;
+ return null;
+ }
+ Z_GDU *gdu = init_package.response().get();
+ // we hope to get an init response
+ if (gdu && gdu->which == Z_GDU_Z3950 && gdu->u.z3950->which ==
+ Z_APDU_initResponse)
+ {
+ if (ODR_MASK_GET(gdu->u.z3950->u.initResponse->options,
+ Z_Options_namedResultSets))
+ {
+ b->m_named_result_sets = true;
+ }
+ }
+ else
+ {
+ error_code = YAZ_BIB1_DATABASE_UNAVAILABLE;
+ addinfo = database;
+ BackendPtr null;
+ return null;
+ }
+ m_backend_list.push_back(b);
+ return b;
+}
+
+void yf::Virt_db::Frontend::search(Package &package, Z_APDU *apdu_req)
{
+ Z_SearchRequest *req = apdu_req->u.searchRequest;
+ std::string vhost;
+ 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]);
+
+ BackendPtr b; // null for now
+ 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 backend with a database match
+ 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_database == database)
+ break;
+ }
+ if (map_it != m_backend_list.end())
+ b = *map_it;
+ }
+ else
+ {
+ // new result set.
+
+ // pick up any existing database with named result sets ..
+ // or one which has no result sets.. yet.
+ 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_database == database &&
+ (tmp->m_named_result_sets ||
+ tmp->m_number_of_sets == 0))
+ break;
+ }
+ if (map_it != m_backend_list.end())
+ b = *map_it;
+ }
+ if (!b) // no backend yet. Must create a new one
+ {
+ int error_code;
+ std::string addinfo;
+ b = init_backend(database, 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)
+ {
+ std::cout << "named_result_sets TRUE\n";
+ backend_setname = std::string(req->resultSetName);
+ }
+ else
+ {
+ std::cout << "named_result_sets FALSE\n";
+ backend_setname = "default";
+ req->resultSetName = odr_strdup(odr, backend_setname.c_str());
+ }
+
+ const char *backend_database = b->m_backend_database.c_str();
+ req->databaseNames[0] = odr_strdup(odr, backend_database);
+
+ *req->replaceIndicator = 1;
+
+ search_package.request() = yazpp_1::GDU(apdu_req);
+
+ search_package.move(b->m_route);
+
+ if (search_package.session().is_closed())
+ {
+ Z_APDU *apdu =
+ odr.create_searchResponse(
+ apdu_req,
+ YAZ_BIB1_DATABASE_UNAVAILABLE, database.c_str());
+ package.response() = apdu;
+ return;
+ }
+ package.response() = search_package.response();
+
+ b->m_number_of_sets++;
+
+ m_sets[resultSetId] = Virt_db::Set(b, backend_setname);
+}
+
+yf::Virt_db::Frontend::Frontend(Rep *rep)
+{
+ m_p = rep;
m_is_virtual = false;
}
void yf::Virt_db::Frontend::close(Package &package)
{
- Sets_it sit = m_sets.begin();
- for (; sit != m_sets.end(); sit++)
+ std::list<BackendPtr>::const_iterator b_it;
+
+ for (b_it = m_backend_list.begin(); b_it != m_backend_list.end(); b_it++)
{
- sit->second.m_backend_session.close();
- Package close_package(sit->second.m_backend_session, package.origin());
+ (*b_it)->m_backend_session.close();
+ Package close_package((*b_it)->m_backend_session, package.origin());
close_package.copy_filter(package);
- close_package.move(sit->second.m_route);
+ close_package.move((*b_it)->m_route);
}
- m_sets.clear();
+ m_backend_list.clear();
}
yf::Virt_db::Frontend::~Frontend()
}
m_cond_session_ready.wait(lock);
}
- Frontend *f = new Frontend;
+ Frontend *f = new Frontend(this);
m_clients[package.session()] = f;
f->m_in_use = true;
return f;
}
}
-yf::Virt_db::Set::Set(yp2::Session &id, std::string setname,
- std::string vhost, std::string route,
- bool named_result_sets)
- : m_backend_session(id), m_backend_setname(setname), m_vhost(vhost),
- m_route(route), m_named_result_sets(named_result_sets)
+yf::Virt_db::Set::Set(BackendPtr b, std::string setname)
+ : m_backend(b), m_setname(setname)
{
}
yf::Virt_db::~Virt_db() {
}
-void yf::Virt_db::Frontend::present(Package &package, Z_APDU *apdu)
+void yf::Virt_db::Frontend::present(Package &package, Z_APDU *apdu_req)
{
- Session *id = 0;
- Z_PresentRequest *req = apdu->u.presentRequest;
+ Z_PresentRequest *req = apdu_req->u.presentRequest;
std::string resultSetId = req->resultSetId;
yp2::odr odr;
Sets_it sets_it = m_sets.find(resultSetId);
if (sets_it == m_sets.end())
{
- Z_APDU *apdu = zget_APDU(odr, Z_APDU_presentResponse);
-
- Z_Records *rec = (Z_Records *) odr_malloc(odr, sizeof(Z_Records));
- apdu->u.presentResponse->records = rec;
- rec->which = Z_Records_NSD;
- rec->u.nonSurrogateDiagnostic =
- zget_DefaultDiagFormat(
- odr,
+ Z_APDU *apdu =
+ odr.create_presentResponse(
+ apdu_req,
YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
resultSetId.c_str());
package.response() = apdu;
return;
}
- id = new yp2::Session(sets_it->second.m_backend_session);
+ Session *id =
+ new yp2::Session(sets_it->second.m_backend->m_backend_session);
// sending present to backend
Package present_package(*id, package.origin());
present_package.copy_filter(package);
req->resultSetId = odr_strdup(odr, "default");
- present_package.request() = yazpp_1::GDU(apdu);
+ present_package.request() = yazpp_1::GDU(apdu_req);
present_package.move();
if (present_package.session().is_closed())
{
- Z_APDU *apdu = zget_APDU(odr, Z_APDU_presentResponse);
-
- Z_Records *rec = (Z_Records *) odr_malloc(odr, sizeof(Z_Records));
- apdu->u.presentResponse->records = rec;
- rec->which = Z_Records_NSD;
- rec->u.nonSurrogateDiagnostic =
- zget_DefaultDiagFormat(
- odr,
+ Z_APDU *apdu =
+ odr.create_presentResponse(
+ apdu_req,
YAZ_BIB1_RESULT_SET_NO_LONGER_EXISTS_UNILATERALLY_DELETED_BY_,
resultSetId.c_str());
package.response() = apdu;
-
m_sets.erase(resultSetId);
}
else
delete id;
}
-void yf::Virt_db::Frontend::search(Package &package, Z_APDU *apdu,
- const std::map<std::string, Virt_db::Map> &maps)
+void yf::Virt_db::Frontend::scan(Package &package, Z_APDU *apdu_req)
{
- Z_SearchRequest *req = apdu->u.searchRequest;
+ Z_ScanRequest *req = apdu_req->u.scanRequest;
std::string vhost;
- std::string database;
- std::string resultSetId = req->resultSetName;
- bool support_named_result_sets = false; // whether backend supports it
yp2::odr odr;
-
+
+ // only one datatabase for now
if (req->num_databaseNames != 1)
{ // exactly one database must be specified
- Z_APDU *apdu = zget_APDU(odr, Z_APDU_searchResponse);
-
- Z_Records *rec = (Z_Records *) odr_malloc(odr, sizeof(Z_Records));
- apdu->u.searchResponse->records = rec;
- rec->which = Z_Records_NSD;
- rec->u.nonSurrogateDiagnostic =
- zget_DefaultDiagFormat(
- odr, YAZ_BIB1_TOO_MANY_DATABASES_SPECIFIED, 0);
+ Z_APDU *apdu =
+ odr.create_scanResponse(
+ apdu_req,
+ YAZ_BIB1_TOO_MANY_DATABASES_SPECIFIED, 0);
package.response() = apdu;
-
return;
}
- database = req->databaseNames[0];
- std::map<std::string, Virt_db::Map>::const_iterator map_it;
- map_it = maps.find(database);
- if (map_it == maps.end())
- { // no map for database: return diagnostic
- Z_APDU *apdu = zget_APDU(odr, Z_APDU_searchResponse);
-
- Z_Records *rec = (Z_Records *) odr_malloc(odr, sizeof(Z_Records));
- apdu->u.searchResponse->records = rec;
- rec->which = Z_Records_NSD;
- rec->u.nonSurrogateDiagnostic =
- zget_DefaultDiagFormat(
- odr, YAZ_BIB1_DATABASE_DOES_NOT_EXIST, database.c_str());
- package.response() = apdu;
-
- return;
- }
- if (*req->replaceIndicator == 0)
+ std::string database = std::string(req->databaseNames[0]);
+
+ BackendPtr b;
+ // pick up any existing backend with a database match
+ std::list<BackendPtr>::const_iterator map_it;
+ map_it = m_backend_list.begin();
+ for (; map_it != m_backend_list.end(); map_it++)
{
- Sets_it sets_it = m_sets.find(req->resultSetName);
- if (sets_it != m_sets.end())
- {
- Z_APDU *apdu = zget_APDU(odr, Z_APDU_searchResponse);
-
- Z_Records *rec = (Z_Records *) odr_malloc(odr, sizeof(Z_Records));
- apdu->u.searchResponse->records = rec;
- rec->which = Z_Records_NSD;
- rec->u.nonSurrogateDiagnostic =
- zget_DefaultDiagFormat(
- odr,
- YAZ_BIB1_RESULT_SET_EXISTS_AND_REPLACE_INDICATOR_OFF,
- 0);
- package.response() = apdu;
-
- return;
- }
+ BackendPtr tmp = *map_it;
+ if (tmp->m_frontend_database == database)
+ break;
}
- m_sets.erase(req->resultSetName);
- vhost = map_it->second.m_vhost;
- std::string route = map_it->second.m_route;
- // we might look for an existing session with same vhost
- Session id;
- const char *vhost_cstr = vhost.c_str();
- if (true)
- { // sending init to backend
- Package init_package(id, package.origin());
- init_package.copy_filter(package);
-
- Z_APDU *init_apdu = zget_APDU(odr, Z_APDU_initRequest);
-
- yaz_oi_set_string_oidval(&init_apdu->u.initRequest->otherInfo, odr,
- VAL_PROXY, 1, vhost_cstr);
-
- init_package.request() = init_apdu;
-
- init_package.move(route); // sending init
-
- if (init_package.session().is_closed())
- {
- Z_APDU *apdu = zget_APDU(odr, Z_APDU_searchResponse);
-
- Z_Records *rec = (Z_Records *) odr_malloc(odr, sizeof(Z_Records));
- apdu->u.searchResponse->records = rec;
- rec->which = Z_Records_NSD;
- rec->u.nonSurrogateDiagnostic =
- zget_DefaultDiagFormat(
- odr, YAZ_BIB1_DATABASE_UNAVAILABLE, database.c_str());
- package.response() = apdu;
- }
- Z_GDU *gdu = init_package.response().get();
- // we hope to get an init response
- if (gdu && gdu->which == Z_GDU_Z3950 && gdu->u.z3950->which ==
- Z_APDU_initResponse)
- {
- if (ODR_MASK_GET(gdu->u.z3950->u.initResponse->options,
- Z_Options_namedResultSets))
- support_named_result_sets = true;
- }
- else
+ if (map_it != m_backend_list.end())
+ b = *map_it;
+ if (!b) // no backend yet. Must create a new one
+ {
+ int error_code;
+ std::string addinfo;
+ b = init_backend(database, package, error_code, addinfo);
+ if (!b)
{
- Z_APDU *apdu = zget_APDU(odr, Z_APDU_searchResponse);
-
- Z_Records *rec = (Z_Records *) odr_malloc(odr, sizeof(Z_Records));
- apdu->u.searchResponse->records = rec;
- rec->which = Z_Records_NSD;
- rec->u.nonSurrogateDiagnostic =
- zget_DefaultDiagFormat(
- odr, YAZ_BIB1_DATABASE_UNAVAILABLE, database.c_str());
+ // did not get a backend (unavailable somehow?)
+ Z_APDU *apdu =
+ odr.create_scanResponse(
+ apdu_req, error_code, addinfo.c_str());
package.response() = apdu;
return;
}
}
- // sending search to backend
- Package search_package(id, package.origin());
+ // sending scan to backend
+ Package scan_package(b->m_backend_session, package.origin());
- search_package.copy_filter(package);
- const char *sep = strchr(vhost_cstr, '/');
- if (sep)
- req->databaseNames[0] = odr_strdup(odr, sep+1);
+ scan_package.copy_filter(package);
- *req->replaceIndicator = 1;
+ const char *backend_database = b->m_backend_database.c_str();
+ req->databaseNames[0] = odr_strdup(odr, backend_database);
- std::string backend_resultSetId = "default";
- req->resultSetName = odr_strdup(odr, backend_resultSetId.c_str());
- search_package.request() = yazpp_1::GDU(apdu);
+ scan_package.request() = yazpp_1::GDU(apdu_req);
- search_package.move(route);
+ scan_package.move(b->m_route);
- if (search_package.session().is_closed())
+ if (scan_package.session().is_closed())
{
- Z_APDU *apdu = zget_APDU(odr, Z_APDU_searchResponse);
-
- Z_Records *rec = (Z_Records *) odr_malloc(odr, sizeof(Z_Records));
- apdu->u.searchResponse->records = rec;
- rec->which = Z_Records_NSD;
- rec->u.nonSurrogateDiagnostic =
- zget_DefaultDiagFormat(
- odr, YAZ_BIB1_DATABASE_UNAVAILABLE, database.c_str());
+ Z_APDU *apdu =
+ odr.create_scanResponse(
+ apdu_req, YAZ_BIB1_DATABASE_UNAVAILABLE, database.c_str());
package.response() = apdu;
-
return;
}
- package.response() = search_package.response();
-
- m_sets[resultSetId] =
- Virt_db::Set(id, backend_resultSetId, vhost, route,
- support_named_result_sets);
+ package.response() = scan_package.response();
}
+
void yf::Virt_db::add_map_db2vhost(std::string db, std::string vhost,
std::string route)
{
if (!vhost)
{
yp2::odr odr;
- Z_APDU *apdu = zget_APDU(odr, Z_APDU_initResponse);
+ Z_APDU *apdu = odr.create_initResponse(gdu->u.z3950, 0, 0);
Z_InitResponse *resp = apdu->u.initResponse;
int i;
Z_Options_search,
Z_Options_present,
Z_Options_namedResultSets,
+ Z_Options_scan,
-1
};
for (i = 0; masks[i] != -1; i++)
yp2::odr odr;
package.response() = odr.create_close(
+ apdu,
Z_Close_protocolError,
"double init");
}
else if (apdu->which == Z_APDU_searchRequest)
{
- f->search(package, apdu, m_p->m_maps);
+ f->search(package, apdu);
}
else if (apdu->which == Z_APDU_presentRequest)
{
f->present(package, apdu);
}
+ else if (apdu->which == Z_APDU_scanRequest)
+ {
+ f->scan(package, apdu);
+ }
else
{
yp2::odr odr;
package.response() = odr.create_close(
- Z_Close_protocolError,
+ apdu, Z_Close_protocolError,
"unsupported APDU in filter_virt_db");
package.session().close();
-/* $Id: util.cpp,v 1.3 2005-10-30 18:51:21 adam Exp $
+/* $Id: util.cpp,v 1.4 2006-01-13 15:09:35 adam Exp $
Copyright (c) 2005, Index Data.
%LICENSE%
return m_odr;
}
-Z_APDU *yp2::odr::create_close(int reason, const char *addinfo)
+Z_APDU *yp2::odr::create_close(Z_APDU *in_apdu,
+ int reason, const char *addinfo)
{
- Z_APDU *apdu = zget_APDU(m_odr, Z_APDU_close);
+ Z_APDU *apdu = create_APDU(Z_APDU_close, in_apdu);
*apdu->u.close->closeReason = reason;
if (addinfo)
return apdu;
}
-Z_APDU *yp2::odr::create_initResponse(int error, const char *addinfo)
+Z_APDU *yp2::odr::create_APDU(int type, Z_APDU *in_apdu)
{
- Z_APDU *apdu = zget_APDU(m_odr, Z_APDU_initResponse);
+ return yp2::util::create_APDU(m_odr, type, in_apdu);
+}
+
+Z_APDU *yp2::util::create_APDU(ODR odr, int type, Z_APDU *in_apdu)
+{
+ Z_APDU *out_apdu = zget_APDU(odr, type);
+
+ Z_ReferenceId **id_to = yp2::util::get_referenceId(out_apdu);
+ *id_to = 0;
+ if (in_apdu)
+ {
+ Z_ReferenceId **id_from = yp2::util::get_referenceId(in_apdu);
+ if (id_from && *id_from && id_to)
+ {
+ *id_to = (Z_ReferenceId*) odr_malloc (odr, sizeof(**id_to));
+ (*id_to)->size = (*id_to)->len = (*id_from)->len;
+ (*id_to)->buf = (unsigned char*) odr_malloc(odr, (*id_to)->len);
+ memcpy((*id_to)->buf, (*id_from)->buf, (*id_to)->len);
+ }
+ else if (id_to)
+ *id_to = 0;
+ }
+ return out_apdu;
+}
+
+Z_APDU *yp2::odr::create_initResponse(Z_APDU *in_apdu,
+ int error, const char *addinfo)
+{
+ Z_APDU *apdu = create_APDU(Z_APDU_initResponse, in_apdu);
if (error)
{
apdu->u.initResponse->userInformationField =
return apdu;
}
+Z_APDU *yp2::odr::create_searchResponse(Z_APDU *in_apdu,
+ int error, const char *addinfo)
+{
+ Z_APDU *apdu = create_APDU(Z_APDU_searchResponse, in_apdu);
+ if (error)
+ {
+ Z_Records *rec = (Z_Records *) odr_malloc(m_odr, sizeof(Z_Records));
+ *apdu->u.searchResponse->searchStatus = 0;
+ apdu->u.searchResponse->records = rec;
+ rec->which = Z_Records_NSD;
+ rec->u.nonSurrogateDiagnostic =
+ zget_DefaultDiagFormat(m_odr, error, addinfo);
+
+ }
+ return apdu;
+}
+
+Z_APDU *yp2::odr::create_presentResponse(Z_APDU *in_apdu,
+ int error, const char *addinfo)
+{
+ Z_APDU *apdu = create_APDU(Z_APDU_presentResponse, in_apdu);
+ if (error)
+ {
+ Z_Records *rec = (Z_Records *) odr_malloc(m_odr, sizeof(Z_Records));
+ apdu->u.presentResponse->records = rec;
+ rec->which = Z_Records_NSD;
+ rec->u.nonSurrogateDiagnostic =
+ zget_DefaultDiagFormat(m_odr, error, addinfo);
+ }
+ return apdu;
+}
+
+Z_APDU *yp2::odr::create_scanResponse(Z_APDU *in_apdu,
+ int error, const char *addinfo)
+{
+ Z_APDU *apdu = create_APDU(Z_APDU_scanResponse, in_apdu);
+ if (error)
+ {
+ Z_ScanResponse *res = apdu->u.scanResponse;
+ res->entries = (Z_ListEntries *) odr_malloc(m_odr, sizeof(*res->entries));
+ *res->scanStatus = Z_Scan_failure;
+
+ res->entries->num_entries = 0;
+ res->entries->entries = 0;
+ res->entries->num_nonsurrogateDiagnostics = 1;
+ res->entries->nonsurrogateDiagnostics = (Z_DiagRec **)
+ odr_malloc(m_odr, sizeof(Z_DiagRec *));
+ res->entries->nonsurrogateDiagnostics[0] =
+ zget_DiagRec(m_odr, error, addinfo);
+ }
+ return apdu;
+}
+
+Z_ReferenceId **yp2::util::get_referenceId(Z_APDU *apdu)
+{
+ switch (apdu->which)
+ {
+ case Z_APDU_initRequest:
+ return &apdu->u.initRequest->referenceId;
+ case Z_APDU_initResponse:
+ return &apdu->u.initResponse->referenceId;
+ case Z_APDU_searchRequest:
+ return &apdu->u.searchRequest->referenceId;
+ case Z_APDU_searchResponse:
+ return &apdu->u.searchResponse->referenceId;
+ case Z_APDU_presentRequest:
+ return &apdu->u.presentRequest->referenceId;
+ case Z_APDU_presentResponse:
+ return &apdu->u.presentResponse->referenceId;
+ case Z_APDU_deleteResultSetRequest:
+ return &apdu->u.deleteResultSetRequest->referenceId;
+ case Z_APDU_deleteResultSetResponse:
+ return &apdu->u.deleteResultSetResponse->referenceId;
+ case Z_APDU_accessControlRequest:
+ return &apdu->u.accessControlRequest->referenceId;
+ case Z_APDU_accessControlResponse:
+ return &apdu->u.accessControlResponse->referenceId;
+ case Z_APDU_resourceControlRequest:
+ return &apdu->u.resourceControlRequest->referenceId;
+ case Z_APDU_resourceControlResponse:
+ return &apdu->u.resourceControlResponse->referenceId;
+ case Z_APDU_triggerResourceControlRequest:
+ return &apdu->u.triggerResourceControlRequest->referenceId;
+ case Z_APDU_resourceReportRequest:
+ return &apdu->u.resourceReportRequest->referenceId;
+ case Z_APDU_resourceReportResponse:
+ return &apdu->u.resourceReportResponse->referenceId;
+ case Z_APDU_scanRequest:
+ return &apdu->u.scanRequest->referenceId;
+ case Z_APDU_scanResponse:
+ return &apdu->u.scanResponse->referenceId;
+ case Z_APDU_sortRequest:
+ return &apdu->u.sortRequest->referenceId;
+ case Z_APDU_sortResponse:
+ return &apdu->u.sortResponse->referenceId;
+ case Z_APDU_segmentRequest:
+ return &apdu->u.segmentRequest->referenceId;
+ case Z_APDU_extendedServicesRequest:
+ return &apdu->u.extendedServicesRequest->referenceId;
+ case Z_APDU_extendedServicesResponse:
+ return &apdu->u.extendedServicesResponse->referenceId;
+ case Z_APDU_close:
+ return &apdu->u.close->referenceId;
+ }
+ return 0;
+}
+
+
/*
* Local variables:
* c-basic-offset: 4