2 * Copyright (c) 1998-2004, Index Data.
3 * See the file LICENSE for details.
5 * $Id: yaz-z-assoc.cpp,v 1.38 2005-06-25 15:53:19 adam Exp $
12 #include <yaz++/z-assoc.h>
13 #include <yaz/otherinfo.h>
15 using namespace yazpp_1;
17 int Z_Assoc::yaz_init_func()
20 signal (SIGPIPE, SIG_IGN);
25 int Z_Assoc::yaz_init_flag = Z_Assoc::yaz_init_func();
27 Z_Assoc::Z_Assoc(IPDU_Observable *the_PDU_Observable)
29 m_PDU_Observable = the_PDU_Observable;
30 m_odr_in = odr_createmem (ODR_DECODE);
31 m_odr_out = odr_createmem (ODR_ENCODE);
32 m_odr_print = odr_createmem (ODR_PRINT);
40 void Z_Assoc::set_APDU_log(const char *fname)
42 if (m_APDU_file && m_APDU_file != stderr)
47 delete [] m_APDU_fname;
52 m_APDU_fname = new char[strlen(fname)+1];
53 strcpy (m_APDU_fname, fname);
54 if (!strcmp(fname, "-"))
56 else if (*fname == '\0')
59 m_APDU_file = fopen (fname, "a");
60 odr_setprint(m_odr_print, m_APDU_file);
64 int Z_Assoc::set_APDU_yazlog(int v)
66 int old = m_APDU_yazlog;
71 const char *Z_Assoc::get_APDU_log()
78 m_PDU_Observable->destroy();
79 delete m_PDU_Observable;
80 odr_destroy (m_odr_print); // note: also runs fclose on m_APDU_file ..
81 odr_destroy (m_odr_out);
82 odr_destroy (m_odr_in);
83 delete [] m_APDU_fname;
87 void Z_Assoc::recv_PDU(const char *buf, int len)
89 yaz_log (m_log, "recv_PDU len=%d", len);
90 Z_GDU *apdu = decode_GDU (buf, len);
102 Z_APDU *Z_Assoc::create_Z_PDU(int type)
104 Z_APDU *apdu = zget_APDU(m_odr_out, type);
105 if (apdu->which == Z_APDU_initRequest)
107 Z_InitRequest * p = apdu->u.initRequest;
108 char *newName = (char*) odr_malloc(m_odr_out, 50);
109 strcpy (newName, p->implementationName);
110 strcat (newName, " YAZ++");
111 p->implementationName = newName;
116 Z_ReferenceId **Z_Assoc::get_referenceIdP(Z_APDU *apdu)
120 case Z_APDU_initRequest:
121 return &apdu->u.initRequest->referenceId;
122 case Z_APDU_initResponse:
123 return &apdu->u.initResponse->referenceId;
124 case Z_APDU_searchRequest:
125 return &apdu->u.searchRequest->referenceId;
126 case Z_APDU_searchResponse:
127 return &apdu->u.searchResponse->referenceId;
128 case Z_APDU_presentRequest:
129 return &apdu->u.presentRequest->referenceId;
130 case Z_APDU_presentResponse:
131 return &apdu->u.presentResponse->referenceId;
132 case Z_APDU_deleteResultSetRequest:
133 return &apdu->u.deleteResultSetRequest->referenceId;
134 case Z_APDU_deleteResultSetResponse:
135 return &apdu->u.deleteResultSetResponse->referenceId;
136 case Z_APDU_accessControlRequest:
137 return &apdu->u.accessControlRequest->referenceId;
138 case Z_APDU_accessControlResponse:
139 return &apdu->u.accessControlResponse->referenceId;
140 case Z_APDU_resourceControlRequest:
141 return &apdu->u.resourceControlRequest->referenceId;
142 case Z_APDU_resourceControlResponse:
143 return &apdu->u.resourceControlResponse->referenceId;
144 case Z_APDU_triggerResourceControlRequest:
145 return &apdu->u.triggerResourceControlRequest->referenceId;
146 case Z_APDU_resourceReportRequest:
147 return &apdu->u.resourceReportRequest->referenceId;
148 case Z_APDU_resourceReportResponse:
149 return &apdu->u.resourceReportResponse->referenceId;
150 case Z_APDU_scanRequest:
151 return &apdu->u.scanRequest->referenceId;
152 case Z_APDU_scanResponse:
153 return &apdu->u.scanResponse->referenceId;
154 case Z_APDU_sortRequest:
155 return &apdu->u.sortRequest->referenceId;
156 case Z_APDU_sortResponse:
157 return &apdu->u.sortResponse->referenceId;
158 case Z_APDU_segmentRequest:
159 return &apdu->u.segmentRequest->referenceId;
160 case Z_APDU_extendedServicesRequest:
161 return &apdu->u.extendedServicesRequest->referenceId;
162 case Z_APDU_extendedServicesResponse:
163 return &apdu->u.extendedServicesResponse->referenceId;
165 return &apdu->u.close->referenceId;
170 void Z_Assoc::transfer_referenceId(Z_APDU *from, Z_APDU *to)
172 Z_ReferenceId **id_from = get_referenceIdP(from);
173 Z_ReferenceId **id_to = get_referenceIdP(to);
174 if (id_from && *id_from && id_to)
176 *id_to = (Z_ReferenceId*) odr_malloc (m_odr_out, sizeof(**id_to));
177 (*id_to)->size = (*id_to)->len = (*id_from)->len;
178 (*id_to)->buf = (unsigned char*) odr_malloc (m_odr_out, (*id_to)->len);
179 memcpy ((*id_to)->buf, (*id_from)->buf, (*id_to)->len);
185 int Z_Assoc::send_Z_PDU(Z_APDU *apdu, int *plen)
187 Z_GDU *gdu = (Z_GDU*) odr_malloc(odr_encode(), sizeof(*gdu));
188 gdu->which = Z_GDU_Z3950;
190 return send_GDU(gdu, plen);
193 int Z_Assoc::send_GDU(Z_GDU *apdu, int *plen)
197 if (encode_GDU(apdu, &buf, &len) > 0)
201 return m_PDU_Observable->send_PDU(buf, len);
206 Z_GDU *Z_Assoc::decode_GDU(const char *buf, int len)
210 odr_reset (m_odr_in);
211 odr_setbuf (m_odr_in, (char*) buf, len, 0);
213 if (!z_GDU(m_odr_in, &apdu, 0, 0))
215 const char *element = odr_getelement(m_odr_in);
216 yaz_log(YLOG_LOG, "PDU decode failed '%s' near byte %d. Element %s",
217 odr_errmsg(odr_geterror(m_odr_in)),
218 odr_offset(m_odr_in),
219 element ? element : "unknown");
220 yaz_log(YLOG_LOG, "PDU dump:");
221 odr_dumpBER(yaz_log_file(), buf, len);
227 { // use YAZ log FILE
228 FILE *save = m_APDU_file;
230 odr_setprint(m_odr_print, yaz_log_file());
231 z_GDU(m_odr_print, &apdu, 0, "decode");
233 odr_setprint(m_odr_print, save);
237 z_GDU(m_odr_print, &apdu, 0, "decode");
244 int Z_Assoc::encode_GDU(Z_GDU *apdu, char **buf, int *len)
246 const char *element = 0;
247 int r = z_GDU(m_odr_out, &apdu, 0, 0);
249 if (!r) // decoding failed. Get the failed element
250 element = odr_getelement(m_odr_out);
252 if (m_APDU_yazlog || !r)
255 yaz_log (YLOG_LOG, "PDU encode failed. Element %s",
256 element ? element : "unknown");
257 FILE *save = m_APDU_file;
258 FILE *yazf = yaz_log_file();
259 odr_setprint(m_odr_print, yazf); // use YAZ log FILE
260 z_GDU(m_odr_print, &apdu, 0, "encode");
262 odr_setprint(m_odr_print, save);
267 fprintf (m_APDU_file, "PDU encode failed. Element %s",
268 element ? element : "unknown");
269 z_GDU(m_odr_print, &apdu, 0, "encode");
272 if (!r) // encoding failed
274 *buf = odr_getbuf (m_odr_out, len, 0);
275 odr_reset (m_odr_out);
279 const char *Z_Assoc::get_hostname()
284 int Z_Assoc::client(const char *addr)
286 delete [] m_hostname;
287 m_hostname = new char[strlen(addr)+1];
288 strcpy (m_hostname, addr);
289 return m_PDU_Observable->connect (this, addr);
292 void Z_Assoc::close()
294 m_PDU_Observable->close ();
297 int Z_Assoc::server(const char *addr)
299 delete [] m_hostname;
300 m_hostname = new char[strlen(addr)+1];
301 strcpy (m_hostname, addr);
302 return m_PDU_Observable->listen (this, addr);
305 ODR Z_Assoc::odr_encode()
310 ODR Z_Assoc::odr_decode()
314 ODR Z_Assoc::odr_print()
319 void Z_Assoc::timeout(int timeout)
321 m_PDU_Observable->idleTime(timeout);
324 void Z_Assoc::get_otherInfoAPDU(Z_APDU *apdu, Z_OtherInformation ***oip)
328 case Z_APDU_initRequest:
329 *oip = &apdu->u.initRequest->otherInfo;
331 case Z_APDU_searchRequest:
332 *oip = &apdu->u.searchRequest->otherInfo;
334 case Z_APDU_presentRequest:
335 *oip = &apdu->u.presentRequest->otherInfo;
337 case Z_APDU_sortRequest:
338 *oip = &apdu->u.sortRequest->otherInfo;
340 case Z_APDU_scanRequest:
341 *oip = &apdu->u.scanRequest->otherInfo;
343 case Z_APDU_extendedServicesRequest:
344 *oip = &apdu->u.extendedServicesRequest->otherInfo;
346 case Z_APDU_deleteResultSetRequest:
347 *oip = &apdu->u.deleteResultSetRequest->otherInfo;
349 case Z_APDU_initResponse:
350 *oip = &apdu->u.initResponse->otherInfo;
352 case Z_APDU_searchResponse:
353 *oip = &apdu->u.searchResponse->otherInfo;
355 case Z_APDU_presentResponse:
356 *oip = &apdu->u.presentResponse->otherInfo;
358 case Z_APDU_sortResponse:
359 *oip = &apdu->u.sortResponse->otherInfo;
361 case Z_APDU_scanResponse:
362 *oip = &apdu->u.scanResponse->otherInfo;
364 case Z_APDU_extendedServicesResponse:
365 *oip = &apdu->u.extendedServicesResponse->otherInfo;
367 case Z_APDU_deleteResultSetResponse:
368 *oip = &apdu->u.deleteResultSetResponse->otherInfo;
376 void Z_Assoc::set_otherInformationString (
378 int oidval, int categoryValue,
381 Z_OtherInformation **otherInformation;
382 get_otherInfoAPDU(apdu, &otherInformation);
383 if (!otherInformation)
385 set_otherInformationString(otherInformation, oidval, categoryValue, str);
388 void Z_Assoc::set_otherInformationString (
389 Z_OtherInformation **otherInformation,
390 int oidval, int categoryValue,
395 ent.proto = PROTO_Z3950;
396 ent.oclass = CLASS_USERINFO;
397 ent.value = (oid_value) oidval;
398 if (!oid_ent_to_oid (&ent, oid))
400 set_otherInformationString(otherInformation, oid, categoryValue, str);
403 void Z_Assoc::set_otherInformationString (
404 Z_OtherInformation **otherInformation,
405 int *oid, int categoryValue, const char *str)
407 Z_OtherInformationUnit *oi =
408 update_otherInformation(otherInformation, 1, oid, categoryValue, 0);
411 oi->information.characterInfo = odr_strdup (odr_encode(), str);
414 Z_OtherInformationUnit *Z_Assoc::update_otherInformation (
415 Z_OtherInformation **otherInformationP, int createFlag,
416 int *oid, int categoryValue, int deleteFlag)
418 return yaz_oi_update (otherInformationP,
419 (createFlag ? odr_encode() : 0),
420 oid, categoryValue, deleteFlag);
423 Z_ReferenceId* Z_Assoc::getRefID(char* str)
425 Z_ReferenceId* id = NULL;
429 id = (Z_ReferenceId*) odr_malloc (m_odr_out, sizeof(*id));
430 id->size = id->len = strlen(str);
431 id->buf = (unsigned char *) str;
439 * indent-tabs-mode: nil
441 * vim: shiftwidth=4 tabstop=8 expandtab