Implemented various stuff for client and proxy. Updated calls
[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.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.
10  *
11  * Revision 1.5  1999/04/09 11:46:57  adam
12  * Added object Yaz_Z_Assoc. Much more functional client.
13  */
14
15 #include <assert.h>
16
17 #include <log.h>
18 #include <yaz-ir-assoc.h>
19
20 Yaz_IR_Assoc::Yaz_IR_Assoc(IYaz_PDU_Observable *the_PDU_Observable)
21     : Yaz_Z_Assoc(the_PDU_Observable)
22 {
23     m_num_databaseNames = 0;
24     m_databaseNames = 0;
25     m_preferredRecordSyntax = VAL_NONE;
26     m_elementSetNames = 0;
27     m_lastReceived = 0;
28     m_host = 0;
29     m_proxy = 0;
30     m_cookie = 0;
31     const char *db = "Default";
32     set_databaseNames(1, &db);
33 }
34
35 Yaz_IR_Assoc::~Yaz_IR_Assoc()
36 {
37     if (m_elementSetNames)
38         delete [] m_elementSetNames->u.generic;
39     delete [] m_elementSetNames;
40     delete [] m_host;
41     delete [] m_proxy;
42     delete [] m_cookie;
43 }
44
45 void Yaz_IR_Assoc::get_databaseNames (int *num, char ***list)
46 {
47     *num = m_num_databaseNames;
48     *list = m_databaseNames;
49 }
50
51 void Yaz_IR_Assoc::set_databaseNames (int num, const char **list)
52 {
53     int i;
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++)
61     {
62         m_databaseNames[i] = new char[strlen(list[i])+1];
63         strcpy(m_databaseNames[i], list[i]);
64     }
65 }
66
67 void Yaz_IR_Assoc::set_databaseNames(const char *dblist, const char *sep)
68 {
69     const char **list = new (const char*) [strlen(dblist)];
70     char *dbtmp = new char[strlen(dblist)+1];
71     strcpy(dbtmp, dblist);
72     int num = 0;
73     int len = 0;
74     for (char *cp = dbtmp; ; cp++)
75         if (*cp && !strchr(sep, *cp))
76             len++;
77         else
78         {
79             if (len)
80             {
81                 list[num] = cp - len;
82                 num++;
83             }
84             if (!*cp)
85                 break;
86             *cp = '\0';
87             len = 0;
88         }
89     set_databaseNames (num, list);
90     delete [] dbtmp;
91     delete [] list;
92 }
93
94 void Yaz_IR_Assoc::set_preferredRecordSyntax (int value)
95 {
96     m_preferredRecordSyntax = value;
97 }
98
99 void Yaz_IR_Assoc::set_preferredRecordSyntax (const char *syntax)
100 {
101     m_preferredRecordSyntax = VAL_NONE;
102     if (syntax && *syntax)
103         m_preferredRecordSyntax = oid_getvalbyname (syntax);
104 }
105
106 void Yaz_IR_Assoc::get_preferredRecordSyntax (int *value)
107 {
108     *value = m_preferredRecordSyntax;
109 }
110
111 void Yaz_IR_Assoc::get_preferredRecordSyntax (const char **dst)
112 {
113     struct oident ent;
114     ent.proto = PROTO_Z3950;
115     ent.oclass = CLASS_RECSYN;
116     ent.value = (enum oid_value) m_preferredRecordSyntax;
117
118     int oid[OID_SIZE];
119     oid_ent_to_oid (&ent, oid);
120     struct oident *entp = oid_getentbyoid (oid);
121     
122     *dst = entp ? entp->desc : "";
123 }
124
125 void Yaz_IR_Assoc::set_elementSetName (const char *elementSetName)
126 {
127     if (m_elementSetNames)
128         delete [] m_elementSetNames->u.generic;
129     delete m_elementSetNames;
130     m_elementSetNames = 0;
131     if (elementSetName && *elementSetName)
132     {
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);
137     }
138 }
139
140 void Yaz_IR_Assoc::get_elementSetName (Z_ElementSetNames **elementSetNames)
141 {
142     *elementSetNames = m_elementSetNames;
143 }
144
145 void Yaz_IR_Assoc::get_elementSetName (const char **elementSetName)
146 {
147     if (!m_elementSetNames ||
148         m_elementSetNames->which != Z_ElementSetNames_generic)
149     {
150         *elementSetName = 0;
151         return;
152     }
153     *elementSetName = m_elementSetNames->u.generic;
154 }
155
156 void Yaz_IR_Assoc::set_otherInformationString (
157     Z_OtherInformation **otherInformation,
158     int oidval, int categoryValue,
159     const char *str)
160 {
161     int oid[OID_SIZE];
162     struct oident ent;
163     ent.proto = PROTO_Z3950;
164     ent.oclass = CLASS_USERINFO;
165     ent.value = (oid_value) oidval;
166     if (!oid_ent_to_oid (&ent, oid))
167         return ;
168     set_otherInformationString(otherInformation, oid, categoryValue, str);
169 }
170
171 void Yaz_IR_Assoc::set_otherInformationString (
172     Z_OtherInformation **otherInformation,
173     int *oid, int categoryValue,
174     const char *str)
175 {
176     Z_OtherInformationUnit *oi =
177         update_otherInformation(otherInformation, 1, oid, categoryValue);
178     if (!oi)
179         return;
180     oi->information.characterInfo = odr_strdup (odr_encode(), str);
181 }
182
183 void Yaz_IR_Assoc::recv_Z_PDU(Z_APDU *apdu)
184 {
185     logf (LOG_LOG, "recv_Z_PDU");
186     m_lastReceived = apdu->which;
187     switch (apdu->which)
188     {
189     case Z_APDU_initResponse:
190         logf (LOG_LOG, "recv InitResponse");
191         recv_initResponse(apdu->u.initResponse);
192         break;
193     case Z_APDU_initRequest:
194         logf (LOG_LOG, "recv InitRequest");
195         recv_initRequest(apdu->u.initRequest);
196         break;
197     case Z_APDU_searchRequest:
198         logf (LOG_LOG, "recv searchRequest");
199         recv_searchRequest(apdu->u.searchRequest);
200         break;
201     case Z_APDU_searchResponse:
202         logf (LOG_LOG, "recv searchResponse"); 
203         recv_searchResponse(apdu->u.searchResponse);
204         break;
205     case Z_APDU_presentRequest:
206         logf (LOG_LOG, "recv presentRequest");
207         recv_presentRequest(apdu->u.presentRequest);
208         break;
209     case Z_APDU_presentResponse:
210         logf (LOG_LOG, "recv presentResponse");
211         recv_presentResponse(apdu->u.presentResponse);
212         break;
213     }
214 }
215
216 int Yaz_IR_Assoc::send_searchRequest(Yaz_Z_Query *query)
217 {
218     Z_APDU *apdu = create_Z_PDU(Z_APDU_searchRequest);
219     Z_SearchRequest *req = apdu->u.searchRequest;
220     int recordSyntax;
221
222     req->query = query->get_Z_Query();
223     if (!req->query)
224         return -1;
225     get_databaseNames (&req->num_databaseNames, &req->databaseNames);
226     int oid_syntax[OID_SIZE];
227     oident prefsyn;
228     get_preferredRecordSyntax(&recordSyntax);
229     if (recordSyntax != VAL_NONE)
230     {
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;
236     }
237     logf (LOG_LOG, "send_searchRequest");
238     assert (req->otherInfo == 0);
239     if (m_cookie)
240         set_otherInformationString(&req->otherInfo, VAL_COOKIE, 1, m_cookie);
241     return send_Z_PDU(apdu);
242 }
243
244 int Yaz_IR_Assoc::send_presentRequest(int start, int number)
245 {
246     Z_APDU *apdu = create_Z_PDU(Z_APDU_presentRequest);
247     Z_PresentRequest *req = apdu->u.presentRequest;
248
249     req->resultSetStartPoint = &start;
250     req->numberOfRecordsRequested = &number;
251
252     int oid_syntax[OID_SIZE];
253     oident prefsyn;
254     int recordSyntax;
255     get_preferredRecordSyntax (&recordSyntax);
256     if (recordSyntax != VAL_NONE)
257     {
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;
263     }
264     Z_RecordComposition compo;
265     Z_ElementSetNames *elementSetNames;
266     get_elementSetName (&elementSetNames);
267     if (elementSetNames)
268     {
269         req->recordComposition = &compo;
270         compo.which = Z_RecordComp_simple;
271         compo.u.simple = elementSetNames;
272     }
273     if (m_cookie)
274         set_otherInformationString(&req->otherInfo, VAL_COOKIE, 1, m_cookie);
275     return send_Z_PDU(apdu);
276 }
277
278 void Yaz_IR_Assoc::set_proxy(const char *str)
279 {
280     delete m_proxy;
281     m_proxy = new char[strlen(str)+1];
282     strcpy (m_proxy, str);
283 }
284
285 void Yaz_IR_Assoc::set_cookie(const char *str)
286 {
287     delete m_cookie;
288     m_cookie = new char[strlen(str)+1];
289     strcpy(m_cookie, str);
290 }
291
292 const char *Yaz_IR_Assoc::get_cookie()
293 {
294     return m_cookie;
295 }
296
297 void Yaz_IR_Assoc::client(const char *addr)
298 {
299     delete [] m_host;
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, '/');
306     if (dbpart)
307     {
308         set_databaseNames (dbpart+1, "+ ");
309         *dbpart = '\0';
310     }
311     Yaz_Z_Assoc::client(zurl);
312     delete [] zurl;
313 }
314
315 const char *Yaz_IR_Assoc::get_proxy()
316 {
317     return m_proxy;
318 }
319
320 const char *Yaz_IR_Assoc::get_host()
321 {
322     return m_host;
323 }
324
325 void Yaz_IR_Assoc::recv_searchRequest(Z_SearchRequest *searchRequest)
326 {
327     Z_APDU *apdu = create_Z_PDU(Z_APDU_searchResponse);
328     send_Z_PDU(apdu);
329 }
330
331 void Yaz_IR_Assoc::recv_presentRequest(Z_PresentRequest *presentRequest)
332 {
333     Z_APDU *apdu = create_Z_PDU(Z_APDU_presentResponse);
334     send_Z_PDU(apdu);
335 }
336
337 void Yaz_IR_Assoc::recv_initRequest(Z_InitRequest *initRequest)
338 {
339     Z_APDU *apdu = create_Z_PDU(Z_APDU_initResponse);
340     send_Z_PDU(apdu);
341 }
342
343 void Yaz_IR_Assoc::recv_searchResponse (Z_SearchResponse *searchResponse)
344 {
345 }
346
347 void Yaz_IR_Assoc::recv_presentResponse (Z_PresentResponse *presentResponse)
348 {
349 }
350
351 void Yaz_IR_Assoc::recv_initResponse(Z_InitResponse *initResponse)
352 {
353 }
354
355 int Yaz_IR_Assoc::get_lastReceived()
356 {
357     return m_lastReceived;
358 }
359
360 void Yaz_IR_Assoc::set_lastReceived(int lastReceived)
361 {
362     m_lastReceived = lastReceived;
363 }
364
365 int Yaz_IR_Assoc::send_initRequest()
366 {
367     Z_APDU *apdu = create_Z_PDU(Z_APDU_initRequest);
368     Z_InitRequest *req = apdu->u.initRequest;
369     
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);
378
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);
382
383     if (m_proxy && m_host)
384         set_otherInformationString(&req->otherInfo, VAL_PROXY, 1, m_host);
385     if (m_cookie)
386         set_otherInformationString(&req->otherInfo, VAL_COOKIE, 1, m_cookie);
387     return send_Z_PDU(apdu);
388 }
389