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.11 2000-09-04 08:59:16 adam
8 * Changed call to logging functions (yaz_ added).
10 * Revision 1.10 2000/09/04 08:29:22 adam
11 * Fixed memory leak(s). Added re-use of associations, rather than
12 * re-init, when maximum number of targets are in use.
14 * Revision 1.9 2000/08/10 08:42:42 adam
15 * Fixes for {set,get}_APDU_log.
17 * Revision 1.8 2000/08/07 14:19:59 adam
18 * Fixed serious bug regarding timeouts. Improved logging for proxy.
20 * Revision 1.7 2000/05/10 11:36:58 ian
21 * Added default parameters for refid to request functions.
22 * Added default parameter for result set name to search and present request.
23 * Commented out forced logging of PDU contents.
24 * Added send_deleteResultSetRequest
26 * Revision 1.6 1999/12/06 13:52:45 adam
27 * Modified for new location of YAZ header files. Experimental threaded
30 * Revision 1.5 1999/11/10 10:02:34 adam
33 * Revision 1.4 1999/09/13 12:53:44 adam
34 * Proxy removes OtherInfo Proxy Address and Session ID. Other
35 * Otherinfo remains untouched.
37 * Revision 1.3 1999/04/21 12:09:01 adam
38 * Many improvements. Modified to proxy server to work with "sessions"
41 * Revision 1.2 1999/04/20 10:30:05 adam
42 * Implemented various stuff for client and proxy. Updated calls
43 * to ODR to reflect new name parameter.
45 * Revision 1.1 1999/04/09 11:46:57 adam
46 * Added object Yaz_Z_Assoc. Much more functional client.
53 #include <yaz-z-assoc.h>
54 #include <yaz/otherinfo.h>
56 int Yaz_Z_Assoc::yaz_init_func()
62 int Yaz_Z_Assoc::yaz_init_flag = Yaz_Z_Assoc::yaz_init_func();
64 Yaz_Z_Assoc::Yaz_Z_Assoc(IYaz_PDU_Observable *the_PDU_Observable)
66 m_PDU_Observable = the_PDU_Observable;
67 m_odr_in = odr_createmem (ODR_DECODE);
68 m_odr_out = odr_createmem (ODR_ENCODE);
69 m_odr_print = odr_createmem (ODR_PRINT);
76 void Yaz_Z_Assoc::set_APDU_log(const char *fname)
78 if (m_APDU_file && m_APDU_file != stderr)
83 delete [] m_APDU_fname;
88 m_APDU_fname = new char[strlen(fname)+1];
89 strcpy (m_APDU_fname, fname);
90 if (*fname && strcmp(fname, "-"))
91 m_APDU_file = fopen (fname, "a");
94 odr_setprint(m_odr_print, m_APDU_file);
98 const char *Yaz_Z_Assoc::get_APDU_log()
103 Yaz_Z_Assoc::~Yaz_Z_Assoc()
105 m_PDU_Observable->destroy();
106 delete m_PDU_Observable;
107 odr_destroy (m_odr_print); // note: also runs fclose on m_APDU_file ..
108 odr_destroy (m_odr_out);
109 odr_destroy (m_odr_in);
110 delete [] m_APDU_fname;
111 delete [] m_hostname;
114 void Yaz_Z_Assoc::recv_PDU(const char *buf, int len)
116 logf (m_log, "recv_PDU len=%d", len);
117 Z_APDU *apdu = decode_Z_PDU (buf, len);
124 Z_APDU *Yaz_Z_Assoc::create_Z_PDU(int type)
126 Z_APDU *apdu = zget_APDU(m_odr_out, type);
127 if (apdu->which == Z_APDU_initRequest)
129 Z_InitRequest * p = apdu->u.initRequest;
130 char *newName = (char*) odr_malloc(m_odr_out, 50);
131 strcpy (newName, p->implementationName);
132 strcat (newName, " YAZ++");
133 p->implementationName = newName;
138 int Yaz_Z_Assoc::send_Z_PDU(Z_APDU *apdu)
142 if (encode_Z_PDU(apdu, &buf, &len) > 0)
143 return m_PDU_Observable->send_PDU(buf, len);
147 Z_APDU *Yaz_Z_Assoc::decode_Z_PDU(const char *buf, int len)
151 odr_reset (m_odr_in);
152 odr_setbuf (m_odr_in, (char*) buf, len, 0);
154 if (!z_APDU(m_odr_in, &apdu, 0, 0))
156 logf(LOG_LOG, "ODR error on incoming PDU: %s [near byte %d] ",
157 odr_errmsg(odr_geterror(m_odr_in)),
158 odr_offset(m_odr_in));
159 logf(LOG_LOG, "PDU dump:");
160 odr_dumpBER(yaz_log_file(), buf, len);
166 z_APDU(m_odr_print, &apdu, 0, "decode");
171 int Yaz_Z_Assoc::encode_Z_PDU(Z_APDU *apdu, char **buf, int *len)
173 if (!z_APDU(m_odr_out, &apdu, 0, 0))
175 logf (LOG_LOG, "yaz_Z_Assoc::encode_Z_PDU failed");
179 z_APDU(m_odr_print, &apdu, 0, "encode");
180 *buf = odr_getbuf (m_odr_out, len, 0);
181 odr_reset (m_odr_out);
185 const char *Yaz_Z_Assoc::get_hostname()
190 void Yaz_Z_Assoc::client(const char *addr)
192 delete [] m_hostname;
193 m_hostname = new char[strlen(addr)+1];
194 strcpy (m_hostname, addr);
195 m_PDU_Observable->connect (this, addr);
198 void Yaz_Z_Assoc::close()
200 m_PDU_Observable->close ();
203 void Yaz_Z_Assoc::server(const char *addr)
205 delete [] m_hostname;
206 m_hostname = new char[strlen(addr)+1];
207 strcpy (m_hostname, addr);
208 m_PDU_Observable->listen (this, addr);
211 ODR Yaz_Z_Assoc::odr_encode()
216 ODR Yaz_Z_Assoc::odr_decode()
220 ODR Yaz_Z_Assoc::odr_print()
225 void Yaz_Z_Assoc::timeout(int timeout)
227 m_PDU_Observable->idleTime(timeout);
230 void Yaz_Z_Assoc::get_otherInfoAPDU(Z_APDU *apdu, Z_OtherInformation ***oip)
234 case Z_APDU_initRequest:
235 *oip = &apdu->u.initRequest->otherInfo;
237 case Z_APDU_searchRequest:
238 *oip = &apdu->u.searchRequest->otherInfo;
240 case Z_APDU_presentRequest:
241 *oip = &apdu->u.presentRequest->otherInfo;
243 case Z_APDU_sortRequest:
244 *oip = &apdu->u.sortRequest->otherInfo;
246 case Z_APDU_scanRequest:
247 *oip = &apdu->u.scanRequest->otherInfo;
249 case Z_APDU_initResponse:
250 *oip = &apdu->u.initResponse->otherInfo;
252 case Z_APDU_searchResponse:
253 *oip = &apdu->u.searchResponse->otherInfo;
255 case Z_APDU_presentResponse:
256 *oip = &apdu->u.presentResponse->otherInfo;
258 case Z_APDU_sortResponse:
259 *oip = &apdu->u.sortResponse->otherInfo;
261 case Z_APDU_scanResponse:
262 *oip = &apdu->u.scanResponse->otherInfo;
270 void Yaz_Z_Assoc::set_otherInformationString (
272 int oidval, int categoryValue,
275 Z_OtherInformation **otherInformation;
276 get_otherInfoAPDU(apdu, &otherInformation);
277 if (!otherInformation)
279 set_otherInformationString(otherInformation, oidval, categoryValue, str);
282 void Yaz_Z_Assoc::set_otherInformationString (
283 Z_OtherInformation **otherInformation,
284 int oidval, int categoryValue,
289 ent.proto = PROTO_Z3950;
290 ent.oclass = CLASS_USERINFO;
291 ent.value = (oid_value) oidval;
292 if (!oid_ent_to_oid (&ent, oid))
294 set_otherInformationString(otherInformation, oid, categoryValue, str);
297 void Yaz_Z_Assoc::set_otherInformationString (
298 Z_OtherInformation **otherInformation,
299 int *oid, int categoryValue, const char *str)
301 Z_OtherInformationUnit *oi =
302 update_otherInformation(otherInformation, 1, oid, categoryValue, 0);
305 oi->information.characterInfo = odr_strdup (odr_encode(), str);
308 Z_OtherInformationUnit *Yaz_Z_Assoc::update_otherInformation (
309 Z_OtherInformation **otherInformationP, int createFlag,
310 int *oid, int categoryValue, int deleteFlag)
312 return yaz_oi_update (otherInformationP,
313 (createFlag ? odr_encode() : 0),
314 oid, categoryValue, deleteFlag);
317 Z_ReferenceId* Yaz_Z_Assoc::getRefID(char* str)
319 Z_ReferenceId* id = NULL;
323 id = (Z_ReferenceId*) odr_malloc (m_odr_out, sizeof(*id));
324 id->size = id->len = strlen(str);
325 id->buf = (unsigned char *) str;