2 * Copyright (c) 1998-2000, Index Data.
3 * See the file LICENSE for details.
5 * $Log: yaz-z-assoc.cpp,v $
6 * Revision 1.13 2000-09-08 10:23:42 adam
7 * Added skeleton of yaz-z-server.
9 * Revision 1.12 2000/09/05 13:57:28 adam
10 * Fixed get_otherInfoAPDU to return otherInfo for extended services.
12 * Revision 1.11 2000/09/04 08:59:16 adam
13 * Changed call to logging functions (yaz_ added).
15 * Revision 1.10 2000/09/04 08:29:22 adam
16 * Fixed memory leak(s). Added re-use of associations, rather than
17 * re-init, when maximum number of targets are in use.
19 * Revision 1.9 2000/08/10 08:42:42 adam
20 * Fixes for {set,get}_APDU_log.
22 * Revision 1.8 2000/08/07 14:19:59 adam
23 * Fixed serious bug regarding timeouts. Improved logging for proxy.
25 * Revision 1.7 2000/05/10 11:36:58 ian
26 * Added default parameters for refid to request functions.
27 * Added default parameter for result set name to search and present request.
28 * Commented out forced logging of PDU contents.
29 * Added send_deleteResultSetRequest
31 * Revision 1.6 1999/12/06 13:52:45 adam
32 * Modified for new location of YAZ header files. Experimental threaded
35 * Revision 1.5 1999/11/10 10:02:34 adam
38 * Revision 1.4 1999/09/13 12:53:44 adam
39 * Proxy removes OtherInfo Proxy Address and Session ID. Other
40 * Otherinfo remains untouched.
42 * Revision 1.3 1999/04/21 12:09:01 adam
43 * Many improvements. Modified to proxy server to work with "sessions"
46 * Revision 1.2 1999/04/20 10:30:05 adam
47 * Implemented various stuff for client and proxy. Updated calls
48 * to ODR to reflect new name parameter.
50 * Revision 1.1 1999/04/09 11:46:57 adam
51 * Added object Yaz_Z_Assoc. Much more functional client.
58 #include <yaz-z-assoc.h>
59 #include <yaz/otherinfo.h>
61 int Yaz_Z_Assoc::yaz_init_func()
67 int Yaz_Z_Assoc::yaz_init_flag = Yaz_Z_Assoc::yaz_init_func();
69 Yaz_Z_Assoc::Yaz_Z_Assoc(IYaz_PDU_Observable *the_PDU_Observable)
71 m_PDU_Observable = the_PDU_Observable;
72 m_odr_in = odr_createmem (ODR_DECODE);
73 m_odr_out = odr_createmem (ODR_ENCODE);
74 m_odr_print = odr_createmem (ODR_PRINT);
81 void Yaz_Z_Assoc::set_APDU_log(const char *fname)
83 if (m_APDU_file && m_APDU_file != stderr)
88 delete [] m_APDU_fname;
93 m_APDU_fname = new char[strlen(fname)+1];
94 strcpy (m_APDU_fname, fname);
95 if (*fname && strcmp(fname, "-"))
96 m_APDU_file = fopen (fname, "a");
99 odr_setprint(m_odr_print, m_APDU_file);
103 const char *Yaz_Z_Assoc::get_APDU_log()
108 Yaz_Z_Assoc::~Yaz_Z_Assoc()
110 m_PDU_Observable->destroy();
111 delete m_PDU_Observable;
112 odr_destroy (m_odr_print); // note: also runs fclose on m_APDU_file ..
113 odr_destroy (m_odr_out);
114 odr_destroy (m_odr_in);
115 delete [] m_APDU_fname;
116 delete [] m_hostname;
119 void Yaz_Z_Assoc::recv_PDU(const char *buf, int len)
121 logf (m_log, "recv_PDU len=%d", len);
122 Z_APDU *apdu = decode_Z_PDU (buf, len);
129 Z_APDU *Yaz_Z_Assoc::create_Z_PDU(int type)
131 Z_APDU *apdu = zget_APDU(m_odr_out, type);
132 if (apdu->which == Z_APDU_initRequest)
134 Z_InitRequest * p = apdu->u.initRequest;
135 char *newName = (char*) odr_malloc(m_odr_out, 50);
136 strcpy (newName, p->implementationName);
137 strcat (newName, " YAZ++");
138 p->implementationName = newName;
143 int Yaz_Z_Assoc::send_Z_PDU(Z_APDU *apdu)
147 if (encode_Z_PDU(apdu, &buf, &len) > 0)
148 return m_PDU_Observable->send_PDU(buf, len);
152 Z_APDU *Yaz_Z_Assoc::decode_Z_PDU(const char *buf, int len)
156 odr_reset (m_odr_in);
157 odr_setbuf (m_odr_in, (char*) buf, len, 0);
159 if (!z_APDU(m_odr_in, &apdu, 0, 0))
161 logf(LOG_LOG, "ODR error on incoming PDU: %s [near byte %d] ",
162 odr_errmsg(odr_geterror(m_odr_in)),
163 odr_offset(m_odr_in));
164 logf(LOG_LOG, "PDU dump:");
165 odr_dumpBER(yaz_log_file(), buf, len);
171 z_APDU(m_odr_print, &apdu, 0, "decode");
176 int Yaz_Z_Assoc::encode_Z_PDU(Z_APDU *apdu, char **buf, int *len)
178 if (!z_APDU(m_odr_out, &apdu, 0, 0))
180 logf (LOG_LOG, "yaz_Z_Assoc::encode_Z_PDU failed");
184 z_APDU(m_odr_print, &apdu, 0, "encode");
185 *buf = odr_getbuf (m_odr_out, len, 0);
186 odr_reset (m_odr_out);
190 const char *Yaz_Z_Assoc::get_hostname()
195 void Yaz_Z_Assoc::client(const char *addr)
197 delete [] m_hostname;
198 m_hostname = new char[strlen(addr)+1];
199 strcpy (m_hostname, addr);
200 m_PDU_Observable->connect (this, addr);
203 void Yaz_Z_Assoc::close()
205 m_PDU_Observable->close ();
208 void Yaz_Z_Assoc::server(const char *addr)
210 delete [] m_hostname;
211 m_hostname = new char[strlen(addr)+1];
212 strcpy (m_hostname, addr);
213 m_PDU_Observable->listen (this, addr);
216 ODR Yaz_Z_Assoc::odr_encode()
221 ODR Yaz_Z_Assoc::odr_decode()
225 ODR Yaz_Z_Assoc::odr_print()
230 void Yaz_Z_Assoc::timeout(int timeout)
232 m_PDU_Observable->idleTime(timeout);
235 void Yaz_Z_Assoc::get_otherInfoAPDU(Z_APDU *apdu, Z_OtherInformation ***oip)
239 case Z_APDU_initRequest:
240 *oip = &apdu->u.initRequest->otherInfo;
242 case Z_APDU_searchRequest:
243 *oip = &apdu->u.searchRequest->otherInfo;
245 case Z_APDU_presentRequest:
246 *oip = &apdu->u.presentRequest->otherInfo;
248 case Z_APDU_sortRequest:
249 *oip = &apdu->u.sortRequest->otherInfo;
251 case Z_APDU_scanRequest:
252 *oip = &apdu->u.scanRequest->otherInfo;
254 case Z_APDU_extendedServicesRequest:
255 *oip = &apdu->u.extendedServicesRequest->otherInfo;
257 case Z_APDU_deleteResultSetRequest:
258 *oip = &apdu->u.deleteResultSetRequest->otherInfo;
260 case Z_APDU_initResponse:
261 *oip = &apdu->u.initResponse->otherInfo;
263 case Z_APDU_searchResponse:
264 *oip = &apdu->u.searchResponse->otherInfo;
266 case Z_APDU_presentResponse:
267 *oip = &apdu->u.presentResponse->otherInfo;
269 case Z_APDU_sortResponse:
270 *oip = &apdu->u.sortResponse->otherInfo;
272 case Z_APDU_scanResponse:
273 *oip = &apdu->u.scanResponse->otherInfo;
275 case Z_APDU_extendedServicesResponse:
276 *oip = &apdu->u.extendedServicesResponse->otherInfo;
278 case Z_APDU_deleteResultSetResponse:
279 *oip = &apdu->u.deleteResultSetResponse->otherInfo;
287 void Yaz_Z_Assoc::set_otherInformationString (
289 int oidval, int categoryValue,
292 Z_OtherInformation **otherInformation;
293 get_otherInfoAPDU(apdu, &otherInformation);
294 if (!otherInformation)
296 set_otherInformationString(otherInformation, oidval, categoryValue, str);
299 void Yaz_Z_Assoc::set_otherInformationString (
300 Z_OtherInformation **otherInformation,
301 int oidval, int categoryValue,
306 ent.proto = PROTO_Z3950;
307 ent.oclass = CLASS_USERINFO;
308 ent.value = (oid_value) oidval;
309 if (!oid_ent_to_oid (&ent, oid))
311 set_otherInformationString(otherInformation, oid, categoryValue, str);
314 void Yaz_Z_Assoc::set_otherInformationString (
315 Z_OtherInformation **otherInformation,
316 int *oid, int categoryValue, const char *str)
318 Z_OtherInformationUnit *oi =
319 update_otherInformation(otherInformation, 1, oid, categoryValue, 0);
322 oi->information.characterInfo = odr_strdup (odr_encode(), str);
325 Z_OtherInformationUnit *Yaz_Z_Assoc::update_otherInformation (
326 Z_OtherInformation **otherInformationP, int createFlag,
327 int *oid, int categoryValue, int deleteFlag)
329 return yaz_oi_update (otherInformationP,
330 (createFlag ? odr_encode() : 0),
331 oid, categoryValue, deleteFlag);
334 Z_ReferenceId* Yaz_Z_Assoc::getRefID(char* str)
336 Z_ReferenceId* id = NULL;
340 id = (Z_ReferenceId*) odr_malloc (m_odr_out, sizeof(*id));
341 id->size = id->len = strlen(str);
342 id->buf = (unsigned char *) str;