1 /* This file is part of the yazpp toolkit.
2 * Copyright (C) Index Data
3 * See the file LICENSE for details.
12 #include <yazpp/ir-assoc.h>
13 #include <yaz/oid_db.h>
14 #include <yaz/comstack.h>
16 using namespace yazpp_1;
18 IR_Assoc::IR_Assoc(IPDU_Observable *the_PDU_Observable)
19 : Z_Assoc(the_PDU_Observable)
21 m_num_databaseNames = 0;
23 m_preferredRecordSyntax = 0;
24 m_elementSetNames = 0;
30 const char *db = "Default";
31 set_databaseNames(1, &db);
36 xfree(m_preferredRecordSyntax);
37 if (m_elementSetNames)
38 delete [] m_elementSetNames->u.generic;
39 delete [] m_elementSetNames;
45 void IR_Assoc::get_databaseNames (int *num, char ***list)
47 *num = m_num_databaseNames;
48 *list = m_databaseNames;
52 void IR_Assoc::set_databaseNames (int num, const char **list)
55 yaz_log (m_log, "IR_Assoc::set_databaseNames num=%d", num);
56 for (i = 0; i<m_num_databaseNames; i++)
57 delete [] m_databaseNames[i];
58 delete [] m_databaseNames;
59 m_num_databaseNames = num;
61 m_databaseNames = new char *[num];
62 for (i = 0; i<m_num_databaseNames; i++)
64 m_databaseNames[i] = new char[strlen(list[i])+1];
65 strcpy(m_databaseNames[i], list[i]);
69 void IR_Assoc::set_databaseNames(const char *dblist, const char *sep)
71 const char **list = new const char* [strlen(dblist)];
72 char *dbtmp = new char[strlen(dblist)+1];
73 strcpy(dbtmp, dblist);
76 for (char *cp = dbtmp; ; cp++)
77 if (*cp && !strchr(sep, *cp))
91 set_databaseNames (num, list);
96 void IR_Assoc::set_preferredRecordSyntax (const char *syntax)
98 xfree(m_preferredRecordSyntax);
99 m_preferredRecordSyntax = 0;
100 if (syntax && *syntax)
101 m_preferredRecordSyntax = xstrdup(syntax);
104 void IR_Assoc::get_preferredRecordSyntax (const char **dst)
106 if (m_preferredRecordSyntax)
107 *dst = m_preferredRecordSyntax;
112 void IR_Assoc::set_elementSetName (const char *elementSetName)
114 if (m_elementSetNames)
115 delete [] m_elementSetNames->u.generic;
116 delete m_elementSetNames;
117 m_elementSetNames = 0;
118 if (elementSetName && *elementSetName)
120 m_elementSetNames = new Z_ElementSetNames;
121 m_elementSetNames->which = Z_ElementSetNames_generic;
122 m_elementSetNames->u.generic = new char[strlen(elementSetName)+1];
123 strcpy (m_elementSetNames->u.generic, elementSetName);
127 void IR_Assoc::get_elementSetName (Z_ElementSetNames **elementSetNames)
129 *elementSetNames = m_elementSetNames;
132 void IR_Assoc::get_elementSetName (const char **elementSetName)
134 if (!m_elementSetNames ||
135 m_elementSetNames->which != Z_ElementSetNames_generic)
140 *elementSetName = m_elementSetNames->u.generic;
144 void IR_Assoc::recv_GDU(Z_GDU *apdu, int len)
146 if (apdu->which == Z_GDU_Z3950)
147 recv_Z_PDU(apdu->u.z3950, len);
150 void IR_Assoc::recv_Z_PDU(Z_APDU *apdu, int len)
152 yaz_log (m_log, "recv_Z_PDU %d bytes", len);
153 m_lastReceived = apdu->which;
156 case Z_APDU_initResponse:
157 yaz_log (m_log, "recv InitResponse");
158 recv_initResponse(apdu->u.initResponse);
160 case Z_APDU_initRequest:
161 yaz_log (m_log, "recv InitRequest");
162 recv_initRequest(apdu->u.initRequest);
164 case Z_APDU_searchRequest:
165 yaz_log (m_log, "recv searchRequest");
166 recv_searchRequest(apdu->u.searchRequest);
168 case Z_APDU_searchResponse:
169 yaz_log (m_log, "recv searchResponse");
170 recv_searchResponse(apdu->u.searchResponse);
172 case Z_APDU_presentRequest:
173 yaz_log (m_log, "recv presentRequest");
174 recv_presentRequest(apdu->u.presentRequest);
176 case Z_APDU_presentResponse:
177 yaz_log (m_log, "recv presentResponse");
178 recv_presentResponse(apdu->u.presentResponse);
180 case Z_APDU_extendedServicesResponse:
181 yaz_log (m_log, "recv extendedServiceResponse");
182 recv_extendedServicesResponse(apdu->u.extendedServicesResponse);
187 int IR_Assoc::send_searchRequest(Yaz_Z_Query *query,
191 Z_APDU *apdu = create_Z_PDU(Z_APDU_searchRequest);
192 Z_SearchRequest *req = apdu->u.searchRequest;
194 req->query = query->get_Z_Query();
197 get_databaseNames (&req->num_databaseNames, &req->databaseNames);
198 const char *recordSyntax;
199 get_preferredRecordSyntax(&recordSyntax);
200 if (recordSyntax && *recordSyntax)
202 req->preferredRecordSyntax
203 = yaz_string_to_oid_odr(yaz_oid_std(), CLASS_RECSYN, recordSyntax,
206 yaz_log (m_log, "send_searchRequest");
207 assert (req->otherInfo == 0);
210 set_otherInformationString(&req->otherInfo, yaz_oid_userinfo_cookie,
212 assert (req->otherInfo);
217 req->referenceId = getRefID(pRefId);
222 req->resultSetName = pResultSetId;
225 return send_Z_PDU(apdu, 0);
228 int IR_Assoc::send_presentRequest(Odr_int start,
233 Z_APDU *apdu = create_Z_PDU(Z_APDU_presentRequest);
234 Z_PresentRequest *req = apdu->u.presentRequest;
236 req->resultSetStartPoint = &start;
237 req->numberOfRecordsRequested = &number;
239 const char *recordSyntax;
240 get_preferredRecordSyntax (&recordSyntax);
241 if (recordSyntax && *recordSyntax)
243 req->preferredRecordSyntax =
244 yaz_string_to_oid_odr(yaz_oid_std(), CLASS_RECSYN, recordSyntax,
247 Z_RecordComposition compo;
248 Z_ElementSetNames *elementSetNames;
249 get_elementSetName (&elementSetNames);
252 req->recordComposition = &compo;
253 compo.which = Z_RecordComp_simple;
254 compo.u.simple = elementSetNames;
258 set_otherInformationString(&req->otherInfo, yaz_oid_userinfo_cookie,
263 req->referenceId = getRefID(pRefId);
268 req->resultSetId = pResultSetId;
271 return send_Z_PDU(apdu, 0);
274 void IR_Assoc::set_proxy(const char *str)
280 m_proxy = new char[strlen(str)+1];
281 strcpy (m_proxy, str);
285 void IR_Assoc::set_cookie(const char *str)
291 m_cookie = new char[strlen(str)+1];
292 strcpy(m_cookie, str);
296 const char *IR_Assoc::get_cookie()
301 void IR_Assoc::client(const char *addr)
304 m_host = new char[strlen(addr)+1];
305 strcpy(m_host, addr);
306 const char *dbpart = 0;
307 cs_get_host_args(m_host, &dbpart);
308 if (dbpart && *dbpart)
309 set_databaseNames (dbpart, "+ ");
310 Z_Assoc::client(m_proxy ? m_proxy : m_host);
313 const char *IR_Assoc::get_proxy()
318 const char *IR_Assoc::get_host()
323 void IR_Assoc::recv_searchRequest(Z_SearchRequest *searchRequest)
325 Z_APDU *apdu = create_Z_PDU(Z_APDU_searchResponse);
329 void IR_Assoc::recv_presentRequest(Z_PresentRequest *presentRequest)
331 Z_APDU *apdu = create_Z_PDU(Z_APDU_presentResponse);
335 void IR_Assoc::recv_initRequest(Z_InitRequest *initRequest)
337 Z_APDU *apdu = create_Z_PDU(Z_APDU_initResponse);
341 void IR_Assoc::recv_searchResponse (Z_SearchResponse *searchResponse)
345 void IR_Assoc::recv_presentResponse (Z_PresentResponse *presentResponse)
349 void IR_Assoc::recv_initResponse(Z_InitResponse *initResponse)
353 void IR_Assoc::recv_extendedServicesResponse(Z_ExtendedServicesResponse *ExtendedServicesResponse)
357 int IR_Assoc::get_lastReceived()
359 return m_lastReceived;
362 void IR_Assoc::set_lastReceived(int lastReceived)
364 m_lastReceived = lastReceived;
367 int IR_Assoc::send_initRequest(char* pRefId)
369 Z_APDU *apdu = create_Z_PDU(Z_APDU_initRequest);
370 Z_InitRequest *req = apdu->u.initRequest;
372 ODR_MASK_SET(req->options, Z_Options_search);
373 ODR_MASK_SET(req->options, Z_Options_present);
374 ODR_MASK_SET(req->options, Z_Options_namedResultSets);
375 ODR_MASK_SET(req->options, Z_Options_triggerResourceCtrl);
376 ODR_MASK_SET(req->options, Z_Options_scan);
377 ODR_MASK_SET(req->options, Z_Options_sort);
378 ODR_MASK_SET(req->options, Z_Options_extendedServices);
379 ODR_MASK_SET(req->options, Z_Options_delSet);
381 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_1);
382 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_2);
383 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_3);
387 req->referenceId = getRefID(pRefId);
390 if (m_proxy && m_host)
391 set_otherInformationString(&req->otherInfo, yaz_oid_userinfo_proxy,
394 set_otherInformationString(&req->otherInfo, yaz_oid_userinfo_cookie,
396 return send_Z_PDU(apdu, 0);
399 int IR_Assoc::send_deleteResultSetRequest(char* pResultSetId, char* pRefId)
401 char* ResultSetIds[1];
403 Z_APDU *apdu = create_Z_PDU(Z_APDU_deleteResultSetRequest);
404 Z_DeleteResultSetRequest *req = apdu->u.deleteResultSetRequest;
408 *req->deleteFunction = Z_DeleteResultSetRequest_list;
409 req->num_resultSetList = 1;
410 ResultSetIds[0] = pResultSetId;
411 req->resultSetList = ResultSetIds;
415 *req->deleteFunction = Z_DeleteResultSetRequest_all;
420 req->referenceId = getRefID(pRefId);
423 if (m_proxy && m_host)
424 set_otherInformationString(&req->otherInfo, yaz_oid_userinfo_proxy,
427 set_otherInformationString(&req->otherInfo, yaz_oid_userinfo_cookie,
430 return send_Z_PDU(apdu, 0);
437 * c-file-style: "Stroustrup"
438 * indent-tabs-mode: nil
440 * vim: shiftwidth=4 tabstop=8 expandtab