Moved header files to include/yaz++. Switched to libtool and automake.
[yazpp-moved-to-github.git] / src / yaz-ir-assoc.cpp
1 /*
2  * Copyright (c) 1998-2000, Index Data.
3  * See the file LICENSE for details.
4  * 
5  * $Log: yaz-ir-assoc.cpp,v $
6  * Revision 1.14  2000-10-11 11:58:16  adam
7  * Moved header files to include/yaz++. Switched to libtool and automake.
8  * Configure script creates yaz++-config script.
9  *
10  * Revision 1.13  2000/09/06 14:23:45  adam
11  * WIN32 updates.
12  *
13  * Revision 1.12  2000/05/10 11:36:58  ian
14  * Added default parameters for refid to request functions.
15  * Added default parameter for result set name to search and present request.
16  * Commented out forced logging of PDU contents.
17  * Added send_deleteResultSetRequest
18  *
19  * Revision 1.11  1999/12/06 13:52:45  adam
20  * Modified for new location of YAZ header files. Experimental threaded
21  * operation.
22  *
23  * Revision 1.10  1999/04/29 07:33:28  adam
24  * Changed setting of host in connect/proxy setting. YAZ' strtoaddr now
25  * ignores database part of host.
26  *
27  * Revision 1.9  1999/04/28 13:29:14  adam
28  * Yet another fix regarding database settings.
29  *
30  * Revision 1.8  1999/04/28 13:04:03  adam
31  * Fixed setting of proxy otherInfo so that database(s) are removed.
32  *
33  * Revision 1.7  1999/04/21 12:09:01  adam
34  * Many improvements. Modified to proxy server to work with "sessions"
35  * based on cookies.
36  *
37  * Revision 1.6  1999/04/20 10:30:05  adam
38  * Implemented various stuff for client and proxy. Updated calls
39  * to ODR to reflect new name parameter.
40  *
41  * Revision 1.5  1999/04/09 11:46:57  adam
42  * Added object Yaz_Z_Assoc. Much more functional client.
43  */
44
45 #include <assert.h>
46
47 #include <yaz/log.h>
48 #include <yaz++/yaz-ir-assoc.h>
49
50 Yaz_IR_Assoc::Yaz_IR_Assoc(IYaz_PDU_Observable *the_PDU_Observable)
51     : Yaz_Z_Assoc(the_PDU_Observable)
52 {
53     m_num_databaseNames = 0;
54     m_databaseNames = 0;
55     m_preferredRecordSyntax = VAL_NONE;
56     m_elementSetNames = 0;
57     m_lastReceived = 0;
58     m_host = 0;
59     m_proxy = 0;
60     m_cookie = 0;
61     m_log = LOG_DEBUG;
62     const char *db = "Default";
63     set_databaseNames(1, &db);
64 }
65
66 Yaz_IR_Assoc::~Yaz_IR_Assoc()
67 {
68     if (m_elementSetNames)
69         delete [] m_elementSetNames->u.generic;
70     delete [] m_elementSetNames;
71     delete [] m_host;
72     delete [] m_proxy;
73     delete [] m_cookie;
74 }
75
76 void Yaz_IR_Assoc::get_databaseNames (int *num, char ***list)
77 {
78     *num = m_num_databaseNames;
79     *list = m_databaseNames;
80 }
81
82 typedef char *charp;
83 void Yaz_IR_Assoc::set_databaseNames (int num, const char **list)
84 {
85     int i;
86     logf (m_log, "Yaz_IR_Assoc::set_databaseNames num=%d", num);
87     for (i = 0; i<m_num_databaseNames; i++)
88         delete [] m_databaseNames[i];
89     delete [] m_databaseNames;
90     m_num_databaseNames = num;
91
92     m_databaseNames = new char *[num];
93     for (i = 0; i<m_num_databaseNames; i++)
94     {
95         m_databaseNames[i] = new char[strlen(list[i])+1];
96         strcpy(m_databaseNames[i], list[i]);
97     }
98 }
99
100 void Yaz_IR_Assoc::set_databaseNames(const char *dblist, const char *sep)
101 {
102     const char **list = new const char* [strlen(dblist)];
103     char *dbtmp = new char[strlen(dblist)+1];
104     strcpy(dbtmp, dblist);
105     int num = 0;
106     int len = 0;
107     for (char *cp = dbtmp; ; cp++)
108         if (*cp && !strchr(sep, *cp))
109             len++;
110         else
111         {
112             if (len)
113             {
114                 list[num] = cp - len;
115                 num++;
116             }
117             if (!*cp)
118                 break;
119             *cp = '\0';
120             len = 0;
121         }
122     set_databaseNames (num, list);
123     delete [] dbtmp;
124     delete [] list;
125 }
126
127 void Yaz_IR_Assoc::set_preferredRecordSyntax (int value)
128 {
129     m_preferredRecordSyntax = value;
130 }
131
132 void Yaz_IR_Assoc::set_preferredRecordSyntax (const char *syntax)
133 {
134     m_preferredRecordSyntax = VAL_NONE;
135     if (syntax && *syntax)
136         m_preferredRecordSyntax = oid_getvalbyname (syntax);
137 }
138
139 void Yaz_IR_Assoc::get_preferredRecordSyntax (int *value)
140 {
141     *value = m_preferredRecordSyntax;
142 }
143
144 void Yaz_IR_Assoc::get_preferredRecordSyntax (const char **dst)
145 {
146     struct oident ent;
147     ent.proto = PROTO_Z3950;
148     ent.oclass = CLASS_RECSYN;
149     ent.value = (enum oid_value) m_preferredRecordSyntax;
150
151     int oid[OID_SIZE];
152     oid_ent_to_oid (&ent, oid);
153     struct oident *entp = oid_getentbyoid (oid);
154     
155     *dst = entp ? entp->desc : "";
156 }
157
158 void Yaz_IR_Assoc::set_elementSetName (const char *elementSetName)
159 {
160     if (m_elementSetNames)
161         delete [] m_elementSetNames->u.generic;
162     delete m_elementSetNames;
163     m_elementSetNames = 0;
164     if (elementSetName && *elementSetName)
165     {
166         m_elementSetNames = new Z_ElementSetNames;
167         m_elementSetNames->which = Z_ElementSetNames_generic;
168         m_elementSetNames->u.generic = new char[strlen(elementSetName)+1];
169         strcpy (m_elementSetNames->u.generic, elementSetName);
170     }
171 }
172
173 void Yaz_IR_Assoc::get_elementSetName (Z_ElementSetNames **elementSetNames)
174 {
175     *elementSetNames = m_elementSetNames;
176 }
177
178 void Yaz_IR_Assoc::get_elementSetName (const char **elementSetName)
179 {
180     if (!m_elementSetNames ||
181         m_elementSetNames->which != Z_ElementSetNames_generic)
182     {
183         *elementSetName = 0;
184         return;
185     }
186     *elementSetName = m_elementSetNames->u.generic;
187 }
188
189 void Yaz_IR_Assoc::recv_Z_PDU(Z_APDU *apdu)
190 {
191     logf (m_log, "recv_Z_PDU");
192     m_lastReceived = apdu->which;
193     switch (apdu->which)
194     {
195     case Z_APDU_initResponse:
196         logf (m_log, "recv InitResponse");
197         recv_initResponse(apdu->u.initResponse);
198         break;
199     case Z_APDU_initRequest:
200         logf (m_log, "recv InitRequest");
201         recv_initRequest(apdu->u.initRequest);
202         break;
203     case Z_APDU_searchRequest:
204         logf (m_log, "recv searchRequest");
205         recv_searchRequest(apdu->u.searchRequest);
206         break;
207     case Z_APDU_searchResponse:
208         logf (m_log, "recv searchResponse"); 
209         recv_searchResponse(apdu->u.searchResponse);
210         break;
211     case Z_APDU_presentRequest:
212         logf (m_log, "recv presentRequest");
213         recv_presentRequest(apdu->u.presentRequest);
214         break;
215     case Z_APDU_presentResponse:
216         logf (m_log, "recv presentResponse");
217         recv_presentResponse(apdu->u.presentResponse);
218         break;
219     }
220 }
221
222 int Yaz_IR_Assoc::send_searchRequest(Yaz_Z_Query *query,
223                                      char* pResultSetId,
224                                      char* pRefId)
225 {
226     Z_APDU *apdu = create_Z_PDU(Z_APDU_searchRequest);
227     Z_SearchRequest *req = apdu->u.searchRequest;
228     int recordSyntax;
229
230     req->query = query->get_Z_Query();
231     if (!req->query)
232         return -1;
233     get_databaseNames (&req->num_databaseNames, &req->databaseNames);
234     int oid_syntax[OID_SIZE];
235     oident prefsyn;
236     get_preferredRecordSyntax(&recordSyntax);
237     if (recordSyntax != VAL_NONE)
238     {
239         prefsyn.proto = PROTO_Z3950;
240         prefsyn.oclass = CLASS_RECSYN;
241         prefsyn.value = (enum oid_value) recordSyntax;
242         oid_ent_to_oid(&prefsyn, oid_syntax);
243         req->preferredRecordSyntax = oid_syntax;
244     }
245     logf (m_log, "send_searchRequest");
246     assert (req->otherInfo == 0);
247     if (m_cookie)
248     {
249         set_otherInformationString(&req->otherInfo, VAL_COOKIE, 1, m_cookie);
250         assert (req->otherInfo);
251     }
252
253     if ( pRefId )
254     {
255         req->referenceId = getRefID(pRefId);
256     }
257
258     if ( pResultSetId )
259     {
260         req->resultSetName = pResultSetId;
261     }
262
263     return send_Z_PDU(apdu);
264 }
265
266 int Yaz_IR_Assoc::send_presentRequest(int start, 
267                                       int number, 
268                                       char* pResultSetId,
269                                       char* pRefId)
270 {
271     Z_APDU *apdu = create_Z_PDU(Z_APDU_presentRequest);
272     Z_PresentRequest *req = apdu->u.presentRequest;
273
274     req->resultSetStartPoint = &start;
275     req->numberOfRecordsRequested = &number;
276
277     int oid_syntax[OID_SIZE];
278     oident prefsyn;
279     int recordSyntax;
280     get_preferredRecordSyntax (&recordSyntax);
281     if (recordSyntax != VAL_NONE)
282     {
283         prefsyn.proto = PROTO_Z3950;
284         prefsyn.oclass = CLASS_RECSYN;
285         prefsyn.value = (enum oid_value) recordSyntax;
286         oid_ent_to_oid(&prefsyn, oid_syntax);
287         req->preferredRecordSyntax = oid_syntax;
288     }
289     Z_RecordComposition compo;
290     Z_ElementSetNames *elementSetNames;
291     get_elementSetName (&elementSetNames);
292     if (elementSetNames)
293     {
294         req->recordComposition = &compo;
295         compo.which = Z_RecordComp_simple;
296         compo.u.simple = elementSetNames;
297     }
298
299     if (m_cookie)
300         set_otherInformationString(&req->otherInfo, VAL_COOKIE, 1, m_cookie);
301
302     if ( pRefId )
303     {
304         req->referenceId = getRefID(pRefId);
305     }
306
307     if ( pResultSetId )
308     {
309         req->resultSetId = pResultSetId;
310     }
311
312     return send_Z_PDU(apdu);
313 }
314
315 void Yaz_IR_Assoc::set_proxy(const char *str)
316 {
317     delete [] m_proxy;
318     m_proxy = 0;
319     if (str)
320     {
321         m_proxy = new char[strlen(str)+1];
322         strcpy (m_proxy, str);
323     }
324 }
325
326 void Yaz_IR_Assoc::set_cookie(const char *str)
327 {
328     delete [] m_cookie;
329     m_cookie = 0;
330     if (str)
331     {
332         m_cookie = new char[strlen(str)+1];
333         strcpy(m_cookie, str);
334     }
335 }
336
337 const char *Yaz_IR_Assoc::get_cookie()
338 {
339     return m_cookie;
340 }
341
342 void Yaz_IR_Assoc::client(const char *addr)
343 {
344     delete [] m_host;
345     m_host = new char[strlen(addr)+1];
346     strcpy(m_host, addr);
347     const char *dbpart = strchr(m_host, '/');
348     if (dbpart)
349         set_databaseNames (dbpart+1, "+ ");
350     Yaz_Z_Assoc::client(m_proxy ? m_proxy : m_host);
351 }
352
353 const char *Yaz_IR_Assoc::get_proxy()
354 {
355     return m_proxy;
356 }
357
358 const char *Yaz_IR_Assoc::get_host()
359 {
360     return m_host;
361 }
362
363 void Yaz_IR_Assoc::recv_searchRequest(Z_SearchRequest *searchRequest)
364 {
365     Z_APDU *apdu = create_Z_PDU(Z_APDU_searchResponse);
366     send_Z_PDU(apdu);
367 }
368
369 void Yaz_IR_Assoc::recv_presentRequest(Z_PresentRequest *presentRequest)
370 {
371     Z_APDU *apdu = create_Z_PDU(Z_APDU_presentResponse);
372     send_Z_PDU(apdu);
373 }
374
375 void Yaz_IR_Assoc::recv_initRequest(Z_InitRequest *initRequest)
376 {
377     Z_APDU *apdu = create_Z_PDU(Z_APDU_initResponse);
378     send_Z_PDU(apdu);
379 }
380
381 void Yaz_IR_Assoc::recv_searchResponse (Z_SearchResponse *searchResponse)
382 {
383 }
384
385 void Yaz_IR_Assoc::recv_presentResponse (Z_PresentResponse *presentResponse)
386 {
387 }
388
389 void Yaz_IR_Assoc::recv_initResponse(Z_InitResponse *initResponse)
390 {
391 }
392
393 int Yaz_IR_Assoc::get_lastReceived()
394 {
395     return m_lastReceived;
396 }
397
398 void Yaz_IR_Assoc::set_lastReceived(int lastReceived)
399 {
400     m_lastReceived = lastReceived;
401 }
402
403 int Yaz_IR_Assoc::send_initRequest(char* pRefId)
404 {
405     Z_APDU *apdu = create_Z_PDU(Z_APDU_initRequest);
406     Z_InitRequest *req = apdu->u.initRequest;
407     
408     ODR_MASK_SET(req->options, Z_Options_search);
409     ODR_MASK_SET(req->options, Z_Options_present);
410     ODR_MASK_SET(req->options, Z_Options_namedResultSets);
411     ODR_MASK_SET(req->options, Z_Options_triggerResourceCtrl);
412     ODR_MASK_SET(req->options, Z_Options_scan);
413     ODR_MASK_SET(req->options, Z_Options_sort);
414     ODR_MASK_SET(req->options, Z_Options_extendedServices);
415     ODR_MASK_SET(req->options, Z_Options_delSet);
416
417     ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_1);
418     ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_2);
419     ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_3);
420
421     if ( pRefId )
422     {
423         req->referenceId = getRefID(pRefId);
424     }
425
426     if (m_proxy && m_host)
427         set_otherInformationString(&req->otherInfo, VAL_PROXY, 1, m_host);
428     if (m_cookie)
429         set_otherInformationString(&req->otherInfo, VAL_COOKIE, 1, m_cookie);
430     return send_Z_PDU(apdu);
431 }
432
433 int Yaz_IR_Assoc::send_deleteResultSetRequest(char* pResultSetId, char* pRefId)
434 {
435     char* ResultSetIds[1];
436
437     Z_APDU *apdu = create_Z_PDU(Z_APDU_deleteResultSetRequest);
438     Z_DeleteResultSetRequest *req = apdu->u.deleteResultSetRequest;
439
440     if ( pResultSetId )
441     {
442         *req->deleteFunction = Z_DeleteResultSetRequest_list;
443         req->num_resultSetList = 1;
444         ResultSetIds[0] = pResultSetId;
445         req->resultSetList = ResultSetIds;
446     }
447     else
448     {
449         *req->deleteFunction = Z_DeleteResultSetRequest_all;
450     }
451     
452     if ( pRefId )
453     {
454         req->referenceId = getRefID(pRefId);
455     }
456
457     if (m_proxy && m_host)
458         set_otherInformationString(&req->otherInfo, VAL_PROXY, 1, m_host);
459     if (m_cookie)
460         set_otherInformationString(&req->otherInfo, VAL_COOKIE, 1, m_cookie);
461
462
463     return send_Z_PDU(apdu);
464 }
465
466