2 * Copyright (c) 1998-1999, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
6 * $Log: yaz-z-assoc.cpp,v $
7 * Revision 1.10 2000-09-04 08:29:22 adam
8 * Fixed memory leak(s). Added re-use of associations, rather than
9 * re-init, when maximum number of targets are in use.
11 * Revision 1.9 2000/08/10 08:42:42 adam
12 * Fixes for {set,get}_APDU_log.
14 * Revision 1.8 2000/08/07 14:19:59 adam
15 * Fixed serious bug regarding timeouts. Improved logging for proxy.
17 * Revision 1.7 2000/05/10 11:36:58 ian
18 * Added default parameters for refid to request functions.
19 * Added default parameter for result set name to search and present request.
20 * Commented out forced logging of PDU contents.
21 * Added send_deleteResultSetRequest
23 * Revision 1.6 1999/12/06 13:52:45 adam
24 * Modified for new location of YAZ header files. Experimental threaded
27 * Revision 1.5 1999/11/10 10:02:34 adam
30 * Revision 1.4 1999/09/13 12:53:44 adam
31 * Proxy removes OtherInfo Proxy Address and Session ID. Other
32 * Otherinfo remains untouched.
34 * Revision 1.3 1999/04/21 12:09:01 adam
35 * Many improvements. Modified to proxy server to work with "sessions"
38 * Revision 1.2 1999/04/20 10:30:05 adam
39 * Implemented various stuff for client and proxy. Updated calls
40 * to ODR to reflect new name parameter.
42 * Revision 1.1 1999/04/09 11:46:57 adam
43 * Added object Yaz_Z_Assoc. Much more functional client.
50 #include <yaz-z-assoc.h>
51 #include <yaz/otherinfo.h>
53 int Yaz_Z_Assoc::yaz_init_func()
59 int Yaz_Z_Assoc::yaz_init_flag = Yaz_Z_Assoc::yaz_init_func();
61 Yaz_Z_Assoc::Yaz_Z_Assoc(IYaz_PDU_Observable *the_PDU_Observable)
63 m_PDU_Observable = the_PDU_Observable;
64 m_odr_in = odr_createmem (ODR_DECODE);
65 m_odr_out = odr_createmem (ODR_ENCODE);
66 m_odr_print = odr_createmem (ODR_PRINT);
73 void Yaz_Z_Assoc::set_APDU_log(const char *fname)
75 if (m_APDU_file && m_APDU_file != stderr)
80 delete [] m_APDU_fname;
85 m_APDU_fname = new char[strlen(fname)+1];
86 strcpy (m_APDU_fname, fname);
87 if (*fname && strcmp(fname, "-"))
88 m_APDU_file = fopen (fname, "a");
91 odr_setprint(m_odr_print, m_APDU_file);
95 const char *Yaz_Z_Assoc::get_APDU_log()
100 Yaz_Z_Assoc::~Yaz_Z_Assoc()
102 m_PDU_Observable->destroy();
103 delete m_PDU_Observable;
104 odr_destroy (m_odr_print); // note: also runs fclose on m_APDU_file ..
105 odr_destroy (m_odr_out);
106 odr_destroy (m_odr_in);
107 delete [] m_APDU_fname;
108 delete [] m_hostname;
111 void Yaz_Z_Assoc::recv_PDU(const char *buf, int len)
113 logf (m_log, "recv_PDU len=%d", len);
114 Z_APDU *apdu = decode_Z_PDU (buf, len);
121 Z_APDU *Yaz_Z_Assoc::create_Z_PDU(int type)
123 Z_APDU *apdu = zget_APDU(m_odr_out, type);
124 if (apdu->which == Z_APDU_initRequest)
126 Z_InitRequest * p = apdu->u.initRequest;
127 char *newName = (char*) odr_malloc(m_odr_out, 50);
128 strcpy (newName, p->implementationName);
129 strcat (newName, " YAZ++");
130 p->implementationName = newName;
135 int Yaz_Z_Assoc::send_Z_PDU(Z_APDU *apdu)
139 if (encode_Z_PDU(apdu, &buf, &len) > 0)
140 return m_PDU_Observable->send_PDU(buf, len);
144 Z_APDU *Yaz_Z_Assoc::decode_Z_PDU(const char *buf, int len)
148 odr_reset (m_odr_in);
149 odr_setbuf (m_odr_in, (char*) buf, len, 0);
151 if (!z_APDU(m_odr_in, &apdu, 0, 0))
153 logf(LOG_LOG, "ODR error on incoming PDU: %s [near byte %d] ",
154 odr_errmsg(odr_geterror(m_odr_in)),
155 odr_offset(m_odr_in));
156 logf(LOG_LOG, "PDU dump:");
157 odr_dumpBER(log_file(), buf, len);
163 z_APDU(m_odr_print, &apdu, 0, "decode");
168 int Yaz_Z_Assoc::encode_Z_PDU(Z_APDU *apdu, char **buf, int *len)
170 if (!z_APDU(m_odr_out, &apdu, 0, 0))
172 logf (LOG_LOG, "yaz_Z_Assoc::encode_Z_PDU failed");
176 z_APDU(m_odr_print, &apdu, 0, "encode");
177 *buf = odr_getbuf (m_odr_out, len, 0);
178 odr_reset (m_odr_out);
182 const char *Yaz_Z_Assoc::get_hostname()
187 void Yaz_Z_Assoc::client(const char *addr)
189 delete [] m_hostname;
190 m_hostname = new char[strlen(addr)+1];
191 strcpy (m_hostname, addr);
192 m_PDU_Observable->connect (this, addr);
195 void Yaz_Z_Assoc::close()
197 m_PDU_Observable->close ();
200 void Yaz_Z_Assoc::server(const char *addr)
202 delete [] m_hostname;
203 m_hostname = new char[strlen(addr)+1];
204 strcpy (m_hostname, addr);
205 m_PDU_Observable->listen (this, addr);
208 ODR Yaz_Z_Assoc::odr_encode()
213 ODR Yaz_Z_Assoc::odr_decode()
217 ODR Yaz_Z_Assoc::odr_print()
222 void Yaz_Z_Assoc::timeout(int timeout)
224 m_PDU_Observable->idleTime(timeout);
227 void Yaz_Z_Assoc::get_otherInfoAPDU(Z_APDU *apdu, Z_OtherInformation ***oip)
231 case Z_APDU_initRequest:
232 *oip = &apdu->u.initRequest->otherInfo;
234 case Z_APDU_searchRequest:
235 *oip = &apdu->u.searchRequest->otherInfo;
237 case Z_APDU_presentRequest:
238 *oip = &apdu->u.presentRequest->otherInfo;
240 case Z_APDU_sortRequest:
241 *oip = &apdu->u.sortRequest->otherInfo;
243 case Z_APDU_scanRequest:
244 *oip = &apdu->u.scanRequest->otherInfo;
246 case Z_APDU_initResponse:
247 *oip = &apdu->u.initResponse->otherInfo;
249 case Z_APDU_searchResponse:
250 *oip = &apdu->u.searchResponse->otherInfo;
252 case Z_APDU_presentResponse:
253 *oip = &apdu->u.presentResponse->otherInfo;
255 case Z_APDU_sortResponse:
256 *oip = &apdu->u.sortResponse->otherInfo;
258 case Z_APDU_scanResponse:
259 *oip = &apdu->u.scanResponse->otherInfo;
267 void Yaz_Z_Assoc::set_otherInformationString (
269 int oidval, int categoryValue,
272 Z_OtherInformation **otherInformation;
273 get_otherInfoAPDU(apdu, &otherInformation);
274 if (!otherInformation)
276 set_otherInformationString(otherInformation, oidval, categoryValue, str);
279 void Yaz_Z_Assoc::set_otherInformationString (
280 Z_OtherInformation **otherInformation,
281 int oidval, int categoryValue,
286 ent.proto = PROTO_Z3950;
287 ent.oclass = CLASS_USERINFO;
288 ent.value = (oid_value) oidval;
289 if (!oid_ent_to_oid (&ent, oid))
291 set_otherInformationString(otherInformation, oid, categoryValue, str);
294 void Yaz_Z_Assoc::set_otherInformationString (
295 Z_OtherInformation **otherInformation,
296 int *oid, int categoryValue, const char *str)
298 Z_OtherInformationUnit *oi =
299 update_otherInformation(otherInformation, 1, oid, categoryValue, 0);
302 oi->information.characterInfo = odr_strdup (odr_encode(), str);
305 Z_OtherInformationUnit *Yaz_Z_Assoc::update_otherInformation (
306 Z_OtherInformation **otherInformationP, int createFlag,
307 int *oid, int categoryValue, int deleteFlag)
309 return yaz_oi_update (otherInformationP,
310 (createFlag ? odr_encode() : 0),
311 oid, categoryValue, deleteFlag);
314 Z_ReferenceId* Yaz_Z_Assoc::getRefID(char* str)
316 Z_ReferenceId* id = NULL;
320 id = (Z_ReferenceId*) odr_malloc (m_odr_out, sizeof(*id));
321 id->size = id->len = strlen(str);
322 id->buf = (unsigned char *) str;