2 * Copyright (c) 1998-2001, Index Data.
3 * See the file LICENSE for details.
5 * $Id: yaz-z-assoc.cpp,v 1.27 2003-10-10 12:37:26 adam Exp $
12 #include <yaz++/z-assoc.h>
13 #include <yaz/otherinfo.h>
15 int Yaz_Z_Assoc::yaz_init_func()
18 signal (SIGPIPE, SIG_IGN);
23 int Yaz_Z_Assoc::yaz_init_flag = Yaz_Z_Assoc::yaz_init_func();
25 Yaz_Z_Assoc::Yaz_Z_Assoc(IYaz_PDU_Observable *the_PDU_Observable)
27 m_PDU_Observable = the_PDU_Observable;
28 m_odr_in = odr_createmem (ODR_DECODE);
29 m_odr_out = odr_createmem (ODR_ENCODE);
30 m_odr_print = odr_createmem (ODR_PRINT);
37 void Yaz_Z_Assoc::set_APDU_log(const char *fname)
39 if (m_APDU_file && m_APDU_file != stderr)
44 delete [] m_APDU_fname;
49 m_APDU_fname = new char[strlen(fname)+1];
50 strcpy (m_APDU_fname, fname);
51 if (*fname && strcmp(fname, "-"))
52 m_APDU_file = fopen (fname, "a");
55 odr_setprint(m_odr_print, m_APDU_file);
59 const char *Yaz_Z_Assoc::get_APDU_log()
64 Yaz_Z_Assoc::~Yaz_Z_Assoc()
66 m_PDU_Observable->destroy();
67 delete m_PDU_Observable;
68 odr_destroy (m_odr_print); // note: also runs fclose on m_APDU_file ..
69 odr_destroy (m_odr_out);
70 odr_destroy (m_odr_in);
71 delete [] m_APDU_fname;
75 void Yaz_Z_Assoc::recv_PDU(const char *buf, int len)
77 yaz_log (m_log, "recv_PDU len=%d", len);
78 Z_APDU *apdu = decode_Z_PDU (buf, len);
81 recv_Z_PDU (apdu, len);
89 Z_APDU *Yaz_Z_Assoc::create_Z_PDU(int type)
91 Z_APDU *apdu = zget_APDU(m_odr_out, type);
92 if (apdu->which == Z_APDU_initRequest)
94 Z_InitRequest * p = apdu->u.initRequest;
95 char *newName = (char*) odr_malloc(m_odr_out, 50);
96 strcpy (newName, p->implementationName);
97 strcat (newName, " YAZ++");
98 p->implementationName = newName;
103 Z_ReferenceId **Yaz_Z_Assoc::get_referenceIdP(Z_APDU *apdu)
107 case Z_APDU_initRequest:
108 return &apdu->u.initRequest->referenceId;
109 case Z_APDU_initResponse:
110 return &apdu->u.initResponse->referenceId;
111 case Z_APDU_searchRequest:
112 return &apdu->u.searchRequest->referenceId;
113 case Z_APDU_searchResponse:
114 return &apdu->u.searchResponse->referenceId;
115 case Z_APDU_presentRequest:
116 return &apdu->u.presentRequest->referenceId;
117 case Z_APDU_presentResponse:
118 return &apdu->u.presentResponse->referenceId;
119 case Z_APDU_deleteResultSetRequest:
120 return &apdu->u.deleteResultSetRequest->referenceId;
121 case Z_APDU_deleteResultSetResponse:
122 return &apdu->u.deleteResultSetResponse->referenceId;
123 case Z_APDU_accessControlRequest:
124 return &apdu->u.accessControlRequest->referenceId;
125 case Z_APDU_accessControlResponse:
126 return &apdu->u.accessControlResponse->referenceId;
127 case Z_APDU_resourceControlRequest:
128 return &apdu->u.resourceControlRequest->referenceId;
129 case Z_APDU_resourceControlResponse:
130 return &apdu->u.resourceControlResponse->referenceId;
131 case Z_APDU_triggerResourceControlRequest:
132 return &apdu->u.triggerResourceControlRequest->referenceId;
133 case Z_APDU_resourceReportRequest:
134 return &apdu->u.resourceReportRequest->referenceId;
135 case Z_APDU_resourceReportResponse:
136 return &apdu->u.resourceReportResponse->referenceId;
137 case Z_APDU_scanRequest:
138 return &apdu->u.scanRequest->referenceId;
139 case Z_APDU_scanResponse:
140 return &apdu->u.scanResponse->referenceId;
141 case Z_APDU_sortRequest:
142 return &apdu->u.sortRequest->referenceId;
143 case Z_APDU_sortResponse:
144 return &apdu->u.sortResponse->referenceId;
145 case Z_APDU_segmentRequest:
146 return &apdu->u.segmentRequest->referenceId;
147 case Z_APDU_extendedServicesRequest:
148 return &apdu->u.extendedServicesRequest->referenceId;
149 case Z_APDU_extendedServicesResponse:
150 return &apdu->u.extendedServicesResponse->referenceId;
152 return &apdu->u.close->referenceId;
157 void Yaz_Z_Assoc::transfer_referenceId(Z_APDU *from, Z_APDU *to)
159 Z_ReferenceId **id_from = get_referenceIdP(from);
160 Z_ReferenceId **id_to = get_referenceIdP(to);
161 if (id_from && *id_from && id_to)
163 *id_to = (Z_ReferenceId*) odr_malloc (m_odr_out, sizeof(**id_to));
164 (*id_to)->size = (*id_to)->len = (*id_from)->len;
165 (*id_to)->buf = (unsigned char*) odr_malloc (m_odr_out, (*id_to)->len);
166 memcpy ((*id_to)->buf, (*id_from)->buf, (*id_to)->len);
172 int Yaz_Z_Assoc::send_Z_PDU(Z_APDU *apdu, int *plen)
176 if (encode_Z_PDU(apdu, &buf, &len) > 0)
180 return m_PDU_Observable->send_PDU(buf, len);
185 Z_APDU *Yaz_Z_Assoc::decode_Z_PDU(const char *buf, int len)
189 odr_reset (m_odr_in);
190 odr_setbuf (m_odr_in, (char*) buf, len, 0);
192 if (!z_APDU(m_odr_in, &apdu, 0, 0))
194 yaz_log(LOG_LOG, "ODR error on incoming PDU: %s [near byte %d] ",
195 odr_errmsg(odr_geterror(m_odr_in)),
196 odr_offset(m_odr_in));
197 yaz_log(LOG_LOG, "PDU dump:");
198 odr_dumpBER(yaz_log_file(), buf, len);
205 z_APDU(m_odr_print, &apdu, 0, "decode");
212 int Yaz_Z_Assoc::encode_Z_PDU(Z_APDU *apdu, char **buf, int *len)
216 z_APDU(m_odr_print, &apdu, 0, "encode");
219 if (!z_APDU(m_odr_out, &apdu, 0, 0))
222 fprintf (m_APDU_file, "PDU encode failed (above)");
223 yaz_log (LOG_LOG, "yaz_Z_Assoc::encode_Z_PDU failed");
226 *buf = odr_getbuf (m_odr_out, len, 0);
227 odr_reset (m_odr_out);
231 const char *Yaz_Z_Assoc::get_hostname()
236 int Yaz_Z_Assoc::client(const char *addr)
238 delete [] m_hostname;
239 m_hostname = new char[strlen(addr)+1];
240 strcpy (m_hostname, addr);
241 return m_PDU_Observable->connect (this, addr);
244 void Yaz_Z_Assoc::close()
246 m_PDU_Observable->close ();
249 void Yaz_Z_Assoc::server(const char *addr)
251 delete [] m_hostname;
252 m_hostname = new char[strlen(addr)+1];
253 strcpy (m_hostname, addr);
254 m_PDU_Observable->listen (this, addr);
257 ODR Yaz_Z_Assoc::odr_encode()
262 ODR Yaz_Z_Assoc::odr_decode()
266 ODR Yaz_Z_Assoc::odr_print()
271 void Yaz_Z_Assoc::timeout(int timeout)
273 m_PDU_Observable->idleTime(timeout);
276 void Yaz_Z_Assoc::get_otherInfoAPDU(Z_APDU *apdu, Z_OtherInformation ***oip)
280 case Z_APDU_initRequest:
281 *oip = &apdu->u.initRequest->otherInfo;
283 case Z_APDU_searchRequest:
284 *oip = &apdu->u.searchRequest->otherInfo;
286 case Z_APDU_presentRequest:
287 *oip = &apdu->u.presentRequest->otherInfo;
289 case Z_APDU_sortRequest:
290 *oip = &apdu->u.sortRequest->otherInfo;
292 case Z_APDU_scanRequest:
293 *oip = &apdu->u.scanRequest->otherInfo;
295 case Z_APDU_extendedServicesRequest:
296 *oip = &apdu->u.extendedServicesRequest->otherInfo;
298 case Z_APDU_deleteResultSetRequest:
299 *oip = &apdu->u.deleteResultSetRequest->otherInfo;
301 case Z_APDU_initResponse:
302 *oip = &apdu->u.initResponse->otherInfo;
304 case Z_APDU_searchResponse:
305 *oip = &apdu->u.searchResponse->otherInfo;
307 case Z_APDU_presentResponse:
308 *oip = &apdu->u.presentResponse->otherInfo;
310 case Z_APDU_sortResponse:
311 *oip = &apdu->u.sortResponse->otherInfo;
313 case Z_APDU_scanResponse:
314 *oip = &apdu->u.scanResponse->otherInfo;
316 case Z_APDU_extendedServicesResponse:
317 *oip = &apdu->u.extendedServicesResponse->otherInfo;
319 case Z_APDU_deleteResultSetResponse:
320 *oip = &apdu->u.deleteResultSetResponse->otherInfo;
328 void Yaz_Z_Assoc::set_otherInformationString (
330 int oidval, int categoryValue,
333 Z_OtherInformation **otherInformation;
334 get_otherInfoAPDU(apdu, &otherInformation);
335 if (!otherInformation)
337 set_otherInformationString(otherInformation, oidval, categoryValue, str);
340 void Yaz_Z_Assoc::set_otherInformationString (
341 Z_OtherInformation **otherInformation,
342 int oidval, int categoryValue,
347 ent.proto = PROTO_Z3950;
348 ent.oclass = CLASS_USERINFO;
349 ent.value = (oid_value) oidval;
350 if (!oid_ent_to_oid (&ent, oid))
352 set_otherInformationString(otherInformation, oid, categoryValue, str);
355 void Yaz_Z_Assoc::set_otherInformationString (
356 Z_OtherInformation **otherInformation,
357 int *oid, int categoryValue, const char *str)
359 Z_OtherInformationUnit *oi =
360 update_otherInformation(otherInformation, 1, oid, categoryValue, 0);
363 oi->information.characterInfo = odr_strdup (odr_encode(), str);
366 Z_OtherInformationUnit *Yaz_Z_Assoc::update_otherInformation (
367 Z_OtherInformation **otherInformationP, int createFlag,
368 int *oid, int categoryValue, int deleteFlag)
370 return yaz_oi_update (otherInformationP,
371 (createFlag ? odr_encode() : 0),
372 oid, categoryValue, deleteFlag);
375 Z_ReferenceId* Yaz_Z_Assoc::getRefID(char* str)
377 Z_ReferenceId* id = NULL;
381 id = (Z_ReferenceId*) odr_malloc (m_odr_out, sizeof(*id));
382 id->size = id->len = strlen(str);
383 id->buf = (unsigned char *) str;