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.7 1999-04-21 12:09:01 adam
8 * Many improvements. Modified to proxy server to work with "sessions"
11 * Revision 1.6 1999/04/20 10:30:05 adam
12 * Implemented various stuff for client and proxy. Updated calls
13 * to ODR to reflect new name parameter.
15 * Revision 1.5 1999/04/09 11:46:57 adam
16 * Added object Yaz_Z_Assoc. Much more functional client.
22 #include <yaz-ir-assoc.h>
24 Yaz_IR_Assoc::Yaz_IR_Assoc(IYaz_PDU_Observable *the_PDU_Observable)
25 : Yaz_Z_Assoc(the_PDU_Observable)
27 m_num_databaseNames = 0;
29 m_preferredRecordSyntax = VAL_NONE;
30 m_elementSetNames = 0;
35 const char *db = "Default";
36 set_databaseNames(1, &db);
39 Yaz_IR_Assoc::~Yaz_IR_Assoc()
41 if (m_elementSetNames)
42 delete [] m_elementSetNames->u.generic;
43 delete [] m_elementSetNames;
49 void Yaz_IR_Assoc::get_databaseNames (int *num, char ***list)
51 *num = m_num_databaseNames;
52 *list = m_databaseNames;
55 void Yaz_IR_Assoc::set_databaseNames (int num, const char **list)
58 logf (LOG_LOG, "Yaz_IR_Assoc::set_databaseNames num=%d", num);
59 for (i = 0; i<m_num_databaseNames; i++)
60 delete [] m_databaseNames[i];
61 delete [] m_databaseNames;
62 m_num_databaseNames = num;
63 m_databaseNames = new (char*) [num];
64 for (i = 0; i<m_num_databaseNames; i++)
66 m_databaseNames[i] = new char[strlen(list[i])+1];
67 strcpy(m_databaseNames[i], list[i]);
71 void Yaz_IR_Assoc::set_databaseNames(const char *dblist, const char *sep)
73 const char **list = new (const char*) [strlen(dblist)];
74 char *dbtmp = new char[strlen(dblist)+1];
75 strcpy(dbtmp, dblist);
78 for (char *cp = dbtmp; ; cp++)
79 if (*cp && !strchr(sep, *cp))
93 set_databaseNames (num, list);
98 void Yaz_IR_Assoc::set_preferredRecordSyntax (int value)
100 m_preferredRecordSyntax = value;
103 void Yaz_IR_Assoc::set_preferredRecordSyntax (const char *syntax)
105 m_preferredRecordSyntax = VAL_NONE;
106 if (syntax && *syntax)
107 m_preferredRecordSyntax = oid_getvalbyname (syntax);
110 void Yaz_IR_Assoc::get_preferredRecordSyntax (int *value)
112 *value = m_preferredRecordSyntax;
115 void Yaz_IR_Assoc::get_preferredRecordSyntax (const char **dst)
118 ent.proto = PROTO_Z3950;
119 ent.oclass = CLASS_RECSYN;
120 ent.value = (enum oid_value) m_preferredRecordSyntax;
123 oid_ent_to_oid (&ent, oid);
124 struct oident *entp = oid_getentbyoid (oid);
126 *dst = entp ? entp->desc : "";
129 void Yaz_IR_Assoc::set_elementSetName (const char *elementSetName)
131 if (m_elementSetNames)
132 delete [] m_elementSetNames->u.generic;
133 delete m_elementSetNames;
134 m_elementSetNames = 0;
135 if (elementSetName && *elementSetName)
137 m_elementSetNames = new Z_ElementSetNames;
138 m_elementSetNames->which = Z_ElementSetNames_generic;
139 m_elementSetNames->u.generic = new char[strlen(elementSetName)+1];
140 strcpy (m_elementSetNames->u.generic, elementSetName);
144 void Yaz_IR_Assoc::get_elementSetName (Z_ElementSetNames **elementSetNames)
146 *elementSetNames = m_elementSetNames;
149 void Yaz_IR_Assoc::get_elementSetName (const char **elementSetName)
151 if (!m_elementSetNames ||
152 m_elementSetNames->which != Z_ElementSetNames_generic)
157 *elementSetName = m_elementSetNames->u.generic;
160 void Yaz_IR_Assoc::recv_Z_PDU(Z_APDU *apdu)
162 logf (LOG_LOG, "recv_Z_PDU");
163 m_lastReceived = apdu->which;
166 case Z_APDU_initResponse:
167 logf (LOG_LOG, "recv InitResponse");
168 recv_initResponse(apdu->u.initResponse);
170 case Z_APDU_initRequest:
171 logf (LOG_LOG, "recv InitRequest");
172 recv_initRequest(apdu->u.initRequest);
174 case Z_APDU_searchRequest:
175 logf (LOG_LOG, "recv searchRequest");
176 recv_searchRequest(apdu->u.searchRequest);
178 case Z_APDU_searchResponse:
179 logf (LOG_LOG, "recv searchResponse");
180 recv_searchResponse(apdu->u.searchResponse);
182 case Z_APDU_presentRequest:
183 logf (LOG_LOG, "recv presentRequest");
184 recv_presentRequest(apdu->u.presentRequest);
186 case Z_APDU_presentResponse:
187 logf (LOG_LOG, "recv presentResponse");
188 recv_presentResponse(apdu->u.presentResponse);
193 int Yaz_IR_Assoc::send_searchRequest(Yaz_Z_Query *query)
195 Z_APDU *apdu = create_Z_PDU(Z_APDU_searchRequest);
196 Z_SearchRequest *req = apdu->u.searchRequest;
199 req->query = query->get_Z_Query();
202 get_databaseNames (&req->num_databaseNames, &req->databaseNames);
203 int oid_syntax[OID_SIZE];
205 get_preferredRecordSyntax(&recordSyntax);
206 if (recordSyntax != VAL_NONE)
208 prefsyn.proto = PROTO_Z3950;
209 prefsyn.oclass = CLASS_RECSYN;
210 prefsyn.value = (enum oid_value) recordSyntax;
211 oid_ent_to_oid(&prefsyn, oid_syntax);
212 req->preferredRecordSyntax = oid_syntax;
214 logf (LOG_LOG, "send_searchRequest");
215 assert (req->otherInfo == 0);
218 set_otherInformationString(&req->otherInfo, VAL_COOKIE, 1, m_cookie);
219 assert (req->otherInfo);
221 return send_Z_PDU(apdu);
224 int Yaz_IR_Assoc::send_presentRequest(int start, int number)
226 Z_APDU *apdu = create_Z_PDU(Z_APDU_presentRequest);
227 Z_PresentRequest *req = apdu->u.presentRequest;
229 req->resultSetStartPoint = &start;
230 req->numberOfRecordsRequested = &number;
232 int oid_syntax[OID_SIZE];
235 get_preferredRecordSyntax (&recordSyntax);
236 if (recordSyntax != VAL_NONE)
238 prefsyn.proto = PROTO_Z3950;
239 prefsyn.oclass = CLASS_RECSYN;
240 prefsyn.value = (enum oid_value) recordSyntax;
241 oid_ent_to_oid(&prefsyn, oid_syntax);
242 req->preferredRecordSyntax = oid_syntax;
244 Z_RecordComposition compo;
245 Z_ElementSetNames *elementSetNames;
246 get_elementSetName (&elementSetNames);
249 req->recordComposition = &compo;
250 compo.which = Z_RecordComp_simple;
251 compo.u.simple = elementSetNames;
254 set_otherInformationString(&req->otherInfo, VAL_COOKIE, 1, m_cookie);
255 return send_Z_PDU(apdu);
258 void Yaz_IR_Assoc::set_proxy(const char *str)
264 m_proxy = new char[strlen(str)+1];
265 strcpy (m_proxy, str);
269 void Yaz_IR_Assoc::set_cookie(const char *str)
275 m_cookie = new char[strlen(str)+1];
276 strcpy(m_cookie, str);
280 const char *Yaz_IR_Assoc::get_cookie()
285 void Yaz_IR_Assoc::client(const char *addr)
288 m_host = new char[strlen(addr)+1];
289 strcpy(m_host, addr);
290 const char *zurl_p = (m_proxy ? m_proxy : m_host);
291 char *zurl = new char[strlen(zurl_p)+1];
292 strcpy(zurl, zurl_p);
293 char *dbpart = strchr(zurl, '/');
296 set_databaseNames (dbpart+1, "+ ");
299 Yaz_Z_Assoc::client(zurl);
303 const char *Yaz_IR_Assoc::get_proxy()
308 const char *Yaz_IR_Assoc::get_host()
313 void Yaz_IR_Assoc::recv_searchRequest(Z_SearchRequest *searchRequest)
315 Z_APDU *apdu = create_Z_PDU(Z_APDU_searchResponse);
319 void Yaz_IR_Assoc::recv_presentRequest(Z_PresentRequest *presentRequest)
321 Z_APDU *apdu = create_Z_PDU(Z_APDU_presentResponse);
325 void Yaz_IR_Assoc::recv_initRequest(Z_InitRequest *initRequest)
327 Z_APDU *apdu = create_Z_PDU(Z_APDU_initResponse);
331 void Yaz_IR_Assoc::recv_searchResponse (Z_SearchResponse *searchResponse)
335 void Yaz_IR_Assoc::recv_presentResponse (Z_PresentResponse *presentResponse)
339 void Yaz_IR_Assoc::recv_initResponse(Z_InitResponse *initResponse)
343 int Yaz_IR_Assoc::get_lastReceived()
345 return m_lastReceived;
348 void Yaz_IR_Assoc::set_lastReceived(int lastReceived)
350 m_lastReceived = lastReceived;
353 int Yaz_IR_Assoc::send_initRequest()
355 Z_APDU *apdu = create_Z_PDU(Z_APDU_initRequest);
356 Z_InitRequest *req = apdu->u.initRequest;
358 ODR_MASK_SET(req->options, Z_Options_search);
359 ODR_MASK_SET(req->options, Z_Options_present);
360 ODR_MASK_SET(req->options, Z_Options_namedResultSets);
361 ODR_MASK_SET(req->options, Z_Options_triggerResourceCtrl);
362 ODR_MASK_SET(req->options, Z_Options_scan);
363 ODR_MASK_SET(req->options, Z_Options_sort);
364 ODR_MASK_SET(req->options, Z_Options_extendedServices);
365 ODR_MASK_SET(req->options, Z_Options_delSet);
367 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_1);
368 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_2);
369 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_3);
371 if (m_proxy && m_host)
372 set_otherInformationString(&req->otherInfo, VAL_PROXY, 1, m_host);
374 set_otherInformationString(&req->otherInfo, VAL_COOKIE, 1, m_cookie);
375 return send_Z_PDU(apdu);