Implemented ZOOM_connection_peek_event.
[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.43 2007-01-10 13:25:46 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,
76                               int portnum);
77
78 /* destroy connection (close connection also) */
79 ZOOM_API(void)
80 ZOOM_connection_destroy (ZOOM_connection c);
81
82 /* get/set option for connection */
83 ZOOM_API(const char *)
84 ZOOM_connection_option_get (ZOOM_connection c, const char *key);
85
86 ZOOM_API(const char *)
87 ZOOM_connection_option_getl (ZOOM_connection c, const char *key, int *lenp);
88
89 ZOOM_API(void)
90 ZOOM_connection_option_set (ZOOM_connection c, const char *key,
91                             const char *val);
92
93 ZOOM_API(void)
94 ZOOM_connection_option_setl (ZOOM_connection c, const char *key,
95                              const char *val, int len);
96
97 /* return error code (0 == success, failure otherwise). cp
98    holds error string on failure, addinfo holds addititional info (if any)
99 */
100 ZOOM_API(int)
101 ZOOM_connection_error (ZOOM_connection c, const char **cp,
102                             const char **addinfo);
103
104 ZOOM_API(int)
105 ZOOM_connection_error_x (ZOOM_connection c, const char **cp,
106                          const char **addinfo, const char **diagset);
107
108 /* returns error code */
109 ZOOM_API(int)
110 ZOOM_connection_errcode (ZOOM_connection c);
111 /* returns error message */
112 ZOOM_API(const char *)
113 ZOOM_connection_errmsg (ZOOM_connection c);
114 /* returns additional info */
115 ZOOM_API(const char *)
116 ZOOM_connection_addinfo (ZOOM_connection c);
117 /* returns diagnostic set */
118 ZOOM_API(const char *)
119 ZOOM_connection_diagset (ZOOM_connection c);
120 /* translates error code into human-readable string */
121 ZOOM_API(const char *)
122 ZOOM_diag_str (int error);
123
124 #define ZOOM_ERROR_NONE 0
125 #define ZOOM_ERROR_CONNECT 10000
126 #define ZOOM_ERROR_MEMORY  10001
127 #define ZOOM_ERROR_ENCODE  10002
128 #define ZOOM_ERROR_DECODE  10003
129 #define ZOOM_ERROR_CONNECTION_LOST 10004
130 #define ZOOM_ERROR_INIT 10005
131 #define ZOOM_ERROR_INTERNAL 10006
132 #define ZOOM_ERROR_TIMEOUT 10007
133 #define ZOOM_ERROR_UNSUPPORTED_PROTOCOL 10008
134 #define ZOOM_ERROR_UNSUPPORTED_QUERY 10009
135 #define ZOOM_ERROR_INVALID_QUERY 10010
136 #define ZOOM_ERROR_CQL_PARSE 10011
137 #define ZOOM_ERROR_CQL_TRANSFORM 10012
138 #define ZOOM_ERROR_CCL_CONFIG 10013
139 #define ZOOM_ERROR_CCL_PARSE 10014
140
141 ZOOM_API(int)
142 ZOOM_connection_last_event(ZOOM_connection cs);
143
144 #define ZOOM_EVENT_NONE 0
145 #define ZOOM_EVENT_CONNECT 1
146 #define ZOOM_EVENT_SEND_DATA  2
147 #define ZOOM_EVENT_RECV_DATA 3
148 #define ZOOM_EVENT_TIMEOUT 4
149 #define ZOOM_EVENT_UNKNOWN 5
150 #define ZOOM_EVENT_SEND_APDU 6
151 #define ZOOM_EVENT_RECV_APDU 7
152 #define ZOOM_EVENT_RECV_RECORD 8
153 #define ZOOM_EVENT_RECV_SEARCH 9
154 #define ZOOM_EVENT_END 10
155
156 /* ----------------------------------------------------------- */
157 /* result sets */
158
159 /* create result set given a search */
160 ZOOM_API(ZOOM_resultset)
161 ZOOM_connection_search(ZOOM_connection, ZOOM_query q);
162 /* create result set given PQF query */
163 ZOOM_API(ZOOM_resultset)
164 ZOOM_connection_search_pqf(ZOOM_connection c, const char *q);
165
166 /* destroy result set */
167 ZOOM_API(void)
168 ZOOM_resultset_destroy(ZOOM_resultset r);
169
170 /* result set option */
171 ZOOM_API(const char *)
172 ZOOM_resultset_option_get (ZOOM_resultset r, const char *key);
173 ZOOM_API(void)
174 ZOOM_resultset_option_set (ZOOM_resultset r, const char *key, const char *val);
175
176 /* return size of result set (alias hit count AKA result count) */
177 ZOOM_API(size_t)
178 ZOOM_resultset_size (ZOOM_resultset r);
179
180 /* retrieve records */
181 ZOOM_API(void)
182 ZOOM_resultset_records (ZOOM_resultset r, ZOOM_record *recs,
183                         size_t start, size_t count);
184
185 /* return record object at pos. Returns 0 if unavailable */
186 ZOOM_API(ZOOM_record)
187 ZOOM_resultset_record (ZOOM_resultset s, size_t pos);
188
189 /* like ZOOM_resultset_record - but never blocks .. */
190 ZOOM_API(ZOOM_record)
191 ZOOM_resultset_record_immediate (ZOOM_resultset s, size_t pos);
192
193 /* reset record cache for result set */
194 ZOOM_API(void)
195 ZOOM_resultset_cache_reset(ZOOM_resultset r);
196
197 /* ----------------------------------------------------------- */
198 /* records */
199
200 /* get record information, in a form given by type */
201 ZOOM_API(const char *)
202 ZOOM_record_get (ZOOM_record rec, const char *type, int *len);
203
204 /* destroy record */
205 ZOOM_API(void)
206 ZOOM_record_destroy (ZOOM_record rec);
207
208 /* return copy of record */
209 ZOOM_API(ZOOM_record)
210 ZOOM_record_clone (ZOOM_record srec);
211
212 /* return error info (surrogate diagnostic) for record */
213 ZOOM_API(int)
214     ZOOM_record_error(ZOOM_record rec, const char **msg,
215                       const char **addinfo, const char **diagset);
216     
217 /* ----------------------------------------------------------- */
218 /* queries */
219
220 /* create search object */
221 ZOOM_API(ZOOM_query)
222 ZOOM_query_create(void);
223 /* destroy it */
224 ZOOM_API(void)
225 ZOOM_query_destroy(ZOOM_query s);
226 /* CQL */
227 ZOOM_API(int)
228 ZOOM_query_cql(ZOOM_query s, const char *str);
229 /* CQL translated client-side into RPN: `conn' is optional for diagnostics */
230 ZOOM_API(int)
231 ZOOM_query_cql2rpn(ZOOM_query s, const char *str, ZOOM_connection conn);
232 /* CCL translated client-side into RPN: `conn' is optional for diagnostics */
233 ZOOM_API(int)
234 ZOOM_query_ccl2rpn(ZOOM_query s, const char *query_str,
235                    const char *config, 
236                    int *ccl_error, const char **error_string, int *error_pos);
237 /* PQF */
238 ZOOM_API(int)
239 ZOOM_query_prefix(ZOOM_query s, const char *str);
240 /* specify sort criteria for search */
241 ZOOM_API(int)
242 ZOOM_query_sortby(ZOOM_query s, const char *criteria);
243
244 /* ----------------------------------------------------------- */
245 /* scan */
246 ZOOM_API(ZOOM_scanset)
247 ZOOM_connection_scan (ZOOM_connection c, const char *startterm);
248
249 ZOOM_API(ZOOM_scanset)
250 ZOOM_connection_scan1 (ZOOM_connection c, ZOOM_query startterm);
251
252 ZOOM_API(const char *)
253 ZOOM_scanset_term(ZOOM_scanset scan, size_t pos,
254                   int *occ, int *len);
255
256 ZOOM_API(const char *)
257 ZOOM_scanset_display_term(ZOOM_scanset scan, size_t pos,
258                           int *occ, int *len);
259
260 ZOOM_API(size_t)
261 ZOOM_scanset_size(ZOOM_scanset scan);
262
263 ZOOM_API(void)
264 ZOOM_scanset_destroy (ZOOM_scanset scan);
265
266 ZOOM_API(const char *)
267     ZOOM_scanset_option_get (ZOOM_scanset scan, const char *key);
268
269 ZOOM_API(void)
270     ZOOM_scanset_option_set (ZOOM_scanset scan, const char *key,
271                              const char *val);
272 /* ----------------------------------------------------------- */
273 /* Extended Services Packages */
274
275 ZOOM_API(ZOOM_package)
276     ZOOM_connection_package (ZOOM_connection c, ZOOM_options options);
277
278 ZOOM_API(void)
279     ZOOM_package_destroy(ZOOM_package p);
280
281 ZOOM_API(void)
282     ZOOM_package_send (ZOOM_package p, const char *type);
283
284 ZOOM_API(const char *)
285     ZOOM_package_option_get (ZOOM_package p, const char *key);
286
287 ZOOM_API(void)
288     ZOOM_package_option_set (ZOOM_package p, const char *key,
289                              const char *val);
290
291 /* ----------------------------------------------------------- */
292 /* Sort.  First function is deprecated, use second instead */
293 ZOOM_API(void)
294     ZOOM_resultset_sort(ZOOM_resultset r,
295                         const char *sort_type, const char *sort_spec);
296 ZOOM_API(int)
297     ZOOM_resultset_sort1(ZOOM_resultset r,
298                         const char *sort_type, const char *sort_spec);
299
300 /* ----------------------------------------------------------- */
301 /* options */
302
303 ZOOM_API(ZOOM_options_callback)
304     ZOOM_options_set_callback (ZOOM_options opt,
305                                ZOOM_options_callback c,
306                                void *handle);
307 ZOOM_API(ZOOM_options)
308 ZOOM_options_create (void);
309
310 ZOOM_API(ZOOM_options)
311 ZOOM_options_create_with_parent (ZOOM_options parent);
312
313 ZOOM_API(ZOOM_options)
314     ZOOM_options_create_with_parent2 (ZOOM_options parent1,
315                                       ZOOM_options parent2);
316
317 ZOOM_API(const char *)
318 ZOOM_options_get (ZOOM_options opt, const char *name);
319
320 ZOOM_API(const char *)
321 ZOOM_options_getl (ZOOM_options opt, const char *name, int *lenp);
322
323 ZOOM_API(void)
324 ZOOM_options_set (ZOOM_options opt, const char *name, const char *v);
325
326 ZOOM_API(void)
327 ZOOM_options_setl (ZOOM_options opt, const char *name, const char *value,
328                    int len);
329
330 ZOOM_API(void)
331 ZOOM_options_destroy (ZOOM_options opt);
332
333 ZOOM_API(int)
334 ZOOM_options_get_bool (ZOOM_options opt, const char *name, int defa);
335
336 ZOOM_API(int)
337 ZOOM_options_get_int (ZOOM_options opt, const char *name, int defa);
338
339 ZOOM_API(void)
340 ZOOM_options_set_int(ZOOM_options opt, const char *name, int value);
341
342 /** \brief select/poll socket mask: read */
343 #define ZOOM_SELECT_READ 1
344 /** \brief select/poll socket mask: write */
345 #define ZOOM_SELECT_WRITE 2
346 /** \brief select/poll socket mask: except */
347 #define ZOOM_SELECT_EXCEPT 4
348
349 /** \brief wait for events on connection(s) (BLOCKING)
350     \param no number of connections (size of cs)
351     \param cs connection array
352     \retval 0 no event was fired
353     \retval >0 event was fired for connection at (retval-1)
354     
355     blocking poll for events on a number of connections. Returns positive
356     integer if event occurred ; zero if none occurred and no more
357     events are pending. The positive integer specifies the
358     connection for which the event occurred.
359 */
360 ZOOM_API(int)
361 ZOOM_event (int no, ZOOM_connection *cs);
362
363
364 /** \brief determines if connection is idle (no active or pending work)
365     \param c connection
366     \retval 1 is idle
367     \retval 0 is non-idle (active)
368 */
369 ZOOM_API(int)
370 ZOOM_connection_is_idle(ZOOM_connection c);
371
372
373 /** \brief processes one event for one of connections given
374     \param no number of connections (size of cs)
375     \param cs connection array
376     \retval 0 no event was processed
377     \retval >0 event was processed for connection at (retval-1)
378
379     This function attemps to deal with outstandings events in 
380     a non-blocking fashion. If no events was processed (return value of 0),
381     then the system should attempt to deal with sockets in blocking mode
382     using socket select/poll which means calling the following functions:
383     ZOOM_connection_get_socket, ZOOM_connection_get_mask,
384     ZOOM_connection_get_timeout.
385 */
386 ZOOM_API(int)
387     ZOOM_process_event(int no, ZOOM_connection *cs);
388
389
390 /** \brief get socket fd for ZOOM connection
391     \param c connection
392     \retval -1 no socket assigned for connection
393     \retval >=0 socket for connection
394
395     Use this function when preparing for socket/poll and
396     in conjunction with ZOOM_connection_get_mask.
397 */
398 ZOOM_API(int)
399     ZOOM_connection_get_socket(ZOOM_connection c);
400
401
402 /** \brief get socket mask for connection 
403     \param c connection
404     \returns mask for connection (possibly 0)
405
406     Use this function when preparing for socket select/poll and
407     in conjunction with ZOOM_connection_get_socket.
408 */
409 ZOOM_API(int)
410     ZOOM_connection_get_mask(ZOOM_connection c);
411
412
413 /** \brief set socket mask for connection (DO NOT call outside zoom) */
414 ZOOM_API(int)
415     ZOOM_connection_set_mask(ZOOM_connection c, int mask);
416
417
418 /** \brief get timeout in seconds for ZOOM connection
419     \param c connection
420     \returns timeout value in seconds
421
422     Use this function when preparing for socket/poll and
423     in conjunction with ZOOM_connection_get_socket.
424 */
425 ZOOM_API(int)
426     ZOOM_connection_get_timeout(ZOOM_connection c);
427
428
429 /** \brief fire socket event timeout
430     \param c connection
431     \retval 0 event was fired OK
432     \retval -1 event was not fired
433
434     Call this function when a timeout occurs - for example in the
435     case of select(2) returning 0.
436 */
437 ZOOM_API(int)
438     ZOOM_connection_fire_event_timeout(ZOOM_connection c);
439
440
441 /** \brief fire socket event activity (read,write,except)
442     \param c connection
443     \param mask or'ed mask of ZOOM_SELECT_.. values
444     \retval 0 event was fired OK
445     \retval -1 event was not fired
446 */
447 ZOOM_API(int)
448     ZOOM_connection_fire_event_socket(ZOOM_connection c, int mask);
449
450
451
452 /** \brief peek at next event
453     \param c connection
454     \returns ZOOM_EVENT_NONE (for no events in queue), ZOOM_EVENT_CONNECT, ..
455
456     Does not actually remove the event from the event queue. ZOOM_event and
457     ZOOM_process_event removes one event.
458 */
459
460 ZOOM_API(int)
461     ZOOM_connection_peek_event(ZOOM_connection c);
462
463 ZOOM_END_CDECL
464 /*
465  * Local variables:
466  * c-basic-offset: 4
467  * indent-tabs-mode: nil
468  * End:
469  * vim: shiftwidth=4 tabstop=8 expandtab
470  */
471