2 * Copyright (c) 1998-2000, Index Data.
3 * See the file LICENSE for details.
5 * $Id: yaz-ir-assoc.cpp,v 1.18 2002-10-09 12:50:26 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;
152 void Yaz_IR_Assoc::recv_Z_PDU(Z_APDU *apdu)
154 yaz_log (m_log, "recv_Z_PDU");
155 m_lastReceived = apdu->which;
158 case Z_APDU_initResponse:
159 yaz_log (m_log, "recv InitResponse");
160 recv_initResponse(apdu->u.initResponse);
162 case Z_APDU_initRequest:
163 yaz_log (m_log, "recv InitRequest");
164 recv_initRequest(apdu->u.initRequest);
166 case Z_APDU_searchRequest:
167 yaz_log (m_log, "recv searchRequest");
168 recv_searchRequest(apdu->u.searchRequest);
170 case Z_APDU_searchResponse:
171 yaz_log (m_log, "recv searchResponse");
172 recv_searchResponse(apdu->u.searchResponse);
174 case Z_APDU_presentRequest:
175 yaz_log (m_log, "recv presentRequest");
176 recv_presentRequest(apdu->u.presentRequest);
178 case Z_APDU_presentResponse:
179 yaz_log (m_log, "recv presentResponse");
180 recv_presentResponse(apdu->u.presentResponse);
182 case Z_APDU_extendedServicesResponse:
183 yaz_log (m_log, "recv extendedServiceResponse");
184 recv_extendedServicesResponse(apdu->u.extendedServicesResponse);
189 int Yaz_IR_Assoc::send_searchRequest(Yaz_Z_Query *query,
193 Z_APDU *apdu = create_Z_PDU(Z_APDU_searchRequest);
194 Z_SearchRequest *req = apdu->u.searchRequest;
197 req->query = query->get_Z_Query();
200 get_databaseNames (&req->num_databaseNames, &req->databaseNames);
201 int oid_syntax[OID_SIZE];
203 get_preferredRecordSyntax(&recordSyntax);
204 if (recordSyntax != VAL_NONE)
206 prefsyn.proto = PROTO_Z3950;
207 prefsyn.oclass = CLASS_RECSYN;
208 prefsyn.value = (enum oid_value) recordSyntax;
209 oid_ent_to_oid(&prefsyn, oid_syntax);
210 req->preferredRecordSyntax = oid_syntax;
212 yaz_log (m_log, "send_searchRequest");
213 assert (req->otherInfo == 0);
216 set_otherInformationString(&req->otherInfo, VAL_COOKIE, 1, m_cookie);
217 assert (req->otherInfo);
222 req->referenceId = getRefID(pRefId);
227 req->resultSetName = pResultSetId;
230 return send_Z_PDU(apdu);
233 int Yaz_IR_Assoc::send_presentRequest(int start,
238 Z_APDU *apdu = create_Z_PDU(Z_APDU_presentRequest);
239 Z_PresentRequest *req = apdu->u.presentRequest;
241 req->resultSetStartPoint = &start;
242 req->numberOfRecordsRequested = &number;
244 int oid_syntax[OID_SIZE];
247 get_preferredRecordSyntax (&recordSyntax);
248 if (recordSyntax != VAL_NONE)
250 prefsyn.proto = PROTO_Z3950;
251 prefsyn.oclass = CLASS_RECSYN;
252 prefsyn.value = (enum oid_value) recordSyntax;
253 oid_ent_to_oid(&prefsyn, oid_syntax);
254 req->preferredRecordSyntax = oid_syntax;
256 Z_RecordComposition compo;
257 Z_ElementSetNames *elementSetNames;
258 get_elementSetName (&elementSetNames);
261 req->recordComposition = &compo;
262 compo.which = Z_RecordComp_simple;
263 compo.u.simple = elementSetNames;
267 set_otherInformationString(&req->otherInfo, VAL_COOKIE, 1, m_cookie);
271 req->referenceId = getRefID(pRefId);
276 req->resultSetId = pResultSetId;
279 return send_Z_PDU(apdu);
282 void Yaz_IR_Assoc::set_proxy(const char *str)
288 m_proxy = new char[strlen(str)+1];
289 strcpy (m_proxy, str);
293 void Yaz_IR_Assoc::set_cookie(const char *str)
299 m_cookie = new char[strlen(str)+1];
300 strcpy(m_cookie, str);
304 const char *Yaz_IR_Assoc::get_cookie()
309 void Yaz_IR_Assoc::client(const char *addr)
312 m_host = new char[strlen(addr)+1];
313 strcpy(m_host, addr);
314 const char *dbpart = strchr(m_host, '/');
316 set_databaseNames (dbpart+1, "+ ");
317 Yaz_Z_Assoc::client(m_proxy ? m_proxy : m_host);
320 const char *Yaz_IR_Assoc::get_proxy()
325 const char *Yaz_IR_Assoc::get_host()
330 void Yaz_IR_Assoc::recv_searchRequest(Z_SearchRequest *searchRequest)
332 Z_APDU *apdu = create_Z_PDU(Z_APDU_searchResponse);
336 void Yaz_IR_Assoc::recv_presentRequest(Z_PresentRequest *presentRequest)
338 Z_APDU *apdu = create_Z_PDU(Z_APDU_presentResponse);
342 void Yaz_IR_Assoc::recv_initRequest(Z_InitRequest *initRequest)
344 Z_APDU *apdu = create_Z_PDU(Z_APDU_initResponse);
348 void Yaz_IR_Assoc::recv_searchResponse (Z_SearchResponse *searchResponse)
352 void Yaz_IR_Assoc::recv_presentResponse (Z_PresentResponse *presentResponse)
356 void Yaz_IR_Assoc::recv_initResponse(Z_InitResponse *initResponse)
360 void Yaz_IR_Assoc::recv_extendedServicesResponse(Z_ExtendedServicesResponse *ExtendedServicesResponse)
364 int Yaz_IR_Assoc::get_lastReceived()
366 return m_lastReceived;
369 void Yaz_IR_Assoc::set_lastReceived(int lastReceived)
371 m_lastReceived = lastReceived;
374 int Yaz_IR_Assoc::send_initRequest(char* pRefId)
376 Z_APDU *apdu = create_Z_PDU(Z_APDU_initRequest);
377 Z_InitRequest *req = apdu->u.initRequest;
379 ODR_MASK_SET(req->options, Z_Options_search);
380 ODR_MASK_SET(req->options, Z_Options_present);
381 ODR_MASK_SET(req->options, Z_Options_namedResultSets);
382 ODR_MASK_SET(req->options, Z_Options_triggerResourceCtrl);
383 ODR_MASK_SET(req->options, Z_Options_scan);
384 ODR_MASK_SET(req->options, Z_Options_sort);
385 ODR_MASK_SET(req->options, Z_Options_extendedServices);
386 ODR_MASK_SET(req->options, Z_Options_delSet);
388 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_1);
389 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_2);
390 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_3);
394 req->referenceId = getRefID(pRefId);
397 if (m_proxy && m_host)
398 set_otherInformationString(&req->otherInfo, VAL_PROXY, 1, m_host);
400 set_otherInformationString(&req->otherInfo, VAL_COOKIE, 1, m_cookie);
401 return send_Z_PDU(apdu);
404 int Yaz_IR_Assoc::send_deleteResultSetRequest(char* pResultSetId, char* pRefId)
406 char* ResultSetIds[1];
408 Z_APDU *apdu = create_Z_PDU(Z_APDU_deleteResultSetRequest);
409 Z_DeleteResultSetRequest *req = apdu->u.deleteResultSetRequest;
413 *req->deleteFunction = Z_DeleteResultSetRequest_list;
414 req->num_resultSetList = 1;
415 ResultSetIds[0] = pResultSetId;
416 req->resultSetList = ResultSetIds;
420 *req->deleteFunction = Z_DeleteResultSetRequest_all;
425 req->referenceId = getRefID(pRefId);
428 if (m_proxy && m_host)
429 set_otherInformationString(&req->otherInfo, VAL_PROXY, 1, m_host);
431 set_otherInformationString(&req->otherInfo, VAL_COOKIE, 1, m_cookie);
434 return send_Z_PDU(apdu);