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.7 2000-05-10 11:36:58 ian
8 * Added default parameters for refid to request functions.
9 * Added default parameter for result set name to search and present request.
10 * Commented out forced logging of PDU contents.
11 * Added send_deleteResultSetRequest
13 * Revision 1.6 1999/12/06 13:52:45 adam
14 * Modified for new location of YAZ header files. Experimental threaded
17 * Revision 1.5 1999/11/10 10:02:34 adam
20 * Revision 1.4 1999/09/13 12:53:44 adam
21 * Proxy removes OtherInfo Proxy Address and Session ID. Other
22 * Otherinfo remains untouched.
24 * Revision 1.3 1999/04/21 12:09:01 adam
25 * Many improvements. Modified to proxy server to work with "sessions"
28 * Revision 1.2 1999/04/20 10:30:05 adam
29 * Implemented various stuff for client and proxy. Updated calls
30 * to ODR to reflect new name parameter.
32 * Revision 1.1 1999/04/09 11:46:57 adam
33 * Added object Yaz_Z_Assoc. Much more functional client.
40 #include <yaz-z-assoc.h>
41 #include <yaz/otherinfo.h>
43 int Yaz_Z_Assoc::yaz_init_func()
49 int Yaz_Z_Assoc::yaz_init_flag = Yaz_Z_Assoc::yaz_init_func();
51 Yaz_Z_Assoc::Yaz_Z_Assoc(IYaz_PDU_Observable *the_PDU_Observable)
53 m_PDU_Observable = the_PDU_Observable;
54 m_odr_in = odr_createmem (ODR_DECODE);
55 m_odr_out = odr_createmem (ODR_ENCODE);
56 m_odr_print = odr_createmem (ODR_PRINT);
60 Yaz_Z_Assoc::~Yaz_Z_Assoc()
62 m_PDU_Observable->destroy();
63 delete m_PDU_Observable;
64 odr_destroy (m_odr_print);
65 odr_destroy (m_odr_out);
66 odr_destroy (m_odr_in);
69 void Yaz_Z_Assoc::recv_PDU(const char *buf, int len)
71 logf (m_log, "recv_PDU len=%d", len);
72 Z_APDU *apdu = decode_Z_PDU (buf, len);
79 Z_APDU *Yaz_Z_Assoc::create_Z_PDU(int type)
81 Z_APDU *apdu = zget_APDU(m_odr_out, type);
82 if (apdu->which == Z_APDU_initRequest)
84 Z_InitRequest * p = apdu->u.initRequest;
85 char *newName = (char*) odr_malloc(m_odr_out, 50);
86 strcpy (newName, p->implementationName);
87 strcat (newName, " YAZ++");
88 p->implementationName = newName;
93 int Yaz_Z_Assoc::send_Z_PDU(Z_APDU *apdu)
97 if (encode_Z_PDU(apdu, &buf, &len) > 0)
98 return m_PDU_Observable->send_PDU(buf, len);
102 Z_APDU *Yaz_Z_Assoc::decode_Z_PDU(const char *buf, int len)
106 odr_reset (m_odr_in);
107 odr_setbuf (m_odr_in, (char*) buf, len, 0);
109 if (!z_APDU(m_odr_in, &apdu, 0, 0))
111 logf(LOG_LOG, "ODR error on incoming PDU: %s [near byte %d] ",
112 odr_errmsg(odr_geterror(m_odr_in)),
113 odr_offset(m_odr_in));
114 logf(LOG_LOG, "PDU dump:");
115 odr_dumpBER(log_file(), buf, len);
120 // z_APDU(m_odr_print, &apdu, 0, "decode");
125 int Yaz_Z_Assoc::encode_Z_PDU(Z_APDU *apdu, char **buf, int *len)
127 if (!z_APDU(m_odr_out, &apdu, 0, 0))
129 logf (LOG_LOG, "yaz_Z_Assoc::encode_Z_PDU failed");
132 // z_APDU(m_odr_print, &apdu, 0, "encode");
133 *buf = odr_getbuf (m_odr_out, len, 0);
134 odr_reset (m_odr_out);
138 void Yaz_Z_Assoc::client(const char *addr)
140 m_PDU_Observable->connect (this, addr);
143 void Yaz_Z_Assoc::close()
145 m_PDU_Observable->close ();
148 void Yaz_Z_Assoc::server(const char *addr)
150 m_PDU_Observable->listen (this, addr);
153 ODR Yaz_Z_Assoc::odr_encode()
158 ODR Yaz_Z_Assoc::odr_decode()
162 ODR Yaz_Z_Assoc::odr_print()
167 void Yaz_Z_Assoc::timeout(int timeout)
169 m_PDU_Observable->idleTime(timeout);
173 void Yaz_Z_Assoc::get_otherInfoAPDU(Z_APDU *apdu, Z_OtherInformation ***oip)
177 case Z_APDU_initRequest:
178 *oip = &apdu->u.initRequest->otherInfo;
180 case Z_APDU_searchRequest:
181 *oip = &apdu->u.searchRequest->otherInfo;
183 case Z_APDU_presentRequest:
184 *oip = &apdu->u.presentRequest->otherInfo;
186 case Z_APDU_sortRequest:
187 *oip = &apdu->u.sortRequest->otherInfo;
189 case Z_APDU_scanRequest:
190 *oip = &apdu->u.scanRequest->otherInfo;
192 case Z_APDU_initResponse:
193 *oip = &apdu->u.initResponse->otherInfo;
195 case Z_APDU_searchResponse:
196 *oip = &apdu->u.searchResponse->otherInfo;
198 case Z_APDU_presentResponse:
199 *oip = &apdu->u.presentResponse->otherInfo;
201 case Z_APDU_sortResponse:
202 *oip = &apdu->u.sortResponse->otherInfo;
204 case Z_APDU_scanResponse:
205 *oip = &apdu->u.scanResponse->otherInfo;
213 void Yaz_Z_Assoc::set_otherInformationString (
215 int oidval, int categoryValue,
218 Z_OtherInformation **otherInformation;
219 get_otherInfoAPDU(apdu, &otherInformation);
220 if (!otherInformation)
222 set_otherInformationString(otherInformation, oidval, categoryValue, str);
225 void Yaz_Z_Assoc::set_otherInformationString (
226 Z_OtherInformation **otherInformation,
227 int oidval, int categoryValue,
232 ent.proto = PROTO_Z3950;
233 ent.oclass = CLASS_USERINFO;
234 ent.value = (oid_value) oidval;
235 if (!oid_ent_to_oid (&ent, oid))
237 set_otherInformationString(otherInformation, oid, categoryValue, str);
240 void Yaz_Z_Assoc::set_otherInformationString (
241 Z_OtherInformation **otherInformation,
242 int *oid, int categoryValue, const char *str)
244 Z_OtherInformationUnit *oi =
245 update_otherInformation(otherInformation, 1, oid, categoryValue, 0);
248 oi->information.characterInfo = odr_strdup (odr_encode(), str);
251 Z_OtherInformationUnit *Yaz_Z_Assoc::update_otherInformation (
252 Z_OtherInformation **otherInformationP, int createFlag,
253 int *oid, int categoryValue, int deleteFlag)
255 return yaz_oi_update (otherInformationP,
256 (createFlag ? odr_encode() : 0),
257 oid, categoryValue, deleteFlag);
260 Z_ReferenceId* Yaz_Z_Assoc::getRefID(char* str)
262 Z_ReferenceId* id = NULL;
266 id = (Z_ReferenceId*) odr_malloc (m_odr_out, sizeof(*id));
267 id->size = id->len = strlen(str);
268 id->buf = (unsigned char *) str;