1 /* This file is part of Metaproxy.
2 Copyright (C) 2005-2013 Index Data
4 Metaproxy is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
9 Metaproxy is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 #include "filter_present_chunk.hpp"
24 #include <yaz/copy_types.h>
25 #include <metaproxy/package.hpp>
26 #include <metaproxy/util.hpp>
28 namespace mp = metaproxy_1;
29 namespace yf = mp::filter;
31 namespace metaproxy_1 {
33 class PresentChunk::Impl {
37 void process(metaproxy_1::Package & package);
38 void configure(const xmlNode * ptr);
39 void chunk_it(metaproxy_1::Package & package, Z_APDU *apdu);
46 yf::PresentChunk::PresentChunk() : m_p(new Impl)
50 yf::PresentChunk::~PresentChunk()
51 { // must have a destructor because of boost::scoped_ptr
54 void yf::PresentChunk::configure(const xmlNode *xmlnode, bool test_only,
57 m_p->configure(xmlnode);
60 void yf::PresentChunk::process(mp::Package &package) const
62 m_p->process(package);
65 yf::PresentChunk::Impl::Impl() : chunk_number(0)
69 yf::PresentChunk::Impl::~Impl()
73 void yf::PresentChunk::Impl::configure(const xmlNode *ptr)
75 for (ptr = ptr->children; ptr; ptr = ptr->next)
77 if (ptr->type != XML_ELEMENT_NODE)
79 if (!strcmp((const char *) ptr->name, "chunk"))
81 chunk_number = mp::xml::get_int(ptr, 0);
85 throw mp::filter::FilterException("Bad element "
86 + std::string((const char *)
92 void yf::PresentChunk::Impl::chunk_it(mp::Package &package,
96 Z_PresentRequest *pr = apdu->u.presentRequest;
98 Odr_int total = *pr->numberOfRecordsRequested;
99 Odr_int start = *pr->resultSetStartPoint;
101 Z_NamePlusRecordList *npl = (Z_NamePlusRecordList *)
102 odr_malloc(odr, sizeof(*npl));
103 npl->num_records = total;
104 npl->records = (Z_NamePlusRecord **)
105 odr_malloc(odr, sizeof(*npl->records) * total);
106 while (offset < total)
108 Odr_int left = total - offset;
110 Package pp(package.session(), package.origin());
112 *pr->numberOfRecordsRequested =
113 left > chunk_number ? chunk_number : left;
115 *pr->resultSetStartPoint = start + offset;
117 pp.copy_filter(package);
122 if (pp.session().is_closed())
124 package.session().close();
127 Z_GDU *gdu_res = pp.response().get();
128 if (gdu_res && gdu_res->which == Z_GDU_Z3950 &&
129 gdu_res->u.z3950->which == Z_APDU_presentResponse)
131 Z_PresentResponse *pres =
132 gdu_res->u.z3950->u.presentResponse;
134 pres->records->which == Z_Records_DBOSD)
136 Z_NamePlusRecordList *nprl =
137 pres->records->u.databaseOrSurDiagnostics;
139 for (i = 0; i < nprl->num_records; i++)
142 npl->records[offset+i] = yaz_clone_z_NamePlusRecord(
143 nprl->records[i], o->mem);
145 offset += nprl->num_records;
149 package.response() = pp.response();
155 package.response() = pp.response();
160 yaz_log(YLOG_LOG, "building response . %lld", offset);
162 Z_APDU *a = zget_APDU(odr, Z_APDU_presentResponse);
163 Z_PresentResponse *pres = a->u.presentResponse;
164 pres->records = (Z_Records *)
165 odr_malloc(odr, sizeof(Z_Records));
166 pres->records->which = Z_Records_DBOSD;
167 pres->records->u.databaseOrSurDiagnostics = npl;
168 npl->num_records = offset;
169 *pres->numberOfRecordsReturned = offset;
171 package.response() = a;
174 void yf::PresentChunk::Impl::process(mp::Package &package)
176 Z_GDU *gdu = package.request().get();
177 if (gdu && gdu->which == Z_GDU_Z3950)
179 Z_APDU *apdu = gdu->u.z3950;
180 if (apdu->which == Z_APDU_presentRequest && chunk_number > 0)
181 chunk_it(package, apdu);
189 static mp::filter::Base* filter_creator()
191 return new mp::filter::PresentChunk;
195 struct metaproxy_1_filter_struct metaproxy_1_filter_present_chunk = {
206 * c-file-style: "Stroustrup"
207 * indent-tabs-mode: nil
209 * vim: shiftwidth=4 tabstop=8 expandtab