disable-zoom configure option
[yazpp-moved-to-github.git] / src / yaz-z-server.cpp
1 /*
2  * Copyright (c) 2000-2001, Index Data.
3  * See the file LICENSE for details.
4  * 
5  * $Id: yaz-z-server.cpp,v 1.15 2002-10-09 12:50:26 adam Exp $
6  */
7
8 #include <yaz/log.h>
9 #include <yaz++/z-server.h>
10
11 Yaz_Z_Server::Yaz_Z_Server(IYaz_PDU_Observable *the_PDU_Observable)
12     : Yaz_Z_Assoc(the_PDU_Observable)
13 {
14     m_facilities = 0;
15 }
16
17 Yaz_Z_Server::~Yaz_Z_Server()
18 {
19     facility_reset();
20 }
21
22 void Yaz_Z_Server::facility_reset ()
23 {
24     Yaz_Z_Server_Facility_Info *p = m_facilities;
25     while (p)
26     {
27         Yaz_Z_Server_Facility_Info *p_next = p->m_next;
28
29         delete [] p->m_name;
30         delete p;
31         p = p_next;
32     }
33     m_facilities = 0;
34 }
35
36 void Yaz_Z_Server::facility_add(IYaz_Server_Facility *facility,
37                                 const char *name)
38 {
39     Yaz_Z_Server_Facility_Info **p = &m_facilities;
40     while (*p)
41         p = &(*p)->m_next;
42
43     *p = new Yaz_Z_Server_Facility_Info;
44
45     (*p)->m_next = 0;
46     (*p)->m_name = new char [strlen(name)+1];
47     strcpy ((*p)->m_name, name);
48     (*p)->m_facility = facility;
49 }
50
51 void Yaz_Z_Server::recv_Z_PDU (Z_APDU *apdu_request)
52 {   
53     Yaz_Z_Server_Facility_Info *f = m_facilities;
54     
55     if (apdu_request->which == Z_APDU_initRequest)
56     {
57         Z_APDU *apdu_response = create_Z_PDU(Z_APDU_initResponse);
58
59         Z_InitRequest *req = apdu_request->u.initRequest;
60         Z_InitResponse *resp = apdu_response->u.initResponse;
61         
62         if (ODR_MASK_GET(req->protocolVersion, Z_ProtocolVersion_1))
63         {
64             ODR_MASK_SET(resp->protocolVersion, Z_ProtocolVersion_1);
65         }
66         if (ODR_MASK_GET(req->protocolVersion, Z_ProtocolVersion_2))
67         {
68             ODR_MASK_SET(resp->protocolVersion, Z_ProtocolVersion_2);
69         }
70         if (ODR_MASK_GET(req->protocolVersion, Z_ProtocolVersion_3))
71         {
72             ODR_MASK_SET(resp->protocolVersion, Z_ProtocolVersion_3);
73         }
74         while (f)
75         {
76             f->m_facility->init(this, req, resp);
77             f = f->m_next;
78         }
79         transfer_referenceId(apdu_request, apdu_response);
80         send_Z_PDU(apdu_response);
81     }
82     else
83     {
84         f = m_facilities;
85         int taken = 0;
86         while (f)
87         {
88             taken = f->m_facility->recv(this, apdu_request);
89             if (taken)
90                 break;
91             f = f->m_next;
92         }
93         if (!taken)
94         {
95             yaz_log (LOG_WARN, "unhandled request = %d", apdu_request->which);
96             delete this;
97         }
98     }
99 }
100
101 /*
102  * database record.
103  */
104 void Yaz_Z_ServerUtility::create_databaseRecord (
105     ODR odr, Z_NamePlusRecord *rec, const char *dbname, int format,
106     const void *buf, int len)
107 {
108     rec->databaseName = dbname ? odr_strdup (odr, dbname) : 0;
109     rec->which = Z_NamePlusRecord_databaseRecord;
110     rec->u.databaseRecord = z_ext_record (odr, format,
111                                           (const char *) buf, len);
112 }
113
114 /*
115  * surrogate diagnostic.
116  */
117 void Yaz_Z_ServerUtility::create_surrogateDiagnostics(
118     ODR odr, Z_NamePlusRecord *rec, const char *dbname,
119     int error, char *const addinfo)
120 {
121     int *err = (int *)odr_malloc (odr, sizeof(*err));
122     Z_DiagRec *drec = (Z_DiagRec *)odr_malloc (odr, sizeof(*drec));
123     Z_DefaultDiagFormat *dr = (Z_DefaultDiagFormat *)
124         odr_malloc (odr, sizeof(*dr));
125     
126     yaz_log(LOG_DEBUG, "SurrogateDiagnotic: %d -- %s", error, addinfo);
127     *err = error;
128     rec->databaseName = dbname ? odr_strdup (odr, dbname) : 0;
129     rec->which = Z_NamePlusRecord_surrogateDiagnostic;
130     rec->u.surrogateDiagnostic = drec;
131     drec->which = Z_DiagRec_defaultFormat;
132     drec->u.defaultFormat = dr;
133     dr->diagnosticSetId =
134         yaz_oidval_to_z3950oid (odr, CLASS_DIAGSET, VAL_BIB1);
135
136     dr->condition = err;
137     dr->which = Z_DefaultDiagFormat_v2Addinfo;
138     dr->u.v2Addinfo = odr_strdup (odr, addinfo ? addinfo : "");
139 }
140
141 Z_Records *Yaz_Z_ServerUtility::create_nonSurrogateDiagnostics (
142     ODR odr, int error, const char *addinfo)
143 {
144     Z_Records *rec = (Z_Records *)
145         odr_malloc (odr, sizeof(*rec));
146     int *err = (int *)
147         odr_malloc (odr, sizeof(*err));
148     Z_DiagRec *drec = (Z_DiagRec *)
149         odr_malloc (odr, sizeof(*drec));
150     Z_DefaultDiagFormat *dr = (Z_DefaultDiagFormat *)
151         odr_malloc (odr, sizeof(*dr));
152
153     *err = error;
154     rec->which = Z_Records_NSD;
155     rec->u.nonSurrogateDiagnostic = dr;
156     dr->diagnosticSetId =
157         yaz_oidval_to_z3950oid (odr, CLASS_DIAGSET, VAL_BIB1);
158
159     dr->condition = err;
160     dr->which = Z_DefaultDiagFormat_v2Addinfo;
161     dr->u.v2Addinfo = odr_strdup (odr, addinfo ? addinfo : "");
162     return rec;
163 }
164
165 void Yaz_Z_ServerUtility::create_diagnostics (
166     ODR odr, int error, const char *addinfo,
167     Z_DiagRec ***dreca, int *num)
168 {
169     Z_DiagRec *drec = (Z_DiagRec *) odr_malloc (odr, sizeof(*drec));
170     Z_DefaultDiagFormat *dr = (Z_DefaultDiagFormat *)
171         odr_malloc (odr, sizeof(*dr));
172     
173     *num = 1;
174     *dreca = (Z_DiagRec **) odr_malloc (odr, sizeof(*dreca));
175     (*dreca)[0] = drec;
176         
177     drec->which = Z_DiagRec_defaultFormat;
178     drec->u.defaultFormat = dr;
179     dr->diagnosticSetId =
180         yaz_oidval_to_z3950oid (odr, CLASS_DIAGSET, VAL_BIB1);
181     dr->condition = odr_intdup (odr, error);
182     dr->which = Z_DefaultDiagFormat_v2Addinfo;
183     dr->u.v2Addinfo = odr_strdup (odr, addinfo ? addinfo : "");
184 }