2 * Copyright (c) 1998-2000, Index Data.
3 * See the file LICENSE for details.
5 * $Log: yaz-z-assoc.cpp,v $
6 * Revision 1.14 2000-09-12 16:40:33 heikki
9 * Revision 1.13 2000/09/08 10:23:42 adam
10 * Added skeleton of yaz-z-server.
12 * Revision 1.12 2000/09/05 13:57:28 adam
13 * Fixed get_otherInfoAPDU to return otherInfo for extended services.
15 * Revision 1.11 2000/09/04 08:59:16 adam
16 * Changed call to logging functions (yaz_ added).
18 * Revision 1.10 2000/09/04 08:29:22 adam
19 * Fixed memory leak(s). Added re-use of associations, rather than
20 * re-init, when maximum number of targets are in use.
22 * Revision 1.9 2000/08/10 08:42:42 adam
23 * Fixes for {set,get}_APDU_log.
25 * Revision 1.8 2000/08/07 14:19:59 adam
26 * Fixed serious bug regarding timeouts. Improved logging for proxy.
28 * Revision 1.7 2000/05/10 11:36:58 ian
29 * Added default parameters for refid to request functions.
30 * Added default parameter for result set name to search and present request.
31 * Commented out forced logging of PDU contents.
32 * Added send_deleteResultSetRequest
34 * Revision 1.6 1999/12/06 13:52:45 adam
35 * Modified for new location of YAZ header files. Experimental threaded
38 * Revision 1.5 1999/11/10 10:02:34 adam
41 * Revision 1.4 1999/09/13 12:53:44 adam
42 * Proxy removes OtherInfo Proxy Address and Session ID. Other
43 * Otherinfo remains untouched.
45 * Revision 1.3 1999/04/21 12:09:01 adam
46 * Many improvements. Modified to proxy server to work with "sessions"
49 * Revision 1.2 1999/04/20 10:30:05 adam
50 * Implemented various stuff for client and proxy. Updated calls
51 * to ODR to reflect new name parameter.
53 * Revision 1.1 1999/04/09 11:46:57 adam
54 * Added object Yaz_Z_Assoc. Much more functional client.
61 #include <yaz-z-assoc.h>
62 #include <yaz/otherinfo.h>
64 int Yaz_Z_Assoc::yaz_init_func()
70 int Yaz_Z_Assoc::yaz_init_flag = Yaz_Z_Assoc::yaz_init_func();
72 Yaz_Z_Assoc::Yaz_Z_Assoc(IYaz_PDU_Observable *the_PDU_Observable)
74 m_PDU_Observable = the_PDU_Observable;
75 m_odr_in = odr_createmem (ODR_DECODE);
76 m_odr_out = odr_createmem (ODR_ENCODE);
77 m_odr_print = odr_createmem (ODR_PRINT);
84 void Yaz_Z_Assoc::set_APDU_log(const char *fname)
86 if (m_APDU_file && m_APDU_file != stderr)
91 delete [] m_APDU_fname;
96 m_APDU_fname = new char[strlen(fname)+1];
97 strcpy (m_APDU_fname, fname);
98 if (*fname && strcmp(fname, "-"))
99 m_APDU_file = fopen (fname, "a");
101 m_APDU_file = stderr;
102 odr_setprint(m_odr_print, m_APDU_file);
106 const char *Yaz_Z_Assoc::get_APDU_log()
111 Yaz_Z_Assoc::~Yaz_Z_Assoc()
113 m_PDU_Observable->destroy();
114 delete m_PDU_Observable;
115 odr_destroy (m_odr_print); // note: also runs fclose on m_APDU_file ..
116 odr_destroy (m_odr_out);
117 odr_destroy (m_odr_in);
118 delete [] m_APDU_fname;
119 delete [] m_hostname;
122 void Yaz_Z_Assoc::recv_PDU(const char *buf, int len)
124 logf (m_log, "recv_PDU len=%d", len);
125 Z_APDU *apdu = decode_Z_PDU (buf, len);
132 Z_APDU *Yaz_Z_Assoc::create_Z_PDU(int type)
134 Z_APDU *apdu = zget_APDU(m_odr_out, type);
135 if (apdu->which == Z_APDU_initRequest)
137 Z_InitRequest * p = apdu->u.initRequest;
138 char *newName = (char*) odr_malloc(m_odr_out, 50);
139 strcpy (newName, p->implementationName);
140 strcat (newName, " YAZ++");
141 p->implementationName = newName;
146 int Yaz_Z_Assoc::send_Z_PDU(Z_APDU *apdu)
150 if (encode_Z_PDU(apdu, &buf, &len) > 0)
151 return m_PDU_Observable->send_PDU(buf, len);
155 Z_APDU *Yaz_Z_Assoc::decode_Z_PDU(const char *buf, int len)
159 odr_reset (m_odr_in);
160 odr_setbuf (m_odr_in, (char*) buf, len, 0);
162 if (!z_APDU(m_odr_in, &apdu, 0, 0))
164 logf(LOG_LOG, "ODR error on incoming PDU: %s [near byte %d] ",
165 odr_errmsg(odr_geterror(m_odr_in)),
166 odr_offset(m_odr_in));
167 logf(LOG_LOG, "PDU dump:");
168 odr_dumpBER(yaz_log_file(), buf, len);
174 z_APDU(m_odr_print, &apdu, 0, "decode");
179 int Yaz_Z_Assoc::encode_Z_PDU(Z_APDU *apdu, char **buf, int *len)
181 if (!z_APDU(m_odr_out, &apdu, 0, 0))
183 logf (LOG_LOG, "yaz_Z_Assoc::encode_Z_PDU failed");
187 z_APDU(m_odr_print, &apdu, 0, "encode");
188 *buf = odr_getbuf (m_odr_out, len, 0);
189 odr_reset (m_odr_out);
193 const char *Yaz_Z_Assoc::get_hostname()
198 void Yaz_Z_Assoc::client(const char *addr)
200 delete [] m_hostname;
201 m_hostname = new char[strlen(addr)+1];
202 strcpy (m_hostname, addr);
203 m_PDU_Observable->connect (this, addr);
206 void Yaz_Z_Assoc::close()
208 m_PDU_Observable->close ();
211 void Yaz_Z_Assoc::server(const char *addr)
213 delete [] m_hostname;
214 m_hostname = new char[strlen(addr)+1];
215 strcpy (m_hostname, addr);
216 m_PDU_Observable->listen (this, addr);
219 ODR Yaz_Z_Assoc::odr_encode()
224 ODR Yaz_Z_Assoc::odr_decode()
228 ODR Yaz_Z_Assoc::odr_print()
233 void Yaz_Z_Assoc::timeout(int timeout)
235 m_PDU_Observable->idleTime(timeout);
238 void Yaz_Z_Assoc::get_otherInfoAPDU(Z_APDU *apdu, Z_OtherInformation ***oip)
242 case Z_APDU_initRequest:
243 *oip = &apdu->u.initRequest->otherInfo;
245 case Z_APDU_searchRequest:
246 *oip = &apdu->u.searchRequest->otherInfo;
248 case Z_APDU_presentRequest:
249 *oip = &apdu->u.presentRequest->otherInfo;
251 case Z_APDU_sortRequest:
252 *oip = &apdu->u.sortRequest->otherInfo;
254 case Z_APDU_scanRequest:
255 *oip = &apdu->u.scanRequest->otherInfo;
257 case Z_APDU_extendedServicesRequest:
258 *oip = &apdu->u.extendedServicesRequest->otherInfo;
260 case Z_APDU_deleteResultSetRequest:
261 *oip = &apdu->u.deleteResultSetRequest->otherInfo;
263 case Z_APDU_initResponse:
264 *oip = &apdu->u.initResponse->otherInfo;
266 case Z_APDU_searchResponse:
267 *oip = &apdu->u.searchResponse->otherInfo;
269 case Z_APDU_presentResponse:
270 *oip = &apdu->u.presentResponse->otherInfo;
272 case Z_APDU_sortResponse:
273 *oip = &apdu->u.sortResponse->otherInfo;
275 case Z_APDU_scanResponse:
276 *oip = &apdu->u.scanResponse->otherInfo;
278 case Z_APDU_extendedServicesResponse:
279 *oip = &apdu->u.extendedServicesResponse->otherInfo;
281 case Z_APDU_deleteResultSetResponse:
282 *oip = &apdu->u.deleteResultSetResponse->otherInfo;
290 void Yaz_Z_Assoc::set_otherInformationString (
292 int oidval, int categoryValue,
295 Z_OtherInformation **otherInformation;
296 get_otherInfoAPDU(apdu, &otherInformation);
297 if (!otherInformation)
299 set_otherInformationString(otherInformation, oidval, categoryValue, str);
302 void Yaz_Z_Assoc::set_otherInformationString (
303 Z_OtherInformation **otherInformation,
304 int oidval, int categoryValue,
309 ent.proto = PROTO_Z3950;
310 ent.oclass = CLASS_USERINFO;
311 ent.value = (oid_value) oidval;
312 if (!oid_ent_to_oid (&ent, oid))
314 set_otherInformationString(otherInformation, oid, categoryValue, str);
317 void Yaz_Z_Assoc::set_otherInformationString (
318 Z_OtherInformation **otherInformation,
319 int *oid, int categoryValue, const char *str)
321 Z_OtherInformationUnit *oi =
322 update_otherInformation(otherInformation, 1, oid, categoryValue, 0);
325 oi->information.characterInfo = odr_strdup (odr_encode(), str);
328 Z_OtherInformationUnit *Yaz_Z_Assoc::update_otherInformation (
329 Z_OtherInformation **otherInformationP, int createFlag,
330 int *oid, int categoryValue, int deleteFlag)
332 return yaz_oi_update (otherInformationP,
333 (createFlag ? odr_encode() : 0),
334 oid, categoryValue, deleteFlag);
337 Z_ReferenceId* Yaz_Z_Assoc::getRefID(char* str)
339 Z_ReferenceId* id = NULL;
343 id = (Z_ReferenceId*) odr_malloc (m_odr_out, sizeof(*id));
344 id->size = id->len = strlen(str);
345 id->buf = (unsigned char *) str;