2 * Copyright (c) 1998-2003, Index Data.
3 * See the file LICENSE for details.
5 * $Id: yaz-z-assoc.cpp,v 1.31 2003-10-23 13:49:58 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);
38 void Yaz_Z_Assoc::set_APDU_log(const char *fname)
40 if (m_APDU_file && m_APDU_file != stderr)
45 delete [] m_APDU_fname;
50 m_APDU_fname = new char[strlen(fname)+1];
51 strcpy (m_APDU_fname, fname);
52 if (!strcmp(fname, "-"))
54 else if (*fname == '\0')
57 m_APDU_file = fopen (fname, "a");
58 odr_setprint(m_odr_print, m_APDU_file);
62 int Yaz_Z_Assoc::set_APDU_yazlog(int v)
64 int old = m_APDU_yazlog;
69 const char *Yaz_Z_Assoc::get_APDU_log()
74 Yaz_Z_Assoc::~Yaz_Z_Assoc()
76 m_PDU_Observable->destroy();
77 delete m_PDU_Observable;
78 odr_destroy (m_odr_print); // note: also runs fclose on m_APDU_file ..
79 odr_destroy (m_odr_out);
80 odr_destroy (m_odr_in);
81 delete [] m_APDU_fname;
85 void Yaz_Z_Assoc::recv_PDU(const char *buf, int len)
87 yaz_log (m_log, "recv_PDU len=%d", len);
88 Z_APDU *apdu = decode_Z_PDU (buf, len);
91 recv_Z_PDU (apdu, len);
100 Z_APDU *Yaz_Z_Assoc::create_Z_PDU(int type)
102 Z_APDU *apdu = zget_APDU(m_odr_out, type);
103 if (apdu->which == Z_APDU_initRequest)
105 Z_InitRequest * p = apdu->u.initRequest;
106 char *newName = (char*) odr_malloc(m_odr_out, 50);
107 strcpy (newName, p->implementationName);
108 strcat (newName, " YAZ++");
109 p->implementationName = newName;
114 Z_ReferenceId **Yaz_Z_Assoc::get_referenceIdP(Z_APDU *apdu)
118 case Z_APDU_initRequest:
119 return &apdu->u.initRequest->referenceId;
120 case Z_APDU_initResponse:
121 return &apdu->u.initResponse->referenceId;
122 case Z_APDU_searchRequest:
123 return &apdu->u.searchRequest->referenceId;
124 case Z_APDU_searchResponse:
125 return &apdu->u.searchResponse->referenceId;
126 case Z_APDU_presentRequest:
127 return &apdu->u.presentRequest->referenceId;
128 case Z_APDU_presentResponse:
129 return &apdu->u.presentResponse->referenceId;
130 case Z_APDU_deleteResultSetRequest:
131 return &apdu->u.deleteResultSetRequest->referenceId;
132 case Z_APDU_deleteResultSetResponse:
133 return &apdu->u.deleteResultSetResponse->referenceId;
134 case Z_APDU_accessControlRequest:
135 return &apdu->u.accessControlRequest->referenceId;
136 case Z_APDU_accessControlResponse:
137 return &apdu->u.accessControlResponse->referenceId;
138 case Z_APDU_resourceControlRequest:
139 return &apdu->u.resourceControlRequest->referenceId;
140 case Z_APDU_resourceControlResponse:
141 return &apdu->u.resourceControlResponse->referenceId;
142 case Z_APDU_triggerResourceControlRequest:
143 return &apdu->u.triggerResourceControlRequest->referenceId;
144 case Z_APDU_resourceReportRequest:
145 return &apdu->u.resourceReportRequest->referenceId;
146 case Z_APDU_resourceReportResponse:
147 return &apdu->u.resourceReportResponse->referenceId;
148 case Z_APDU_scanRequest:
149 return &apdu->u.scanRequest->referenceId;
150 case Z_APDU_scanResponse:
151 return &apdu->u.scanResponse->referenceId;
152 case Z_APDU_sortRequest:
153 return &apdu->u.sortRequest->referenceId;
154 case Z_APDU_sortResponse:
155 return &apdu->u.sortResponse->referenceId;
156 case Z_APDU_segmentRequest:
157 return &apdu->u.segmentRequest->referenceId;
158 case Z_APDU_extendedServicesRequest:
159 return &apdu->u.extendedServicesRequest->referenceId;
160 case Z_APDU_extendedServicesResponse:
161 return &apdu->u.extendedServicesResponse->referenceId;
163 return &apdu->u.close->referenceId;
168 void Yaz_Z_Assoc::transfer_referenceId(Z_APDU *from, Z_APDU *to)
170 Z_ReferenceId **id_from = get_referenceIdP(from);
171 Z_ReferenceId **id_to = get_referenceIdP(to);
172 if (id_from && *id_from && id_to)
174 *id_to = (Z_ReferenceId*) odr_malloc (m_odr_out, sizeof(**id_to));
175 (*id_to)->size = (*id_to)->len = (*id_from)->len;
176 (*id_to)->buf = (unsigned char*) odr_malloc (m_odr_out, (*id_to)->len);
177 memcpy ((*id_to)->buf, (*id_from)->buf, (*id_to)->len);
183 int Yaz_Z_Assoc::send_Z_PDU(Z_APDU *apdu, int *plen)
187 if (encode_Z_PDU(apdu, &buf, &len) > 0)
191 return m_PDU_Observable->send_PDU(buf, len);
196 Z_APDU *Yaz_Z_Assoc::decode_Z_PDU(const char *buf, int len)
200 odr_reset (m_odr_in);
201 odr_setbuf (m_odr_in, (char*) buf, len, 0);
203 if (!z_APDU(m_odr_in, &apdu, 0, 0))
205 yaz_log(LOG_LOG, "ODR error on incoming PDU: %s [near byte %d] ",
206 odr_errmsg(odr_geterror(m_odr_in)),
207 odr_offset(m_odr_in));
208 yaz_log(LOG_LOG, "PDU dump:");
209 odr_dumpBER(yaz_log_file(), buf, len);
215 { // use YAZ log FILE
216 FILE *save = m_APDU_file;
218 odr_setprint(m_odr_print, yaz_log_file());
219 z_APDU(m_odr_print, &apdu, 0, "decode");
221 odr_setprint(m_odr_print, save);
225 z_APDU(m_odr_print, &apdu, 0, "decode");
232 int Yaz_Z_Assoc::encode_Z_PDU(Z_APDU *apdu, char **buf, int *len)
236 FILE *save = m_APDU_file;
237 odr_setprint(m_odr_print, yaz_log_file()); // use YAZ log FILE
238 z_APDU(m_odr_print, &apdu, 0, "encode");
240 odr_setprint(m_odr_print, save);
244 z_APDU(m_odr_print, &apdu, 0, "encode");
247 if (!z_APDU(m_odr_out, &apdu, 0, 0))
250 fprintf (m_APDU_file, "PDU encode failed (above)");
251 yaz_log (LOG_LOG, "yaz_Z_Assoc::encode_Z_PDU failed");
254 *buf = odr_getbuf (m_odr_out, len, 0);
255 odr_reset (m_odr_out);
259 const char *Yaz_Z_Assoc::get_hostname()
264 int Yaz_Z_Assoc::client(const char *addr)
266 delete [] m_hostname;
267 m_hostname = new char[strlen(addr)+1];
268 strcpy (m_hostname, addr);
269 return m_PDU_Observable->connect (this, addr);
272 void Yaz_Z_Assoc::close()
274 m_PDU_Observable->close ();
277 int Yaz_Z_Assoc::server(const char *addr)
279 delete [] m_hostname;
280 m_hostname = new char[strlen(addr)+1];
281 strcpy (m_hostname, addr);
282 return m_PDU_Observable->listen (this, addr);
285 ODR Yaz_Z_Assoc::odr_encode()
290 ODR Yaz_Z_Assoc::odr_decode()
294 ODR Yaz_Z_Assoc::odr_print()
299 void Yaz_Z_Assoc::timeout(int timeout)
301 m_PDU_Observable->idleTime(timeout);
304 void Yaz_Z_Assoc::get_otherInfoAPDU(Z_APDU *apdu, Z_OtherInformation ***oip)
308 case Z_APDU_initRequest:
309 *oip = &apdu->u.initRequest->otherInfo;
311 case Z_APDU_searchRequest:
312 *oip = &apdu->u.searchRequest->otherInfo;
314 case Z_APDU_presentRequest:
315 *oip = &apdu->u.presentRequest->otherInfo;
317 case Z_APDU_sortRequest:
318 *oip = &apdu->u.sortRequest->otherInfo;
320 case Z_APDU_scanRequest:
321 *oip = &apdu->u.scanRequest->otherInfo;
323 case Z_APDU_extendedServicesRequest:
324 *oip = &apdu->u.extendedServicesRequest->otherInfo;
326 case Z_APDU_deleteResultSetRequest:
327 *oip = &apdu->u.deleteResultSetRequest->otherInfo;
329 case Z_APDU_initResponse:
330 *oip = &apdu->u.initResponse->otherInfo;
332 case Z_APDU_searchResponse:
333 *oip = &apdu->u.searchResponse->otherInfo;
335 case Z_APDU_presentResponse:
336 *oip = &apdu->u.presentResponse->otherInfo;
338 case Z_APDU_sortResponse:
339 *oip = &apdu->u.sortResponse->otherInfo;
341 case Z_APDU_scanResponse:
342 *oip = &apdu->u.scanResponse->otherInfo;
344 case Z_APDU_extendedServicesResponse:
345 *oip = &apdu->u.extendedServicesResponse->otherInfo;
347 case Z_APDU_deleteResultSetResponse:
348 *oip = &apdu->u.deleteResultSetResponse->otherInfo;
356 void Yaz_Z_Assoc::set_otherInformationString (
358 int oidval, int categoryValue,
361 Z_OtherInformation **otherInformation;
362 get_otherInfoAPDU(apdu, &otherInformation);
363 if (!otherInformation)
365 set_otherInformationString(otherInformation, oidval, categoryValue, str);
368 void Yaz_Z_Assoc::set_otherInformationString (
369 Z_OtherInformation **otherInformation,
370 int oidval, int categoryValue,
375 ent.proto = PROTO_Z3950;
376 ent.oclass = CLASS_USERINFO;
377 ent.value = (oid_value) oidval;
378 if (!oid_ent_to_oid (&ent, oid))
380 set_otherInformationString(otherInformation, oid, categoryValue, str);
383 void Yaz_Z_Assoc::set_otherInformationString (
384 Z_OtherInformation **otherInformation,
385 int *oid, int categoryValue, const char *str)
387 Z_OtherInformationUnit *oi =
388 update_otherInformation(otherInformation, 1, oid, categoryValue, 0);
391 oi->information.characterInfo = odr_strdup (odr_encode(), str);
394 Z_OtherInformationUnit *Yaz_Z_Assoc::update_otherInformation (
395 Z_OtherInformation **otherInformationP, int createFlag,
396 int *oid, int categoryValue, int deleteFlag)
398 return yaz_oi_update (otherInformationP,
399 (createFlag ? odr_encode() : 0),
400 oid, categoryValue, deleteFlag);
403 Z_ReferenceId* Yaz_Z_Assoc::getRefID(char* str)
405 Z_ReferenceId* id = NULL;
409 id = (Z_ReferenceId*) odr_malloc (m_odr_out, sizeof(*id));
410 id->size = id->len = strlen(str);
411 id->buf = (unsigned char *) str;