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.12 2000-09-05 13:57:28 adam
8 * Fixed get_otherInfoAPDU to return otherInfo for extended services.
10 * Revision 1.11 2000/09/04 08:59:16 adam
11 * Changed call to logging functions (yaz_ added).
13 * Revision 1.10 2000/09/04 08:29:22 adam
14 * Fixed memory leak(s). Added re-use of associations, rather than
15 * re-init, when maximum number of targets are in use.
17 * Revision 1.9 2000/08/10 08:42:42 adam
18 * Fixes for {set,get}_APDU_log.
20 * Revision 1.8 2000/08/07 14:19:59 adam
21 * Fixed serious bug regarding timeouts. Improved logging for proxy.
23 * Revision 1.7 2000/05/10 11:36:58 ian
24 * Added default parameters for refid to request functions.
25 * Added default parameter for result set name to search and present request.
26 * Commented out forced logging of PDU contents.
27 * Added send_deleteResultSetRequest
29 * Revision 1.6 1999/12/06 13:52:45 adam
30 * Modified for new location of YAZ header files. Experimental threaded
33 * Revision 1.5 1999/11/10 10:02:34 adam
36 * Revision 1.4 1999/09/13 12:53:44 adam
37 * Proxy removes OtherInfo Proxy Address and Session ID. Other
38 * Otherinfo remains untouched.
40 * Revision 1.3 1999/04/21 12:09:01 adam
41 * Many improvements. Modified to proxy server to work with "sessions"
44 * Revision 1.2 1999/04/20 10:30:05 adam
45 * Implemented various stuff for client and proxy. Updated calls
46 * to ODR to reflect new name parameter.
48 * Revision 1.1 1999/04/09 11:46:57 adam
49 * Added object Yaz_Z_Assoc. Much more functional client.
56 #include <yaz-z-assoc.h>
57 #include <yaz/otherinfo.h>
59 int Yaz_Z_Assoc::yaz_init_func()
65 int Yaz_Z_Assoc::yaz_init_flag = Yaz_Z_Assoc::yaz_init_func();
67 Yaz_Z_Assoc::Yaz_Z_Assoc(IYaz_PDU_Observable *the_PDU_Observable)
69 m_PDU_Observable = the_PDU_Observable;
70 m_odr_in = odr_createmem (ODR_DECODE);
71 m_odr_out = odr_createmem (ODR_ENCODE);
72 m_odr_print = odr_createmem (ODR_PRINT);
79 void Yaz_Z_Assoc::set_APDU_log(const char *fname)
81 if (m_APDU_file && m_APDU_file != stderr)
86 delete [] m_APDU_fname;
91 m_APDU_fname = new char[strlen(fname)+1];
92 strcpy (m_APDU_fname, fname);
93 if (*fname && strcmp(fname, "-"))
94 m_APDU_file = fopen (fname, "a");
97 odr_setprint(m_odr_print, m_APDU_file);
101 const char *Yaz_Z_Assoc::get_APDU_log()
106 Yaz_Z_Assoc::~Yaz_Z_Assoc()
108 m_PDU_Observable->destroy();
109 delete m_PDU_Observable;
110 odr_destroy (m_odr_print); // note: also runs fclose on m_APDU_file ..
111 odr_destroy (m_odr_out);
112 odr_destroy (m_odr_in);
113 delete [] m_APDU_fname;
114 delete [] m_hostname;
117 void Yaz_Z_Assoc::recv_PDU(const char *buf, int len)
119 logf (m_log, "recv_PDU len=%d", len);
120 Z_APDU *apdu = decode_Z_PDU (buf, len);
127 Z_APDU *Yaz_Z_Assoc::create_Z_PDU(int type)
129 Z_APDU *apdu = zget_APDU(m_odr_out, type);
130 if (apdu->which == Z_APDU_initRequest)
132 Z_InitRequest * p = apdu->u.initRequest;
133 char *newName = (char*) odr_malloc(m_odr_out, 50);
134 strcpy (newName, p->implementationName);
135 strcat (newName, " YAZ++");
136 p->implementationName = newName;
141 int Yaz_Z_Assoc::send_Z_PDU(Z_APDU *apdu)
145 if (encode_Z_PDU(apdu, &buf, &len) > 0)
146 return m_PDU_Observable->send_PDU(buf, len);
150 Z_APDU *Yaz_Z_Assoc::decode_Z_PDU(const char *buf, int len)
154 odr_reset (m_odr_in);
155 odr_setbuf (m_odr_in, (char*) buf, len, 0);
157 if (!z_APDU(m_odr_in, &apdu, 0, 0))
159 logf(LOG_LOG, "ODR error on incoming PDU: %s [near byte %d] ",
160 odr_errmsg(odr_geterror(m_odr_in)),
161 odr_offset(m_odr_in));
162 logf(LOG_LOG, "PDU dump:");
163 odr_dumpBER(yaz_log_file(), buf, len);
169 z_APDU(m_odr_print, &apdu, 0, "decode");
174 int Yaz_Z_Assoc::encode_Z_PDU(Z_APDU *apdu, char **buf, int *len)
176 if (!z_APDU(m_odr_out, &apdu, 0, 0))
178 logf (LOG_LOG, "yaz_Z_Assoc::encode_Z_PDU failed");
182 z_APDU(m_odr_print, &apdu, 0, "encode");
183 *buf = odr_getbuf (m_odr_out, len, 0);
184 odr_reset (m_odr_out);
188 const char *Yaz_Z_Assoc::get_hostname()
193 void Yaz_Z_Assoc::client(const char *addr)
195 delete [] m_hostname;
196 m_hostname = new char[strlen(addr)+1];
197 strcpy (m_hostname, addr);
198 m_PDU_Observable->connect (this, addr);
201 void Yaz_Z_Assoc::close()
203 m_PDU_Observable->close ();
206 void Yaz_Z_Assoc::server(const char *addr)
208 delete [] m_hostname;
209 m_hostname = new char[strlen(addr)+1];
210 strcpy (m_hostname, addr);
211 m_PDU_Observable->listen (this, addr);
214 ODR Yaz_Z_Assoc::odr_encode()
219 ODR Yaz_Z_Assoc::odr_decode()
223 ODR Yaz_Z_Assoc::odr_print()
228 void Yaz_Z_Assoc::timeout(int timeout)
230 m_PDU_Observable->idleTime(timeout);
233 void Yaz_Z_Assoc::get_otherInfoAPDU(Z_APDU *apdu, Z_OtherInformation ***oip)
237 case Z_APDU_initRequest:
238 *oip = &apdu->u.initRequest->otherInfo;
240 case Z_APDU_searchRequest:
241 *oip = &apdu->u.searchRequest->otherInfo;
243 case Z_APDU_presentRequest:
244 *oip = &apdu->u.presentRequest->otherInfo;
246 case Z_APDU_sortRequest:
247 *oip = &apdu->u.sortRequest->otherInfo;
249 case Z_APDU_scanRequest:
250 *oip = &apdu->u.scanRequest->otherInfo;
252 case Z_APDU_extendedServicesRequest:
253 *oip = &apdu->u.extendedServicesRequest->otherInfo;
255 case Z_APDU_deleteResultSetRequest:
256 *oip = &apdu->u.deleteResultSetRequest->otherInfo;
258 case Z_APDU_initResponse:
259 *oip = &apdu->u.initResponse->otherInfo;
261 case Z_APDU_searchResponse:
262 *oip = &apdu->u.searchResponse->otherInfo;
264 case Z_APDU_presentResponse:
265 *oip = &apdu->u.presentResponse->otherInfo;
267 case Z_APDU_sortResponse:
268 *oip = &apdu->u.sortResponse->otherInfo;
270 case Z_APDU_scanResponse:
271 *oip = &apdu->u.scanResponse->otherInfo;
273 case Z_APDU_extendedServicesResponse:
274 *oip = &apdu->u.extendedServicesResponse->otherInfo;
276 case Z_APDU_deleteResultSetResponse:
277 *oip = &apdu->u.deleteResultSetResponse->otherInfo;
285 void Yaz_Z_Assoc::set_otherInformationString (
287 int oidval, int categoryValue,
290 Z_OtherInformation **otherInformation;
291 get_otherInfoAPDU(apdu, &otherInformation);
292 if (!otherInformation)
294 set_otherInformationString(otherInformation, oidval, categoryValue, str);
297 void Yaz_Z_Assoc::set_otherInformationString (
298 Z_OtherInformation **otherInformation,
299 int oidval, int categoryValue,
304 ent.proto = PROTO_Z3950;
305 ent.oclass = CLASS_USERINFO;
306 ent.value = (oid_value) oidval;
307 if (!oid_ent_to_oid (&ent, oid))
309 set_otherInformationString(otherInformation, oid, categoryValue, str);
312 void Yaz_Z_Assoc::set_otherInformationString (
313 Z_OtherInformation **otherInformation,
314 int *oid, int categoryValue, const char *str)
316 Z_OtherInformationUnit *oi =
317 update_otherInformation(otherInformation, 1, oid, categoryValue, 0);
320 oi->information.characterInfo = odr_strdup (odr_encode(), str);
323 Z_OtherInformationUnit *Yaz_Z_Assoc::update_otherInformation (
324 Z_OtherInformation **otherInformationP, int createFlag,
325 int *oid, int categoryValue, int deleteFlag)
327 return yaz_oi_update (otherInformationP,
328 (createFlag ? odr_encode() : 0),
329 oid, categoryValue, deleteFlag);
332 Z_ReferenceId* Yaz_Z_Assoc::getRefID(char* str)
334 Z_ReferenceId* id = NULL;
338 id = (Z_ReferenceId*) odr_malloc (m_odr_out, sizeof(*id));
339 id->size = id->len = strlen(str);
340 id->buf = (unsigned char *) str;