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