Added object Yaz_Z_Assoc. Much more functional client.
[yazpp-moved-to-github.git] / src / yaz-ir-assoc.cpp
1 /*
2  * Copyright (c) 1998-1999, Index Data.
3  * See the file LICENSE for details.
4  * Sebastian Hammer, Adam Dickmeiss
5  * 
6  * $Log: yaz-ir-assoc.cpp,v $
7  * Revision 1.5  1999-04-09 11:46:57  adam
8  * Added object Yaz_Z_Assoc. Much more functional client.
9  *
10  */
11
12 #include <assert.h>
13
14 #include <log.h>
15 #include <yaz-ir-assoc.h>
16
17 Yaz_IR_Assoc::Yaz_IR_Assoc(IYaz_PDU_Observable *the_PDU_Observable)
18     : Yaz_Z_Assoc(the_PDU_Observable)
19 {
20     m_num_databaseNames = 0;
21     m_databaseNames = 0;
22     m_preferredRecordSyntax = VAL_NONE;
23     m_elementSetNames = 0;
24     m_lastReceived = 0;
25     m_host = 0;
26     m_proxy = 0;
27
28     const char *db = "Default";
29     set_databaseNames(1, &db);
30 }
31
32 Yaz_IR_Assoc::~Yaz_IR_Assoc()
33 {
34     if (m_elementSetNames)
35         delete [] m_elementSetNames->u.generic;
36     delete m_elementSetNames;
37 }
38
39 void Yaz_IR_Assoc::get_databaseNames (int *num, char ***list)
40 {
41     *num = m_num_databaseNames;
42     *list = m_databaseNames;
43 }
44
45 void Yaz_IR_Assoc::set_databaseNames (int num, const char **list)
46 {
47     int i;
48     logf (LOG_LOG, "Yaz_IR_Assoc::set_databaseNames num=%d", num);
49     for (i = 0; i<m_num_databaseNames; i++)
50         delete [] m_databaseNames[i];
51     delete [] m_databaseNames;
52     m_num_databaseNames = num;
53     m_databaseNames = new (char*) [num];
54     for (i = 0; i<m_num_databaseNames; i++)
55     {
56         m_databaseNames[i] = new char[strlen(list[i])+1];
57         strcpy(m_databaseNames[i], list[i]);
58     }
59 }
60
61 void Yaz_IR_Assoc::set_databaseNames(const char *dblist, const char *sep)
62 {
63     const char **list = new (const char*) [strlen(dblist)];
64     char *dbtmp = new char[strlen(dblist)+1];
65     strcpy(dbtmp, dblist);
66     int num = 0;
67     int len = 0;
68     for (char *cp = dbtmp; ; cp++)
69         if (*cp && !strchr(sep, *cp))
70             len++;
71         else
72         {
73             if (len)
74             {
75                 list[num] = cp - len;
76                 num++;
77             }
78             if (!*cp)
79                 break;
80             *cp = '\0';
81             len = 0;
82         }
83     set_databaseNames (num, list);
84     delete [] dbtmp;
85     delete [] list;
86 }
87
88 void Yaz_IR_Assoc::set_preferredRecordSyntax (int value)
89 {
90     m_preferredRecordSyntax = value;
91 }
92
93 void Yaz_IR_Assoc::set_preferredRecordSyntax (const char *syntax)
94 {
95     m_preferredRecordSyntax = VAL_NONE;
96     if (syntax && *syntax)
97         m_preferredRecordSyntax = oid_getvalbyname (syntax);
98 }
99
100 void Yaz_IR_Assoc::get_preferredRecordSyntax (int *value)
101 {
102     *value = m_preferredRecordSyntax;
103 }
104
105 void Yaz_IR_Assoc::get_preferredRecordSyntax (const char **dst)
106 {
107     struct oident ent;
108     ent.proto = PROTO_Z3950;
109     ent.oclass = CLASS_RECSYN;
110     ent.value = (enum oid_value) m_preferredRecordSyntax;
111
112     int oid[OID_SIZE];
113     oid_ent_to_oid (&ent, oid);
114     struct oident *entp = oid_getentbyoid (oid);
115     
116     *dst = entp ? entp->desc : "";
117 }
118
119 void Yaz_IR_Assoc::set_elementSetName (const char *elementSetName)
120 {
121     if (m_elementSetNames)
122         delete [] m_elementSetNames->u.generic;
123     delete m_elementSetNames;
124     m_elementSetNames = 0;
125     if (elementSetName && *elementSetName)
126     {
127         m_elementSetNames = new Z_ElementSetNames;
128         m_elementSetNames->which = Z_ElementSetNames_generic;
129         m_elementSetNames->u.generic = new char[strlen(elementSetName)+1];
130         strcpy (m_elementSetNames->u.generic, elementSetName);
131     }
132 }
133
134 void Yaz_IR_Assoc::get_elementSetName (Z_ElementSetNames **elementSetNames)
135 {
136     *elementSetNames = m_elementSetNames;
137 }
138
139 void Yaz_IR_Assoc::get_elementSetName (const char **elementSetName)
140 {
141     if (!m_elementSetNames ||
142         m_elementSetNames->which != Z_ElementSetNames_generic)
143     {
144         *elementSetName = 0;
145         return;
146     }
147     *elementSetName = m_elementSetNames->u.generic;
148 }
149
150 void Yaz_IR_Assoc::set_otherInformationString (
151     Z_OtherInformation **otherInformation,
152     int *oid, int categoryValue,
153     const char *str)
154 {
155     Z_OtherInformationUnit *oi =
156         set_otherInformation(otherInformation, oid, categoryValue);
157     if (!oi)
158         return;
159     oi->information.characterInfo = odr_strdup (odr_encode(), str);
160 }
161
162 Z_OtherInformationUnit *Yaz_IR_Assoc::set_otherInformation (
163     Z_OtherInformation **otherInformationP,
164     int *oid, int categoryValue)
165 {
166     int i;
167     Z_OtherInformation *otherInformation = *otherInformationP;
168     if (!otherInformation)
169     {
170         otherInformation = *otherInformationP = (Z_OtherInformation *)
171             odr_malloc (odr_encode(), sizeof(*otherInformation));
172         otherInformation->num_elements = 0;
173         otherInformation->list = (Z_OtherInformationUnit **)
174             odr_malloc (odr_encode(), 8*sizeof(*otherInformation));
175         for (i = 0; i<8; i++)
176             otherInformation->list[i] = 0;
177     }
178     for (i = 0; i<otherInformation->num_elements; i++)
179     {
180         assert (otherInformation->list[i]);
181         if (!oid)
182         {
183             if (!otherInformation->list[i]->category)
184                 return otherInformation->list[i];
185         }
186         else
187         {
188             if (otherInformation->list[i]->category &&
189                 categoryValue ==
190                 *otherInformation->list[i]->category->categoryValue &&
191                 !oid_oidcmp (oid, otherInformation->list[i]->category->
192                              categoryTypeId))
193                 return otherInformation->list[i];
194         }
195     }
196     otherInformation->list[i] = (Z_OtherInformationUnit*)
197         odr_malloc (odr_encode(), sizeof(Z_OtherInformationUnit));
198     if (oid)
199     {
200         otherInformation->list[i]->category = (Z_InfoCategory*)
201             odr_malloc (odr_encode(), sizeof(Z_InfoCategory));
202         otherInformation->list[i]->category->categoryTypeId = (int*)
203             odr_oiddup (odr_encode(), oid);
204         otherInformation->list[i]->category->categoryValue = (int*)
205             odr_malloc (odr_encode(), sizeof(int));
206         *otherInformation->list[i]->category->categoryValue =
207             categoryValue;
208     }
209     else
210         otherInformation->list[i]->category = 0;
211     otherInformation->list[i]->which = Z_OtherInfo_characterInfo;
212     otherInformation->list[i]->information.characterInfo = 0;
213     
214     otherInformation->num_elements = i+1;
215     return otherInformation->list[i];
216 }
217
218 void Yaz_IR_Assoc::recv_Z_PDU(Z_APDU *apdu)
219 {
220     logf (LOG_LOG, "recv_Z_PDU");
221     m_lastReceived = apdu->which;
222     switch (apdu->which)
223     {
224     case Z_APDU_initResponse:
225         logf (LOG_LOG, "recv InitResponse");
226         recv_initResponse(apdu->u.initResponse);
227         break;
228     case Z_APDU_initRequest:
229         logf (LOG_LOG, "recv InitRequest");
230         recv_initRequest(apdu->u.initRequest);
231         break;
232     case Z_APDU_searchRequest:
233         logf (LOG_LOG, "recv searchRequest");
234         recv_searchRequest(apdu->u.searchRequest);
235         break;
236     case Z_APDU_searchResponse:
237         logf (LOG_LOG, "recv searchResponse"); 
238         recv_searchResponse(apdu->u.searchResponse);
239         break;
240     case Z_APDU_presentRequest:
241         logf (LOG_LOG, "recv presentRequest");
242         recv_presentRequest(apdu->u.presentRequest);
243         break;
244     case Z_APDU_presentResponse:
245         logf (LOG_LOG, "recv presentResponse");
246         recv_presentResponse(apdu->u.presentResponse);
247         break;
248     }
249 }
250
251 int Yaz_IR_Assoc::send_searchRequest(Yaz_Z_Query *query)
252 {
253     Z_APDU *apdu = create_Z_PDU(Z_APDU_searchRequest);
254     Z_SearchRequest *req = apdu->u.searchRequest;
255     int recordSyntax;
256
257     req->query = query->get_Z_Query();
258     if (!req->query)
259         return -1;
260     get_databaseNames (&req->num_databaseNames, &req->databaseNames);
261     for (int i = 0; i<req->num_databaseNames; i++)
262         logf (LOG_LOG, "Database %s", req->databaseNames[i]);
263     int oid_syntax[OID_SIZE];
264     oident prefsyn;
265     get_preferredRecordSyntax(&recordSyntax);
266     if (recordSyntax != VAL_NONE)
267     {
268         prefsyn.proto = PROTO_Z3950;
269         prefsyn.oclass = CLASS_RECSYN;
270         prefsyn.value = (enum oid_value) recordSyntax;
271         oid_ent_to_oid(&prefsyn, oid_syntax);
272         req->preferredRecordSyntax = oid_syntax;
273     }
274     logf (LOG_LOG, "send_searchRequest");
275     return send_Z_PDU(apdu);
276 }
277
278 int Yaz_IR_Assoc::send_presentRequest(int start, int number)
279 {
280     Z_APDU *apdu = create_Z_PDU(Z_APDU_presentRequest);
281     Z_PresentRequest *req = apdu->u.presentRequest;
282
283     req->resultSetStartPoint = &start;
284     req->numberOfRecordsRequested = &number;
285
286     int oid_syntax[OID_SIZE];
287     oident prefsyn;
288     int recordSyntax;
289     get_preferredRecordSyntax (&recordSyntax);
290     if (recordSyntax != VAL_NONE)
291     {
292         prefsyn.proto = PROTO_Z3950;
293         prefsyn.oclass = CLASS_RECSYN;
294         prefsyn.value = (enum oid_value) recordSyntax;
295         oid_ent_to_oid(&prefsyn, oid_syntax);
296         req->preferredRecordSyntax = oid_syntax;
297     }
298     Z_RecordComposition compo;
299     Z_ElementSetNames *elementSetNames;
300     get_elementSetName (&elementSetNames);
301     if (elementSetNames)
302     {
303         req->recordComposition = &compo;
304         compo.which = Z_RecordComp_simple;
305         compo.u.simple = elementSetNames;
306     }
307     return send_Z_PDU(apdu);
308 }
309
310 void Yaz_IR_Assoc::set_proxy(const char *str)
311 {
312     delete m_proxy;
313     m_proxy = new char[strlen(str)+1];
314     strcpy (m_proxy, str);
315 }
316
317 void Yaz_IR_Assoc::client(const char *addr)
318 {
319     delete [] m_host;
320     m_host = new char[strlen(addr)+1];
321     strcpy(m_host, addr);
322     const char *zurl_p = (m_proxy ? m_proxy : m_host);
323     char *zurl = new char[strlen(zurl_p)+1];
324     strcpy(zurl, zurl_p);
325     char *dbpart = strchr(zurl, '/');
326     if (dbpart)
327     {
328         set_databaseNames (dbpart+1, "+ ");
329         *dbpart = '\0';
330     }
331     Yaz_Z_Assoc::client(zurl);
332     delete [] zurl;
333 }
334
335 const char *Yaz_IR_Assoc::get_proxy()
336 {
337     return m_proxy;
338 }
339
340 const char *Yaz_IR_Assoc::get_host()
341 {
342     return m_host;
343 }
344
345 void Yaz_IR_Assoc::recv_searchRequest(Z_SearchRequest *searchRequest)
346 {
347     Z_APDU *apdu = create_Z_PDU(Z_APDU_searchResponse);
348     send_Z_PDU(apdu);
349 }
350
351 void Yaz_IR_Assoc::recv_presentRequest(Z_PresentRequest *presentRequest)
352 {
353     Z_APDU *apdu = create_Z_PDU(Z_APDU_presentResponse);
354     send_Z_PDU(apdu);
355 }
356
357 void Yaz_IR_Assoc::recv_initRequest(Z_InitRequest *initRequest)
358 {
359     Z_APDU *apdu = create_Z_PDU(Z_APDU_initResponse);
360     send_Z_PDU(apdu);
361 }
362
363 void Yaz_IR_Assoc::recv_searchResponse (Z_SearchResponse *searchResponse)
364 {
365 }
366
367 void Yaz_IR_Assoc::recv_presentResponse (Z_PresentResponse *presentResponse)
368 {
369 }
370
371 void Yaz_IR_Assoc::recv_initResponse(Z_InitResponse *initResponse)
372 {
373 }
374
375 int Yaz_IR_Assoc::get_lastReceived()
376 {
377     return m_lastReceived;
378 }
379
380 void Yaz_IR_Assoc::set_lastReceived(int lastReceived)
381 {
382     m_lastReceived = lastReceived;
383 }
384
385 int Yaz_IR_Assoc::send_initRequest()
386 {
387
388     Z_APDU *apdu = create_Z_PDU(Z_APDU_initRequest);
389     Z_InitRequest *req = apdu->u.initRequest;
390     
391     ODR_MASK_SET(req->options, Z_Options_search);
392     ODR_MASK_SET(req->options, Z_Options_present);
393     ODR_MASK_SET(req->options, Z_Options_namedResultSets);
394     ODR_MASK_SET(req->options, Z_Options_triggerResourceCtrl);
395     ODR_MASK_SET(req->options, Z_Options_scan);
396     ODR_MASK_SET(req->options, Z_Options_sort);
397     ODR_MASK_SET(req->options, Z_Options_extendedServices);
398     ODR_MASK_SET(req->options, Z_Options_delSet);
399
400     ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_1);
401     ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_2);
402     ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_3);
403
404     if (m_proxy && m_host)
405     {
406         int oid[OID_SIZE];
407         struct oident ent;
408         ent.proto = PROTO_Z3950;
409         ent.oclass = CLASS_USERINFO;
410         ent.value = VAL_PROXY;
411         oid_ent_to_oid (&ent, oid);
412         logf (LOG_LOG, "proxy host %s", m_host);
413         set_otherInformationString(&req->otherInfo, oid, 1, m_host);
414     }
415     return send_Z_PDU(apdu);
416 }
417