1 /* This file is part of the yazpp toolkit.
2 * Copyright (C) 1998-2011 Index Data and Mike Taylor
3 * See the file LICENSE for details.
9 #include <yazpp/ir-assoc.h>
10 #include <yaz/oid_db.h>
12 using namespace yazpp_1;
14 IR_Assoc::IR_Assoc(IPDU_Observable *the_PDU_Observable)
15 : Z_Assoc(the_PDU_Observable)
17 m_num_databaseNames = 0;
19 m_preferredRecordSyntax = 0;
20 m_elementSetNames = 0;
26 const char *db = "Default";
27 set_databaseNames(1, &db);
32 xfree(m_preferredRecordSyntax);
33 if (m_elementSetNames)
34 delete [] m_elementSetNames->u.generic;
35 delete [] m_elementSetNames;
41 void IR_Assoc::get_databaseNames (int *num, char ***list)
43 *num = m_num_databaseNames;
44 *list = m_databaseNames;
48 void IR_Assoc::set_databaseNames (int num, const char **list)
51 yaz_log (m_log, "IR_Assoc::set_databaseNames num=%d", num);
52 for (i = 0; i<m_num_databaseNames; i++)
53 delete [] m_databaseNames[i];
54 delete [] m_databaseNames;
55 m_num_databaseNames = num;
57 m_databaseNames = new char *[num];
58 for (i = 0; i<m_num_databaseNames; i++)
60 m_databaseNames[i] = new char[strlen(list[i])+1];
61 strcpy(m_databaseNames[i], list[i]);
65 void IR_Assoc::set_databaseNames(const char *dblist, const char *sep)
67 const char **list = new const char* [strlen(dblist)];
68 char *dbtmp = new char[strlen(dblist)+1];
69 strcpy(dbtmp, dblist);
72 for (char *cp = dbtmp; ; cp++)
73 if (*cp && !strchr(sep, *cp))
87 set_databaseNames (num, list);
92 void IR_Assoc::set_preferredRecordSyntax (const char *syntax)
94 xfree(m_preferredRecordSyntax);
95 m_preferredRecordSyntax = 0;
96 if (syntax && *syntax)
97 m_preferredRecordSyntax = xstrdup(syntax);
100 void IR_Assoc::get_preferredRecordSyntax (const char **dst)
102 if (m_preferredRecordSyntax)
103 *dst = m_preferredRecordSyntax;
108 void IR_Assoc::set_elementSetName (const char *elementSetName)
110 if (m_elementSetNames)
111 delete [] m_elementSetNames->u.generic;
112 delete m_elementSetNames;
113 m_elementSetNames = 0;
114 if (elementSetName && *elementSetName)
116 m_elementSetNames = new Z_ElementSetNames;
117 m_elementSetNames->which = Z_ElementSetNames_generic;
118 m_elementSetNames->u.generic = new char[strlen(elementSetName)+1];
119 strcpy (m_elementSetNames->u.generic, elementSetName);
123 void IR_Assoc::get_elementSetName (Z_ElementSetNames **elementSetNames)
125 *elementSetNames = m_elementSetNames;
128 void IR_Assoc::get_elementSetName (const char **elementSetName)
130 if (!m_elementSetNames ||
131 m_elementSetNames->which != Z_ElementSetNames_generic)
136 *elementSetName = m_elementSetNames->u.generic;
140 void IR_Assoc::recv_GDU(Z_GDU *apdu, int len)
142 if (apdu->which == Z_GDU_Z3950)
143 recv_Z_PDU(apdu->u.z3950, len);
146 void IR_Assoc::recv_Z_PDU(Z_APDU *apdu, int len)
148 yaz_log (m_log, "recv_Z_PDU %d bytes", len);
149 m_lastReceived = apdu->which;
152 case Z_APDU_initResponse:
153 yaz_log (m_log, "recv InitResponse");
154 recv_initResponse(apdu->u.initResponse);
156 case Z_APDU_initRequest:
157 yaz_log (m_log, "recv InitRequest");
158 recv_initRequest(apdu->u.initRequest);
160 case Z_APDU_searchRequest:
161 yaz_log (m_log, "recv searchRequest");
162 recv_searchRequest(apdu->u.searchRequest);
164 case Z_APDU_searchResponse:
165 yaz_log (m_log, "recv searchResponse");
166 recv_searchResponse(apdu->u.searchResponse);
168 case Z_APDU_presentRequest:
169 yaz_log (m_log, "recv presentRequest");
170 recv_presentRequest(apdu->u.presentRequest);
172 case Z_APDU_presentResponse:
173 yaz_log (m_log, "recv presentResponse");
174 recv_presentResponse(apdu->u.presentResponse);
176 case Z_APDU_extendedServicesResponse:
177 yaz_log (m_log, "recv extendedServiceResponse");
178 recv_extendedServicesResponse(apdu->u.extendedServicesResponse);
183 int IR_Assoc::send_searchRequest(Yaz_Z_Query *query,
187 Z_APDU *apdu = create_Z_PDU(Z_APDU_searchRequest);
188 Z_SearchRequest *req = apdu->u.searchRequest;
190 req->query = query->get_Z_Query();
193 get_databaseNames (&req->num_databaseNames, &req->databaseNames);
194 const char *recordSyntax;
195 get_preferredRecordSyntax(&recordSyntax);
196 if (recordSyntax && *recordSyntax)
198 req->preferredRecordSyntax
199 = yaz_string_to_oid_odr(yaz_oid_std(), CLASS_RECSYN, recordSyntax,
202 yaz_log (m_log, "send_searchRequest");
203 assert (req->otherInfo == 0);
206 set_otherInformationString(&req->otherInfo, yaz_oid_userinfo_cookie,
208 assert (req->otherInfo);
213 req->referenceId = getRefID(pRefId);
218 req->resultSetName = pResultSetId;
221 return send_Z_PDU(apdu, 0);
224 int IR_Assoc::send_presentRequest(Odr_int start,
229 Z_APDU *apdu = create_Z_PDU(Z_APDU_presentRequest);
230 Z_PresentRequest *req = apdu->u.presentRequest;
232 req->resultSetStartPoint = &start;
233 req->numberOfRecordsRequested = &number;
235 const char *recordSyntax;
236 get_preferredRecordSyntax (&recordSyntax);
237 if (recordSyntax && *recordSyntax)
239 req->preferredRecordSyntax =
240 yaz_string_to_oid_odr(yaz_oid_std(), CLASS_RECSYN, recordSyntax,
243 Z_RecordComposition compo;
244 Z_ElementSetNames *elementSetNames;
245 get_elementSetName (&elementSetNames);
248 req->recordComposition = &compo;
249 compo.which = Z_RecordComp_simple;
250 compo.u.simple = elementSetNames;
254 set_otherInformationString(&req->otherInfo, yaz_oid_userinfo_cookie,
259 req->referenceId = getRefID(pRefId);
264 req->resultSetId = pResultSetId;
267 return send_Z_PDU(apdu, 0);
270 void IR_Assoc::set_proxy(const char *str)
276 m_proxy = new char[strlen(str)+1];
277 strcpy (m_proxy, str);
281 void IR_Assoc::set_cookie(const char *str)
287 m_cookie = new char[strlen(str)+1];
288 strcpy(m_cookie, str);
292 const char *IR_Assoc::get_cookie()
297 void IR_Assoc::client(const char *addr)
300 m_host = new char[strlen(addr)+1];
301 strcpy(m_host, addr);
302 const char *dbpart = strchr(m_host, '/');
304 set_databaseNames (dbpart+1, "+ ");
305 Z_Assoc::client(m_proxy ? m_proxy : m_host);
308 const char *IR_Assoc::get_proxy()
313 const char *IR_Assoc::get_host()
318 void IR_Assoc::recv_searchRequest(Z_SearchRequest *searchRequest)
320 Z_APDU *apdu = create_Z_PDU(Z_APDU_searchResponse);
324 void IR_Assoc::recv_presentRequest(Z_PresentRequest *presentRequest)
326 Z_APDU *apdu = create_Z_PDU(Z_APDU_presentResponse);
330 void IR_Assoc::recv_initRequest(Z_InitRequest *initRequest)
332 Z_APDU *apdu = create_Z_PDU(Z_APDU_initResponse);
336 void IR_Assoc::recv_searchResponse (Z_SearchResponse *searchResponse)
340 void IR_Assoc::recv_presentResponse (Z_PresentResponse *presentResponse)
344 void IR_Assoc::recv_initResponse(Z_InitResponse *initResponse)
348 void IR_Assoc::recv_extendedServicesResponse(Z_ExtendedServicesResponse *ExtendedServicesResponse)
352 int IR_Assoc::get_lastReceived()
354 return m_lastReceived;
357 void IR_Assoc::set_lastReceived(int lastReceived)
359 m_lastReceived = lastReceived;
362 int IR_Assoc::send_initRequest(char* pRefId)
364 Z_APDU *apdu = create_Z_PDU(Z_APDU_initRequest);
365 Z_InitRequest *req = apdu->u.initRequest;
367 ODR_MASK_SET(req->options, Z_Options_search);
368 ODR_MASK_SET(req->options, Z_Options_present);
369 ODR_MASK_SET(req->options, Z_Options_namedResultSets);
370 ODR_MASK_SET(req->options, Z_Options_triggerResourceCtrl);
371 ODR_MASK_SET(req->options, Z_Options_scan);
372 ODR_MASK_SET(req->options, Z_Options_sort);
373 ODR_MASK_SET(req->options, Z_Options_extendedServices);
374 ODR_MASK_SET(req->options, Z_Options_delSet);
376 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_1);
377 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_2);
378 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_3);
382 req->referenceId = getRefID(pRefId);
385 if (m_proxy && m_host)
386 set_otherInformationString(&req->otherInfo, yaz_oid_userinfo_proxy,
389 set_otherInformationString(&req->otherInfo, yaz_oid_userinfo_cookie,
391 return send_Z_PDU(apdu, 0);
394 int IR_Assoc::send_deleteResultSetRequest(char* pResultSetId, char* pRefId)
396 char* ResultSetIds[1];
398 Z_APDU *apdu = create_Z_PDU(Z_APDU_deleteResultSetRequest);
399 Z_DeleteResultSetRequest *req = apdu->u.deleteResultSetRequest;
403 *req->deleteFunction = Z_DeleteResultSetRequest_list;
404 req->num_resultSetList = 1;
405 ResultSetIds[0] = pResultSetId;
406 req->resultSetList = ResultSetIds;
410 *req->deleteFunction = Z_DeleteResultSetRequest_all;
415 req->referenceId = getRefID(pRefId);
418 if (m_proxy && m_host)
419 set_otherInformationString(&req->otherInfo, yaz_oid_userinfo_proxy,
422 set_otherInformationString(&req->otherInfo, yaz_oid_userinfo_cookie,
425 return send_Z_PDU(apdu, 0);
432 * c-file-style: "Stroustrup"
433 * indent-tabs-mode: nil
435 * vim: shiftwidth=4 tabstop=8 expandtab