2 * Copyright (c) 1998-2007, Index Data.
3 * See the file LICENSE for details.
5 * $Id: yaz-z-assoc.cpp,v 1.42 2007-04-16 21:54:23 adam Exp $
12 #include <yazpp/z-assoc.h>
13 #include <yaz/otherinfo.h>
14 #include <yaz/oid_db.h>
16 using namespace yazpp_1;
18 int Z_Assoc::yaz_init_func()
21 signal (SIGPIPE, SIG_IGN);
26 int Z_Assoc::yaz_init_flag = Z_Assoc::yaz_init_func();
28 Z_Assoc::Z_Assoc(IPDU_Observable *the_PDU_Observable)
30 m_PDU_Observable = the_PDU_Observable;
31 m_odr_in = odr_createmem (ODR_DECODE);
32 m_odr_out = odr_createmem (ODR_ENCODE);
33 m_odr_print = odr_createmem (ODR_PRINT);
41 void Z_Assoc::set_APDU_log(const char *fname)
43 if (m_APDU_file && m_APDU_file != stderr)
48 delete [] m_APDU_fname;
53 m_APDU_fname = new char[strlen(fname)+1];
54 strcpy (m_APDU_fname, fname);
55 if (!strcmp(fname, "-"))
57 else if (*fname == '\0')
60 m_APDU_file = fopen (fname, "a");
61 odr_setprint(m_odr_print, m_APDU_file);
65 int Z_Assoc::set_APDU_yazlog(int v)
67 int old = m_APDU_yazlog;
72 const char *Z_Assoc::get_APDU_log()
79 m_PDU_Observable->destroy();
80 delete m_PDU_Observable;
81 odr_destroy (m_odr_print); // note: also runs fclose on m_APDU_file ..
82 odr_destroy (m_odr_out);
83 odr_destroy (m_odr_in);
84 delete [] m_APDU_fname;
88 void Z_Assoc::recv_PDU(const char *buf, int len)
90 yaz_log (m_log, "recv_PDU len=%d", len);
91 Z_GDU *apdu = decode_GDU (buf, len);
103 Z_APDU *Z_Assoc::create_Z_PDU(int type)
105 Z_APDU *apdu = zget_APDU(m_odr_out, type);
106 if (apdu->which == Z_APDU_initRequest)
108 Z_InitRequest * p = apdu->u.initRequest;
109 char *newName = (char*) odr_malloc(m_odr_out, 50);
110 strcpy (newName, p->implementationName);
111 strcat (newName, " YAZ++");
112 p->implementationName = newName;
117 Z_ReferenceId **Z_Assoc::get_referenceIdP(Z_APDU *apdu)
121 case Z_APDU_initRequest:
122 return &apdu->u.initRequest->referenceId;
123 case Z_APDU_initResponse:
124 return &apdu->u.initResponse->referenceId;
125 case Z_APDU_searchRequest:
126 return &apdu->u.searchRequest->referenceId;
127 case Z_APDU_searchResponse:
128 return &apdu->u.searchResponse->referenceId;
129 case Z_APDU_presentRequest:
130 return &apdu->u.presentRequest->referenceId;
131 case Z_APDU_presentResponse:
132 return &apdu->u.presentResponse->referenceId;
133 case Z_APDU_deleteResultSetRequest:
134 return &apdu->u.deleteResultSetRequest->referenceId;
135 case Z_APDU_deleteResultSetResponse:
136 return &apdu->u.deleteResultSetResponse->referenceId;
137 case Z_APDU_accessControlRequest:
138 return &apdu->u.accessControlRequest->referenceId;
139 case Z_APDU_accessControlResponse:
140 return &apdu->u.accessControlResponse->referenceId;
141 case Z_APDU_resourceControlRequest:
142 return &apdu->u.resourceControlRequest->referenceId;
143 case Z_APDU_resourceControlResponse:
144 return &apdu->u.resourceControlResponse->referenceId;
145 case Z_APDU_triggerResourceControlRequest:
146 return &apdu->u.triggerResourceControlRequest->referenceId;
147 case Z_APDU_resourceReportRequest:
148 return &apdu->u.resourceReportRequest->referenceId;
149 case Z_APDU_resourceReportResponse:
150 return &apdu->u.resourceReportResponse->referenceId;
151 case Z_APDU_scanRequest:
152 return &apdu->u.scanRequest->referenceId;
153 case Z_APDU_scanResponse:
154 return &apdu->u.scanResponse->referenceId;
155 case Z_APDU_sortRequest:
156 return &apdu->u.sortRequest->referenceId;
157 case Z_APDU_sortResponse:
158 return &apdu->u.sortResponse->referenceId;
159 case Z_APDU_segmentRequest:
160 return &apdu->u.segmentRequest->referenceId;
161 case Z_APDU_extendedServicesRequest:
162 return &apdu->u.extendedServicesRequest->referenceId;
163 case Z_APDU_extendedServicesResponse:
164 return &apdu->u.extendedServicesResponse->referenceId;
166 return &apdu->u.close->referenceId;
171 void Z_Assoc::transfer_referenceId(Z_APDU *from, Z_APDU *to)
173 Z_ReferenceId **id_from = get_referenceIdP(from);
174 Z_ReferenceId **id_to = get_referenceIdP(to);
175 if (id_from && *id_from && id_to)
177 *id_to = (Z_ReferenceId*) odr_malloc (m_odr_out, sizeof(**id_to));
178 (*id_to)->size = (*id_to)->len = (*id_from)->len;
179 (*id_to)->buf = (unsigned char*) odr_malloc (m_odr_out, (*id_to)->len);
180 memcpy ((*id_to)->buf, (*id_from)->buf, (*id_to)->len);
186 int Z_Assoc::send_Z_PDU(Z_APDU *apdu, int *plen)
188 Z_GDU *gdu = (Z_GDU*) odr_malloc(odr_encode(), sizeof(*gdu));
189 gdu->which = Z_GDU_Z3950;
191 return send_GDU(gdu, plen);
194 int Z_Assoc::send_GDU(Z_GDU *apdu, int *plen)
198 if (encode_GDU(apdu, &buf, &len) > 0)
202 return m_PDU_Observable->send_PDU(buf, len);
207 Z_GDU *Z_Assoc::decode_GDU(const char *buf, int len)
211 odr_reset (m_odr_in);
212 odr_setbuf (m_odr_in, (char*) buf, len, 0);
214 if (!z_GDU(m_odr_in, &apdu, 0, 0))
216 const char *element = odr_getelement(m_odr_in);
217 yaz_log(YLOG_LOG, "PDU decode failed '%s' near byte %ld. Element %s",
218 odr_errmsg(odr_geterror(m_odr_in)),
219 (long) odr_offset(m_odr_in),
220 element ? element : "unknown");
221 yaz_log(YLOG_LOG, "PDU dump:");
222 odr_dumpBER(yaz_log_file(), buf, len);
228 { // use YAZ log FILE
229 FILE *save = m_APDU_file;
231 odr_setprint(m_odr_print, yaz_log_file());
232 z_GDU(m_odr_print, &apdu, 0, "decode");
234 odr_setprint(m_odr_print, save);
238 z_GDU(m_odr_print, &apdu, 0, "decode");
245 int Z_Assoc::encode_GDU(Z_GDU *apdu, char **buf, int *len)
247 const char *element = 0;
248 int r = z_GDU(m_odr_out, &apdu, 0, 0);
250 if (!r) // decoding failed. Get the failed element
251 element = odr_getelement(m_odr_out);
253 if (m_APDU_yazlog || !r)
256 yaz_log (YLOG_LOG, "PDU encode failed. Element %s",
257 element ? element : "unknown");
258 FILE *save = m_APDU_file;
259 FILE *yazf = yaz_log_file();
260 odr_setprint(m_odr_print, yazf); // use YAZ log FILE
261 z_GDU(m_odr_print, &apdu, 0, "encode");
263 odr_setprint(m_odr_print, save);
268 fprintf (m_APDU_file, "PDU encode failed. Element %s",
269 element ? element : "unknown");
270 z_GDU(m_odr_print, &apdu, 0, "encode");
273 if (!r) // encoding failed
275 *buf = odr_getbuf (m_odr_out, len, 0);
276 odr_reset (m_odr_out);
280 const char *Z_Assoc::get_hostname()
285 int Z_Assoc::client(const char *addr)
287 delete [] m_hostname;
288 m_hostname = new char[strlen(addr)+1];
289 strcpy (m_hostname, addr);
290 return m_PDU_Observable->connect (this, addr);
293 void Z_Assoc::close()
295 m_PDU_Observable->close ();
298 int Z_Assoc::server(const char *addr)
300 delete [] m_hostname;
301 m_hostname = new char[strlen(addr)+1];
302 strcpy (m_hostname, addr);
303 return m_PDU_Observable->listen (this, addr);
306 ODR Z_Assoc::odr_encode()
311 ODR Z_Assoc::odr_decode()
315 ODR Z_Assoc::odr_print()
320 void Z_Assoc::timeout(int timeout)
322 m_PDU_Observable->idleTime(timeout);
325 void Z_Assoc::get_otherInfoAPDU(Z_APDU *apdu, Z_OtherInformation ***oip)
329 case Z_APDU_initRequest:
330 *oip = &apdu->u.initRequest->otherInfo;
332 case Z_APDU_searchRequest:
333 *oip = &apdu->u.searchRequest->otherInfo;
335 case Z_APDU_presentRequest:
336 *oip = &apdu->u.presentRequest->otherInfo;
338 case Z_APDU_sortRequest:
339 *oip = &apdu->u.sortRequest->otherInfo;
341 case Z_APDU_scanRequest:
342 *oip = &apdu->u.scanRequest->otherInfo;
344 case Z_APDU_extendedServicesRequest:
345 *oip = &apdu->u.extendedServicesRequest->otherInfo;
347 case Z_APDU_deleteResultSetRequest:
348 *oip = &apdu->u.deleteResultSetRequest->otherInfo;
350 case Z_APDU_initResponse:
351 *oip = &apdu->u.initResponse->otherInfo;
353 case Z_APDU_searchResponse:
354 *oip = &apdu->u.searchResponse->otherInfo;
356 case Z_APDU_presentResponse:
357 *oip = &apdu->u.presentResponse->otherInfo;
359 case Z_APDU_sortResponse:
360 *oip = &apdu->u.sortResponse->otherInfo;
362 case Z_APDU_scanResponse:
363 *oip = &apdu->u.scanResponse->otherInfo;
365 case Z_APDU_extendedServicesResponse:
366 *oip = &apdu->u.extendedServicesResponse->otherInfo;
368 case Z_APDU_deleteResultSetResponse:
369 *oip = &apdu->u.deleteResultSetResponse->otherInfo;
377 void Z_Assoc::set_otherInformationString(
379 const int *oid, int categoryValue, const char *str)
381 Z_OtherInformation **otherInformation;
382 get_otherInfoAPDU(apdu, &otherInformation);
383 if (!otherInformation)
385 set_otherInformationString(otherInformation, oid, categoryValue, str);
389 void Z_Assoc::set_otherInformationString (
390 Z_OtherInformation **otherInformation,
391 const int *oid, int categoryValue, const char *str)
393 Z_OtherInformationUnit *oi =
394 update_otherInformation(otherInformation, 1, oid, categoryValue, 0);
397 oi->information.characterInfo = odr_strdup (odr_encode(), str);
400 Z_OtherInformationUnit *Z_Assoc::update_otherInformation (
401 Z_OtherInformation **otherInformationP, int createFlag,
402 const int *oid, int categoryValue, int deleteFlag)
404 return yaz_oi_update (otherInformationP,
405 (createFlag ? odr_encode() : 0),
406 oid, categoryValue, deleteFlag);
409 Z_ReferenceId* Z_Assoc::getRefID(char* str)
411 Z_ReferenceId* id = NULL;
415 id = (Z_ReferenceId*) odr_malloc (m_odr_out, sizeof(*id));
416 id->size = id->len = strlen(str);
417 id->buf = (unsigned char *) str;
425 * indent-tabs-mode: nil
427 * vim: shiftwidth=4 tabstop=8 expandtab