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.6 1999-12-06 13:52:45 adam
8 * Modified for new location of YAZ header files. Experimental threaded
11 * Revision 1.5 1999/11/10 10:02:34 adam
14 * Revision 1.4 1999/09/13 12:53:44 adam
15 * Proxy removes OtherInfo Proxy Address and Session ID. Other
16 * Otherinfo remains untouched.
18 * Revision 1.3 1999/04/21 12:09:01 adam
19 * Many improvements. Modified to proxy server to work with "sessions"
22 * Revision 1.2 1999/04/20 10:30:05 adam
23 * Implemented various stuff for client and proxy. Updated calls
24 * to ODR to reflect new name parameter.
26 * Revision 1.1 1999/04/09 11:46:57 adam
27 * Added object Yaz_Z_Assoc. Much more functional client.
34 #include <yaz-z-assoc.h>
35 #include <yaz/otherinfo.h>
37 int Yaz_Z_Assoc::yaz_init_func()
43 int Yaz_Z_Assoc::yaz_init_flag = Yaz_Z_Assoc::yaz_init_func();
45 Yaz_Z_Assoc::Yaz_Z_Assoc(IYaz_PDU_Observable *the_PDU_Observable)
47 m_PDU_Observable = the_PDU_Observable;
48 m_odr_in = odr_createmem (ODR_DECODE);
49 m_odr_out = odr_createmem (ODR_ENCODE);
50 m_odr_print = odr_createmem (ODR_PRINT);
54 Yaz_Z_Assoc::~Yaz_Z_Assoc()
56 m_PDU_Observable->destroy();
57 delete m_PDU_Observable;
58 odr_destroy (m_odr_print);
59 odr_destroy (m_odr_out);
60 odr_destroy (m_odr_in);
63 void Yaz_Z_Assoc::recv_PDU(const char *buf, int len)
65 logf (m_log, "recv_PDU len=%d", len);
66 Z_APDU *apdu = decode_Z_PDU (buf, len);
73 Z_APDU *Yaz_Z_Assoc::create_Z_PDU(int type)
75 Z_APDU *apdu = zget_APDU(m_odr_out, type);
76 if (apdu->which == Z_APDU_initRequest)
78 Z_InitRequest * p = apdu->u.initRequest;
79 char *newName = (char*) odr_malloc(m_odr_out, 50);
80 strcpy (newName, p->implementationName);
81 strcat (newName, " YAZ++");
82 p->implementationName = newName;
87 int Yaz_Z_Assoc::send_Z_PDU(Z_APDU *apdu)
91 if (encode_Z_PDU(apdu, &buf, &len) > 0)
92 return m_PDU_Observable->send_PDU(buf, len);
96 Z_APDU *Yaz_Z_Assoc::decode_Z_PDU(const char *buf, int len)
100 odr_reset (m_odr_in);
101 odr_setbuf (m_odr_in, (char*) buf, len, 0);
103 if (!z_APDU(m_odr_in, &apdu, 0, 0))
105 logf(LOG_LOG, "ODR error on incoming PDU: %s [near byte %d] ",
106 odr_errmsg(odr_geterror(m_odr_in)),
107 odr_offset(m_odr_in));
108 logf(LOG_LOG, "PDU dump:");
109 odr_dumpBER(log_file(), buf, len);
114 z_APDU(m_odr_print, &apdu, 0, "decode");
119 int Yaz_Z_Assoc::encode_Z_PDU(Z_APDU *apdu, char **buf, int *len)
121 if (!z_APDU(m_odr_out, &apdu, 0, 0))
123 logf (LOG_LOG, "yaz_Z_Assoc::encode_Z_PDU failed");
126 z_APDU(m_odr_print, &apdu, 0, "encode");
127 *buf = odr_getbuf (m_odr_out, len, 0);
128 odr_reset (m_odr_out);
132 void Yaz_Z_Assoc::client(const char *addr)
134 m_PDU_Observable->connect (this, addr);
137 void Yaz_Z_Assoc::close()
139 m_PDU_Observable->close ();
142 void Yaz_Z_Assoc::server(const char *addr)
144 m_PDU_Observable->listen (this, addr);
147 ODR Yaz_Z_Assoc::odr_encode()
152 ODR Yaz_Z_Assoc::odr_decode()
156 ODR Yaz_Z_Assoc::odr_print()
161 void Yaz_Z_Assoc::timeout(int timeout)
163 m_PDU_Observable->idleTime(timeout);
167 void Yaz_Z_Assoc::get_otherInfoAPDU(Z_APDU *apdu, Z_OtherInformation ***oip)
171 case Z_APDU_initRequest:
172 *oip = &apdu->u.initRequest->otherInfo;
174 case Z_APDU_searchRequest:
175 *oip = &apdu->u.searchRequest->otherInfo;
177 case Z_APDU_presentRequest:
178 *oip = &apdu->u.presentRequest->otherInfo;
180 case Z_APDU_sortRequest:
181 *oip = &apdu->u.sortRequest->otherInfo;
183 case Z_APDU_scanRequest:
184 *oip = &apdu->u.scanRequest->otherInfo;
186 case Z_APDU_initResponse:
187 *oip = &apdu->u.initResponse->otherInfo;
189 case Z_APDU_searchResponse:
190 *oip = &apdu->u.searchResponse->otherInfo;
192 case Z_APDU_presentResponse:
193 *oip = &apdu->u.presentResponse->otherInfo;
195 case Z_APDU_sortResponse:
196 *oip = &apdu->u.sortResponse->otherInfo;
198 case Z_APDU_scanResponse:
199 *oip = &apdu->u.scanResponse->otherInfo;
207 void Yaz_Z_Assoc::set_otherInformationString (
209 int oidval, int categoryValue,
212 Z_OtherInformation **otherInformation;
213 get_otherInfoAPDU(apdu, &otherInformation);
214 if (!otherInformation)
216 set_otherInformationString(otherInformation, oidval, categoryValue, str);
219 void Yaz_Z_Assoc::set_otherInformationString (
220 Z_OtherInformation **otherInformation,
221 int oidval, int categoryValue,
226 ent.proto = PROTO_Z3950;
227 ent.oclass = CLASS_USERINFO;
228 ent.value = (oid_value) oidval;
229 if (!oid_ent_to_oid (&ent, oid))
231 set_otherInformationString(otherInformation, oid, categoryValue, str);
234 void Yaz_Z_Assoc::set_otherInformationString (
235 Z_OtherInformation **otherInformation,
236 int *oid, int categoryValue, const char *str)
238 Z_OtherInformationUnit *oi =
239 update_otherInformation(otherInformation, 1, oid, categoryValue, 0);
242 oi->information.characterInfo = odr_strdup (odr_encode(), str);
245 Z_OtherInformationUnit *Yaz_Z_Assoc::update_otherInformation (
246 Z_OtherInformation **otherInformationP, int createFlag,
247 int *oid, int categoryValue, int deleteFlag)
249 return yaz_oi_update (otherInformationP,
250 (createFlag ? odr_encode() : 0),
251 oid, categoryValue, deleteFlag);