2 * Copyright (c) 1998-1999, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
6 * $Log: yaz-ir-assoc.cpp,v $
7 * Revision 1.12 2000-05-10 11:36:58 ian
8 * Added default parameters for refid to request functions.
9 * Added default parameter for result set name to search and present request.
10 * Commented out forced logging of PDU contents.
11 * Added send_deleteResultSetRequest
13 * Revision 1.11 1999/12/06 13:52:45 adam
14 * Modified for new location of YAZ header files. Experimental threaded
17 * Revision 1.10 1999/04/29 07:33:28 adam
18 * Changed setting of host in connect/proxy setting. YAZ' strtoaddr now
19 * ignores database part of host.
21 * Revision 1.9 1999/04/28 13:29:14 adam
22 * Yet another fix regarding database settings.
24 * Revision 1.8 1999/04/28 13:04:03 adam
25 * Fixed setting of proxy otherInfo so that database(s) are removed.
27 * Revision 1.7 1999/04/21 12:09:01 adam
28 * Many improvements. Modified to proxy server to work with "sessions"
31 * Revision 1.6 1999/04/20 10:30:05 adam
32 * Implemented various stuff for client and proxy. Updated calls
33 * to ODR to reflect new name parameter.
35 * Revision 1.5 1999/04/09 11:46:57 adam
36 * Added object Yaz_Z_Assoc. Much more functional client.
42 #include <yaz-ir-assoc.h>
44 Yaz_IR_Assoc::Yaz_IR_Assoc(IYaz_PDU_Observable *the_PDU_Observable)
45 : Yaz_Z_Assoc(the_PDU_Observable)
47 m_num_databaseNames = 0;
49 m_preferredRecordSyntax = VAL_NONE;
50 m_elementSetNames = 0;
56 const char *db = "Default";
57 set_databaseNames(1, &db);
60 Yaz_IR_Assoc::~Yaz_IR_Assoc()
62 if (m_elementSetNames)
63 delete [] m_elementSetNames->u.generic;
64 delete [] m_elementSetNames;
70 void Yaz_IR_Assoc::get_databaseNames (int *num, char ***list)
72 *num = m_num_databaseNames;
73 *list = m_databaseNames;
76 void Yaz_IR_Assoc::set_databaseNames (int num, const char **list)
79 logf (m_log, "Yaz_IR_Assoc::set_databaseNames num=%d", num);
80 for (i = 0; i<m_num_databaseNames; i++)
81 delete [] m_databaseNames[i];
82 delete [] m_databaseNames;
83 m_num_databaseNames = num;
84 m_databaseNames = new (char*) [num];
85 for (i = 0; i<m_num_databaseNames; i++)
87 m_databaseNames[i] = new char[strlen(list[i])+1];
88 strcpy(m_databaseNames[i], list[i]);
92 void Yaz_IR_Assoc::set_databaseNames(const char *dblist, const char *sep)
94 const char **list = new (const char*) [strlen(dblist)];
95 char *dbtmp = new char[strlen(dblist)+1];
96 strcpy(dbtmp, dblist);
99 for (char *cp = dbtmp; ; cp++)
100 if (*cp && !strchr(sep, *cp))
106 list[num] = cp - len;
114 set_databaseNames (num, list);
119 void Yaz_IR_Assoc::set_preferredRecordSyntax (int value)
121 m_preferredRecordSyntax = value;
124 void Yaz_IR_Assoc::set_preferredRecordSyntax (const char *syntax)
126 m_preferredRecordSyntax = VAL_NONE;
127 if (syntax && *syntax)
128 m_preferredRecordSyntax = oid_getvalbyname (syntax);
131 void Yaz_IR_Assoc::get_preferredRecordSyntax (int *value)
133 *value = m_preferredRecordSyntax;
136 void Yaz_IR_Assoc::get_preferredRecordSyntax (const char **dst)
139 ent.proto = PROTO_Z3950;
140 ent.oclass = CLASS_RECSYN;
141 ent.value = (enum oid_value) m_preferredRecordSyntax;
144 oid_ent_to_oid (&ent, oid);
145 struct oident *entp = oid_getentbyoid (oid);
147 *dst = entp ? entp->desc : "";
150 void Yaz_IR_Assoc::set_elementSetName (const char *elementSetName)
152 if (m_elementSetNames)
153 delete [] m_elementSetNames->u.generic;
154 delete m_elementSetNames;
155 m_elementSetNames = 0;
156 if (elementSetName && *elementSetName)
158 m_elementSetNames = new Z_ElementSetNames;
159 m_elementSetNames->which = Z_ElementSetNames_generic;
160 m_elementSetNames->u.generic = new char[strlen(elementSetName)+1];
161 strcpy (m_elementSetNames->u.generic, elementSetName);
165 void Yaz_IR_Assoc::get_elementSetName (Z_ElementSetNames **elementSetNames)
167 *elementSetNames = m_elementSetNames;
170 void Yaz_IR_Assoc::get_elementSetName (const char **elementSetName)
172 if (!m_elementSetNames ||
173 m_elementSetNames->which != Z_ElementSetNames_generic)
178 *elementSetName = m_elementSetNames->u.generic;
181 void Yaz_IR_Assoc::recv_Z_PDU(Z_APDU *apdu)
183 logf (m_log, "recv_Z_PDU");
184 m_lastReceived = apdu->which;
187 case Z_APDU_initResponse:
188 logf (m_log, "recv InitResponse");
189 recv_initResponse(apdu->u.initResponse);
191 case Z_APDU_initRequest:
192 logf (m_log, "recv InitRequest");
193 recv_initRequest(apdu->u.initRequest);
195 case Z_APDU_searchRequest:
196 logf (m_log, "recv searchRequest");
197 recv_searchRequest(apdu->u.searchRequest);
199 case Z_APDU_searchResponse:
200 logf (m_log, "recv searchResponse");
201 recv_searchResponse(apdu->u.searchResponse);
203 case Z_APDU_presentRequest:
204 logf (m_log, "recv presentRequest");
205 recv_presentRequest(apdu->u.presentRequest);
207 case Z_APDU_presentResponse:
208 logf (m_log, "recv presentResponse");
209 recv_presentResponse(apdu->u.presentResponse);
214 int Yaz_IR_Assoc::send_searchRequest(Yaz_Z_Query *query,
218 Z_APDU *apdu = create_Z_PDU(Z_APDU_searchRequest);
219 Z_SearchRequest *req = apdu->u.searchRequest;
222 req->query = query->get_Z_Query();
225 get_databaseNames (&req->num_databaseNames, &req->databaseNames);
226 int oid_syntax[OID_SIZE];
228 get_preferredRecordSyntax(&recordSyntax);
229 if (recordSyntax != VAL_NONE)
231 prefsyn.proto = PROTO_Z3950;
232 prefsyn.oclass = CLASS_RECSYN;
233 prefsyn.value = (enum oid_value) recordSyntax;
234 oid_ent_to_oid(&prefsyn, oid_syntax);
235 req->preferredRecordSyntax = oid_syntax;
237 logf (m_log, "send_searchRequest");
238 assert (req->otherInfo == 0);
241 set_otherInformationString(&req->otherInfo, VAL_COOKIE, 1, m_cookie);
242 assert (req->otherInfo);
247 req->referenceId = getRefID(pRefId);
252 req->resultSetName = pResultSetId;
255 return send_Z_PDU(apdu);
258 int Yaz_IR_Assoc::send_presentRequest(int start,
263 Z_APDU *apdu = create_Z_PDU(Z_APDU_presentRequest);
264 Z_PresentRequest *req = apdu->u.presentRequest;
266 req->resultSetStartPoint = &start;
267 req->numberOfRecordsRequested = &number;
269 int oid_syntax[OID_SIZE];
272 get_preferredRecordSyntax (&recordSyntax);
273 if (recordSyntax != VAL_NONE)
275 prefsyn.proto = PROTO_Z3950;
276 prefsyn.oclass = CLASS_RECSYN;
277 prefsyn.value = (enum oid_value) recordSyntax;
278 oid_ent_to_oid(&prefsyn, oid_syntax);
279 req->preferredRecordSyntax = oid_syntax;
281 Z_RecordComposition compo;
282 Z_ElementSetNames *elementSetNames;
283 get_elementSetName (&elementSetNames);
286 req->recordComposition = &compo;
287 compo.which = Z_RecordComp_simple;
288 compo.u.simple = elementSetNames;
292 set_otherInformationString(&req->otherInfo, VAL_COOKIE, 1, m_cookie);
296 req->referenceId = getRefID(pRefId);
301 req->resultSetId = pResultSetId;
304 return send_Z_PDU(apdu);
307 void Yaz_IR_Assoc::set_proxy(const char *str)
313 m_proxy = new char[strlen(str)+1];
314 strcpy (m_proxy, str);
318 void Yaz_IR_Assoc::set_cookie(const char *str)
324 m_cookie = new char[strlen(str)+1];
325 strcpy(m_cookie, str);
329 const char *Yaz_IR_Assoc::get_cookie()
334 void Yaz_IR_Assoc::client(const char *addr)
337 m_host = new char[strlen(addr)+1];
338 strcpy(m_host, addr);
339 const char *dbpart = strchr(m_host, '/');
341 set_databaseNames (dbpart+1, "+ ");
342 Yaz_Z_Assoc::client(m_proxy ? m_proxy : m_host);
345 const char *Yaz_IR_Assoc::get_proxy()
350 const char *Yaz_IR_Assoc::get_host()
355 void Yaz_IR_Assoc::recv_searchRequest(Z_SearchRequest *searchRequest)
357 Z_APDU *apdu = create_Z_PDU(Z_APDU_searchResponse);
361 void Yaz_IR_Assoc::recv_presentRequest(Z_PresentRequest *presentRequest)
363 Z_APDU *apdu = create_Z_PDU(Z_APDU_presentResponse);
367 void Yaz_IR_Assoc::recv_initRequest(Z_InitRequest *initRequest)
369 Z_APDU *apdu = create_Z_PDU(Z_APDU_initResponse);
373 void Yaz_IR_Assoc::recv_searchResponse (Z_SearchResponse *searchResponse)
377 void Yaz_IR_Assoc::recv_presentResponse (Z_PresentResponse *presentResponse)
381 void Yaz_IR_Assoc::recv_initResponse(Z_InitResponse *initResponse)
385 int Yaz_IR_Assoc::get_lastReceived()
387 return m_lastReceived;
390 void Yaz_IR_Assoc::set_lastReceived(int lastReceived)
392 m_lastReceived = lastReceived;
395 int Yaz_IR_Assoc::send_initRequest(char* pRefId)
397 Z_APDU *apdu = create_Z_PDU(Z_APDU_initRequest);
398 Z_InitRequest *req = apdu->u.initRequest;
400 ODR_MASK_SET(req->options, Z_Options_search);
401 ODR_MASK_SET(req->options, Z_Options_present);
402 ODR_MASK_SET(req->options, Z_Options_namedResultSets);
403 ODR_MASK_SET(req->options, Z_Options_triggerResourceCtrl);
404 ODR_MASK_SET(req->options, Z_Options_scan);
405 ODR_MASK_SET(req->options, Z_Options_sort);
406 ODR_MASK_SET(req->options, Z_Options_extendedServices);
407 ODR_MASK_SET(req->options, Z_Options_delSet);
409 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_1);
410 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_2);
411 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_3);
415 req->referenceId = getRefID(pRefId);
418 if (m_proxy && m_host)
419 set_otherInformationString(&req->otherInfo, VAL_PROXY, 1, m_host);
421 set_otherInformationString(&req->otherInfo, VAL_COOKIE, 1, m_cookie);
422 return send_Z_PDU(apdu);
425 int Yaz_IR_Assoc::send_deleteResultSetRequest(char* pResultSetId = NULL, char* pRefId = NULL)
427 char* ResultSetIds[1];
429 Z_APDU *apdu = create_Z_PDU(Z_APDU_deleteResultSetRequest);
430 Z_DeleteResultSetRequest *req = apdu->u.deleteResultSetRequest;
435 *req->deleteFunction = Z_DeleteResultSetRequest_list;
436 req->num_resultSetList = 1;
437 ResultSetIds[0] = pResultSetId;
438 req->resultSetList = ResultSetIds;
442 *req->deleteFunction = Z_DeleteResultSetRequest_all;
447 req->referenceId = getRefID(pRefId);
450 if (m_proxy && m_host)
451 set_otherInformationString(&req->otherInfo, VAL_PROXY, 1, m_host);
453 set_otherInformationString(&req->otherInfo, VAL_COOKIE, 1, m_cookie);
456 return send_Z_PDU(apdu);