2 * Copyright (c) 1998-2003, Index Data.
3 * See the file LICENSE for details.
5 * $Id: yaz-ir-assoc.cpp,v 1.27 2006-03-29 13:14:15 adam Exp $
11 #include <yazpp/ir-assoc.h>
13 using namespace yazpp_1;
15 IR_Assoc::IR_Assoc(IPDU_Observable *the_PDU_Observable)
16 : Z_Assoc(the_PDU_Observable)
18 m_num_databaseNames = 0;
20 m_preferredRecordSyntax = VAL_NONE;
21 m_elementSetNames = 0;
27 const char *db = "Default";
28 set_databaseNames(1, &db);
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 (int value)
94 m_preferredRecordSyntax = value;
97 void IR_Assoc::set_preferredRecordSyntax (const char *syntax)
99 m_preferredRecordSyntax = VAL_NONE;
100 if (syntax && *syntax)
101 m_preferredRecordSyntax = oid_getvalbyname (syntax);
104 void IR_Assoc::get_preferredRecordSyntax (int *value)
106 *value = m_preferredRecordSyntax;
109 void IR_Assoc::get_preferredRecordSyntax (const char **dst)
112 ent.proto = PROTO_Z3950;
113 ent.oclass = CLASS_RECSYN;
114 ent.value = (enum oid_value) m_preferredRecordSyntax;
117 oid_ent_to_oid (&ent, oid);
118 struct oident *entp = oid_getentbyoid (oid);
120 *dst = entp ? entp->desc : "";
123 void IR_Assoc::set_elementSetName (const char *elementSetName)
125 if (m_elementSetNames)
126 delete [] m_elementSetNames->u.generic;
127 delete m_elementSetNames;
128 m_elementSetNames = 0;
129 if (elementSetName && *elementSetName)
131 m_elementSetNames = new Z_ElementSetNames;
132 m_elementSetNames->which = Z_ElementSetNames_generic;
133 m_elementSetNames->u.generic = new char[strlen(elementSetName)+1];
134 strcpy (m_elementSetNames->u.generic, elementSetName);
138 void IR_Assoc::get_elementSetName (Z_ElementSetNames **elementSetNames)
140 *elementSetNames = m_elementSetNames;
143 void IR_Assoc::get_elementSetName (const char **elementSetName)
145 if (!m_elementSetNames ||
146 m_elementSetNames->which != Z_ElementSetNames_generic)
151 *elementSetName = m_elementSetNames->u.generic;
155 void IR_Assoc::recv_GDU(Z_GDU *apdu, int len)
157 if (apdu->which == Z_GDU_Z3950)
158 recv_Z_PDU(apdu->u.z3950, len);
161 void IR_Assoc::recv_Z_PDU(Z_APDU *apdu, int len)
163 yaz_log (m_log, "recv_Z_PDU %d bytes", len);
164 m_lastReceived = apdu->which;
167 case Z_APDU_initResponse:
168 yaz_log (m_log, "recv InitResponse");
169 recv_initResponse(apdu->u.initResponse);
171 case Z_APDU_initRequest:
172 yaz_log (m_log, "recv InitRequest");
173 recv_initRequest(apdu->u.initRequest);
175 case Z_APDU_searchRequest:
176 yaz_log (m_log, "recv searchRequest");
177 recv_searchRequest(apdu->u.searchRequest);
179 case Z_APDU_searchResponse:
180 yaz_log (m_log, "recv searchResponse");
181 recv_searchResponse(apdu->u.searchResponse);
183 case Z_APDU_presentRequest:
184 yaz_log (m_log, "recv presentRequest");
185 recv_presentRequest(apdu->u.presentRequest);
187 case Z_APDU_presentResponse:
188 yaz_log (m_log, "recv presentResponse");
189 recv_presentResponse(apdu->u.presentResponse);
191 case Z_APDU_extendedServicesResponse:
192 yaz_log (m_log, "recv extendedServiceResponse");
193 recv_extendedServicesResponse(apdu->u.extendedServicesResponse);
198 int IR_Assoc::send_searchRequest(Yaz_Z_Query *query,
202 Z_APDU *apdu = create_Z_PDU(Z_APDU_searchRequest);
203 Z_SearchRequest *req = apdu->u.searchRequest;
206 req->query = query->get_Z_Query();
209 get_databaseNames (&req->num_databaseNames, &req->databaseNames);
210 int oid_syntax[OID_SIZE];
212 get_preferredRecordSyntax(&recordSyntax);
213 if (recordSyntax != VAL_NONE)
215 prefsyn.proto = PROTO_Z3950;
216 prefsyn.oclass = CLASS_RECSYN;
217 prefsyn.value = (enum oid_value) recordSyntax;
218 oid_ent_to_oid(&prefsyn, oid_syntax);
219 req->preferredRecordSyntax = oid_syntax;
221 yaz_log (m_log, "send_searchRequest");
222 assert (req->otherInfo == 0);
225 set_otherInformationString(&req->otherInfo, VAL_COOKIE, 1, m_cookie);
226 assert (req->otherInfo);
231 req->referenceId = getRefID(pRefId);
236 req->resultSetName = pResultSetId;
239 return send_Z_PDU(apdu, 0);
242 int IR_Assoc::send_presentRequest(int start,
247 Z_APDU *apdu = create_Z_PDU(Z_APDU_presentRequest);
248 Z_PresentRequest *req = apdu->u.presentRequest;
250 req->resultSetStartPoint = &start;
251 req->numberOfRecordsRequested = &number;
253 int oid_syntax[OID_SIZE];
256 get_preferredRecordSyntax (&recordSyntax);
257 if (recordSyntax != VAL_NONE)
259 prefsyn.proto = PROTO_Z3950;
260 prefsyn.oclass = CLASS_RECSYN;
261 prefsyn.value = (enum oid_value) recordSyntax;
262 oid_ent_to_oid(&prefsyn, oid_syntax);
263 req->preferredRecordSyntax = oid_syntax;
265 Z_RecordComposition compo;
266 Z_ElementSetNames *elementSetNames;
267 get_elementSetName (&elementSetNames);
270 req->recordComposition = &compo;
271 compo.which = Z_RecordComp_simple;
272 compo.u.simple = elementSetNames;
276 set_otherInformationString(&req->otherInfo, VAL_COOKIE, 1, m_cookie);
280 req->referenceId = getRefID(pRefId);
285 req->resultSetId = pResultSetId;
288 return send_Z_PDU(apdu, 0);
291 void IR_Assoc::set_proxy(const char *str)
297 m_proxy = new char[strlen(str)+1];
298 strcpy (m_proxy, str);
302 void IR_Assoc::set_cookie(const char *str)
308 m_cookie = new char[strlen(str)+1];
309 strcpy(m_cookie, str);
313 const char *IR_Assoc::get_cookie()
318 void IR_Assoc::client(const char *addr)
321 m_host = new char[strlen(addr)+1];
322 strcpy(m_host, addr);
323 const char *dbpart = strchr(m_host, '/');
325 set_databaseNames (dbpart+1, "+ ");
326 Z_Assoc::client(m_proxy ? m_proxy : m_host);
329 const char *IR_Assoc::get_proxy()
334 const char *IR_Assoc::get_host()
339 void IR_Assoc::recv_searchRequest(Z_SearchRequest *searchRequest)
341 Z_APDU *apdu = create_Z_PDU(Z_APDU_searchResponse);
345 void IR_Assoc::recv_presentRequest(Z_PresentRequest *presentRequest)
347 Z_APDU *apdu = create_Z_PDU(Z_APDU_presentResponse);
351 void IR_Assoc::recv_initRequest(Z_InitRequest *initRequest)
353 Z_APDU *apdu = create_Z_PDU(Z_APDU_initResponse);
357 void IR_Assoc::recv_searchResponse (Z_SearchResponse *searchResponse)
361 void IR_Assoc::recv_presentResponse (Z_PresentResponse *presentResponse)
365 void IR_Assoc::recv_initResponse(Z_InitResponse *initResponse)
369 void IR_Assoc::recv_extendedServicesResponse(Z_ExtendedServicesResponse *ExtendedServicesResponse)
373 int IR_Assoc::get_lastReceived()
375 return m_lastReceived;
378 void IR_Assoc::set_lastReceived(int lastReceived)
380 m_lastReceived = lastReceived;
383 int IR_Assoc::send_initRequest(char* pRefId)
385 Z_APDU *apdu = create_Z_PDU(Z_APDU_initRequest);
386 Z_InitRequest *req = apdu->u.initRequest;
388 ODR_MASK_SET(req->options, Z_Options_search);
389 ODR_MASK_SET(req->options, Z_Options_present);
390 ODR_MASK_SET(req->options, Z_Options_namedResultSets);
391 ODR_MASK_SET(req->options, Z_Options_triggerResourceCtrl);
392 ODR_MASK_SET(req->options, Z_Options_scan);
393 ODR_MASK_SET(req->options, Z_Options_sort);
394 ODR_MASK_SET(req->options, Z_Options_extendedServices);
395 ODR_MASK_SET(req->options, Z_Options_delSet);
397 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_1);
398 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_2);
399 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_3);
403 req->referenceId = getRefID(pRefId);
406 if (m_proxy && m_host)
407 set_otherInformationString(&req->otherInfo, VAL_PROXY, 1, m_host);
409 set_otherInformationString(&req->otherInfo, VAL_COOKIE, 1, m_cookie);
410 return send_Z_PDU(apdu, 0);
413 int IR_Assoc::send_deleteResultSetRequest(char* pResultSetId, char* pRefId)
415 char* ResultSetIds[1];
417 Z_APDU *apdu = create_Z_PDU(Z_APDU_deleteResultSetRequest);
418 Z_DeleteResultSetRequest *req = apdu->u.deleteResultSetRequest;
422 *req->deleteFunction = Z_DeleteResultSetRequest_list;
423 req->num_resultSetList = 1;
424 ResultSetIds[0] = pResultSetId;
425 req->resultSetList = ResultSetIds;
429 *req->deleteFunction = Z_DeleteResultSetRequest_all;
434 req->referenceId = getRefID(pRefId);
437 if (m_proxy && m_host)
438 set_otherInformationString(&req->otherInfo, VAL_PROXY, 1, m_host);
440 set_otherInformationString(&req->otherInfo, VAL_COOKIE, 1, m_cookie);
442 return send_Z_PDU(apdu, 0);
449 * indent-tabs-mode: nil
451 * vim: shiftwidth=4 tabstop=8 expandtab