2 * Copyright (c) 1998-2000, Index Data.
3 * See the file LICENSE for details.
5 * $Log: yaz-ir-assoc.cpp,v $
6 * Revision 1.13 2000-09-06 14:23:45 adam
9 * Revision 1.12 2000/05/10 11:36:58 ian
10 * Added default parameters for refid to request functions.
11 * Added default parameter for result set name to search and present request.
12 * Commented out forced logging of PDU contents.
13 * Added send_deleteResultSetRequest
15 * Revision 1.11 1999/12/06 13:52:45 adam
16 * Modified for new location of YAZ header files. Experimental threaded
19 * Revision 1.10 1999/04/29 07:33:28 adam
20 * Changed setting of host in connect/proxy setting. YAZ' strtoaddr now
21 * ignores database part of host.
23 * Revision 1.9 1999/04/28 13:29:14 adam
24 * Yet another fix regarding database settings.
26 * Revision 1.8 1999/04/28 13:04:03 adam
27 * Fixed setting of proxy otherInfo so that database(s) are removed.
29 * Revision 1.7 1999/04/21 12:09:01 adam
30 * Many improvements. Modified to proxy server to work with "sessions"
33 * Revision 1.6 1999/04/20 10:30:05 adam
34 * Implemented various stuff for client and proxy. Updated calls
35 * to ODR to reflect new name parameter.
37 * Revision 1.5 1999/04/09 11:46:57 adam
38 * Added object Yaz_Z_Assoc. Much more functional client.
44 #include <yaz-ir-assoc.h>
46 Yaz_IR_Assoc::Yaz_IR_Assoc(IYaz_PDU_Observable *the_PDU_Observable)
47 : Yaz_Z_Assoc(the_PDU_Observable)
49 m_num_databaseNames = 0;
51 m_preferredRecordSyntax = VAL_NONE;
52 m_elementSetNames = 0;
58 const char *db = "Default";
59 set_databaseNames(1, &db);
62 Yaz_IR_Assoc::~Yaz_IR_Assoc()
64 if (m_elementSetNames)
65 delete [] m_elementSetNames->u.generic;
66 delete [] m_elementSetNames;
72 void Yaz_IR_Assoc::get_databaseNames (int *num, char ***list)
74 *num = m_num_databaseNames;
75 *list = m_databaseNames;
79 void Yaz_IR_Assoc::set_databaseNames (int num, const char **list)
82 logf (m_log, "Yaz_IR_Assoc::set_databaseNames num=%d", num);
83 for (i = 0; i<m_num_databaseNames; i++)
84 delete [] m_databaseNames[i];
85 delete [] m_databaseNames;
86 m_num_databaseNames = num;
88 m_databaseNames = new char *[num];
89 for (i = 0; i<m_num_databaseNames; i++)
91 m_databaseNames[i] = new char[strlen(list[i])+1];
92 strcpy(m_databaseNames[i], list[i]);
96 void Yaz_IR_Assoc::set_databaseNames(const char *dblist, const char *sep)
98 const char **list = new const char* [strlen(dblist)];
99 char *dbtmp = new char[strlen(dblist)+1];
100 strcpy(dbtmp, dblist);
103 for (char *cp = dbtmp; ; cp++)
104 if (*cp && !strchr(sep, *cp))
110 list[num] = cp - len;
118 set_databaseNames (num, list);
123 void Yaz_IR_Assoc::set_preferredRecordSyntax (int value)
125 m_preferredRecordSyntax = value;
128 void Yaz_IR_Assoc::set_preferredRecordSyntax (const char *syntax)
130 m_preferredRecordSyntax = VAL_NONE;
131 if (syntax && *syntax)
132 m_preferredRecordSyntax = oid_getvalbyname (syntax);
135 void Yaz_IR_Assoc::get_preferredRecordSyntax (int *value)
137 *value = m_preferredRecordSyntax;
140 void Yaz_IR_Assoc::get_preferredRecordSyntax (const char **dst)
143 ent.proto = PROTO_Z3950;
144 ent.oclass = CLASS_RECSYN;
145 ent.value = (enum oid_value) m_preferredRecordSyntax;
148 oid_ent_to_oid (&ent, oid);
149 struct oident *entp = oid_getentbyoid (oid);
151 *dst = entp ? entp->desc : "";
154 void Yaz_IR_Assoc::set_elementSetName (const char *elementSetName)
156 if (m_elementSetNames)
157 delete [] m_elementSetNames->u.generic;
158 delete m_elementSetNames;
159 m_elementSetNames = 0;
160 if (elementSetName && *elementSetName)
162 m_elementSetNames = new Z_ElementSetNames;
163 m_elementSetNames->which = Z_ElementSetNames_generic;
164 m_elementSetNames->u.generic = new char[strlen(elementSetName)+1];
165 strcpy (m_elementSetNames->u.generic, elementSetName);
169 void Yaz_IR_Assoc::get_elementSetName (Z_ElementSetNames **elementSetNames)
171 *elementSetNames = m_elementSetNames;
174 void Yaz_IR_Assoc::get_elementSetName (const char **elementSetName)
176 if (!m_elementSetNames ||
177 m_elementSetNames->which != Z_ElementSetNames_generic)
182 *elementSetName = m_elementSetNames->u.generic;
185 void Yaz_IR_Assoc::recv_Z_PDU(Z_APDU *apdu)
187 logf (m_log, "recv_Z_PDU");
188 m_lastReceived = apdu->which;
191 case Z_APDU_initResponse:
192 logf (m_log, "recv InitResponse");
193 recv_initResponse(apdu->u.initResponse);
195 case Z_APDU_initRequest:
196 logf (m_log, "recv InitRequest");
197 recv_initRequest(apdu->u.initRequest);
199 case Z_APDU_searchRequest:
200 logf (m_log, "recv searchRequest");
201 recv_searchRequest(apdu->u.searchRequest);
203 case Z_APDU_searchResponse:
204 logf (m_log, "recv searchResponse");
205 recv_searchResponse(apdu->u.searchResponse);
207 case Z_APDU_presentRequest:
208 logf (m_log, "recv presentRequest");
209 recv_presentRequest(apdu->u.presentRequest);
211 case Z_APDU_presentResponse:
212 logf (m_log, "recv presentResponse");
213 recv_presentResponse(apdu->u.presentResponse);
218 int Yaz_IR_Assoc::send_searchRequest(Yaz_Z_Query *query,
222 Z_APDU *apdu = create_Z_PDU(Z_APDU_searchRequest);
223 Z_SearchRequest *req = apdu->u.searchRequest;
226 req->query = query->get_Z_Query();
229 get_databaseNames (&req->num_databaseNames, &req->databaseNames);
230 int oid_syntax[OID_SIZE];
232 get_preferredRecordSyntax(&recordSyntax);
233 if (recordSyntax != VAL_NONE)
235 prefsyn.proto = PROTO_Z3950;
236 prefsyn.oclass = CLASS_RECSYN;
237 prefsyn.value = (enum oid_value) recordSyntax;
238 oid_ent_to_oid(&prefsyn, oid_syntax);
239 req->preferredRecordSyntax = oid_syntax;
241 logf (m_log, "send_searchRequest");
242 assert (req->otherInfo == 0);
245 set_otherInformationString(&req->otherInfo, VAL_COOKIE, 1, m_cookie);
246 assert (req->otherInfo);
251 req->referenceId = getRefID(pRefId);
256 req->resultSetName = pResultSetId;
259 return send_Z_PDU(apdu);
262 int Yaz_IR_Assoc::send_presentRequest(int start,
267 Z_APDU *apdu = create_Z_PDU(Z_APDU_presentRequest);
268 Z_PresentRequest *req = apdu->u.presentRequest;
270 req->resultSetStartPoint = &start;
271 req->numberOfRecordsRequested = &number;
273 int oid_syntax[OID_SIZE];
276 get_preferredRecordSyntax (&recordSyntax);
277 if (recordSyntax != VAL_NONE)
279 prefsyn.proto = PROTO_Z3950;
280 prefsyn.oclass = CLASS_RECSYN;
281 prefsyn.value = (enum oid_value) recordSyntax;
282 oid_ent_to_oid(&prefsyn, oid_syntax);
283 req->preferredRecordSyntax = oid_syntax;
285 Z_RecordComposition compo;
286 Z_ElementSetNames *elementSetNames;
287 get_elementSetName (&elementSetNames);
290 req->recordComposition = &compo;
291 compo.which = Z_RecordComp_simple;
292 compo.u.simple = elementSetNames;
296 set_otherInformationString(&req->otherInfo, VAL_COOKIE, 1, m_cookie);
300 req->referenceId = getRefID(pRefId);
305 req->resultSetId = pResultSetId;
308 return send_Z_PDU(apdu);
311 void Yaz_IR_Assoc::set_proxy(const char *str)
317 m_proxy = new char[strlen(str)+1];
318 strcpy (m_proxy, str);
322 void Yaz_IR_Assoc::set_cookie(const char *str)
328 m_cookie = new char[strlen(str)+1];
329 strcpy(m_cookie, str);
333 const char *Yaz_IR_Assoc::get_cookie()
338 void Yaz_IR_Assoc::client(const char *addr)
341 m_host = new char[strlen(addr)+1];
342 strcpy(m_host, addr);
343 const char *dbpart = strchr(m_host, '/');
345 set_databaseNames (dbpart+1, "+ ");
346 Yaz_Z_Assoc::client(m_proxy ? m_proxy : m_host);
349 const char *Yaz_IR_Assoc::get_proxy()
354 const char *Yaz_IR_Assoc::get_host()
359 void Yaz_IR_Assoc::recv_searchRequest(Z_SearchRequest *searchRequest)
361 Z_APDU *apdu = create_Z_PDU(Z_APDU_searchResponse);
365 void Yaz_IR_Assoc::recv_presentRequest(Z_PresentRequest *presentRequest)
367 Z_APDU *apdu = create_Z_PDU(Z_APDU_presentResponse);
371 void Yaz_IR_Assoc::recv_initRequest(Z_InitRequest *initRequest)
373 Z_APDU *apdu = create_Z_PDU(Z_APDU_initResponse);
377 void Yaz_IR_Assoc::recv_searchResponse (Z_SearchResponse *searchResponse)
381 void Yaz_IR_Assoc::recv_presentResponse (Z_PresentResponse *presentResponse)
385 void Yaz_IR_Assoc::recv_initResponse(Z_InitResponse *initResponse)
389 int Yaz_IR_Assoc::get_lastReceived()
391 return m_lastReceived;
394 void Yaz_IR_Assoc::set_lastReceived(int lastReceived)
396 m_lastReceived = lastReceived;
399 int Yaz_IR_Assoc::send_initRequest(char* pRefId)
401 Z_APDU *apdu = create_Z_PDU(Z_APDU_initRequest);
402 Z_InitRequest *req = apdu->u.initRequest;
404 ODR_MASK_SET(req->options, Z_Options_search);
405 ODR_MASK_SET(req->options, Z_Options_present);
406 ODR_MASK_SET(req->options, Z_Options_namedResultSets);
407 ODR_MASK_SET(req->options, Z_Options_triggerResourceCtrl);
408 ODR_MASK_SET(req->options, Z_Options_scan);
409 ODR_MASK_SET(req->options, Z_Options_sort);
410 ODR_MASK_SET(req->options, Z_Options_extendedServices);
411 ODR_MASK_SET(req->options, Z_Options_delSet);
413 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_1);
414 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_2);
415 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_3);
419 req->referenceId = getRefID(pRefId);
422 if (m_proxy && m_host)
423 set_otherInformationString(&req->otherInfo, VAL_PROXY, 1, m_host);
425 set_otherInformationString(&req->otherInfo, VAL_COOKIE, 1, m_cookie);
426 return send_Z_PDU(apdu);
429 int Yaz_IR_Assoc::send_deleteResultSetRequest(char* pResultSetId, char* pRefId)
431 char* ResultSetIds[1];
433 Z_APDU *apdu = create_Z_PDU(Z_APDU_deleteResultSetRequest);
434 Z_DeleteResultSetRequest *req = apdu->u.deleteResultSetRequest;
438 *req->deleteFunction = Z_DeleteResultSetRequest_list;
439 req->num_resultSetList = 1;
440 ResultSetIds[0] = pResultSetId;
441 req->resultSetList = ResultSetIds;
445 *req->deleteFunction = Z_DeleteResultSetRequest_all;
450 req->referenceId = getRefID(pRefId);
453 if (m_proxy && m_host)
454 set_otherInformationString(&req->otherInfo, VAL_PROXY, 1, m_host);
456 set_otherInformationString(&req->otherInfo, VAL_COOKIE, 1, m_cookie);
459 return send_Z_PDU(apdu);