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.11 1999-12-06 13:52:45 adam
8 * Modified for new location of YAZ header files. Experimental threaded
11 * Revision 1.10 1999/04/29 07:33:28 adam
12 * Changed setting of host in connect/proxy setting. YAZ' strtoaddr now
13 * ignores database part of host.
15 * Revision 1.9 1999/04/28 13:29:14 adam
16 * Yet another fix regarding database settings.
18 * Revision 1.8 1999/04/28 13:04:03 adam
19 * Fixed setting of proxy otherInfo so that database(s) are removed.
21 * Revision 1.7 1999/04/21 12:09:01 adam
22 * Many improvements. Modified to proxy server to work with "sessions"
25 * Revision 1.6 1999/04/20 10:30:05 adam
26 * Implemented various stuff for client and proxy. Updated calls
27 * to ODR to reflect new name parameter.
29 * Revision 1.5 1999/04/09 11:46:57 adam
30 * Added object Yaz_Z_Assoc. Much more functional client.
36 #include <yaz-ir-assoc.h>
38 Yaz_IR_Assoc::Yaz_IR_Assoc(IYaz_PDU_Observable *the_PDU_Observable)
39 : Yaz_Z_Assoc(the_PDU_Observable)
41 m_num_databaseNames = 0;
43 m_preferredRecordSyntax = VAL_NONE;
44 m_elementSetNames = 0;
50 const char *db = "Default";
51 set_databaseNames(1, &db);
54 Yaz_IR_Assoc::~Yaz_IR_Assoc()
56 if (m_elementSetNames)
57 delete [] m_elementSetNames->u.generic;
58 delete [] m_elementSetNames;
64 void Yaz_IR_Assoc::get_databaseNames (int *num, char ***list)
66 *num = m_num_databaseNames;
67 *list = m_databaseNames;
70 void Yaz_IR_Assoc::set_databaseNames (int num, const char **list)
73 logf (m_log, "Yaz_IR_Assoc::set_databaseNames num=%d", num);
74 for (i = 0; i<m_num_databaseNames; i++)
75 delete [] m_databaseNames[i];
76 delete [] m_databaseNames;
77 m_num_databaseNames = num;
78 m_databaseNames = new (char*) [num];
79 for (i = 0; i<m_num_databaseNames; i++)
81 m_databaseNames[i] = new char[strlen(list[i])+1];
82 strcpy(m_databaseNames[i], list[i]);
86 void Yaz_IR_Assoc::set_databaseNames(const char *dblist, const char *sep)
88 const char **list = new (const char*) [strlen(dblist)];
89 char *dbtmp = new char[strlen(dblist)+1];
90 strcpy(dbtmp, dblist);
93 for (char *cp = dbtmp; ; cp++)
94 if (*cp && !strchr(sep, *cp))
100 list[num] = cp - len;
108 set_databaseNames (num, list);
113 void Yaz_IR_Assoc::set_preferredRecordSyntax (int value)
115 m_preferredRecordSyntax = value;
118 void Yaz_IR_Assoc::set_preferredRecordSyntax (const char *syntax)
120 m_preferredRecordSyntax = VAL_NONE;
121 if (syntax && *syntax)
122 m_preferredRecordSyntax = oid_getvalbyname (syntax);
125 void Yaz_IR_Assoc::get_preferredRecordSyntax (int *value)
127 *value = m_preferredRecordSyntax;
130 void Yaz_IR_Assoc::get_preferredRecordSyntax (const char **dst)
133 ent.proto = PROTO_Z3950;
134 ent.oclass = CLASS_RECSYN;
135 ent.value = (enum oid_value) m_preferredRecordSyntax;
138 oid_ent_to_oid (&ent, oid);
139 struct oident *entp = oid_getentbyoid (oid);
141 *dst = entp ? entp->desc : "";
144 void Yaz_IR_Assoc::set_elementSetName (const char *elementSetName)
146 if (m_elementSetNames)
147 delete [] m_elementSetNames->u.generic;
148 delete m_elementSetNames;
149 m_elementSetNames = 0;
150 if (elementSetName && *elementSetName)
152 m_elementSetNames = new Z_ElementSetNames;
153 m_elementSetNames->which = Z_ElementSetNames_generic;
154 m_elementSetNames->u.generic = new char[strlen(elementSetName)+1];
155 strcpy (m_elementSetNames->u.generic, elementSetName);
159 void Yaz_IR_Assoc::get_elementSetName (Z_ElementSetNames **elementSetNames)
161 *elementSetNames = m_elementSetNames;
164 void Yaz_IR_Assoc::get_elementSetName (const char **elementSetName)
166 if (!m_elementSetNames ||
167 m_elementSetNames->which != Z_ElementSetNames_generic)
172 *elementSetName = m_elementSetNames->u.generic;
175 void Yaz_IR_Assoc::recv_Z_PDU(Z_APDU *apdu)
177 logf (m_log, "recv_Z_PDU");
178 m_lastReceived = apdu->which;
181 case Z_APDU_initResponse:
182 logf (m_log, "recv InitResponse");
183 recv_initResponse(apdu->u.initResponse);
185 case Z_APDU_initRequest:
186 logf (m_log, "recv InitRequest");
187 recv_initRequest(apdu->u.initRequest);
189 case Z_APDU_searchRequest:
190 logf (m_log, "recv searchRequest");
191 recv_searchRequest(apdu->u.searchRequest);
193 case Z_APDU_searchResponse:
194 logf (m_log, "recv searchResponse");
195 recv_searchResponse(apdu->u.searchResponse);
197 case Z_APDU_presentRequest:
198 logf (m_log, "recv presentRequest");
199 recv_presentRequest(apdu->u.presentRequest);
201 case Z_APDU_presentResponse:
202 logf (m_log, "recv presentResponse");
203 recv_presentResponse(apdu->u.presentResponse);
208 int Yaz_IR_Assoc::send_searchRequest(Yaz_Z_Query *query)
210 Z_APDU *apdu = create_Z_PDU(Z_APDU_searchRequest);
211 Z_SearchRequest *req = apdu->u.searchRequest;
214 req->query = query->get_Z_Query();
217 get_databaseNames (&req->num_databaseNames, &req->databaseNames);
218 int oid_syntax[OID_SIZE];
220 get_preferredRecordSyntax(&recordSyntax);
221 if (recordSyntax != VAL_NONE)
223 prefsyn.proto = PROTO_Z3950;
224 prefsyn.oclass = CLASS_RECSYN;
225 prefsyn.value = (enum oid_value) recordSyntax;
226 oid_ent_to_oid(&prefsyn, oid_syntax);
227 req->preferredRecordSyntax = oid_syntax;
229 logf (m_log, "send_searchRequest");
230 assert (req->otherInfo == 0);
233 set_otherInformationString(&req->otherInfo, VAL_COOKIE, 1, m_cookie);
234 assert (req->otherInfo);
236 return send_Z_PDU(apdu);
239 int Yaz_IR_Assoc::send_presentRequest(int start, int number)
241 Z_APDU *apdu = create_Z_PDU(Z_APDU_presentRequest);
242 Z_PresentRequest *req = apdu->u.presentRequest;
244 req->resultSetStartPoint = &start;
245 req->numberOfRecordsRequested = &number;
247 int oid_syntax[OID_SIZE];
250 get_preferredRecordSyntax (&recordSyntax);
251 if (recordSyntax != VAL_NONE)
253 prefsyn.proto = PROTO_Z3950;
254 prefsyn.oclass = CLASS_RECSYN;
255 prefsyn.value = (enum oid_value) recordSyntax;
256 oid_ent_to_oid(&prefsyn, oid_syntax);
257 req->preferredRecordSyntax = oid_syntax;
259 Z_RecordComposition compo;
260 Z_ElementSetNames *elementSetNames;
261 get_elementSetName (&elementSetNames);
264 req->recordComposition = &compo;
265 compo.which = Z_RecordComp_simple;
266 compo.u.simple = elementSetNames;
269 set_otherInformationString(&req->otherInfo, VAL_COOKIE, 1, m_cookie);
270 return send_Z_PDU(apdu);
273 void Yaz_IR_Assoc::set_proxy(const char *str)
279 m_proxy = new char[strlen(str)+1];
280 strcpy (m_proxy, str);
284 void Yaz_IR_Assoc::set_cookie(const char *str)
290 m_cookie = new char[strlen(str)+1];
291 strcpy(m_cookie, str);
295 const char *Yaz_IR_Assoc::get_cookie()
300 void Yaz_IR_Assoc::client(const char *addr)
303 m_host = new char[strlen(addr)+1];
304 strcpy(m_host, addr);
305 const char *dbpart = strchr(m_host, '/');
307 set_databaseNames (dbpart+1, "+ ");
308 Yaz_Z_Assoc::client(m_proxy ? m_proxy : m_host);
311 const char *Yaz_IR_Assoc::get_proxy()
316 const char *Yaz_IR_Assoc::get_host()
321 void Yaz_IR_Assoc::recv_searchRequest(Z_SearchRequest *searchRequest)
323 Z_APDU *apdu = create_Z_PDU(Z_APDU_searchResponse);
327 void Yaz_IR_Assoc::recv_presentRequest(Z_PresentRequest *presentRequest)
329 Z_APDU *apdu = create_Z_PDU(Z_APDU_presentResponse);
333 void Yaz_IR_Assoc::recv_initRequest(Z_InitRequest *initRequest)
335 Z_APDU *apdu = create_Z_PDU(Z_APDU_initResponse);
339 void Yaz_IR_Assoc::recv_searchResponse (Z_SearchResponse *searchResponse)
343 void Yaz_IR_Assoc::recv_presentResponse (Z_PresentResponse *presentResponse)
347 void Yaz_IR_Assoc::recv_initResponse(Z_InitResponse *initResponse)
351 int Yaz_IR_Assoc::get_lastReceived()
353 return m_lastReceived;
356 void Yaz_IR_Assoc::set_lastReceived(int lastReceived)
358 m_lastReceived = lastReceived;
361 int Yaz_IR_Assoc::send_initRequest()
363 Z_APDU *apdu = create_Z_PDU(Z_APDU_initRequest);
364 Z_InitRequest *req = apdu->u.initRequest;
366 ODR_MASK_SET(req->options, Z_Options_search);
367 ODR_MASK_SET(req->options, Z_Options_present);
368 ODR_MASK_SET(req->options, Z_Options_namedResultSets);
369 ODR_MASK_SET(req->options, Z_Options_triggerResourceCtrl);
370 ODR_MASK_SET(req->options, Z_Options_scan);
371 ODR_MASK_SET(req->options, Z_Options_sort);
372 ODR_MASK_SET(req->options, Z_Options_extendedServices);
373 ODR_MASK_SET(req->options, Z_Options_delSet);
375 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_1);
376 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_2);
377 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_3);
379 if (m_proxy && m_host)
380 set_otherInformationString(&req->otherInfo, VAL_PROXY, 1, m_host);
382 set_otherInformationString(&req->otherInfo, VAL_COOKIE, 1, m_cookie);
383 return send_Z_PDU(apdu);