New threaded PDU association.
[yazpp-moved-to-github.git] / src / yaz-z-assoc.cpp
1 /*
2  * Copyright (c) 1998-2000, Index Data.
3  * See the file LICENSE for details.
4  * 
5  * $Log: yaz-z-assoc.cpp,v $
6  * Revision 1.18  2001-03-26 14:43:49  adam
7  * New threaded PDU association.
8  *
9  * Revision 1.17  2001/01/04 14:25:25  heikki
10  * No SIGPIPES in windows...
11  *
12  * Revision 1.16  2000/12/14 16:00:39  adam
13  * Ignoring signal SIGPIPE.
14  *
15  * Revision 1.15  2000/10/11 11:58:17  adam
16  * Moved header files to include/yaz++. Switched to libtool and automake.
17  * Configure script creates yaz++-config script.
18  *
19  * Revision 1.14  2000/09/12 16:40:33  heikki
20  * minor
21  *
22  * Revision 1.13  2000/09/08 10:23:42  adam
23  * Added skeleton of yaz-z-server.
24  *
25  * Revision 1.12  2000/09/05 13:57:28  adam
26  * Fixed get_otherInfoAPDU to return otherInfo for extended services.
27  *
28  * Revision 1.11  2000/09/04 08:59:16  adam
29  * Changed call to logging functions (yaz_ added).
30  *
31  * Revision 1.10  2000/09/04 08:29:22  adam
32  * Fixed memory leak(s). Added re-use of associations, rather than
33  * re-init, when maximum number of targets are in use.
34  *
35  * Revision 1.9  2000/08/10 08:42:42  adam
36  * Fixes for {set,get}_APDU_log.
37  *
38  * Revision 1.8  2000/08/07 14:19:59  adam
39  * Fixed serious bug regarding timeouts. Improved logging for proxy.
40  *
41  * Revision 1.7  2000/05/10 11:36:58  ian
42  * Added default parameters for refid to request functions.
43  * Added default parameter for result set name to search and present request.
44  * Commented out forced logging of PDU contents.
45  * Added send_deleteResultSetRequest
46  *
47  * Revision 1.6  1999/12/06 13:52:45  adam
48  * Modified for new location of YAZ header files. Experimental threaded
49  * operation.
50  *
51  * Revision 1.5  1999/11/10 10:02:34  adam
52  * Work on proxy.
53  *
54  * Revision 1.4  1999/09/13 12:53:44  adam
55  * Proxy removes OtherInfo Proxy Address and Session ID. Other
56  * Otherinfo remains untouched.
57  *
58  * Revision 1.3  1999/04/21 12:09:01  adam
59  * Many improvements. Modified to proxy server to work with "sessions"
60  * based on cookies.
61  *
62  * Revision 1.2  1999/04/20 10:30:05  adam
63  * Implemented various stuff for client and proxy. Updated calls
64  * to ODR to reflect new name parameter.
65  *
66  * Revision 1.1  1999/04/09 11:46:57  adam
67  * Added object Yaz_Z_Assoc. Much more functional client.
68  *
69  */
70
71 #include <assert.h>
72 #include <signal.h>
73
74 #include <yaz/log.h>
75 #include <yaz++/yaz-z-assoc.h>
76 #include <yaz/otherinfo.h>
77
78 int Yaz_Z_Assoc::yaz_init_func()
79 {
80 #ifndef WIN32
81     signal (SIGPIPE, SIG_IGN);
82 #endif
83     nmem_init();
84     return 1;
85 }
86
87 int Yaz_Z_Assoc::yaz_init_flag =  Yaz_Z_Assoc::yaz_init_func();  
88
89 Yaz_Z_Assoc::Yaz_Z_Assoc(IYaz_PDU_Observable *the_PDU_Observable)
90 {
91     m_PDU_Observable = the_PDU_Observable;
92     m_odr_in = odr_createmem (ODR_DECODE);
93     m_odr_out = odr_createmem (ODR_ENCODE);
94     m_odr_print = odr_createmem (ODR_PRINT);
95     m_log = LOG_DEBUG;
96     m_APDU_file = 0;
97     m_APDU_fname = 0;
98     m_hostname = 0;
99 }
100
101 void Yaz_Z_Assoc::set_APDU_log(const char *fname)
102 {
103     if (m_APDU_file && m_APDU_file != stderr)
104     {
105         fclose (m_APDU_file);
106         m_APDU_file = 0;
107     }
108     delete [] m_APDU_fname;
109     m_APDU_fname = 0;
110
111     if (fname) 
112     {
113         m_APDU_fname = new char[strlen(fname)+1];
114         strcpy (m_APDU_fname, fname);
115         if (*fname && strcmp(fname, "-"))
116             m_APDU_file = fopen (fname, "a");
117         else
118             m_APDU_file = stderr;
119         odr_setprint(m_odr_print, m_APDU_file);
120     }
121 }
122
123 const char *Yaz_Z_Assoc::get_APDU_log()
124 {
125     return m_APDU_fname;
126 }
127
128 Yaz_Z_Assoc::~Yaz_Z_Assoc()
129 {
130     m_PDU_Observable->destroy();  
131     delete m_PDU_Observable;
132     odr_destroy (m_odr_print);     // note: also runs fclose on m_APDU_file ..
133     odr_destroy (m_odr_out);
134     odr_destroy (m_odr_in);
135     delete [] m_APDU_fname;
136     delete [] m_hostname;
137 }
138
139 void Yaz_Z_Assoc::recv_PDU(const char *buf, int len)
140 {
141     logf (m_log, "recv_PDU len=%d", len);
142     Z_APDU *apdu = decode_Z_PDU (buf, len);
143     if (apdu)
144     {
145         recv_Z_PDU (apdu);
146     }
147     else
148     {
149         close();
150     }
151 }
152
153 Z_APDU *Yaz_Z_Assoc::create_Z_PDU(int type)
154 {
155     Z_APDU *apdu = zget_APDU(m_odr_out, type);
156     if (apdu->which == Z_APDU_initRequest)
157     {
158         Z_InitRequest * p = apdu->u.initRequest;
159         char *newName = (char*) odr_malloc(m_odr_out, 50);
160         strcpy (newName, p->implementationName);
161         strcat (newName, " YAZ++");
162         p->implementationName = newName;
163     }
164     return apdu;
165 }
166
167 int Yaz_Z_Assoc::send_Z_PDU(Z_APDU *apdu)
168 {
169     char *buf;
170     int len;
171     if (encode_Z_PDU(apdu, &buf, &len) > 0)
172         return m_PDU_Observable->send_PDU(buf, len);
173     return -1;
174 }
175
176 Z_APDU *Yaz_Z_Assoc::decode_Z_PDU(const char *buf, int len)
177 {
178     Z_APDU *apdu;
179
180     odr_reset (m_odr_in);
181     odr_setbuf (m_odr_in, (char*) buf, len, 0);
182
183     if (!z_APDU(m_odr_in, &apdu, 0, 0))
184     {
185         logf(LOG_LOG, "ODR error on incoming PDU: %s [near byte %d] ",
186              odr_errmsg(odr_geterror(m_odr_in)),
187              odr_offset(m_odr_in));
188         logf(LOG_LOG, "PDU dump:");
189         odr_dumpBER(yaz_log_file(), buf, len);
190         return 0;
191     }
192     else
193     {
194         if (m_APDU_file)
195             z_APDU(m_odr_print, &apdu, 0, "decode");
196         return apdu;
197     }
198 }
199
200 int Yaz_Z_Assoc::encode_Z_PDU(Z_APDU *apdu, char **buf, int *len)
201 {
202     if (!z_APDU(m_odr_out, &apdu, 0, 0))
203     {
204         logf (LOG_LOG, "yaz_Z_Assoc::encode_Z_PDU failed");
205         return -1;
206     }
207     if (m_APDU_file)
208         z_APDU(m_odr_print, &apdu, 0, "encode");
209     *buf = odr_getbuf (m_odr_out, len, 0);
210     odr_reset (m_odr_out);
211     return *len;
212 }
213
214 const char *Yaz_Z_Assoc::get_hostname()
215 {
216     return m_hostname;
217 }
218
219 void Yaz_Z_Assoc::client(const char *addr)
220 {
221     delete [] m_hostname;
222     m_hostname = new char[strlen(addr)+1];
223     strcpy (m_hostname, addr);
224     m_PDU_Observable->connect (this, addr);
225 }
226
227 void Yaz_Z_Assoc::close()
228 {
229     m_PDU_Observable->close ();
230 }
231
232 void Yaz_Z_Assoc::server(const char *addr)
233 {
234     delete [] m_hostname;
235     m_hostname = new char[strlen(addr)+1];
236     strcpy (m_hostname, addr);
237     m_PDU_Observable->listen (this, addr);
238 }
239
240 ODR Yaz_Z_Assoc::odr_encode()
241 {
242     return m_odr_out;
243 }
244
245 ODR Yaz_Z_Assoc::odr_decode()
246 {
247     return m_odr_in;
248 }
249 ODR Yaz_Z_Assoc::odr_print()
250 {
251     return m_odr_print;
252 }
253
254 void Yaz_Z_Assoc::timeout(int timeout)
255 {
256     m_PDU_Observable->idleTime(timeout);
257 }
258
259 void Yaz_Z_Assoc::get_otherInfoAPDU(Z_APDU *apdu, Z_OtherInformation ***oip)
260 {
261     switch (apdu->which)
262     {
263     case Z_APDU_initRequest:
264         *oip = &apdu->u.initRequest->otherInfo;
265         break;
266     case Z_APDU_searchRequest:
267         *oip = &apdu->u.searchRequest->otherInfo;
268         break;
269     case Z_APDU_presentRequest:
270         *oip = &apdu->u.presentRequest->otherInfo;
271         break;
272     case Z_APDU_sortRequest:
273         *oip = &apdu->u.sortRequest->otherInfo;
274         break;
275     case Z_APDU_scanRequest:
276         *oip = &apdu->u.scanRequest->otherInfo;
277         break;
278     case Z_APDU_extendedServicesRequest:
279         *oip = &apdu->u.extendedServicesRequest->otherInfo;
280         break;
281     case Z_APDU_deleteResultSetRequest:
282         *oip = &apdu->u.deleteResultSetRequest->otherInfo;
283         break;
284     case Z_APDU_initResponse:
285         *oip = &apdu->u.initResponse->otherInfo;
286         break;
287     case Z_APDU_searchResponse:
288         *oip = &apdu->u.searchResponse->otherInfo;
289         break;
290     case Z_APDU_presentResponse:
291         *oip = &apdu->u.presentResponse->otherInfo;
292         break;
293     case Z_APDU_sortResponse:
294         *oip = &apdu->u.sortResponse->otherInfo;
295         break;
296     case Z_APDU_scanResponse:
297         *oip = &apdu->u.scanResponse->otherInfo;
298         break;
299     case Z_APDU_extendedServicesResponse:
300         *oip = &apdu->u.extendedServicesResponse->otherInfo;
301         break;
302     case Z_APDU_deleteResultSetResponse:
303         *oip = &apdu->u.deleteResultSetResponse->otherInfo;
304         break;
305     default:
306         *oip = 0;
307         break;
308     }
309 }
310
311 void Yaz_Z_Assoc::set_otherInformationString (
312     Z_APDU *apdu,
313     int oidval, int categoryValue,
314     const char *str)
315 {
316     Z_OtherInformation **otherInformation;
317     get_otherInfoAPDU(apdu, &otherInformation);
318     if (!otherInformation)
319         return;
320     set_otherInformationString(otherInformation, oidval, categoryValue, str);
321 }
322
323 void Yaz_Z_Assoc::set_otherInformationString (
324     Z_OtherInformation **otherInformation,
325     int oidval, int categoryValue,
326     const char *str)
327 {
328     int oid[OID_SIZE];
329     struct oident ent;
330     ent.proto = PROTO_Z3950;
331     ent.oclass = CLASS_USERINFO;
332     ent.value = (oid_value) oidval;
333     if (!oid_ent_to_oid (&ent, oid))
334         return ;
335     set_otherInformationString(otherInformation, oid, categoryValue, str);
336 }
337
338 void Yaz_Z_Assoc::set_otherInformationString (
339     Z_OtherInformation **otherInformation,
340     int *oid, int categoryValue, const char *str)
341 {
342     Z_OtherInformationUnit *oi =
343         update_otherInformation(otherInformation, 1, oid, categoryValue, 0);
344     if (!oi)
345         return;
346     oi->information.characterInfo = odr_strdup (odr_encode(), str);
347 }
348
349 Z_OtherInformationUnit *Yaz_Z_Assoc::update_otherInformation (
350     Z_OtherInformation **otherInformationP, int createFlag,
351     int *oid, int categoryValue, int deleteFlag)
352 {
353     return yaz_oi_update (otherInformationP,
354                           (createFlag ? odr_encode() : 0),
355                           oid, categoryValue, deleteFlag);
356 }
357
358 Z_ReferenceId* Yaz_Z_Assoc::getRefID(char* str)
359 {
360     Z_ReferenceId* id = NULL;
361
362     if ( str )
363     {
364         id = (Z_ReferenceId*) odr_malloc (m_odr_out, sizeof(*id));
365         id->size = id->len = strlen(str);
366         id->buf = (unsigned char *) str;
367     }
368
369     return id;
370 }
371