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.6 1999-04-20 10:30:05 adam
8 * Implemented various stuff for client and proxy. Updated calls
9 * to ODR to reflect new name parameter.
11 * Revision 1.5 1999/04/09 11:46:57 adam
12 * Added object Yaz_Z_Assoc. Much more functional client.
18 #include <yaz-ir-assoc.h>
20 Yaz_IR_Assoc::Yaz_IR_Assoc(IYaz_PDU_Observable *the_PDU_Observable)
21 : Yaz_Z_Assoc(the_PDU_Observable)
23 m_num_databaseNames = 0;
25 m_preferredRecordSyntax = VAL_NONE;
26 m_elementSetNames = 0;
31 const char *db = "Default";
32 set_databaseNames(1, &db);
35 Yaz_IR_Assoc::~Yaz_IR_Assoc()
37 if (m_elementSetNames)
38 delete [] m_elementSetNames->u.generic;
39 delete [] m_elementSetNames;
45 void Yaz_IR_Assoc::get_databaseNames (int *num, char ***list)
47 *num = m_num_databaseNames;
48 *list = m_databaseNames;
51 void Yaz_IR_Assoc::set_databaseNames (int num, const char **list)
54 logf (LOG_LOG, "Yaz_IR_Assoc::set_databaseNames num=%d", num);
55 for (i = 0; i<m_num_databaseNames; i++)
56 delete [] m_databaseNames[i];
57 delete [] m_databaseNames;
58 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 Yaz_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 Yaz_IR_Assoc::set_preferredRecordSyntax (int value)
96 m_preferredRecordSyntax = value;
99 void Yaz_IR_Assoc::set_preferredRecordSyntax (const char *syntax)
101 m_preferredRecordSyntax = VAL_NONE;
102 if (syntax && *syntax)
103 m_preferredRecordSyntax = oid_getvalbyname (syntax);
106 void Yaz_IR_Assoc::get_preferredRecordSyntax (int *value)
108 *value = m_preferredRecordSyntax;
111 void Yaz_IR_Assoc::get_preferredRecordSyntax (const char **dst)
114 ent.proto = PROTO_Z3950;
115 ent.oclass = CLASS_RECSYN;
116 ent.value = (enum oid_value) m_preferredRecordSyntax;
119 oid_ent_to_oid (&ent, oid);
120 struct oident *entp = oid_getentbyoid (oid);
122 *dst = entp ? entp->desc : "";
125 void Yaz_IR_Assoc::set_elementSetName (const char *elementSetName)
127 if (m_elementSetNames)
128 delete [] m_elementSetNames->u.generic;
129 delete m_elementSetNames;
130 m_elementSetNames = 0;
131 if (elementSetName && *elementSetName)
133 m_elementSetNames = new Z_ElementSetNames;
134 m_elementSetNames->which = Z_ElementSetNames_generic;
135 m_elementSetNames->u.generic = new char[strlen(elementSetName)+1];
136 strcpy (m_elementSetNames->u.generic, elementSetName);
140 void Yaz_IR_Assoc::get_elementSetName (Z_ElementSetNames **elementSetNames)
142 *elementSetNames = m_elementSetNames;
145 void Yaz_IR_Assoc::get_elementSetName (const char **elementSetName)
147 if (!m_elementSetNames ||
148 m_elementSetNames->which != Z_ElementSetNames_generic)
153 *elementSetName = m_elementSetNames->u.generic;
156 void Yaz_IR_Assoc::set_otherInformationString (
157 Z_OtherInformation **otherInformation,
158 int oidval, int categoryValue,
163 ent.proto = PROTO_Z3950;
164 ent.oclass = CLASS_USERINFO;
165 ent.value = (oid_value) oidval;
166 if (!oid_ent_to_oid (&ent, oid))
168 set_otherInformationString(otherInformation, oid, categoryValue, str);
171 void Yaz_IR_Assoc::set_otherInformationString (
172 Z_OtherInformation **otherInformation,
173 int *oid, int categoryValue,
176 Z_OtherInformationUnit *oi =
177 update_otherInformation(otherInformation, 1, oid, categoryValue);
180 oi->information.characterInfo = odr_strdup (odr_encode(), str);
183 void Yaz_IR_Assoc::recv_Z_PDU(Z_APDU *apdu)
185 logf (LOG_LOG, "recv_Z_PDU");
186 m_lastReceived = apdu->which;
189 case Z_APDU_initResponse:
190 logf (LOG_LOG, "recv InitResponse");
191 recv_initResponse(apdu->u.initResponse);
193 case Z_APDU_initRequest:
194 logf (LOG_LOG, "recv InitRequest");
195 recv_initRequest(apdu->u.initRequest);
197 case Z_APDU_searchRequest:
198 logf (LOG_LOG, "recv searchRequest");
199 recv_searchRequest(apdu->u.searchRequest);
201 case Z_APDU_searchResponse:
202 logf (LOG_LOG, "recv searchResponse");
203 recv_searchResponse(apdu->u.searchResponse);
205 case Z_APDU_presentRequest:
206 logf (LOG_LOG, "recv presentRequest");
207 recv_presentRequest(apdu->u.presentRequest);
209 case Z_APDU_presentResponse:
210 logf (LOG_LOG, "recv presentResponse");
211 recv_presentResponse(apdu->u.presentResponse);
216 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 (LOG_LOG, "send_searchRequest");
238 assert (req->otherInfo == 0);
240 set_otherInformationString(&req->otherInfo, VAL_COOKIE, 1, m_cookie);
241 return send_Z_PDU(apdu);
244 int Yaz_IR_Assoc::send_presentRequest(int start, int number)
246 Z_APDU *apdu = create_Z_PDU(Z_APDU_presentRequest);
247 Z_PresentRequest *req = apdu->u.presentRequest;
249 req->resultSetStartPoint = &start;
250 req->numberOfRecordsRequested = &number;
252 int oid_syntax[OID_SIZE];
255 get_preferredRecordSyntax (&recordSyntax);
256 if (recordSyntax != VAL_NONE)
258 prefsyn.proto = PROTO_Z3950;
259 prefsyn.oclass = CLASS_RECSYN;
260 prefsyn.value = (enum oid_value) recordSyntax;
261 oid_ent_to_oid(&prefsyn, oid_syntax);
262 req->preferredRecordSyntax = oid_syntax;
264 Z_RecordComposition compo;
265 Z_ElementSetNames *elementSetNames;
266 get_elementSetName (&elementSetNames);
269 req->recordComposition = &compo;
270 compo.which = Z_RecordComp_simple;
271 compo.u.simple = elementSetNames;
274 set_otherInformationString(&req->otherInfo, VAL_COOKIE, 1, m_cookie);
275 return send_Z_PDU(apdu);
278 void Yaz_IR_Assoc::set_proxy(const char *str)
281 m_proxy = new char[strlen(str)+1];
282 strcpy (m_proxy, str);
285 void Yaz_IR_Assoc::set_cookie(const char *str)
288 m_cookie = new char[strlen(str)+1];
289 strcpy(m_cookie, str);
292 const char *Yaz_IR_Assoc::get_cookie()
297 void Yaz_IR_Assoc::client(const char *addr)
300 m_host = new char[strlen(addr)+1];
301 strcpy(m_host, addr);
302 const char *zurl_p = (m_proxy ? m_proxy : m_host);
303 char *zurl = new char[strlen(zurl_p)+1];
304 strcpy(zurl, zurl_p);
305 char *dbpart = strchr(zurl, '/');
308 set_databaseNames (dbpart+1, "+ ");
311 Yaz_Z_Assoc::client(zurl);
315 const char *Yaz_IR_Assoc::get_proxy()
320 const char *Yaz_IR_Assoc::get_host()
325 void Yaz_IR_Assoc::recv_searchRequest(Z_SearchRequest *searchRequest)
327 Z_APDU *apdu = create_Z_PDU(Z_APDU_searchResponse);
331 void Yaz_IR_Assoc::recv_presentRequest(Z_PresentRequest *presentRequest)
333 Z_APDU *apdu = create_Z_PDU(Z_APDU_presentResponse);
337 void Yaz_IR_Assoc::recv_initRequest(Z_InitRequest *initRequest)
339 Z_APDU *apdu = create_Z_PDU(Z_APDU_initResponse);
343 void Yaz_IR_Assoc::recv_searchResponse (Z_SearchResponse *searchResponse)
347 void Yaz_IR_Assoc::recv_presentResponse (Z_PresentResponse *presentResponse)
351 void Yaz_IR_Assoc::recv_initResponse(Z_InitResponse *initResponse)
355 int Yaz_IR_Assoc::get_lastReceived()
357 return m_lastReceived;
360 void Yaz_IR_Assoc::set_lastReceived(int lastReceived)
362 m_lastReceived = lastReceived;
365 int Yaz_IR_Assoc::send_initRequest()
367 Z_APDU *apdu = create_Z_PDU(Z_APDU_initRequest);
368 Z_InitRequest *req = apdu->u.initRequest;
370 ODR_MASK_SET(req->options, Z_Options_search);
371 ODR_MASK_SET(req->options, Z_Options_present);
372 ODR_MASK_SET(req->options, Z_Options_namedResultSets);
373 ODR_MASK_SET(req->options, Z_Options_triggerResourceCtrl);
374 ODR_MASK_SET(req->options, Z_Options_scan);
375 ODR_MASK_SET(req->options, Z_Options_sort);
376 ODR_MASK_SET(req->options, Z_Options_extendedServices);
377 ODR_MASK_SET(req->options, Z_Options_delSet);
379 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_1);
380 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_2);
381 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_3);
383 if (m_proxy && m_host)
384 set_otherInformationString(&req->otherInfo, VAL_PROXY, 1, m_host);
386 set_otherInformationString(&req->otherInfo, VAL_COOKIE, 1, m_cookie);
387 return send_Z_PDU(apdu);