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.8 2000-08-07 14:19:59 adam
8 * Fixed serious bug regarding timeouts. Improved logging for proxy.
10 * Revision 1.7 2000/05/10 11:36:58 ian
11 * Added default parameters for refid to request functions.
12 * Added default parameter for result set name to search and present request.
13 * Commented out forced logging of PDU contents.
14 * Added send_deleteResultSetRequest
16 * Revision 1.6 1999/12/06 13:52:45 adam
17 * Modified for new location of YAZ header files. Experimental threaded
20 * Revision 1.5 1999/11/10 10:02:34 adam
23 * Revision 1.4 1999/09/13 12:53:44 adam
24 * Proxy removes OtherInfo Proxy Address and Session ID. Other
25 * Otherinfo remains untouched.
27 * Revision 1.3 1999/04/21 12:09:01 adam
28 * Many improvements. Modified to proxy server to work with "sessions"
31 * Revision 1.2 1999/04/20 10:30:05 adam
32 * Implemented various stuff for client and proxy. Updated calls
33 * to ODR to reflect new name parameter.
35 * Revision 1.1 1999/04/09 11:46:57 adam
36 * Added object Yaz_Z_Assoc. Much more functional client.
43 #include <yaz-z-assoc.h>
44 #include <yaz/otherinfo.h>
46 int Yaz_Z_Assoc::yaz_init_func()
52 int Yaz_Z_Assoc::yaz_init_flag = Yaz_Z_Assoc::yaz_init_func();
54 Yaz_Z_Assoc::Yaz_Z_Assoc(IYaz_PDU_Observable *the_PDU_Observable)
56 m_PDU_Observable = the_PDU_Observable;
57 m_odr_in = odr_createmem (ODR_DECODE);
58 m_odr_out = odr_createmem (ODR_ENCODE);
59 m_odr_print = odr_createmem (ODR_PRINT);
66 void Yaz_Z_Assoc::set_APDU_log(const char *fname)
68 if (m_APDU_file && m_APDU_file != stderr)
71 delete [] m_APDU_fname;
76 m_APDU_fname = new char[strlen(fname)+1];
77 strcpy (m_APDU_fname, fname);
82 m_APDU_file = fopen (fname, "a");
85 odr_setprint(m_odr_print, m_APDU_file);
89 const char *Yaz_Z_Assoc::get_APDU_log()
94 Yaz_Z_Assoc::~Yaz_Z_Assoc()
96 m_PDU_Observable->destroy();
97 delete m_PDU_Observable;
98 odr_destroy (m_odr_print);
99 odr_destroy (m_odr_out);
100 odr_destroy (m_odr_in);
101 delete [] m_APDU_fname;
102 if (m_APDU_file && m_APDU_file != stderr)
103 fclose (m_APDU_file);
104 delete [] m_hostname;
107 void Yaz_Z_Assoc::recv_PDU(const char *buf, int len)
109 logf (m_log, "recv_PDU len=%d", len);
110 Z_APDU *apdu = decode_Z_PDU (buf, len);
117 Z_APDU *Yaz_Z_Assoc::create_Z_PDU(int type)
119 Z_APDU *apdu = zget_APDU(m_odr_out, type);
120 if (apdu->which == Z_APDU_initRequest)
122 Z_InitRequest * p = apdu->u.initRequest;
123 char *newName = (char*) odr_malloc(m_odr_out, 50);
124 strcpy (newName, p->implementationName);
125 strcat (newName, " YAZ++");
126 p->implementationName = newName;
131 int Yaz_Z_Assoc::send_Z_PDU(Z_APDU *apdu)
135 if (encode_Z_PDU(apdu, &buf, &len) > 0)
136 return m_PDU_Observable->send_PDU(buf, len);
140 Z_APDU *Yaz_Z_Assoc::decode_Z_PDU(const char *buf, int len)
144 odr_reset (m_odr_in);
145 odr_setbuf (m_odr_in, (char*) buf, len, 0);
147 if (!z_APDU(m_odr_in, &apdu, 0, 0))
149 logf(LOG_LOG, "ODR error on incoming PDU: %s [near byte %d] ",
150 odr_errmsg(odr_geterror(m_odr_in)),
151 odr_offset(m_odr_in));
152 logf(LOG_LOG, "PDU dump:");
153 odr_dumpBER(log_file(), buf, len);
159 z_APDU(m_odr_print, &apdu, 0, "decode");
164 int Yaz_Z_Assoc::encode_Z_PDU(Z_APDU *apdu, char **buf, int *len)
166 if (!z_APDU(m_odr_out, &apdu, 0, 0))
168 logf (LOG_LOG, "yaz_Z_Assoc::encode_Z_PDU failed");
172 z_APDU(m_odr_print, &apdu, 0, "encode");
173 *buf = odr_getbuf (m_odr_out, len, 0);
174 odr_reset (m_odr_out);
178 const char *Yaz_Z_Assoc::get_hostname()
183 void Yaz_Z_Assoc::client(const char *addr)
185 delete [] m_hostname;
186 m_hostname = new char[strlen(addr)+1];
187 strcpy (m_hostname, addr);
188 m_PDU_Observable->connect (this, addr);
191 void Yaz_Z_Assoc::close()
193 m_PDU_Observable->close ();
196 void Yaz_Z_Assoc::server(const char *addr)
198 delete [] m_hostname;
199 m_hostname = new char[strlen(addr)+1];
200 strcpy (m_hostname, addr);
201 m_PDU_Observable->listen (this, addr);
204 ODR Yaz_Z_Assoc::odr_encode()
209 ODR Yaz_Z_Assoc::odr_decode()
213 ODR Yaz_Z_Assoc::odr_print()
218 void Yaz_Z_Assoc::timeout(int timeout)
220 m_PDU_Observable->idleTime(timeout);
223 void Yaz_Z_Assoc::get_otherInfoAPDU(Z_APDU *apdu, Z_OtherInformation ***oip)
227 case Z_APDU_initRequest:
228 *oip = &apdu->u.initRequest->otherInfo;
230 case Z_APDU_searchRequest:
231 *oip = &apdu->u.searchRequest->otherInfo;
233 case Z_APDU_presentRequest:
234 *oip = &apdu->u.presentRequest->otherInfo;
236 case Z_APDU_sortRequest:
237 *oip = &apdu->u.sortRequest->otherInfo;
239 case Z_APDU_scanRequest:
240 *oip = &apdu->u.scanRequest->otherInfo;
242 case Z_APDU_initResponse:
243 *oip = &apdu->u.initResponse->otherInfo;
245 case Z_APDU_searchResponse:
246 *oip = &apdu->u.searchResponse->otherInfo;
248 case Z_APDU_presentResponse:
249 *oip = &apdu->u.presentResponse->otherInfo;
251 case Z_APDU_sortResponse:
252 *oip = &apdu->u.sortResponse->otherInfo;
254 case Z_APDU_scanResponse:
255 *oip = &apdu->u.scanResponse->otherInfo;
263 void Yaz_Z_Assoc::set_otherInformationString (
265 int oidval, int categoryValue,
268 Z_OtherInformation **otherInformation;
269 get_otherInfoAPDU(apdu, &otherInformation);
270 if (!otherInformation)
272 set_otherInformationString(otherInformation, oidval, categoryValue, str);
275 void Yaz_Z_Assoc::set_otherInformationString (
276 Z_OtherInformation **otherInformation,
277 int oidval, int categoryValue,
282 ent.proto = PROTO_Z3950;
283 ent.oclass = CLASS_USERINFO;
284 ent.value = (oid_value) oidval;
285 if (!oid_ent_to_oid (&ent, oid))
287 set_otherInformationString(otherInformation, oid, categoryValue, str);
290 void Yaz_Z_Assoc::set_otherInformationString (
291 Z_OtherInformation **otherInformation,
292 int *oid, int categoryValue, const char *str)
294 Z_OtherInformationUnit *oi =
295 update_otherInformation(otherInformation, 1, oid, categoryValue, 0);
298 oi->information.characterInfo = odr_strdup (odr_encode(), str);
301 Z_OtherInformationUnit *Yaz_Z_Assoc::update_otherInformation (
302 Z_OtherInformation **otherInformationP, int createFlag,
303 int *oid, int categoryValue, int deleteFlag)
305 return yaz_oi_update (otherInformationP,
306 (createFlag ? odr_encode() : 0),
307 oid, categoryValue, deleteFlag);
310 Z_ReferenceId* Yaz_Z_Assoc::getRefID(char* str)
312 Z_ReferenceId* id = NULL;
316 id = (Z_ReferenceId*) odr_malloc (m_odr_out, sizeof(*id));
317 id->size = id->len = strlen(str);
318 id->buf = (unsigned char *) str;