Update for YAZ 3s new OID system
[yazpp-moved-to-github.git] / src / yaz-z-assoc.cpp
1 /*
2  * Copyright (c) 1998-2007, Index Data.
3  * See the file LICENSE for details.
4  * 
5  * $Id: yaz-z-assoc.cpp,v 1.41 2007-04-12 15:00:33 adam Exp $
6  */
7
8 #include <assert.h>
9 #include <signal.h>
10
11 #include <yaz/log.h>
12 #include <yazpp/z-assoc.h>
13 #include <yaz/otherinfo.h>
14 #include <yaz/oid_db.h>
15
16 using namespace yazpp_1;
17
18 int Z_Assoc::yaz_init_func()
19 {
20 #ifndef WIN32
21     signal (SIGPIPE, SIG_IGN);
22 #endif
23     return 1;
24 }
25
26 int Z_Assoc::yaz_init_flag =  Z_Assoc::yaz_init_func();  
27
28 Z_Assoc::Z_Assoc(IPDU_Observable *the_PDU_Observable)
29 {
30     m_PDU_Observable = the_PDU_Observable;
31     m_odr_in = odr_createmem (ODR_DECODE);
32     m_odr_out = odr_createmem (ODR_ENCODE);
33     m_odr_print = odr_createmem (ODR_PRINT);
34     m_log = YLOG_DEBUG;
35     m_APDU_file = 0;
36     m_APDU_fname = 0;
37     m_hostname = 0;
38     m_APDU_yazlog = 0;
39 }
40
41 void Z_Assoc::set_APDU_log(const char *fname)
42 {
43     if (m_APDU_file && m_APDU_file != stderr)
44     {
45         fclose (m_APDU_file);
46         m_APDU_file = 0;
47     }
48     delete [] m_APDU_fname;
49     m_APDU_fname = 0;
50
51     if (fname) 
52     {
53         m_APDU_fname = new char[strlen(fname)+1];
54         strcpy (m_APDU_fname, fname);
55         if (!strcmp(fname, "-"))
56             m_APDU_file = stderr;
57         else if (*fname == '\0')
58             m_APDU_file = 0;
59         else
60             m_APDU_file = fopen (fname, "a");
61         odr_setprint(m_odr_print, m_APDU_file);
62     }
63 }
64
65 int Z_Assoc::set_APDU_yazlog(int v)
66 {
67     int old = m_APDU_yazlog;
68     m_APDU_yazlog = v;
69     return old;
70 }
71
72 const char *Z_Assoc::get_APDU_log()
73 {
74     return m_APDU_fname;
75 }
76
77 Z_Assoc::~Z_Assoc()
78 {
79     m_PDU_Observable->destroy();  
80     delete m_PDU_Observable;
81     odr_destroy (m_odr_print);     // note: also runs fclose on m_APDU_file ..
82     odr_destroy (m_odr_out);
83     odr_destroy (m_odr_in);
84     delete [] m_APDU_fname;
85     delete [] m_hostname;
86 }
87
88 void Z_Assoc::recv_PDU(const char *buf, int len)
89 {
90     yaz_log (m_log, "recv_PDU len=%d", len);
91     Z_GDU *apdu = decode_GDU (buf, len);
92     if (apdu)
93     {
94         recv_GDU (apdu, len);
95     }
96     else
97     {
98         close();
99         failNotify();
100     }
101 }
102
103 Z_APDU *Z_Assoc::create_Z_PDU(int type)
104 {
105     Z_APDU *apdu = zget_APDU(m_odr_out, type);
106     if (apdu->which == Z_APDU_initRequest)
107     {
108         Z_InitRequest * p = apdu->u.initRequest;
109         char *newName = (char*) odr_malloc(m_odr_out, 50);
110         strcpy (newName, p->implementationName);
111         strcat (newName, " YAZ++");
112         p->implementationName = newName;
113     }
114     return apdu;
115 }
116
117 Z_ReferenceId **Z_Assoc::get_referenceIdP(Z_APDU *apdu)
118 {
119     switch (apdu->which)
120     {
121     case  Z_APDU_initRequest:
122         return &apdu->u.initRequest->referenceId; 
123     case  Z_APDU_initResponse:
124         return &apdu->u.initResponse->referenceId;
125     case  Z_APDU_searchRequest:
126         return &apdu->u.searchRequest->referenceId;
127     case  Z_APDU_searchResponse:
128         return &apdu->u.searchResponse->referenceId;
129     case  Z_APDU_presentRequest:
130         return &apdu->u.presentRequest->referenceId;
131     case  Z_APDU_presentResponse:
132         return &apdu->u.presentResponse->referenceId;
133     case  Z_APDU_deleteResultSetRequest:
134         return &apdu->u.deleteResultSetRequest->referenceId;
135     case  Z_APDU_deleteResultSetResponse:
136         return &apdu->u.deleteResultSetResponse->referenceId;
137     case  Z_APDU_accessControlRequest:
138         return &apdu->u.accessControlRequest->referenceId;
139     case  Z_APDU_accessControlResponse:
140         return &apdu->u.accessControlResponse->referenceId;
141     case  Z_APDU_resourceControlRequest:
142         return &apdu->u.resourceControlRequest->referenceId;
143     case  Z_APDU_resourceControlResponse:
144         return &apdu->u.resourceControlResponse->referenceId;
145     case  Z_APDU_triggerResourceControlRequest:
146         return &apdu->u.triggerResourceControlRequest->referenceId;
147     case  Z_APDU_resourceReportRequest:
148         return &apdu->u.resourceReportRequest->referenceId;
149     case  Z_APDU_resourceReportResponse:
150         return &apdu->u.resourceReportResponse->referenceId;
151     case  Z_APDU_scanRequest:
152         return &apdu->u.scanRequest->referenceId;
153     case  Z_APDU_scanResponse:
154         return &apdu->u.scanResponse->referenceId;
155     case  Z_APDU_sortRequest:
156         return &apdu->u.sortRequest->referenceId;
157     case  Z_APDU_sortResponse:
158         return &apdu->u.sortResponse->referenceId;
159     case  Z_APDU_segmentRequest:
160         return &apdu->u.segmentRequest->referenceId;
161     case  Z_APDU_extendedServicesRequest:
162         return &apdu->u.extendedServicesRequest->referenceId;
163     case  Z_APDU_extendedServicesResponse:
164         return &apdu->u.extendedServicesResponse->referenceId;
165     case  Z_APDU_close:
166         return &apdu->u.close->referenceId;
167     }
168     return 0;
169 }
170
171 void Z_Assoc::transfer_referenceId(Z_APDU *from, Z_APDU *to)
172 {
173     Z_ReferenceId **id_from = get_referenceIdP(from);
174     Z_ReferenceId **id_to = get_referenceIdP(to);
175     if (id_from && *id_from && id_to)
176     {
177         *id_to = (Z_ReferenceId*) odr_malloc (m_odr_out, sizeof(**id_to));
178         (*id_to)->size = (*id_to)->len = (*id_from)->len;
179         (*id_to)->buf = (unsigned char*) odr_malloc (m_odr_out, (*id_to)->len);
180         memcpy ((*id_to)->buf, (*id_from)->buf, (*id_to)->len);
181     }
182     else if (id_to)
183         *id_to = 0;
184 }
185
186 int Z_Assoc::send_Z_PDU(Z_APDU *apdu, int *plen)
187 {
188     Z_GDU *gdu = (Z_GDU*) odr_malloc(odr_encode(), sizeof(*gdu));
189     gdu->which = Z_GDU_Z3950;
190     gdu->u.z3950 = apdu;
191     return send_GDU(gdu, plen);
192 }
193
194 int Z_Assoc::send_GDU(Z_GDU *apdu, int *plen)
195 {
196     char *buf;
197     int len;
198     if (encode_GDU(apdu, &buf, &len) > 0)
199     {
200         if (plen)
201             *plen = len;
202         return m_PDU_Observable->send_PDU(buf, len);
203     }
204     return -1;
205 }
206
207 Z_GDU *Z_Assoc::decode_GDU(const char *buf, int len)
208 {
209     Z_GDU *apdu;
210
211     odr_reset (m_odr_in);
212     odr_setbuf (m_odr_in, (char*) buf, len, 0);
213
214     if (!z_GDU(m_odr_in, &apdu, 0, 0))
215     {
216         const char *element = odr_getelement(m_odr_in);
217         yaz_log(YLOG_LOG, "PDU decode failed '%s' near byte %ld. Element %s",
218                 odr_errmsg(odr_geterror(m_odr_in)),
219                 (long) odr_offset(m_odr_in),
220                 element ? element : "unknown");
221         yaz_log(YLOG_LOG, "PDU dump:");
222         odr_dumpBER(yaz_log_file(), buf, len);
223         return 0;
224     }
225     else
226     {
227         if (m_APDU_yazlog)
228         {   // use YAZ log FILE
229             FILE *save = m_APDU_file;
230
231             odr_setprint(m_odr_print, yaz_log_file());
232             z_GDU(m_odr_print, &apdu, 0, "decode");
233             m_APDU_file = save;
234             odr_setprint(m_odr_print, save);
235         }
236         if (m_APDU_file)
237         {
238             z_GDU(m_odr_print, &apdu, 0, "decode");
239             fflush(m_APDU_file);
240         }
241         return apdu;
242     }
243 }
244
245 int Z_Assoc::encode_GDU(Z_GDU *apdu, char **buf, int *len)
246 {
247     const char *element = 0;
248     int r = z_GDU(m_odr_out, &apdu, 0, 0);
249
250     if (!r) // decoding failed. Get the failed element
251         element = odr_getelement(m_odr_out);
252     
253     if (m_APDU_yazlog || !r)
254     {
255         if (!r)
256             yaz_log (YLOG_LOG, "PDU encode failed. Element %s",
257                      element ? element : "unknown");
258         FILE *save = m_APDU_file;
259         FILE *yazf = yaz_log_file();
260         odr_setprint(m_odr_print, yazf); // use YAZ log FILE
261         z_GDU(m_odr_print, &apdu, 0, "encode");
262         m_APDU_file = save;
263         odr_setprint(m_odr_print, save);
264     }
265     if (m_APDU_file)
266     {
267         if (!r)
268             fprintf (m_APDU_file, "PDU encode failed. Element %s",
269                      element ? element : "unknown");
270         z_GDU(m_odr_print, &apdu, 0, "encode");
271         fflush(m_APDU_file);
272     }
273     if (!r)  // encoding failed
274         return -1;
275     *buf = odr_getbuf (m_odr_out, len, 0);
276     odr_reset (m_odr_out);
277     return *len;
278 }
279
280 const char *Z_Assoc::get_hostname()
281 {
282     return m_hostname;
283 }
284
285 int Z_Assoc::client(const char *addr)
286 {
287     delete [] m_hostname;
288     m_hostname = new char[strlen(addr)+1];
289     strcpy (m_hostname, addr);
290     return m_PDU_Observable->connect (this, addr);
291 }
292
293 void Z_Assoc::close()
294 {
295     m_PDU_Observable->close ();
296 }
297
298 int Z_Assoc::server(const char *addr)
299 {
300     delete [] m_hostname;
301     m_hostname = new char[strlen(addr)+1];
302     strcpy (m_hostname, addr);
303     return m_PDU_Observable->listen (this, addr);
304 }
305
306 ODR Z_Assoc::odr_encode()
307 {
308     return m_odr_out;
309 }
310
311 ODR Z_Assoc::odr_decode()
312 {
313     return m_odr_in;
314 }
315 ODR Z_Assoc::odr_print()
316 {
317     return m_odr_print;
318 }
319
320 void Z_Assoc::timeout(int timeout)
321 {
322     m_PDU_Observable->idleTime(timeout);
323 }
324
325 void Z_Assoc::get_otherInfoAPDU(Z_APDU *apdu, Z_OtherInformation ***oip)
326 {
327     switch (apdu->which)
328     {
329     case Z_APDU_initRequest:
330         *oip = &apdu->u.initRequest->otherInfo;
331         break;
332     case Z_APDU_searchRequest:
333         *oip = &apdu->u.searchRequest->otherInfo;
334         break;
335     case Z_APDU_presentRequest:
336         *oip = &apdu->u.presentRequest->otherInfo;
337         break;
338     case Z_APDU_sortRequest:
339         *oip = &apdu->u.sortRequest->otherInfo;
340         break;
341     case Z_APDU_scanRequest:
342         *oip = &apdu->u.scanRequest->otherInfo;
343         break;
344     case Z_APDU_extendedServicesRequest:
345         *oip = &apdu->u.extendedServicesRequest->otherInfo;
346         break;
347     case Z_APDU_deleteResultSetRequest:
348         *oip = &apdu->u.deleteResultSetRequest->otherInfo;
349         break;
350     case Z_APDU_initResponse:
351         *oip = &apdu->u.initResponse->otherInfo;
352         break;
353     case Z_APDU_searchResponse:
354         *oip = &apdu->u.searchResponse->otherInfo;
355         break;
356     case Z_APDU_presentResponse:
357         *oip = &apdu->u.presentResponse->otherInfo;
358         break;
359     case Z_APDU_sortResponse:
360         *oip = &apdu->u.sortResponse->otherInfo;
361         break;
362     case Z_APDU_scanResponse:
363         *oip = &apdu->u.scanResponse->otherInfo;
364         break;
365     case Z_APDU_extendedServicesResponse:
366         *oip = &apdu->u.extendedServicesResponse->otherInfo;
367         break;
368     case Z_APDU_deleteResultSetResponse:
369         *oip = &apdu->u.deleteResultSetResponse->otherInfo;
370         break;
371     default:
372         *oip = 0;
373         break;
374     }
375 }
376
377 void Z_Assoc::set_otherInformationString (
378     Z_APDU *apdu,
379     const char *oidname, int categoryValue,
380     const char *str)
381 {
382     Z_OtherInformation **otherInformation;
383     get_otherInfoAPDU(apdu, &otherInformation);
384     if (!otherInformation)
385         return;
386     set_otherInformationString(otherInformation, oidname, categoryValue, str);
387 }
388
389 void Z_Assoc::set_otherInformationString (
390     Z_OtherInformation **otherInformation,
391     const char *oidname, int categoryValue,
392     const char *str)
393 {
394     int *oid = yaz_string_to_oid_odr(yaz_oid_std(), CLASS_USERINFO, oidname,
395                                      odr_encode());
396     if (!oid)
397         return ;
398     set_otherInformationString(otherInformation, oid, categoryValue, str);
399 }
400
401 void Z_Assoc::set_otherInformationString (
402     Z_OtherInformation **otherInformation,
403     const int *oid, int categoryValue, const char *str)
404 {
405     Z_OtherInformationUnit *oi =
406         update_otherInformation(otherInformation, 1, oid, categoryValue, 0);
407     if (!oi)
408         return;
409     oi->information.characterInfo = odr_strdup (odr_encode(), str);
410 }
411
412 Z_OtherInformationUnit *Z_Assoc::update_otherInformation (
413     Z_OtherInformation **otherInformationP, int createFlag,
414     const int *oid, int categoryValue, int deleteFlag)
415 {
416     return yaz_oi_update (otherInformationP,
417                           (createFlag ? odr_encode() : 0),
418                           oid, categoryValue, deleteFlag);
419 }
420
421 Z_ReferenceId* Z_Assoc::getRefID(char* str)
422 {
423     Z_ReferenceId* id = NULL;
424
425     if (str)
426     {
427         id = (Z_ReferenceId*) odr_malloc (m_odr_out, sizeof(*id));
428         id->size = id->len = strlen(str);
429         id->buf = (unsigned char *) str;
430     }
431     return id;
432 }
433
434 /*
435  * Local variables:
436  * c-basic-offset: 4
437  * indent-tabs-mode: nil
438  * End:
439  * vim: shiftwidth=4 tabstop=8 expandtab
440  */
441