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.4 1999-09-13 12:53:44 adam
8 * Proxy removes OtherInfo Proxy Address and Session ID. Other
9 * Otherinfo remains untouched.
11 * Revision 1.3 1999/04/21 12:09:01 adam
12 * Many improvements. Modified to proxy server to work with "sessions"
15 * Revision 1.2 1999/04/20 10:30:05 adam
16 * Implemented various stuff for client and proxy. Updated calls
17 * to ODR to reflect new name parameter.
19 * Revision 1.1 1999/04/09 11:46:57 adam
20 * Added object Yaz_Z_Assoc. Much more functional client.
27 #include <yaz-z-assoc.h>
29 int Yaz_Z_Assoc::yaz_init_func()
31 logf (LOG_LOG, "nmem_init");
33 logf (LOG_LOG, "done");
37 int Yaz_Z_Assoc::yaz_init_flag = Yaz_Z_Assoc::yaz_init_func();
39 Yaz_Z_Assoc::Yaz_Z_Assoc(IYaz_PDU_Observable *the_PDU_Observable)
41 m_PDU_Observable = the_PDU_Observable;
42 m_odr_in = odr_createmem (ODR_DECODE);
43 m_odr_out = odr_createmem (ODR_ENCODE);
44 m_odr_print = odr_createmem (ODR_PRINT);
47 Yaz_Z_Assoc::~Yaz_Z_Assoc()
49 m_PDU_Observable->destroy();
50 delete m_PDU_Observable;
51 odr_destroy (m_odr_print);
52 odr_destroy (m_odr_out);
53 odr_destroy (m_odr_in);
56 void Yaz_Z_Assoc::recv_PDU(const char *buf, int len)
58 logf (LOG_LOG, "recv_PDU len=%d", len);
59 Z_APDU *apdu = decode_Z_PDU (buf, len);
66 Z_APDU *Yaz_Z_Assoc::create_Z_PDU(int type)
68 Z_APDU *apdu = zget_APDU(m_odr_out, type);
69 if (apdu->which == Z_APDU_initRequest)
71 Z_InitRequest * p = apdu->u.initRequest;
72 char *newName = (char*) odr_malloc(m_odr_out, 50);
73 strcpy (newName, p->implementationName);
74 strcat (newName, " YAZ++");
75 p->implementationName = newName;
80 int Yaz_Z_Assoc::send_Z_PDU(Z_APDU *apdu)
84 logf (LOG_LOG, "Yaz_Z_Assoc:send_Z_PDU");
85 if (encode_Z_PDU(apdu, &buf, &len) > 0)
86 return m_PDU_Observable->send_PDU(buf, len);
90 Z_APDU *Yaz_Z_Assoc::decode_Z_PDU(const char *buf, int len)
95 odr_setbuf (m_odr_in, (char*) buf, len, 0);
97 if (!z_APDU(m_odr_in, &apdu, 0, 0))
99 logf(LOG_LOG, "ODR error on incoming PDU: %s [near byte %d] ",
100 odr_errmsg(odr_geterror(m_odr_in)),
101 odr_offset(m_odr_in));
102 logf(LOG_LOG, "PDU dump:");
103 odr_dumpBER(log_file(), buf, len);
108 z_APDU(m_odr_print, &apdu, 0, "decode");
113 int Yaz_Z_Assoc::encode_Z_PDU(Z_APDU *apdu, char **buf, int *len)
115 if (!z_APDU(m_odr_out, &apdu, 0, 0))
117 logf (LOG_LOG, "yaz_Z_Assoc::encode_Z_PDU failed");
120 z_APDU(m_odr_print, &apdu, 0, "encode");
121 *buf = odr_getbuf (m_odr_out, len, 0);
122 odr_reset (m_odr_out);
126 void Yaz_Z_Assoc::connectNotify()
128 logf (LOG_LOG, "connectNotify");
131 void Yaz_Z_Assoc::failNotify()
133 logf (LOG_LOG, "failNotify");
136 void Yaz_Z_Assoc::timeoutNotify()
138 logf (LOG_LOG, "timeoutNotify");
141 void Yaz_Z_Assoc::client(const char *addr)
143 m_PDU_Observable->connect (this, addr);
146 void Yaz_Z_Assoc::close()
148 m_PDU_Observable->close ();
151 void Yaz_Z_Assoc::server(const char *addr)
153 m_PDU_Observable->listen (this, addr);
156 ODR Yaz_Z_Assoc::odr_encode()
161 ODR Yaz_Z_Assoc::odr_decode()
165 ODR Yaz_Z_Assoc::odr_print()
170 void Yaz_Z_Assoc::timeout(int timeout)
172 m_PDU_Observable->idleTime(timeout);
175 void Yaz_Z_Assoc::get_otherInfoAPDU(Z_APDU *apdu, Z_OtherInformation ***oip)
179 case Z_APDU_initRequest:
180 *oip = &apdu->u.initRequest->otherInfo;
182 case Z_APDU_searchRequest:
183 *oip = &apdu->u.searchRequest->otherInfo;
185 case Z_APDU_presentRequest:
186 *oip = &apdu->u.presentRequest->otherInfo;
188 case Z_APDU_sortRequest:
189 *oip = &apdu->u.sortRequest->otherInfo;
191 case Z_APDU_scanRequest:
192 *oip = &apdu->u.scanRequest->otherInfo;
194 case Z_APDU_initResponse:
195 *oip = &apdu->u.initResponse->otherInfo;
197 case Z_APDU_searchResponse:
198 *oip = &apdu->u.searchResponse->otherInfo;
200 case Z_APDU_presentResponse:
201 *oip = &apdu->u.presentResponse->otherInfo;
203 case Z_APDU_sortResponse:
204 *oip = &apdu->u.sortResponse->otherInfo;
206 case Z_APDU_scanResponse:
207 *oip = &apdu->u.scanResponse->otherInfo;
215 void Yaz_Z_Assoc::set_otherInformationString (
217 int oidval, int categoryValue,
220 Z_OtherInformation **otherInformation;
221 get_otherInfoAPDU(apdu, &otherInformation);
222 if (!otherInformation)
224 set_otherInformationString(otherInformation, oidval, categoryValue, str);
227 void Yaz_Z_Assoc::set_otherInformationString (
228 Z_OtherInformation **otherInformation,
229 int oidval, int categoryValue,
234 ent.proto = PROTO_Z3950;
235 ent.oclass = CLASS_USERINFO;
236 ent.value = (oid_value) oidval;
237 if (!oid_ent_to_oid (&ent, oid))
239 set_otherInformationString(otherInformation, oid, categoryValue, str);
242 void Yaz_Z_Assoc::set_otherInformationString (
243 Z_OtherInformation **otherInformation,
244 int *oid, int categoryValue, const char *str)
246 Z_OtherInformationUnit *oi =
247 update_otherInformation(otherInformation, 1, oid, categoryValue, 0);
250 oi->information.characterInfo = odr_strdup (odr_encode(), str);
253 Z_OtherInformationUnit *Yaz_Z_Assoc::update_otherInformation (
254 Z_OtherInformation **otherInformationP, int createFlag,
255 int *oid, int categoryValue, int deleteFlag)
257 return yaz_oi_update (otherInformationP,
258 (createFlag ? odr_encode() : 0),
259 oid, categoryValue, deleteFlag);