2 * Copyright (c) 1998-2003, Index Data.
3 * See the file LICENSE for details.
5 * $Id: yaz-ir-assoc.cpp,v 1.28 2007-04-12 15:00:33 adam Exp $
11 #include <yazpp/ir-assoc.h>
12 #include <yaz/oid_db.h>
14 using namespace yazpp_1;
16 IR_Assoc::IR_Assoc(IPDU_Observable *the_PDU_Observable)
17 : Z_Assoc(the_PDU_Observable)
19 m_num_databaseNames = 0;
21 m_preferredRecordSyntax = 0;
22 m_elementSetNames = 0;
28 const char *db = "Default";
29 set_databaseNames(1, &db);
34 xfree(m_preferredRecordSyntax);
35 if (m_elementSetNames)
36 delete [] m_elementSetNames->u.generic;
37 delete [] m_elementSetNames;
43 void IR_Assoc::get_databaseNames (int *num, char ***list)
45 *num = m_num_databaseNames;
46 *list = m_databaseNames;
50 void IR_Assoc::set_databaseNames (int num, const char **list)
53 yaz_log (m_log, "IR_Assoc::set_databaseNames num=%d", num);
54 for (i = 0; i<m_num_databaseNames; i++)
55 delete [] m_databaseNames[i];
56 delete [] m_databaseNames;
57 m_num_databaseNames = num;
59 m_databaseNames = new char *[num];
60 for (i = 0; i<m_num_databaseNames; i++)
62 m_databaseNames[i] = new char[strlen(list[i])+1];
63 strcpy(m_databaseNames[i], list[i]);
67 void IR_Assoc::set_databaseNames(const char *dblist, const char *sep)
69 const char **list = new const char* [strlen(dblist)];
70 char *dbtmp = new char[strlen(dblist)+1];
71 strcpy(dbtmp, dblist);
74 for (char *cp = dbtmp; ; cp++)
75 if (*cp && !strchr(sep, *cp))
89 set_databaseNames (num, list);
94 void IR_Assoc::set_preferredRecordSyntax (const char *syntax)
96 xfree(m_preferredRecordSyntax);
97 m_preferredRecordSyntax = 0;
98 if (syntax && *syntax)
99 m_preferredRecordSyntax = xstrdup(syntax);
102 void IR_Assoc::get_preferredRecordSyntax (const char **dst)
104 if (m_preferredRecordSyntax)
105 *dst = m_preferredRecordSyntax;
110 void IR_Assoc::set_elementSetName (const char *elementSetName)
112 if (m_elementSetNames)
113 delete [] m_elementSetNames->u.generic;
114 delete m_elementSetNames;
115 m_elementSetNames = 0;
116 if (elementSetName && *elementSetName)
118 m_elementSetNames = new Z_ElementSetNames;
119 m_elementSetNames->which = Z_ElementSetNames_generic;
120 m_elementSetNames->u.generic = new char[strlen(elementSetName)+1];
121 strcpy (m_elementSetNames->u.generic, elementSetName);
125 void IR_Assoc::get_elementSetName (Z_ElementSetNames **elementSetNames)
127 *elementSetNames = m_elementSetNames;
130 void IR_Assoc::get_elementSetName (const char **elementSetName)
132 if (!m_elementSetNames ||
133 m_elementSetNames->which != Z_ElementSetNames_generic)
138 *elementSetName = m_elementSetNames->u.generic;
142 void IR_Assoc::recv_GDU(Z_GDU *apdu, int len)
144 if (apdu->which == Z_GDU_Z3950)
145 recv_Z_PDU(apdu->u.z3950, len);
148 void IR_Assoc::recv_Z_PDU(Z_APDU *apdu, int len)
150 yaz_log (m_log, "recv_Z_PDU %d bytes", len);
151 m_lastReceived = apdu->which;
154 case Z_APDU_initResponse:
155 yaz_log (m_log, "recv InitResponse");
156 recv_initResponse(apdu->u.initResponse);
158 case Z_APDU_initRequest:
159 yaz_log (m_log, "recv InitRequest");
160 recv_initRequest(apdu->u.initRequest);
162 case Z_APDU_searchRequest:
163 yaz_log (m_log, "recv searchRequest");
164 recv_searchRequest(apdu->u.searchRequest);
166 case Z_APDU_searchResponse:
167 yaz_log (m_log, "recv searchResponse");
168 recv_searchResponse(apdu->u.searchResponse);
170 case Z_APDU_presentRequest:
171 yaz_log (m_log, "recv presentRequest");
172 recv_presentRequest(apdu->u.presentRequest);
174 case Z_APDU_presentResponse:
175 yaz_log (m_log, "recv presentResponse");
176 recv_presentResponse(apdu->u.presentResponse);
178 case Z_APDU_extendedServicesResponse:
179 yaz_log (m_log, "recv extendedServiceResponse");
180 recv_extendedServicesResponse(apdu->u.extendedServicesResponse);
185 int IR_Assoc::send_searchRequest(Yaz_Z_Query *query,
189 Z_APDU *apdu = create_Z_PDU(Z_APDU_searchRequest);
190 Z_SearchRequest *req = apdu->u.searchRequest;
192 req->query = query->get_Z_Query();
195 get_databaseNames (&req->num_databaseNames, &req->databaseNames);
196 const char *recordSyntax;
197 get_preferredRecordSyntax(&recordSyntax);
198 if (recordSyntax && *recordSyntax)
200 req->preferredRecordSyntax
201 = yaz_string_to_oid_odr(yaz_oid_std(), CLASS_RECSYN, recordSyntax,
204 yaz_log (m_log, "send_searchRequest");
205 assert (req->otherInfo == 0);
208 set_otherInformationString(&req->otherInfo, OID_STR_COOKIE, 1, m_cookie);
209 assert (req->otherInfo);
214 req->referenceId = getRefID(pRefId);
219 req->resultSetName = pResultSetId;
222 return send_Z_PDU(apdu, 0);
225 int IR_Assoc::send_presentRequest(int start,
230 Z_APDU *apdu = create_Z_PDU(Z_APDU_presentRequest);
231 Z_PresentRequest *req = apdu->u.presentRequest;
233 req->resultSetStartPoint = &start;
234 req->numberOfRecordsRequested = &number;
236 const char *recordSyntax;
237 get_preferredRecordSyntax (&recordSyntax);
238 if (recordSyntax && *recordSyntax)
240 req->preferredRecordSyntax =
241 yaz_string_to_oid_odr(yaz_oid_std(), CLASS_RECSYN, recordSyntax,
244 Z_RecordComposition compo;
245 Z_ElementSetNames *elementSetNames;
246 get_elementSetName (&elementSetNames);
249 req->recordComposition = &compo;
250 compo.which = Z_RecordComp_simple;
251 compo.u.simple = elementSetNames;
255 set_otherInformationString(&req->otherInfo, OID_STR_COOKIE,
260 req->referenceId = getRefID(pRefId);
265 req->resultSetId = pResultSetId;
268 return send_Z_PDU(apdu, 0);
271 void IR_Assoc::set_proxy(const char *str)
277 m_proxy = new char[strlen(str)+1];
278 strcpy (m_proxy, str);
282 void IR_Assoc::set_cookie(const char *str)
288 m_cookie = new char[strlen(str)+1];
289 strcpy(m_cookie, str);
293 const char *IR_Assoc::get_cookie()
298 void IR_Assoc::client(const char *addr)
301 m_host = new char[strlen(addr)+1];
302 strcpy(m_host, addr);
303 const char *dbpart = strchr(m_host, '/');
305 set_databaseNames (dbpart+1, "+ ");
306 Z_Assoc::client(m_proxy ? m_proxy : m_host);
309 const char *IR_Assoc::get_proxy()
314 const char *IR_Assoc::get_host()
319 void IR_Assoc::recv_searchRequest(Z_SearchRequest *searchRequest)
321 Z_APDU *apdu = create_Z_PDU(Z_APDU_searchResponse);
325 void IR_Assoc::recv_presentRequest(Z_PresentRequest *presentRequest)
327 Z_APDU *apdu = create_Z_PDU(Z_APDU_presentResponse);
331 void IR_Assoc::recv_initRequest(Z_InitRequest *initRequest)
333 Z_APDU *apdu = create_Z_PDU(Z_APDU_initResponse);
337 void IR_Assoc::recv_searchResponse (Z_SearchResponse *searchResponse)
341 void IR_Assoc::recv_presentResponse (Z_PresentResponse *presentResponse)
345 void IR_Assoc::recv_initResponse(Z_InitResponse *initResponse)
349 void IR_Assoc::recv_extendedServicesResponse(Z_ExtendedServicesResponse *ExtendedServicesResponse)
353 int IR_Assoc::get_lastReceived()
355 return m_lastReceived;
358 void IR_Assoc::set_lastReceived(int lastReceived)
360 m_lastReceived = lastReceived;
363 int IR_Assoc::send_initRequest(char* pRefId)
365 Z_APDU *apdu = create_Z_PDU(Z_APDU_initRequest);
366 Z_InitRequest *req = apdu->u.initRequest;
368 ODR_MASK_SET(req->options, Z_Options_search);
369 ODR_MASK_SET(req->options, Z_Options_present);
370 ODR_MASK_SET(req->options, Z_Options_namedResultSets);
371 ODR_MASK_SET(req->options, Z_Options_triggerResourceCtrl);
372 ODR_MASK_SET(req->options, Z_Options_scan);
373 ODR_MASK_SET(req->options, Z_Options_sort);
374 ODR_MASK_SET(req->options, Z_Options_extendedServices);
375 ODR_MASK_SET(req->options, Z_Options_delSet);
377 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_1);
378 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_2);
379 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_3);
383 req->referenceId = getRefID(pRefId);
386 if (m_proxy && m_host)
387 set_otherInformationString(&req->otherInfo, OID_STR_PROXY, 1, m_host);
389 set_otherInformationString(&req->otherInfo, OID_STR_COOKIE, 1, m_cookie);
390 return send_Z_PDU(apdu, 0);
393 int IR_Assoc::send_deleteResultSetRequest(char* pResultSetId, char* pRefId)
395 char* ResultSetIds[1];
397 Z_APDU *apdu = create_Z_PDU(Z_APDU_deleteResultSetRequest);
398 Z_DeleteResultSetRequest *req = apdu->u.deleteResultSetRequest;
402 *req->deleteFunction = Z_DeleteResultSetRequest_list;
403 req->num_resultSetList = 1;
404 ResultSetIds[0] = pResultSetId;
405 req->resultSetList = ResultSetIds;
409 *req->deleteFunction = Z_DeleteResultSetRequest_all;
414 req->referenceId = getRefID(pRefId);
417 if (m_proxy && m_host)
418 set_otherInformationString(&req->otherInfo, OID_STR_PROXY, 1, m_host);
420 set_otherInformationString(&req->otherInfo, OID_STR_COOKIE, 1, m_cookie);
422 return send_Z_PDU(apdu, 0);
429 * indent-tabs-mode: nil
431 * vim: shiftwidth=4 tabstop=8 expandtab