2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.6 1995-03-21 12:30:09 quinn
8 * Beginning to add support for record packing.
10 * Revision 1.5 1995/03/17 10:44:13 quinn
11 * Added catch of null-string in makediagrec
13 * Revision 1.4 1995/03/17 10:18:08 quinn
14 * Added memory management.
16 * Revision 1.3 1995/03/16 17:42:39 quinn
19 * Revision 1.2 1995/03/16 13:29:01 quinn
22 * Revision 1.1 1995/03/15 16:02:10 quinn
23 * Modded session.c seshigh.c
25 * Revision 1.10 1995/03/15 15:18:51 quinn
26 * Little changes to better support nonblocking I/O
29 * Revision 1.9 1995/03/15 13:20:23 adam
30 * Yet another bug fix in very dummy_database...
32 * Revision 1.8 1995/03/15 11:18:17 quinn
35 * Revision 1.7 1995/03/15 09:40:15 adam
36 * Bug fixes in dummy_database_...
38 * Revision 1.6 1995/03/15 09:08:30 adam
39 * Take care of preferredMessageSize.
41 * Revision 1.5 1995/03/15 08:37:44 quinn
42 * Now we're pretty much set for nonblocking I/O.
44 * Revision 1.4 1995/03/15 08:27:20 adam
45 * PresentRequest changed to return MARC records from file 'dummy-records'.
47 * Revision 1.3 1995/03/14 16:59:48 quinn
50 * Revision 1.2 1995/03/14 11:30:14 quinn
53 * Revision 1.1 1995/03/14 10:28:01 quinn
54 * More work on demo server.
71 #define ENCODE_BUFFER_SIZE 10000
73 static int process_apdu(IOCHAN chan);
74 static int process_initRequest(IOCHAN client, Z_InitRequest *req);
75 static int process_searchRequest(IOCHAN client, Z_SearchRequest *req);
76 static int process_presentRequest(IOCHAN client, Z_PresentRequest *req);
78 association *create_association(IOCHAN channel, COMSTACK link)
82 if (!(new = malloc(sizeof(*new))))
84 new->client_chan = channel;
85 new->client_link = link;
86 if (!(new->decode = odr_createmem(ODR_DECODE)) ||
87 !(new->encode = odr_createmem(ODR_ENCODE)))
89 if (!(new->encode_buffer = malloc(ENCODE_BUFFER_SIZE)))
91 odr_setbuf(new->encode, new->encode_buffer, ENCODE_BUFFER_SIZE);
92 new->state = ASSOC_UNINIT;
93 new->input_buffer = 0;
94 new->input_buffer_len = 0;
98 void destroy_association(association *h)
100 odr_destroy(h->decode);
101 odr_destroy(h->encode);
102 free(h->encode_buffer);
104 free(h->input_buffer);
108 void ir_session(IOCHAN h, int event)
111 association *assoc = iochan_getdata(h);
112 COMSTACK conn = assoc->client_link;
114 if (event == EVENT_INPUT)
116 assert(assoc && conn);
117 res = cs_get(conn, &assoc->input_buffer, &assoc->input_buffer_len);
120 case 0: case -1: /* connection closed by peer */
121 fprintf(stderr, "Closed connection\n");
123 destroy_association(assoc);
126 case 1: /* incomplete read */
129 assoc->input_apdu_len = res;
130 if (process_apdu(h) < 0)
132 fprintf(stderr, "Operation failed\n");
134 destroy_association(assoc);
137 else if (cs_more(conn)) /* arrange to be called again */
138 iochan_setevent(h, EVENT_INPUT);
141 else if (event == EVENT_OUTPUT)
143 switch (res = cs_put(conn, assoc->encode_buffer, assoc->encoded_len))
146 fprintf(stderr, "Closed connection\n");
148 destroy_association(assoc);
150 case 0: /* all sent */
151 iochan_setflags(h, EVENT_INPUT | EVENT_EXCEPT); /* reset */
153 case 1: /* partial send */
154 break; /* we'll get called again */
157 else if (event == EVENT_EXCEPT)
159 fprintf(stderr, "Exception on line\n");
161 destroy_association(assoc);
166 static int process_apdu(IOCHAN chan)
170 association *assoc = iochan_getdata(chan);
172 odr_setbuf(assoc->decode, assoc->input_buffer, assoc->input_apdu_len);
173 if (!z_APDU(assoc->decode, &apdu, 0))
175 odr_perror(assoc->decode, "Incoming APDU");
180 case Z_APDU_initRequest:
181 res = process_initRequest(chan, apdu->u.initRequest); break;
182 case Z_APDU_searchRequest:
183 res = process_searchRequest(chan, apdu->u.searchRequest); break;
184 case Z_APDU_presentRequest:
185 res = process_presentRequest(chan, apdu->u.presentRequest); break;
187 fprintf(stderr, "Bad APDU\n");
190 odr_reset(assoc->decode);
194 static int process_initRequest(IOCHAN client, Z_InitRequest *req)
199 association *assoc = iochan_getdata(client);
200 bend_initrequest binitreq;
201 bend_initresult *binitres;
203 fprintf(stderr, "Got initRequest.\n");
204 if (req->implementationId)
205 fprintf(stderr, "Id: %s\n", req->implementationId);
206 if (req->implementationName)
207 fprintf(stderr, "Name: %s\n", req->implementationName);
208 if (req->implementationVersion)
209 fprintf(stderr, "Version: %s\n", req->implementationVersion);
211 binitreq.configname = "default-config";
212 if (!(binitres = bend_init(&binitreq)) || binitres->errcode)
214 fprintf(stderr, "Bad response from backend\n");
219 apdu.which = Z_APDU_initResponse;
220 apdu.u.initResponse = &resp;
221 resp.referenceId = req->referenceId;
222 resp.options = req->options; /* should check these */
223 resp.protocolVersion = req->protocolVersion;
224 assoc->maximumRecordSize = *req->maximumRecordSize;
225 if (assoc->maximumRecordSize > ENCODE_BUFFER_SIZE - 500)
226 assoc->maximumRecordSize = ENCODE_BUFFER_SIZE - 500;
227 assoc->preferredMessageSize = *req->preferredMessageSize;
228 if (assoc->preferredMessageSize > assoc->maximumRecordSize)
229 assoc->preferredMessageSize = assoc->maximumRecordSize;
230 resp.preferredMessageSize = &assoc->preferredMessageSize;
231 resp.maximumRecordSize = &assoc->maximumRecordSize;
232 resp.result = &result;
233 resp.implementationId = "YAZ";
234 resp.implementationName = "YAZ/Simple asynchronous test server";
235 resp.implementationVersion = "$Revision: 1.6 $";
236 resp.userInformationField = 0;
237 if (!z_APDU(assoc->encode, &apdup, 0))
239 odr_perror(assoc->encode, "Encode initres");
242 odr_getbuf(assoc->encode, &assoc->encoded_len);
243 odr_reset(assoc->encode);
244 iochan_setflags(client, EVENT_OUTPUT | EVENT_EXCEPT);
248 static Z_Records *diagrec(int error, char *addinfo)
250 static Z_Records rec;
251 static Odr_oid bib1[] = { 1, 2, 3, 4, 5, -1 };
255 fprintf(stderr, "Diagnostic: %d -- %s\n", error, addinfo);
257 rec.which = Z_Records_NSD;
258 rec.u.nonSurrogateDiagnostic = &dr;
259 dr.diagnosticSetId = bib1;
261 dr.addinfo = addinfo ? addinfo : "";
265 static Z_NamePlusRecord *surrogatediagrec(char *dbname, int error,
268 static Z_NamePlusRecord rec;
270 static Odr_oid bib1[] = { 1, 2, 3, 4, 5, -1 };
273 fprintf(stderr, "SurrogateDiagnotic: %d -- %s\n", error, addinfo);
274 rec.databaseName = dbname;
275 rec.which = Z_NamePlusRecord_surrogateDiagnostic;
276 rec.u.surrogateDiagnostic = &dr;
277 dr.diagnosticSetId = bib1;
279 dr.addinfo = addinfo ? addinfo : "";
283 static Z_Records *pack_records(association *a, int num, Z_ElementSetNames *esn)
287 static int process_searchRequest(IOCHAN client, Z_SearchRequest *req)
290 Z_SearchResponse resp;
291 association *assoc = iochan_getdata(client);
295 bend_searchrequest bsrq;
296 bend_searchresult *bsrt;
298 fprintf(stderr, "Got SearchRequest.\n");
300 apdu.which = Z_APDU_searchResponse;
301 apdu.u.searchResponse = &resp;
302 resp.referenceId = req->referenceId;
304 bsrq.setname = req->resultSetName;
305 bsrq.replace_set = *req->replaceIndicator;
306 bsrq.num_bases = req->num_databaseNames;
307 bsrq.basenames = req->databaseNames;
308 bsrq.query = req->query;
310 if (!(bsrt = bend_search(&bsrq)))
312 else if (bsrt->errcode)
313 resp.records = diagrec(bsrt->errcode, bsrt->errstring);
317 resp.resultCount = &bsrt->hits;
318 resp.numberOfRecordsReturned = &nulint;
319 nrp = bsrt->hits ? 1 : 0;
320 resp.nextResultSetPosition = &nrp;
321 resp.searchStatus = &sr;
322 resp.resultSetStatus = &sr;
323 resp.presentStatus = 0;
325 if (!z_APDU(assoc->encode, &apdup, 0))
327 odr_perror(assoc->encode, "Encode searchres");
330 odr_getbuf(assoc->encode, &assoc->encoded_len);
331 odr_reset(assoc->encode);
332 iochan_setflags(client, EVENT_OUTPUT | EVENT_EXCEPT);
336 static int process_presentRequest(IOCHAN client, Z_PresentRequest *req)
339 Z_PresentResponse resp;
340 association *assoc = iochan_getdata(client);
343 fprintf(stderr, "Got PresentRequest.\n");
345 apdu.which = Z_APDU_presentResponse;
346 apdu.u.presentResponse = &resp;
347 resp.referenceId = req->referenceId;
349 resp.numberOfRecordsReturned = &nrr;
350 resp.nextResultSetPosition = &nrr;
351 resp.presentStatus = &nrr;
353 resp.records = diagrec(1, "No records yet.");
355 if (!z_APDU(assoc->encode, &apdup, 0))
357 odr_perror(assoc->encode, "Encode presentres");
360 odr_getbuf(assoc->encode, &assoc->encoded_len);
361 odr_reset(assoc->encode);
362 iochan_setflags(client, EVENT_OUTPUT | EVENT_EXCEPT);