1 /* $Id: p2_msg.cpp,v 1.3 2005-10-08 23:29:32 adam Exp $
2 Copyright (c) 1998-2005, Index Data.
4 This file is part of the yaz-proxy.
6 YAZ proxy is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
11 YAZ proxy is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with YAZ proxy; see the file LICENSE. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
24 #include <yaz/diagbib1.h>
25 #include "p2_backend.h"
26 #include "p2_frontend.h"
27 #include "p2_config.h"
28 #include "p2_modules.h"
30 using namespace yazpp_1;
33 IP2_BackendSet::~IP2_BackendSet()
37 IP2_Backend::~IP2_Backend()
42 P2_Backend::P2_Backend(P2_ConfigTarget *cfg, IP2_Backend *backend_int)
44 m_configTarget = new P2_ConfigTarget;
45 *m_configTarget = *cfg;
50 P2_Backend::~P2_Backend()
52 delete m_configTarget;
55 P2_BackendResultSet::P2_BackendResultSet()
60 P2_BackendResultSet::~P2_BackendResultSet()
65 P2_Backend *P2_Msg::select_backend(string db,
67 P2_BackendResultSet **bset)
69 P2_Config *cfg = m_server->lockConfig();
71 // see if some target has done this query before
74 P2_Backend *backend = 0;
76 list<P2_Backend *>::const_iterator it;
77 for (it = m_server->m_backend_list.begin();
78 it != m_server->m_backend_list.end(); it++)
83 if (db != (*it)->m_configTarget->m_virt_database)
89 list<P2_BackendResultSet *>::const_iterator is;
90 for (is = (*it)->m_resultSets.begin();
91 is != (*it)->m_resultSets.end(); is++)
93 if (query->match(&(*is)->m_query))
105 P2_ConfigTarget *target_cfg = cfg->find_target(db);
109 yaz_log(YLOG_WARN, "No backend for database %s",
114 P2_ModuleInterface0 *int0 =
115 reinterpret_cast<P2_ModuleInterface0 *>
116 (m_server->m_modules->get_interface(target_cfg->m_type.c_str(),
118 IP2_Backend *bint = 0;
121 bint = int0->create(target_cfg->m_target_address.c_str());
124 backend = new P2_Backend(target_cfg, bint);
127 m_server->m_backend_list.push_back(backend);
131 backend->m_busy = true;
132 m_server->unlockConfig();
136 void P2_FrontResultSet::setQuery(Z_Query *z_query)
138 m_query.set_Z_Query(z_query);
141 void P2_FrontResultSet::setDatabases(char **db, int num)
146 for (i = 0; i<num; i++)
147 m_db_list.push_back(db[i]);
150 P2_FrontResultSet::P2_FrontResultSet(const char *id)
156 P2_FrontResultSet::~P2_FrontResultSet()
160 P2_Msg::P2_Msg(GDU *gdu, P2_Frontend *front, P2_Server *server)
175 Z_APDU *P2_Msg::frontend_search_resultset(Z_APDU *z_gdu, ODR odr,
176 P2_FrontResultSet **rset)
178 Z_SearchRequest *req = z_gdu->u.searchRequest;
179 list<P2_FrontResultSet *>::iterator it;
180 P2_FrontResultSet *s = 0;
182 string id = req->resultSetName;
183 for (it = m_front->m_resultSets.begin(); it != m_front->m_resultSets.end(); it++)
185 if ((*it)->m_resultSetId == id)
193 // result set already exists
195 if (req->replaceIndicator && *req->replaceIndicator)
196 { // replace indicator true
197 s->setQuery(req->query);
198 s->setDatabases(req->databaseNames, req->num_databaseNames);
201 Z_APDU *apdu = zget_APDU(odr, Z_APDU_searchResponse);
202 Z_Records *rec = (Z_Records *) odr_malloc(odr, sizeof(Z_Records));
203 apdu->u.searchResponse->records = rec;
204 rec->which = Z_Records_NSD;
205 rec->u.nonSurrogateDiagnostic =
206 zget_DefaultDiagFormat(
207 odr, YAZ_BIB1_RESULT_SET_EXISTS_AND_REPLACE_INDICATOR_OFF,
213 s = new P2_FrontResultSet(req->resultSetName);
214 s->setQuery(req->query);
215 s->setDatabases(req->databaseNames, req->num_databaseNames);
216 m_front->m_resultSets.push_back(s);
221 Z_APDU *P2_Msg::frontend_search_apdu(Z_APDU *request_apdu, ODR odr)
223 P2_FrontResultSet *rset;
224 Z_APDU *response_apdu = frontend_search_resultset(request_apdu, odr,
227 return response_apdu;
229 // no immediate error (yet)
231 for (i = 0; i<rset->m_db_list.size(); i++)
233 string db = rset->m_db_list[i];
234 P2_BackendResultSet *bset;
235 P2_Backend *b = select_backend(db, &rset->m_query, &bset);
238 Z_APDU *apdu = zget_APDU(odr, Z_APDU_searchResponse);
239 Z_Records *rec = (Z_Records *) odr_malloc(odr, sizeof(Z_Records));
240 apdu->u.searchResponse->records = rec;
241 rec->which = Z_Records_NSD;
242 rec->u.nonSurrogateDiagnostic =
243 zget_DefaultDiagFormat(
244 odr, YAZ_BIB1_DATABASE_UNAVAILABLE, db.c_str());
249 bset = new P2_BackendResultSet();
251 bset->m_query.set_Z_Query(request_apdu->u.searchRequest->query);
252 bset->m_db_list.push_back(db);
254 b->m_int->search(&bset->m_query, &bset->m_int, &bset->m_hit_count);
255 b->m_resultSets.push_back(bset);
259 bset->m_int->get(1, 1);
261 response_apdu = zget_APDU(odr, Z_APDU_searchResponse);
262 *response_apdu->u.searchResponse->resultCount = bset->m_hit_count;
267 Z_APDU *apdu = zget_APDU(odr, Z_APDU_searchResponse);
268 Z_Records *rec = (Z_Records *) odr_malloc(odr, sizeof(Z_Records));
269 apdu->u.searchResponse->records = rec;
270 rec->which = Z_Records_NSD;
271 rec->u.nonSurrogateDiagnostic =
272 zget_DefaultDiagFormat(odr, YAZ_BIB1_UNSUPP_SEARCH, 0);
275 return response_apdu;
278 Z_APDU *P2_Msg::frontend_present_resultset(Z_APDU *z_gdu, ODR odr,
279 P2_FrontResultSet **rset)
281 Z_PresentRequest *req = z_gdu->u.presentRequest;
282 list<P2_FrontResultSet *>::iterator it;
283 P2_FrontResultSet *s = 0;
285 string id = req->resultSetId;
286 for (it = m_front->m_resultSets.begin(); it != m_front->m_resultSets.end(); it++)
288 if ((*it)->m_resultSetId == id)
296 return 0; // fine result set exists
298 Z_APDU *apdu = zget_APDU(odr, Z_APDU_presentResponse);
300 Z_Records *rec = (Z_Records *) odr_malloc(odr, sizeof(Z_Records));
301 apdu->u.presentResponse->records = rec;
302 rec->which = Z_Records_NSD;
303 rec->u.nonSurrogateDiagnostic =
304 zget_DefaultDiagFormat(
306 YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
311 Z_APDU *P2_Msg::frontend_present_apdu(Z_APDU *request_apdu, ODR odr)
313 P2_FrontResultSet *rset;
314 Z_APDU *response_apdu = frontend_present_resultset(request_apdu, odr,
317 return response_apdu;
318 return zget_APDU(odr, Z_APDU_presentResponse);
321 IThreadPoolMsg *P2_Msg::handle()
323 ODR odr = odr_createmem(ODR_ENCODE);
324 yaz_log(YLOG_LOG, "P2_Msg:handle begin");
325 Z_GDU *request_gdu = m_gdu->get();
327 if (request_gdu->which == Z_GDU_Z3950)
329 Z_APDU *request_apdu = request_gdu->u.z3950;
330 Z_APDU *response_apdu = 0;
331 switch(request_apdu->which)
333 case Z_APDU_initRequest:
334 response_apdu = zget_APDU(odr, Z_APDU_initResponse);
335 ODR_MASK_SET(response_apdu->u.initResponse->options, Z_Options_triggerResourceCtrl);
336 ODR_MASK_SET(response_apdu->u.initResponse->options, Z_Options_search);
337 ODR_MASK_SET(response_apdu->u.initResponse->options, Z_Options_present);
338 ODR_MASK_SET(response_apdu->u.initResponse->options, Z_Options_namedResultSets);
340 case Z_APDU_searchRequest:
341 response_apdu = frontend_search_apdu(request_apdu, odr);
343 case Z_APDU_presentRequest:
344 response_apdu = frontend_present_apdu(request_apdu, odr);
346 case Z_APDU_triggerResourceControlRequest:
349 response_apdu = zget_APDU(odr, Z_APDU_close);
354 m_output = new GDU(response_apdu);
356 yaz_log(YLOG_LOG, "P2_Msg:handle end");
361 void P2_Msg::result()
363 m_front->m_no_requests--;
364 if (!m_front->m_delete_flag)
369 m_front->send_GDU(m_output->get(), &len);
374 m_front->m_delete_flag = 1;
377 if (m_front->m_delete_flag && m_front->m_no_requests == 0)
385 * indent-tabs-mode: nil
387 * vim: shiftwidth=4 tabstop=8 expandtab