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.5 1999-11-10 10:02:34 adam
10 * Revision 1.4 1999/09/13 12:53:44 adam
11 * Proxy removes OtherInfo Proxy Address and Session ID. Other
12 * Otherinfo remains untouched.
14 * Revision 1.3 1999/04/21 12:09:01 adam
15 * Many improvements. Modified to proxy server to work with "sessions"
18 * Revision 1.2 1999/04/20 10:30:05 adam
19 * Implemented various stuff for client and proxy. Updated calls
20 * to ODR to reflect new name parameter.
22 * Revision 1.1 1999/04/09 11:46:57 adam
23 * Added object Yaz_Z_Assoc. Much more functional client.
30 #include <yaz-z-assoc.h>
31 #include <otherinfo.h>
33 int Yaz_Z_Assoc::yaz_init_func()
35 logf (LOG_LOG, "nmem_init");
37 logf (LOG_LOG, "done");
41 int Yaz_Z_Assoc::yaz_init_flag = Yaz_Z_Assoc::yaz_init_func();
43 Yaz_Z_Assoc::Yaz_Z_Assoc(IYaz_PDU_Observable *the_PDU_Observable)
45 m_PDU_Observable = the_PDU_Observable;
46 m_odr_in = odr_createmem (ODR_DECODE);
47 m_odr_out = odr_createmem (ODR_ENCODE);
48 m_odr_print = odr_createmem (ODR_PRINT);
51 Yaz_Z_Assoc::~Yaz_Z_Assoc()
53 m_PDU_Observable->destroy();
54 delete m_PDU_Observable;
55 odr_destroy (m_odr_print);
56 odr_destroy (m_odr_out);
57 odr_destroy (m_odr_in);
60 void Yaz_Z_Assoc::recv_PDU(const char *buf, int len)
62 logf (LOG_LOG, "recv_PDU len=%d", len);
63 Z_APDU *apdu = decode_Z_PDU (buf, len);
70 Z_APDU *Yaz_Z_Assoc::create_Z_PDU(int type)
72 Z_APDU *apdu = zget_APDU(m_odr_out, type);
73 if (apdu->which == Z_APDU_initRequest)
75 Z_InitRequest * p = apdu->u.initRequest;
76 char *newName = (char*) odr_malloc(m_odr_out, 50);
77 strcpy (newName, p->implementationName);
78 strcat (newName, " YAZ++");
79 p->implementationName = newName;
84 int Yaz_Z_Assoc::send_Z_PDU(Z_APDU *apdu)
88 logf (LOG_LOG, "Yaz_Z_Assoc:send_Z_PDU");
89 if (encode_Z_PDU(apdu, &buf, &len) > 0)
90 return m_PDU_Observable->send_PDU(buf, len);
94 Z_APDU *Yaz_Z_Assoc::decode_Z_PDU(const char *buf, int len)
99 odr_setbuf (m_odr_in, (char*) buf, len, 0);
101 if (!z_APDU(m_odr_in, &apdu, 0, 0))
103 logf(LOG_LOG, "ODR error on incoming PDU: %s [near byte %d] ",
104 odr_errmsg(odr_geterror(m_odr_in)),
105 odr_offset(m_odr_in));
106 logf(LOG_LOG, "PDU dump:");
107 odr_dumpBER(log_file(), buf, len);
112 z_APDU(m_odr_print, &apdu, 0, "decode");
117 int Yaz_Z_Assoc::encode_Z_PDU(Z_APDU *apdu, char **buf, int *len)
119 if (!z_APDU(m_odr_out, &apdu, 0, 0))
121 logf (LOG_LOG, "yaz_Z_Assoc::encode_Z_PDU failed");
124 z_APDU(m_odr_print, &apdu, 0, "encode");
125 *buf = odr_getbuf (m_odr_out, len, 0);
126 odr_reset (m_odr_out);
130 void Yaz_Z_Assoc::connectNotify()
132 logf (LOG_LOG, "connectNotify");
135 void Yaz_Z_Assoc::failNotify()
137 logf (LOG_LOG, "failNotify");
140 void Yaz_Z_Assoc::timeoutNotify()
142 logf (LOG_LOG, "timeoutNotify");
145 void Yaz_Z_Assoc::client(const char *addr)
147 m_PDU_Observable->connect (this, addr);
150 void Yaz_Z_Assoc::close()
152 m_PDU_Observable->close ();
155 void Yaz_Z_Assoc::server(const char *addr)
157 m_PDU_Observable->listen (this, addr);
160 ODR Yaz_Z_Assoc::odr_encode()
165 ODR Yaz_Z_Assoc::odr_decode()
169 ODR Yaz_Z_Assoc::odr_print()
174 void Yaz_Z_Assoc::timeout(int timeout)
176 m_PDU_Observable->idleTime(timeout);
179 void Yaz_Z_Assoc::get_otherInfoAPDU(Z_APDU *apdu, Z_OtherInformation ***oip)
183 case Z_APDU_initRequest:
184 *oip = &apdu->u.initRequest->otherInfo;
186 case Z_APDU_searchRequest:
187 *oip = &apdu->u.searchRequest->otherInfo;
189 case Z_APDU_presentRequest:
190 *oip = &apdu->u.presentRequest->otherInfo;
192 case Z_APDU_sortRequest:
193 *oip = &apdu->u.sortRequest->otherInfo;
195 case Z_APDU_scanRequest:
196 *oip = &apdu->u.scanRequest->otherInfo;
198 case Z_APDU_initResponse:
199 *oip = &apdu->u.initResponse->otherInfo;
201 case Z_APDU_searchResponse:
202 *oip = &apdu->u.searchResponse->otherInfo;
204 case Z_APDU_presentResponse:
205 *oip = &apdu->u.presentResponse->otherInfo;
207 case Z_APDU_sortResponse:
208 *oip = &apdu->u.sortResponse->otherInfo;
210 case Z_APDU_scanResponse:
211 *oip = &apdu->u.scanResponse->otherInfo;
219 void Yaz_Z_Assoc::set_otherInformationString (
221 int oidval, int categoryValue,
224 Z_OtherInformation **otherInformation;
225 get_otherInfoAPDU(apdu, &otherInformation);
226 if (!otherInformation)
228 set_otherInformationString(otherInformation, oidval, categoryValue, str);
231 void Yaz_Z_Assoc::set_otherInformationString (
232 Z_OtherInformation **otherInformation,
233 int oidval, int categoryValue,
238 ent.proto = PROTO_Z3950;
239 ent.oclass = CLASS_USERINFO;
240 ent.value = (oid_value) oidval;
241 if (!oid_ent_to_oid (&ent, oid))
243 set_otherInformationString(otherInformation, oid, categoryValue, str);
246 void Yaz_Z_Assoc::set_otherInformationString (
247 Z_OtherInformation **otherInformation,
248 int *oid, int categoryValue, const char *str)
250 Z_OtherInformationUnit *oi =
251 update_otherInformation(otherInformation, 1, oid, categoryValue, 0);
254 oi->information.characterInfo = odr_strdup (odr_encode(), str);
257 Z_OtherInformationUnit *Yaz_Z_Assoc::update_otherInformation (
258 Z_OtherInformation **otherInformationP, int createFlag,
259 int *oid, int categoryValue, int deleteFlag)
261 return yaz_oi_update (otherInformationP,
262 (createFlag ? odr_encode() : 0),
263 oid, categoryValue, deleteFlag);