2 * Copyright (c) 1998-2000, Index Data.
3 * See the file LICENSE for details.
5 * $Log: yaz-z-assoc.cpp,v $
6 * Revision 1.15 2000-10-11 11:58:17 adam
7 * Moved header files to include/yaz++. Switched to libtool and automake.
8 * Configure script creates yaz++-config script.
10 * Revision 1.14 2000/09/12 16:40:33 heikki
13 * Revision 1.13 2000/09/08 10:23:42 adam
14 * Added skeleton of yaz-z-server.
16 * Revision 1.12 2000/09/05 13:57:28 adam
17 * Fixed get_otherInfoAPDU to return otherInfo for extended services.
19 * Revision 1.11 2000/09/04 08:59:16 adam
20 * Changed call to logging functions (yaz_ added).
22 * Revision 1.10 2000/09/04 08:29:22 adam
23 * Fixed memory leak(s). Added re-use of associations, rather than
24 * re-init, when maximum number of targets are in use.
26 * Revision 1.9 2000/08/10 08:42:42 adam
27 * Fixes for {set,get}_APDU_log.
29 * Revision 1.8 2000/08/07 14:19:59 adam
30 * Fixed serious bug regarding timeouts. Improved logging for proxy.
32 * Revision 1.7 2000/05/10 11:36:58 ian
33 * Added default parameters for refid to request functions.
34 * Added default parameter for result set name to search and present request.
35 * Commented out forced logging of PDU contents.
36 * Added send_deleteResultSetRequest
38 * Revision 1.6 1999/12/06 13:52:45 adam
39 * Modified for new location of YAZ header files. Experimental threaded
42 * Revision 1.5 1999/11/10 10:02:34 adam
45 * Revision 1.4 1999/09/13 12:53:44 adam
46 * Proxy removes OtherInfo Proxy Address and Session ID. Other
47 * Otherinfo remains untouched.
49 * Revision 1.3 1999/04/21 12:09:01 adam
50 * Many improvements. Modified to proxy server to work with "sessions"
53 * Revision 1.2 1999/04/20 10:30:05 adam
54 * Implemented various stuff for client and proxy. Updated calls
55 * to ODR to reflect new name parameter.
57 * Revision 1.1 1999/04/09 11:46:57 adam
58 * Added object Yaz_Z_Assoc. Much more functional client.
65 #include <yaz++/yaz-z-assoc.h>
66 #include <yaz/otherinfo.h>
68 int Yaz_Z_Assoc::yaz_init_func()
74 int Yaz_Z_Assoc::yaz_init_flag = Yaz_Z_Assoc::yaz_init_func();
76 Yaz_Z_Assoc::Yaz_Z_Assoc(IYaz_PDU_Observable *the_PDU_Observable)
78 m_PDU_Observable = the_PDU_Observable;
79 m_odr_in = odr_createmem (ODR_DECODE);
80 m_odr_out = odr_createmem (ODR_ENCODE);
81 m_odr_print = odr_createmem (ODR_PRINT);
88 void Yaz_Z_Assoc::set_APDU_log(const char *fname)
90 if (m_APDU_file && m_APDU_file != stderr)
95 delete [] m_APDU_fname;
100 m_APDU_fname = new char[strlen(fname)+1];
101 strcpy (m_APDU_fname, fname);
102 if (*fname && strcmp(fname, "-"))
103 m_APDU_file = fopen (fname, "a");
105 m_APDU_file = stderr;
106 odr_setprint(m_odr_print, m_APDU_file);
110 const char *Yaz_Z_Assoc::get_APDU_log()
115 Yaz_Z_Assoc::~Yaz_Z_Assoc()
117 m_PDU_Observable->destroy();
118 delete m_PDU_Observable;
119 odr_destroy (m_odr_print); // note: also runs fclose on m_APDU_file ..
120 odr_destroy (m_odr_out);
121 odr_destroy (m_odr_in);
122 delete [] m_APDU_fname;
123 delete [] m_hostname;
126 void Yaz_Z_Assoc::recv_PDU(const char *buf, int len)
128 logf (m_log, "recv_PDU len=%d", len);
129 Z_APDU *apdu = decode_Z_PDU (buf, len);
136 Z_APDU *Yaz_Z_Assoc::create_Z_PDU(int type)
138 Z_APDU *apdu = zget_APDU(m_odr_out, type);
139 if (apdu->which == Z_APDU_initRequest)
141 Z_InitRequest * p = apdu->u.initRequest;
142 char *newName = (char*) odr_malloc(m_odr_out, 50);
143 strcpy (newName, p->implementationName);
144 strcat (newName, " YAZ++");
145 p->implementationName = newName;
150 int Yaz_Z_Assoc::send_Z_PDU(Z_APDU *apdu)
154 if (encode_Z_PDU(apdu, &buf, &len) > 0)
155 return m_PDU_Observable->send_PDU(buf, len);
159 Z_APDU *Yaz_Z_Assoc::decode_Z_PDU(const char *buf, int len)
163 odr_reset (m_odr_in);
164 odr_setbuf (m_odr_in, (char*) buf, len, 0);
166 if (!z_APDU(m_odr_in, &apdu, 0, 0))
168 logf(LOG_LOG, "ODR error on incoming PDU: %s [near byte %d] ",
169 odr_errmsg(odr_geterror(m_odr_in)),
170 odr_offset(m_odr_in));
171 logf(LOG_LOG, "PDU dump:");
172 odr_dumpBER(yaz_log_file(), buf, len);
178 z_APDU(m_odr_print, &apdu, 0, "decode");
183 int Yaz_Z_Assoc::encode_Z_PDU(Z_APDU *apdu, char **buf, int *len)
185 if (!z_APDU(m_odr_out, &apdu, 0, 0))
187 logf (LOG_LOG, "yaz_Z_Assoc::encode_Z_PDU failed");
191 z_APDU(m_odr_print, &apdu, 0, "encode");
192 *buf = odr_getbuf (m_odr_out, len, 0);
193 odr_reset (m_odr_out);
197 const char *Yaz_Z_Assoc::get_hostname()
202 void Yaz_Z_Assoc::client(const char *addr)
204 delete [] m_hostname;
205 m_hostname = new char[strlen(addr)+1];
206 strcpy (m_hostname, addr);
207 m_PDU_Observable->connect (this, addr);
210 void Yaz_Z_Assoc::close()
212 m_PDU_Observable->close ();
215 void Yaz_Z_Assoc::server(const char *addr)
217 delete [] m_hostname;
218 m_hostname = new char[strlen(addr)+1];
219 strcpy (m_hostname, addr);
220 m_PDU_Observable->listen (this, addr);
223 ODR Yaz_Z_Assoc::odr_encode()
228 ODR Yaz_Z_Assoc::odr_decode()
232 ODR Yaz_Z_Assoc::odr_print()
237 void Yaz_Z_Assoc::timeout(int timeout)
239 m_PDU_Observable->idleTime(timeout);
242 void Yaz_Z_Assoc::get_otherInfoAPDU(Z_APDU *apdu, Z_OtherInformation ***oip)
246 case Z_APDU_initRequest:
247 *oip = &apdu->u.initRequest->otherInfo;
249 case Z_APDU_searchRequest:
250 *oip = &apdu->u.searchRequest->otherInfo;
252 case Z_APDU_presentRequest:
253 *oip = &apdu->u.presentRequest->otherInfo;
255 case Z_APDU_sortRequest:
256 *oip = &apdu->u.sortRequest->otherInfo;
258 case Z_APDU_scanRequest:
259 *oip = &apdu->u.scanRequest->otherInfo;
261 case Z_APDU_extendedServicesRequest:
262 *oip = &apdu->u.extendedServicesRequest->otherInfo;
264 case Z_APDU_deleteResultSetRequest:
265 *oip = &apdu->u.deleteResultSetRequest->otherInfo;
267 case Z_APDU_initResponse:
268 *oip = &apdu->u.initResponse->otherInfo;
270 case Z_APDU_searchResponse:
271 *oip = &apdu->u.searchResponse->otherInfo;
273 case Z_APDU_presentResponse:
274 *oip = &apdu->u.presentResponse->otherInfo;
276 case Z_APDU_sortResponse:
277 *oip = &apdu->u.sortResponse->otherInfo;
279 case Z_APDU_scanResponse:
280 *oip = &apdu->u.scanResponse->otherInfo;
282 case Z_APDU_extendedServicesResponse:
283 *oip = &apdu->u.extendedServicesResponse->otherInfo;
285 case Z_APDU_deleteResultSetResponse:
286 *oip = &apdu->u.deleteResultSetResponse->otherInfo;
294 void Yaz_Z_Assoc::set_otherInformationString (
296 int oidval, int categoryValue,
299 Z_OtherInformation **otherInformation;
300 get_otherInfoAPDU(apdu, &otherInformation);
301 if (!otherInformation)
303 set_otherInformationString(otherInformation, oidval, categoryValue, str);
306 void Yaz_Z_Assoc::set_otherInformationString (
307 Z_OtherInformation **otherInformation,
308 int oidval, int categoryValue,
313 ent.proto = PROTO_Z3950;
314 ent.oclass = CLASS_USERINFO;
315 ent.value = (oid_value) oidval;
316 if (!oid_ent_to_oid (&ent, oid))
318 set_otherInformationString(otherInformation, oid, categoryValue, str);
321 void Yaz_Z_Assoc::set_otherInformationString (
322 Z_OtherInformation **otherInformation,
323 int *oid, int categoryValue, const char *str)
325 Z_OtherInformationUnit *oi =
326 update_otherInformation(otherInformation, 1, oid, categoryValue, 0);
329 oi->information.characterInfo = odr_strdup (odr_encode(), str);
332 Z_OtherInformationUnit *Yaz_Z_Assoc::update_otherInformation (
333 Z_OtherInformation **otherInformationP, int createFlag,
334 int *oid, int categoryValue, int deleteFlag)
336 return yaz_oi_update (otherInformationP,
337 (createFlag ? odr_encode() : 0),
338 oid, categoryValue, deleteFlag);
341 Z_ReferenceId* Yaz_Z_Assoc::getRefID(char* str)
343 Z_ReferenceId* id = NULL;
347 id = (Z_ReferenceId*) odr_malloc (m_odr_out, sizeof(*id));
348 id->size = id->len = strlen(str);
349 id->buf = (unsigned char *) str;