API changes to ZOOM. Functions for setting options returns int, rather
[yaz-moved-to-github.git] / include / yaz / zoom.h
1 /*
2  * Copyright (c) 1995-2007, Index Data
3  * All rights reserved.
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  *     * Redistributions of source code must retain the above copyright
8  *       notice, this list of conditions and the following disclaimer.
9  *     * Redistributions in binary form must reproduce the above copyright
10  *       notice, this list of conditions and the following disclaimer in the
11  *       documentation and/or other materials provided with the distribution.
12  *     * Neither the name of Index Data nor the names of its contributors
13  *       may be used to endorse or promote products derived from this
14  *       software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 /* $Id: zoom.h,v 1.45 2007-03-19 20:58:34 adam Exp $ */
28
29 /**
30  * \file zoom.h
31  * \brief Header for ZOOM
32  */
33
34 #include <stdlib.h>
35 #include <yaz/yconfig.h>
36
37 #define ZOOM_BEGIN_CDECL YAZ_BEGIN_CDECL
38 #define ZOOM_END_CDECL YAZ_END_CDECL
39
40 #ifdef WIN32
41 #  define ZOOM_API(x) __declspec(dllexport) x __stdcall
42 #else
43 #  define ZOOM_API(x) x
44 # endif
45
46 ZOOM_BEGIN_CDECL
47
48 /* ----------------------------------------------------------- */
49 /* the types we use */
50
51 typedef struct ZOOM_options_p *ZOOM_options;
52 typedef struct ZOOM_query_p *ZOOM_query;
53 typedef struct ZOOM_connection_p *ZOOM_connection;
54 typedef struct ZOOM_resultset_p *ZOOM_resultset;
55 typedef struct ZOOM_record_p *ZOOM_record;
56 typedef struct ZOOM_scanset_p *ZOOM_scanset;
57 typedef struct ZOOM_package_p *ZOOM_package;
58
59 typedef const char *(*ZOOM_options_callback)(void *handle, const char *name);
60
61 /* ----------------------------------------------------------- */
62 /* connections */
63
64 /* create connection, connect to host, if portnum is 0, then port is
65 read from host string (e.g. myhost:9821) */
66 ZOOM_API(ZOOM_connection)
67     ZOOM_connection_new(const char *host, int portnum);
68     
69 /* create connection, don't connect, apply options */
70 ZOOM_API(ZOOM_connection)
71     ZOOM_connection_create(ZOOM_options options);
72
73 /* connect given existing connection */
74 ZOOM_API(void)
75     ZOOM_connection_connect(ZOOM_connection c, const char *host, int portnum);
76
77 /* destroy connection (close connection also) */
78 ZOOM_API(void)
79     ZOOM_connection_destroy(ZOOM_connection c);
80
81 /* get/set option for connection */
82 ZOOM_API(const char *)
83     ZOOM_connection_option_get(ZOOM_connection c, const char *key);
84
85 ZOOM_API(const char *)
86     ZOOM_connection_option_getl(ZOOM_connection c, const char *key, int *lenp);
87
88 ZOOM_API(int)
89     ZOOM_connection_option_set(ZOOM_connection c, const char *key,
90                            const char *val);
91
92 ZOOM_API(int)
93     ZOOM_connection_option_setl(ZOOM_connection c, const char *key,
94                                 const char *val, int len);
95
96 /* return error code (0 == success, failure otherwise). cp
97    holds error string on failure, addinfo holds addititional info (if any)
98 */
99 ZOOM_API(int)
100     ZOOM_connection_error(ZOOM_connection c, const char **cp,
101                           const char **addinfo);
102
103 ZOOM_API(int)
104     ZOOM_connection_error_x(ZOOM_connection c, const char **cp,
105                             const char **addinfo, const char **diagset);
106
107 /* returns error code */
108 ZOOM_API(int)
109     ZOOM_connection_errcode(ZOOM_connection c);
110 /* returns error message */
111 ZOOM_API(const char *)
112     ZOOM_connection_errmsg(ZOOM_connection c);
113 /* returns additional info */
114 ZOOM_API(const char *)
115     ZOOM_connection_addinfo(ZOOM_connection c);
116 /* returns diagnostic set */
117 ZOOM_API(const char *)
118     ZOOM_connection_diagset(ZOOM_connection c);
119 /* translates error code into human-readable string */
120 ZOOM_API(const char *)
121     ZOOM_diag_str(int error);
122     
123 #define ZOOM_ERROR_NONE 0
124 #define ZOOM_ERROR_CONNECT 10000
125 #define ZOOM_ERROR_MEMORY  10001
126 #define ZOOM_ERROR_ENCODE  10002
127 #define ZOOM_ERROR_DECODE  10003
128 #define ZOOM_ERROR_CONNECTION_LOST 10004
129 #define ZOOM_ERROR_INIT 10005
130 #define ZOOM_ERROR_INTERNAL 10006
131 #define ZOOM_ERROR_TIMEOUT 10007
132 #define ZOOM_ERROR_UNSUPPORTED_PROTOCOL 10008
133 #define ZOOM_ERROR_UNSUPPORTED_QUERY 10009
134 #define ZOOM_ERROR_INVALID_QUERY 10010
135 #define ZOOM_ERROR_CQL_PARSE 10011
136 #define ZOOM_ERROR_CQL_TRANSFORM 10012
137 #define ZOOM_ERROR_CCL_CONFIG 10013
138 #define ZOOM_ERROR_CCL_PARSE 10014
139
140 ZOOM_API(int)
141     ZOOM_connection_last_event(ZOOM_connection cs);
142
143 #define ZOOM_EVENT_NONE 0
144 #define ZOOM_EVENT_CONNECT 1
145 #define ZOOM_EVENT_SEND_DATA  2
146 #define ZOOM_EVENT_RECV_DATA 3
147 #define ZOOM_EVENT_TIMEOUT 4
148 #define ZOOM_EVENT_UNKNOWN 5
149 #define ZOOM_EVENT_SEND_APDU 6
150 #define ZOOM_EVENT_RECV_APDU 7
151 #define ZOOM_EVENT_RECV_RECORD 8
152 #define ZOOM_EVENT_RECV_SEARCH 9
153 #define ZOOM_EVENT_END 10
154
155 /* ----------------------------------------------------------- */
156 /* result sets */
157
158 /* create result set given a search */
159 ZOOM_API(ZOOM_resultset)
160     ZOOM_connection_search(ZOOM_connection, ZOOM_query q);
161 /* create result set given PQF query */
162 ZOOM_API(ZOOM_resultset)
163     ZOOM_connection_search_pqf(ZOOM_connection c, const char *q);
164     
165 /* destroy result set */
166 ZOOM_API(void)
167     ZOOM_resultset_destroy(ZOOM_resultset r);
168     
169 /* result set option */
170 ZOOM_API(const char *)
171     ZOOM_resultset_option_get(ZOOM_resultset r, const char *key);
172     
173 /* get option from result set */
174 ZOOM_API(int)
175     ZOOM_resultset_option_set(ZOOM_resultset r, const char *key,
176                               const char *val);
177     
178 /* return size of result set (alias hit count AKA result count) */
179 ZOOM_API(size_t)
180     ZOOM_resultset_size(ZOOM_resultset r);
181     
182 /* retrieve records */
183 ZOOM_API(void)
184     ZOOM_resultset_records(ZOOM_resultset r, ZOOM_record *recs,
185                            size_t start, size_t count);
186     
187 /* return record object at pos. Returns 0 if unavailable */
188 ZOOM_API(ZOOM_record)
189     ZOOM_resultset_record(ZOOM_resultset s, size_t pos);
190     
191 /* like ZOOM_resultset_record - but never blocks .. */
192 ZOOM_API(ZOOM_record)
193     ZOOM_resultset_record_immediate (ZOOM_resultset s, size_t pos);
194     
195 /* reset record cache for result set */
196 ZOOM_API(void)
197     ZOOM_resultset_cache_reset(ZOOM_resultset r);
198     
199 /* ----------------------------------------------------------- */
200 /* records */
201
202 /* get record information, in a form given by type */
203 ZOOM_API(const char *)
204     ZOOM_record_get(ZOOM_record rec, const char *type, int *len);
205     
206 /* destroy record */
207 ZOOM_API(void)
208     ZOOM_record_destroy(ZOOM_record rec);
209
210 /* return copy of record */
211 ZOOM_API(ZOOM_record)
212     ZOOM_record_clone(ZOOM_record srec);
213     
214 /* return error info (surrogate diagnostic) for record */
215 ZOOM_API(int)
216     ZOOM_record_error(ZOOM_record rec, const char **msg,
217                       const char **addinfo, const char **diagset);
218     
219 /* ----------------------------------------------------------- */
220 /* queries */
221
222 /* create search object */
223 ZOOM_API(ZOOM_query)
224     ZOOM_query_create(void);
225 /* destroy it */
226 ZOOM_API(void)
227     ZOOM_query_destroy(ZOOM_query s);
228 /* CQL */
229 ZOOM_API(int)
230     ZOOM_query_cql(ZOOM_query s, const char *str);
231 /* CQL translated client-side into RPN: `conn' is optional for diagnostics */
232 ZOOM_API(int)
233     ZOOM_query_cql2rpn(ZOOM_query s, const char *str, ZOOM_connection conn);
234 /* CCL translated client-side into RPN: `conn' is optional for diagnostics */
235 ZOOM_API(int)
236     ZOOM_query_ccl2rpn(ZOOM_query s, const char *query_str,
237                        const char *config, 
238                        int *ccl_error, const char **error_string,
239                        int *error_pos);
240 /* PQF */
241 ZOOM_API(int)
242     ZOOM_query_prefix(ZOOM_query s, const char *str);
243 /* specify sort criteria for search */
244 ZOOM_API(int)
245     ZOOM_query_sortby(ZOOM_query s, const char *criteria);
246     
247 /* ----------------------------------------------------------- */
248 /* scan */
249 ZOOM_API(ZOOM_scanset)
250     ZOOM_connection_scan_pqf(ZOOM_connection c, const char *startterm);
251     
252 /* scan wtih ZOOM_query start term */
253 ZOOM_API(ZOOM_scanset)
254     ZOOM_connection_scan(ZOOM_connection c, ZOOM_query startterm);
255     
256 /* get scan term */
257 ZOOM_API(const char *)
258     ZOOM_scanset_term(ZOOM_scanset scan, size_t pos,
259                       int *occ, int *len);
260     
261 /* get display scan term */
262 ZOOM_API(const char *)
263     ZOOM_scanset_display_term(ZOOM_scanset scan, size_t pos,
264                               int *occ, int *len);
265     
266 /* get size of scan set */
267 ZOOM_API(size_t)
268     ZOOM_scanset_size(ZOOM_scanset scan);
269     
270 /* destory scan set */
271 ZOOM_API(void)
272 ZOOM_scanset_destroy(ZOOM_scanset scan);
273
274 /* get scan set option */
275 ZOOM_API(const char *)
276     ZOOM_scanset_option_get(ZOOM_scanset scan, const char *key);
277
278 /* set scan set option */
279 ZOOM_API(int)
280     ZOOM_scanset_option_set(ZOOM_scanset scan, const char *key,
281                             const char *val);
282 /* ----------------------------------------------------------- */
283 /* Extended Services Packages */
284
285 ZOOM_API(ZOOM_package)
286     ZOOM_connection_package(ZOOM_connection c, ZOOM_options options);
287
288 ZOOM_API(void)
289     ZOOM_package_destroy(ZOOM_package p);
290
291 ZOOM_API(void)
292     ZOOM_package_send(ZOOM_package p, const char *type);
293
294 ZOOM_API(const char *)
295     ZOOM_package_option_get(ZOOM_package p, const char *key);
296
297 ZOOM_API(int)
298     ZOOM_package_option_set(ZOOM_package p, const char *key,
299                             const char *val);
300
301 /* ----------------------------------------------------------- */
302 /* Sort. */
303 ZOOM_API(int)
304     ZOOM_resultset_sort(ZOOM_resultset r,
305                         const char *sort_type, const char *sort_spec);
306
307 /* ----------------------------------------------------------- */
308 /* options */
309
310 ZOOM_API(ZOOM_options_callback)
311     ZOOM_options_set_callback(ZOOM_options opt,
312                                ZOOM_options_callback c,
313                                void *handle);
314 ZOOM_API(ZOOM_options)
315     ZOOM_options_create(void);
316
317 ZOOM_API(ZOOM_options)
318     ZOOM_options_create_with_parent(ZOOM_options parent);
319
320 ZOOM_API(ZOOM_options)
321     ZOOM_options_create_with_parent2(ZOOM_options parent1,
322                                      ZOOM_options parent2);
323
324 ZOOM_API(const char *)
325     ZOOM_options_get(ZOOM_options opt, const char *name);
326
327 ZOOM_API(const char *)
328     ZOOM_options_getl(ZOOM_options opt, const char *name, int *lenp);
329
330 ZOOM_API(void)
331     ZOOM_options_set(ZOOM_options opt, const char *name, const char *v);
332
333 ZOOM_API(void)
334     ZOOM_options_setl(ZOOM_options opt, const char *name, const char *value,
335                       int len);
336
337 ZOOM_API(void)
338     ZOOM_options_destroy(ZOOM_options opt);
339
340 ZOOM_API(int)
341     ZOOM_options_get_bool(ZOOM_options opt, const char *name, int defa);
342
343 ZOOM_API(int)
344     ZOOM_options_get_int(ZOOM_options opt, const char *name, int defa);
345
346 ZOOM_API(void)
347     ZOOM_options_set_int(ZOOM_options opt, const char *name, int value);
348
349 /** \brief select/poll socket mask: read */
350 #define ZOOM_SELECT_READ 1
351 /** \brief select/poll socket mask: write */
352 #define ZOOM_SELECT_WRITE 2
353 /** \brief select/poll socket mask: except */
354 #define ZOOM_SELECT_EXCEPT 4
355
356 /** \brief wait for events on connection(s) (BLOCKING)
357     \param no number of connections (size of cs)
358     \param cs connection array
359     \retval 0 no event was fired
360     \retval >0 event was fired for connection at (retval-1)
361     
362     blocking poll for events on a number of connections. Returns positive
363     integer if event occurred ; zero if none occurred and no more
364     events are pending. The positive integer specifies the
365     connection for which the event occurred.
366 */
367 ZOOM_API(int)
368     ZOOM_event(int no, ZOOM_connection *cs);
369     
370
371 /** \brief determines if connection is idle (no active or pending work)
372     \param c connection
373     \retval 1 is idle
374     \retval 0 is non-idle (active)
375 */
376 ZOOM_API(int)
377     ZOOM_connection_is_idle(ZOOM_connection c);
378
379
380 /** \brief process one event for one of connections given
381     \param no number of connections (size of cs)
382     \param cs connection array
383     \retval 0 no event was processed
384     \retval >0 event was processed for connection at (retval-1)
385
386     This function attemps to deal with outstandings events in a non-blocking
387     mode. If no events was processed (return value of 0),  then the system
388     should attempt to deal with sockets in blocking mode using socket
389     select/poll which means calling the following functions:
390     ZOOM_connection_get_socket, ZOOM_connection_get_mask,
391     ZOOM_connection_get_timeout.
392 */
393 ZOOM_API(int)
394     ZOOM_event_nonblock(int no, ZOOM_connection *cs);
395
396
397 /** \brief process one event for connection
398     \param c connection
399     \retval 0 no event was processed
400     \retval 1 event was processed for connection
401
402     This function attemps to deal with outstandings events in 
403     a non-blocking fashion. If no event was processed (return value of 0),
404     then the system should attempt to deal with sockets in blocking mode
405     using socket select/poll which means calling the following functions:
406     ZOOM_connection_get_socket, ZOOM_connection_get_mask,
407     ZOOM_connection_get_timeout. If an event was processed call this
408     function again.
409 */
410 ZOOM_API(int)
411     ZOOM_connection_process(ZOOM_connection c);
412
413
414 /** \brief get socket fd for ZOOM connection
415     \param c connection
416     \retval -1 no socket assigned for connection
417     \retval >=0 socket for connection
418
419     Use this function when preparing for socket/poll and
420     in conjunction with ZOOM_connection_get_mask.
421 */
422 ZOOM_API(int)
423     ZOOM_connection_get_socket(ZOOM_connection c);
424
425
426 /** \brief get socket mask for connection 
427     \param c connection
428     \returns mask for connection (possibly 0)
429
430     Use this function when preparing for socket select/poll and
431     in conjunction with ZOOM_connection_get_socket.
432 */
433 ZOOM_API(int)
434     ZOOM_connection_get_mask(ZOOM_connection c);
435
436
437 /** \brief set socket mask for connection (DO NOT call outside zoom) */
438 ZOOM_API(int)
439     ZOOM_connection_set_mask(ZOOM_connection c, int mask);
440
441
442 /** \brief get timeout in seconds for ZOOM connection
443     \param c connection
444     \returns timeout value in seconds
445
446     Use this function when preparing for socket/poll and
447     in conjunction with ZOOM_connection_get_socket.
448 */
449 ZOOM_API(int)
450     ZOOM_connection_get_timeout(ZOOM_connection c);
451
452
453 /** \brief fire socket event timeout
454     \param c connection
455     \retval 0 event was fired OK
456     \retval -1 event was not fired
457
458     Call this function when a timeout occurs - for example in the
459     case of select(2) returning 0.
460 */
461 ZOOM_API(int)
462     ZOOM_connection_fire_event_timeout(ZOOM_connection c);
463
464
465 /** \brief fire socket event activity (read,write,except)
466     \param c connection
467     \param mask or'ed mask of ZOOM_SELECT_.. values
468     \retval 0 event was fired OK
469     \retval -1 event was not fired
470 */
471 ZOOM_API(int)
472     ZOOM_connection_fire_event_socket(ZOOM_connection c, int mask);
473
474
475
476 /** \brief peek at next event
477     \param c connection
478     \returns ZOOM_EVENT_NONE (for no events in queue), ZOOM_EVENT_CONNECT, ..
479
480     Does not actually remove the event from the event queue. ZOOM_event and
481     ZOOM_process_event removes one event.
482 */
483
484 ZOOM_API(int)
485     ZOOM_connection_peek_event(ZOOM_connection c);
486
487 ZOOM_END_CDECL
488 /*
489  * Local variables:
490  * c-basic-offset: 4
491  * indent-tabs-mode: nil
492  * End:
493  * vim: shiftwidth=4 tabstop=8 expandtab
494  */
495