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.3 1999-04-21 12:09:01 adam
8 * Many improvements. Modified to proxy server to work with "sessions"
11 * Revision 1.2 1999/04/20 10:30:05 adam
12 * Implemented various stuff for client and proxy. Updated calls
13 * to ODR to reflect new name parameter.
15 * Revision 1.1 1999/04/09 11:46:57 adam
16 * Added object Yaz_Z_Assoc. Much more functional client.
23 #include <yaz-z-assoc.h>
25 int Yaz_Z_Assoc::yaz_init_func()
27 logf (LOG_LOG, "nmem_init");
29 logf (LOG_LOG, "done");
33 int Yaz_Z_Assoc::yaz_init_flag = Yaz_Z_Assoc::yaz_init_func();
35 Yaz_Z_Assoc::Yaz_Z_Assoc(IYaz_PDU_Observable *the_PDU_Observable)
37 m_PDU_Observable = the_PDU_Observable;
38 m_odr_in = odr_createmem (ODR_DECODE);
39 m_odr_out = odr_createmem (ODR_ENCODE);
40 m_odr_print = odr_createmem (ODR_PRINT);
43 Yaz_Z_Assoc::~Yaz_Z_Assoc()
45 m_PDU_Observable->destroy();
46 delete m_PDU_Observable;
47 odr_destroy (m_odr_print);
48 odr_destroy (m_odr_out);
49 odr_destroy (m_odr_in);
52 void Yaz_Z_Assoc::recv_PDU(const char *buf, int len)
54 logf (LOG_LOG, "recv_PDU len=%d", len);
55 Z_APDU *apdu = decode_Z_PDU (buf, len);
62 Z_APDU *Yaz_Z_Assoc::create_Z_PDU(int type)
64 Z_APDU *apdu = zget_APDU(m_odr_out, type);
65 if (apdu->which == Z_APDU_initRequest)
67 Z_InitRequest * p = apdu->u.initRequest;
68 char *newName = (char*) odr_malloc(m_odr_out, 50);
69 strcpy (newName, p->implementationName);
70 strcat (newName, " YAZ++");
71 p->implementationName = newName;
76 int Yaz_Z_Assoc::send_Z_PDU(Z_APDU *apdu)
80 logf (LOG_LOG, "Yaz_Z_Assoc:send_Z_PDU");
81 if (encode_Z_PDU(apdu, &buf, &len) > 0)
82 return m_PDU_Observable->send_PDU(buf, len);
86 Z_APDU *Yaz_Z_Assoc::decode_Z_PDU(const char *buf, int len)
91 odr_setbuf (m_odr_in, (char*) buf, len, 0);
93 if (!z_APDU(m_odr_in, &apdu, 0, 0))
95 logf(LOG_LOG, "ODR error on incoming PDU: %s [near byte %d] ",
96 odr_errmsg(odr_geterror(m_odr_in)),
97 odr_offset(m_odr_in));
98 logf(LOG_LOG, "PDU dump:");
99 odr_dumpBER(log_file(), buf, len);
104 z_APDU(m_odr_print, &apdu, 0, "decode");
109 int Yaz_Z_Assoc::encode_Z_PDU(Z_APDU *apdu, char **buf, int *len)
111 if (!z_APDU(m_odr_out, &apdu, 0, 0))
113 logf (LOG_LOG, "yaz_Z_Assoc::encode_Z_PDU failed");
116 z_APDU(m_odr_print, &apdu, 0, "encode");
117 *buf = odr_getbuf (m_odr_out, len, 0);
118 odr_reset (m_odr_out);
122 void Yaz_Z_Assoc::connectNotify()
124 logf (LOG_LOG, "connectNotify");
127 void Yaz_Z_Assoc::failNotify()
129 logf (LOG_LOG, "failNotify");
132 void Yaz_Z_Assoc::timeoutNotify()
134 logf (LOG_LOG, "timeoutNotify");
137 void Yaz_Z_Assoc::client(const char *addr)
139 m_PDU_Observable->connect (this, addr);
142 void Yaz_Z_Assoc::close()
144 m_PDU_Observable->close ();
147 void Yaz_Z_Assoc::server(const char *addr)
149 m_PDU_Observable->listen (this, addr);
152 ODR Yaz_Z_Assoc::odr_encode()
157 ODR Yaz_Z_Assoc::odr_decode()
161 ODR Yaz_Z_Assoc::odr_print()
166 void Yaz_Z_Assoc::timeout(int timeout)
168 m_PDU_Observable->idleTime(timeout);
171 void Yaz_Z_Assoc::get_otherInfoAPDU(Z_APDU *apdu, Z_OtherInformation ***oip)
175 case Z_APDU_initRequest:
176 *oip = &apdu->u.initRequest->otherInfo;
178 case Z_APDU_searchRequest:
179 *oip = &apdu->u.searchRequest->otherInfo;
181 case Z_APDU_presentRequest:
182 *oip = &apdu->u.presentRequest->otherInfo;
184 case Z_APDU_sortRequest:
185 *oip = &apdu->u.sortRequest->otherInfo;
187 case Z_APDU_scanRequest:
188 *oip = &apdu->u.scanRequest->otherInfo;
190 case Z_APDU_initResponse:
191 *oip = &apdu->u.initResponse->otherInfo;
193 case Z_APDU_searchResponse:
194 *oip = &apdu->u.searchResponse->otherInfo;
196 case Z_APDU_presentResponse:
197 *oip = &apdu->u.presentResponse->otherInfo;
199 case Z_APDU_sortResponse:
200 *oip = &apdu->u.sortResponse->otherInfo;
202 case Z_APDU_scanResponse:
203 *oip = &apdu->u.scanResponse->otherInfo;
211 void Yaz_Z_Assoc::set_otherInformationString (
213 int oidval, int categoryValue,
216 Z_OtherInformation **otherInformation;
217 get_otherInfoAPDU(apdu, &otherInformation);
218 if (!otherInformation)
220 set_otherInformationString(otherInformation, oidval, categoryValue, str);
223 void Yaz_Z_Assoc::set_otherInformationString (
224 Z_OtherInformation **otherInformation,
225 int oidval, int categoryValue,
230 ent.proto = PROTO_Z3950;
231 ent.oclass = CLASS_USERINFO;
232 ent.value = (oid_value) oidval;
233 if (!oid_ent_to_oid (&ent, oid))
235 set_otherInformationString(otherInformation, oid, categoryValue, str);
238 void Yaz_Z_Assoc::set_otherInformationString (
239 Z_OtherInformation **otherInformation,
240 int *oid, int categoryValue,
243 Z_OtherInformationUnit *oi =
244 update_otherInformation(otherInformation, 1, oid, categoryValue);
247 oi->information.characterInfo = odr_strdup (odr_encode(), str);
250 Z_OtherInformationUnit *Yaz_Z_Assoc::update_otherInformation (
251 Z_OtherInformation **otherInformationP, int createFlag,
252 int *oid, int categoryValue)
255 Z_OtherInformation *otherInformation = *otherInformationP;
256 if (!otherInformation)
260 otherInformation = *otherInformationP = (Z_OtherInformation *)
261 odr_malloc (odr_encode(), sizeof(*otherInformation));
262 otherInformation->num_elements = 0;
263 otherInformation->list = (Z_OtherInformationUnit **)
264 odr_malloc (odr_encode(), 8*sizeof(*otherInformation));
265 for (i = 0; i<8; i++)
266 otherInformation->list[i] = 0;
268 logf (LOG_LOG, "Yaz_Z_Assoc::update_otherInformation num=%d",
269 otherInformation->num_elements);
270 for (i = 0; i<otherInformation->num_elements; i++)
272 assert (otherInformation->list[i]);
275 if (!otherInformation->list[i]->category)
276 return otherInformation->list[i];
280 if (otherInformation->list[i]->category &&
282 *otherInformation->list[i]->category->categoryValue &&
283 !oid_oidcmp (oid, otherInformation->list[i]->category->
285 return otherInformation->list[i];
290 otherInformation->list[i] = (Z_OtherInformationUnit*)
291 odr_malloc (odr_encode(), sizeof(Z_OtherInformationUnit));
294 otherInformation->list[i]->category = (Z_InfoCategory*)
295 odr_malloc (odr_encode(), sizeof(Z_InfoCategory));
296 otherInformation->list[i]->category->categoryTypeId = (int*)
297 odr_oiddup (odr_encode(), oid);
298 otherInformation->list[i]->category->categoryValue = (int*)
299 odr_malloc (odr_encode(), sizeof(int));
300 *otherInformation->list[i]->category->categoryValue =
304 otherInformation->list[i]->category = 0;
305 otherInformation->list[i]->which = Z_OtherInfo_characterInfo;
306 otherInformation->list[i]->information.characterInfo = 0;
308 otherInformation->num_elements = i+1;
309 return otherInformation->list[i];