2 * Copyright (c) 1998-2003, Index Data.
3 * See the file LICENSE for details.
5 * $Id: yaz-ir-assoc.cpp,v 1.22 2004-11-30 21:10:30 adam Exp $
11 #include <yaz++/ir-assoc.h>
13 Yaz_IR_Assoc::Yaz_IR_Assoc(IYaz_PDU_Observable *the_PDU_Observable)
14 : Yaz_Z_Assoc(the_PDU_Observable)
16 m_num_databaseNames = 0;
18 m_preferredRecordSyntax = VAL_NONE;
19 m_elementSetNames = 0;
25 const char *db = "Default";
26 set_databaseNames(1, &db);
29 Yaz_IR_Assoc::~Yaz_IR_Assoc()
31 if (m_elementSetNames)
32 delete [] m_elementSetNames->u.generic;
33 delete [] m_elementSetNames;
39 void Yaz_IR_Assoc::get_databaseNames (int *num, char ***list)
41 *num = m_num_databaseNames;
42 *list = m_databaseNames;
46 void Yaz_IR_Assoc::set_databaseNames (int num, const char **list)
49 yaz_log (m_log, "Yaz_IR_Assoc::set_databaseNames num=%d", num);
50 for (i = 0; i<m_num_databaseNames; i++)
51 delete [] m_databaseNames[i];
52 delete [] m_databaseNames;
53 m_num_databaseNames = num;
55 m_databaseNames = new char *[num];
56 for (i = 0; i<m_num_databaseNames; i++)
58 m_databaseNames[i] = new char[strlen(list[i])+1];
59 strcpy(m_databaseNames[i], list[i]);
63 void Yaz_IR_Assoc::set_databaseNames(const char *dblist, const char *sep)
65 const char **list = new const char* [strlen(dblist)];
66 char *dbtmp = new char[strlen(dblist)+1];
67 strcpy(dbtmp, dblist);
70 for (char *cp = dbtmp; ; cp++)
71 if (*cp && !strchr(sep, *cp))
85 set_databaseNames (num, list);
90 void Yaz_IR_Assoc::set_preferredRecordSyntax (int value)
92 m_preferredRecordSyntax = value;
95 void Yaz_IR_Assoc::set_preferredRecordSyntax (const char *syntax)
97 m_preferredRecordSyntax = VAL_NONE;
98 if (syntax && *syntax)
99 m_preferredRecordSyntax = oid_getvalbyname (syntax);
102 void Yaz_IR_Assoc::get_preferredRecordSyntax (int *value)
104 *value = m_preferredRecordSyntax;
107 void Yaz_IR_Assoc::get_preferredRecordSyntax (const char **dst)
110 ent.proto = PROTO_Z3950;
111 ent.oclass = CLASS_RECSYN;
112 ent.value = (enum oid_value) m_preferredRecordSyntax;
115 oid_ent_to_oid (&ent, oid);
116 struct oident *entp = oid_getentbyoid (oid);
118 *dst = entp ? entp->desc : "";
121 void Yaz_IR_Assoc::set_elementSetName (const char *elementSetName)
123 if (m_elementSetNames)
124 delete [] m_elementSetNames->u.generic;
125 delete m_elementSetNames;
126 m_elementSetNames = 0;
127 if (elementSetName && *elementSetName)
129 m_elementSetNames = new Z_ElementSetNames;
130 m_elementSetNames->which = Z_ElementSetNames_generic;
131 m_elementSetNames->u.generic = new char[strlen(elementSetName)+1];
132 strcpy (m_elementSetNames->u.generic, elementSetName);
136 void Yaz_IR_Assoc::get_elementSetName (Z_ElementSetNames **elementSetNames)
138 *elementSetNames = m_elementSetNames;
141 void Yaz_IR_Assoc::get_elementSetName (const char **elementSetName)
143 if (!m_elementSetNames ||
144 m_elementSetNames->which != Z_ElementSetNames_generic)
149 *elementSetName = m_elementSetNames->u.generic;
153 void Yaz_IR_Assoc::recv_GDU(Z_GDU *apdu, int len)
155 if (apdu->which == Z_GDU_Z3950)
156 recv_Z_PDU(apdu->u.z3950, len);
159 void Yaz_IR_Assoc::recv_Z_PDU(Z_APDU *apdu, int len)
161 yaz_log (m_log, "recv_Z_PDU %d bytes", len);
162 m_lastReceived = apdu->which;
165 case Z_APDU_initResponse:
166 yaz_log (m_log, "recv InitResponse");
167 recv_initResponse(apdu->u.initResponse);
169 case Z_APDU_initRequest:
170 yaz_log (m_log, "recv InitRequest");
171 recv_initRequest(apdu->u.initRequest);
173 case Z_APDU_searchRequest:
174 yaz_log (m_log, "recv searchRequest");
175 recv_searchRequest(apdu->u.searchRequest);
177 case Z_APDU_searchResponse:
178 yaz_log (m_log, "recv searchResponse");
179 recv_searchResponse(apdu->u.searchResponse);
181 case Z_APDU_presentRequest:
182 yaz_log (m_log, "recv presentRequest");
183 recv_presentRequest(apdu->u.presentRequest);
185 case Z_APDU_presentResponse:
186 yaz_log (m_log, "recv presentResponse");
187 recv_presentResponse(apdu->u.presentResponse);
189 case Z_APDU_extendedServicesResponse:
190 yaz_log (m_log, "recv extendedServiceResponse");
191 recv_extendedServicesResponse(apdu->u.extendedServicesResponse);
196 int Yaz_IR_Assoc::send_searchRequest(Yaz_Z_Query *query,
200 Z_APDU *apdu = create_Z_PDU(Z_APDU_searchRequest);
201 Z_SearchRequest *req = apdu->u.searchRequest;
204 req->query = query->get_Z_Query();
207 get_databaseNames (&req->num_databaseNames, &req->databaseNames);
208 int oid_syntax[OID_SIZE];
210 get_preferredRecordSyntax(&recordSyntax);
211 if (recordSyntax != VAL_NONE)
213 prefsyn.proto = PROTO_Z3950;
214 prefsyn.oclass = CLASS_RECSYN;
215 prefsyn.value = (enum oid_value) recordSyntax;
216 oid_ent_to_oid(&prefsyn, oid_syntax);
217 req->preferredRecordSyntax = oid_syntax;
219 yaz_log (m_log, "send_searchRequest");
220 assert (req->otherInfo == 0);
223 set_otherInformationString(&req->otherInfo, VAL_COOKIE, 1, m_cookie);
224 assert (req->otherInfo);
229 req->referenceId = getRefID(pRefId);
234 req->resultSetName = pResultSetId;
237 return send_Z_PDU(apdu, 0);
240 int Yaz_IR_Assoc::send_presentRequest(int start,
245 Z_APDU *apdu = create_Z_PDU(Z_APDU_presentRequest);
246 Z_PresentRequest *req = apdu->u.presentRequest;
248 req->resultSetStartPoint = &start;
249 req->numberOfRecordsRequested = &number;
251 int oid_syntax[OID_SIZE];
254 get_preferredRecordSyntax (&recordSyntax);
255 if (recordSyntax != VAL_NONE)
257 prefsyn.proto = PROTO_Z3950;
258 prefsyn.oclass = CLASS_RECSYN;
259 prefsyn.value = (enum oid_value) recordSyntax;
260 oid_ent_to_oid(&prefsyn, oid_syntax);
261 req->preferredRecordSyntax = oid_syntax;
263 Z_RecordComposition compo;
264 Z_ElementSetNames *elementSetNames;
265 get_elementSetName (&elementSetNames);
268 req->recordComposition = &compo;
269 compo.which = Z_RecordComp_simple;
270 compo.u.simple = elementSetNames;
274 set_otherInformationString(&req->otherInfo, VAL_COOKIE, 1, m_cookie);
278 req->referenceId = getRefID(pRefId);
283 req->resultSetId = pResultSetId;
286 return send_Z_PDU(apdu, 0);
289 void Yaz_IR_Assoc::set_proxy(const char *str)
295 m_proxy = new char[strlen(str)+1];
296 strcpy (m_proxy, str);
300 void Yaz_IR_Assoc::set_cookie(const char *str)
306 m_cookie = new char[strlen(str)+1];
307 strcpy(m_cookie, str);
311 const char *Yaz_IR_Assoc::get_cookie()
316 void Yaz_IR_Assoc::client(const char *addr)
319 m_host = new char[strlen(addr)+1];
320 strcpy(m_host, addr);
321 const char *dbpart = strchr(m_host, '/');
323 set_databaseNames (dbpart+1, "+ ");
324 Yaz_Z_Assoc::client(m_proxy ? m_proxy : m_host);
327 const char *Yaz_IR_Assoc::get_proxy()
332 const char *Yaz_IR_Assoc::get_host()
337 void Yaz_IR_Assoc::recv_searchRequest(Z_SearchRequest *searchRequest)
339 Z_APDU *apdu = create_Z_PDU(Z_APDU_searchResponse);
343 void Yaz_IR_Assoc::recv_presentRequest(Z_PresentRequest *presentRequest)
345 Z_APDU *apdu = create_Z_PDU(Z_APDU_presentResponse);
349 void Yaz_IR_Assoc::recv_initRequest(Z_InitRequest *initRequest)
351 Z_APDU *apdu = create_Z_PDU(Z_APDU_initResponse);
355 void Yaz_IR_Assoc::recv_searchResponse (Z_SearchResponse *searchResponse)
359 void Yaz_IR_Assoc::recv_presentResponse (Z_PresentResponse *presentResponse)
363 void Yaz_IR_Assoc::recv_initResponse(Z_InitResponse *initResponse)
367 void Yaz_IR_Assoc::recv_extendedServicesResponse(Z_ExtendedServicesResponse *ExtendedServicesResponse)
371 int Yaz_IR_Assoc::get_lastReceived()
373 return m_lastReceived;
376 void Yaz_IR_Assoc::set_lastReceived(int lastReceived)
378 m_lastReceived = lastReceived;
381 int Yaz_IR_Assoc::send_initRequest(char* pRefId)
383 Z_APDU *apdu = create_Z_PDU(Z_APDU_initRequest);
384 Z_InitRequest *req = apdu->u.initRequest;
386 ODR_MASK_SET(req->options, Z_Options_search);
387 ODR_MASK_SET(req->options, Z_Options_present);
388 ODR_MASK_SET(req->options, Z_Options_namedResultSets);
389 ODR_MASK_SET(req->options, Z_Options_triggerResourceCtrl);
390 ODR_MASK_SET(req->options, Z_Options_scan);
391 ODR_MASK_SET(req->options, Z_Options_sort);
392 ODR_MASK_SET(req->options, Z_Options_extendedServices);
393 ODR_MASK_SET(req->options, Z_Options_delSet);
395 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_1);
396 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_2);
397 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_3);
401 req->referenceId = getRefID(pRefId);
404 if (m_proxy && m_host)
405 set_otherInformationString(&req->otherInfo, VAL_PROXY, 1, m_host);
407 set_otherInformationString(&req->otherInfo, VAL_COOKIE, 1, m_cookie);
408 return send_Z_PDU(apdu, 0);
411 int Yaz_IR_Assoc::send_deleteResultSetRequest(char* pResultSetId, char* pRefId)
413 char* ResultSetIds[1];
415 Z_APDU *apdu = create_Z_PDU(Z_APDU_deleteResultSetRequest);
416 Z_DeleteResultSetRequest *req = apdu->u.deleteResultSetRequest;
420 *req->deleteFunction = Z_DeleteResultSetRequest_list;
421 req->num_resultSetList = 1;
422 ResultSetIds[0] = pResultSetId;
423 req->resultSetList = ResultSetIds;
427 *req->deleteFunction = Z_DeleteResultSetRequest_all;
432 req->referenceId = getRefID(pRefId);
435 if (m_proxy && m_host)
436 set_otherInformationString(&req->otherInfo, VAL_PROXY, 1, m_host);
438 set_otherInformationString(&req->otherInfo, VAL_COOKIE, 1, m_cookie);
440 return send_Z_PDU(apdu, 0);