2 * Copyright (c) 1998-2003, Index Data.
3 * See the file LICENSE for details.
5 * $Id: yaz-ir-assoc.cpp,v 1.29 2007-04-16 21:54:23 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, yaz_oid_userinfo_cookie,
210 assert (req->otherInfo);
215 req->referenceId = getRefID(pRefId);
220 req->resultSetName = pResultSetId;
223 return send_Z_PDU(apdu, 0);
226 int IR_Assoc::send_presentRequest(int start,
231 Z_APDU *apdu = create_Z_PDU(Z_APDU_presentRequest);
232 Z_PresentRequest *req = apdu->u.presentRequest;
234 req->resultSetStartPoint = &start;
235 req->numberOfRecordsRequested = &number;
237 const char *recordSyntax;
238 get_preferredRecordSyntax (&recordSyntax);
239 if (recordSyntax && *recordSyntax)
241 req->preferredRecordSyntax =
242 yaz_string_to_oid_odr(yaz_oid_std(), CLASS_RECSYN, recordSyntax,
245 Z_RecordComposition compo;
246 Z_ElementSetNames *elementSetNames;
247 get_elementSetName (&elementSetNames);
250 req->recordComposition = &compo;
251 compo.which = Z_RecordComp_simple;
252 compo.u.simple = elementSetNames;
256 set_otherInformationString(&req->otherInfo, yaz_oid_userinfo_cookie,
261 req->referenceId = getRefID(pRefId);
266 req->resultSetId = pResultSetId;
269 return send_Z_PDU(apdu, 0);
272 void IR_Assoc::set_proxy(const char *str)
278 m_proxy = new char[strlen(str)+1];
279 strcpy (m_proxy, str);
283 void IR_Assoc::set_cookie(const char *str)
289 m_cookie = new char[strlen(str)+1];
290 strcpy(m_cookie, str);
294 const char *IR_Assoc::get_cookie()
299 void IR_Assoc::client(const char *addr)
302 m_host = new char[strlen(addr)+1];
303 strcpy(m_host, addr);
304 const char *dbpart = strchr(m_host, '/');
306 set_databaseNames (dbpart+1, "+ ");
307 Z_Assoc::client(m_proxy ? m_proxy : m_host);
310 const char *IR_Assoc::get_proxy()
315 const char *IR_Assoc::get_host()
320 void IR_Assoc::recv_searchRequest(Z_SearchRequest *searchRequest)
322 Z_APDU *apdu = create_Z_PDU(Z_APDU_searchResponse);
326 void IR_Assoc::recv_presentRequest(Z_PresentRequest *presentRequest)
328 Z_APDU *apdu = create_Z_PDU(Z_APDU_presentResponse);
332 void IR_Assoc::recv_initRequest(Z_InitRequest *initRequest)
334 Z_APDU *apdu = create_Z_PDU(Z_APDU_initResponse);
338 void IR_Assoc::recv_searchResponse (Z_SearchResponse *searchResponse)
342 void IR_Assoc::recv_presentResponse (Z_PresentResponse *presentResponse)
346 void IR_Assoc::recv_initResponse(Z_InitResponse *initResponse)
350 void IR_Assoc::recv_extendedServicesResponse(Z_ExtendedServicesResponse *ExtendedServicesResponse)
354 int IR_Assoc::get_lastReceived()
356 return m_lastReceived;
359 void IR_Assoc::set_lastReceived(int lastReceived)
361 m_lastReceived = lastReceived;
364 int IR_Assoc::send_initRequest(char* pRefId)
366 Z_APDU *apdu = create_Z_PDU(Z_APDU_initRequest);
367 Z_InitRequest *req = apdu->u.initRequest;
369 ODR_MASK_SET(req->options, Z_Options_search);
370 ODR_MASK_SET(req->options, Z_Options_present);
371 ODR_MASK_SET(req->options, Z_Options_namedResultSets);
372 ODR_MASK_SET(req->options, Z_Options_triggerResourceCtrl);
373 ODR_MASK_SET(req->options, Z_Options_scan);
374 ODR_MASK_SET(req->options, Z_Options_sort);
375 ODR_MASK_SET(req->options, Z_Options_extendedServices);
376 ODR_MASK_SET(req->options, Z_Options_delSet);
378 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_1);
379 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_2);
380 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_3);
384 req->referenceId = getRefID(pRefId);
387 if (m_proxy && m_host)
388 set_otherInformationString(&req->otherInfo, yaz_oid_userinfo_proxy,
391 set_otherInformationString(&req->otherInfo, yaz_oid_userinfo_cookie,
393 return send_Z_PDU(apdu, 0);
396 int IR_Assoc::send_deleteResultSetRequest(char* pResultSetId, char* pRefId)
398 char* ResultSetIds[1];
400 Z_APDU *apdu = create_Z_PDU(Z_APDU_deleteResultSetRequest);
401 Z_DeleteResultSetRequest *req = apdu->u.deleteResultSetRequest;
405 *req->deleteFunction = Z_DeleteResultSetRequest_list;
406 req->num_resultSetList = 1;
407 ResultSetIds[0] = pResultSetId;
408 req->resultSetList = ResultSetIds;
412 *req->deleteFunction = Z_DeleteResultSetRequest_all;
417 req->referenceId = getRefID(pRefId);
420 if (m_proxy && m_host)
421 set_otherInformationString(&req->otherInfo, yaz_oid_userinfo_proxy,
424 set_otherInformationString(&req->otherInfo, yaz_oid_userinfo_cookie,
427 return send_Z_PDU(apdu, 0);
434 * indent-tabs-mode: nil
436 * vim: shiftwidth=4 tabstop=8 expandtab