b9e4c2f038528f37b6b25f78e1cf3b4253e72286
[yaz-moved-to-github.git] / src / session.h
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2009 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 /**
28  * \file session.h
29  * \brief Internal Header for GFS.
30  */
31 #ifndef SESSION_H
32 #define SESSION_H
33
34 #include <yaz/comstack.h>
35 #include <yaz/cql.h>
36 #include <yaz/ccl.h>
37 #include <yaz/odr.h>
38 #include <yaz/proto.h>
39 #include <yaz/backend.h>
40 #include <yaz/retrieval.h>
41 #include "eventl.h"
42
43 struct gfs_server {
44     statserv_options_block cb;
45     char *host;
46     int listen_ref;
47     cql_transform_t cql_transform;
48     CCL_bibset ccl_transform;
49     void *server_node_ptr;
50     char *directory;
51     char *docpath;
52     char *stylesheet;
53     yaz_retrieval_t retrieval;
54     struct gfs_server *next;
55 };
56
57 struct gfs_listen {
58     char *id;
59     char *address;
60     struct gfs_listen *next;
61 };
62
63 typedef enum {
64     REQUEST_IDLE,    /* the request is just sitting in the queue */
65     REQUEST_PENDING  /* operation pending (b'end processing or network I/O*/
66     /* this list will have more elements when acc/res control is added */
67 } request_state;
68
69 typedef struct request
70 {
71     request_state state;
72
73     Z_GDU *gdu_request;     /* Current request */
74     Z_APDU *apdu_request;   /* Current Z39.50 request */
75     NMEM request_mem;    /* memory handle for request */
76
77     int size_response;     /* size of buffer */
78     int len_response;      /* length of encoded data */
79     char *response;        /* encoded data waiting for transmission */
80
81     void *clientData;
82     struct request *next;
83     struct request_q *q; 
84 } request;
85
86 typedef struct request_q
87 {
88     request *head;
89     request *tail;
90     request *list;
91     int num;
92 } request_q;
93
94 /*
95  * association state.
96  */
97 typedef enum
98 {
99     ASSOC_NEW,                /* not initialized yet or HTTP session */
100     ASSOC_UP,                 /* Z39.50 session is UP */
101     ASSOC_DEAD                /* dead. Close if input arrives */
102 } association_state;
103
104 typedef struct association
105 {
106     IOCHAN client_chan;           /* event-loop control */
107     COMSTACK client_link;         /* communication handle */
108     ODR decode;                   /* decoding stream */
109     ODR encode;                   /* encoding stream */
110     ODR print;                    /* printing stream (for -a) */
111     char *encode_buffer;          /* temporary buffer for encoded data */
112     int encoded_len;              /* length of encoded data */
113     char *input_buffer;           /* input buffer (allocated by comstack) */
114     int input_buffer_len;         /* length (size) of buffer */
115     int input_apdu_len;           /* length of current incoming APDU */
116     oid_proto proto;              /* protocol (PROTO_Z3950/PROTO_SR) */
117     void *backend;                /* backend handle */
118     request_q incoming;           /* Q of incoming PDUs */
119     request_q outgoing;           /* Q of outgoing data buffers (enc. PDUs) */
120     association_state state;
121
122     /* session parameters */
123     Odr_int preferredMessageSize;
124     Odr_int maximumRecordSize;
125     int version;                  /* highest version-bit set (2 or 3) */
126
127     unsigned cs_get_mask;
128     unsigned cs_put_mask;
129     unsigned cs_accept_mask;
130
131     struct bend_initrequest *init;
132     statserv_options_block *last_control;
133
134     struct gfs_server *server;
135 } association;
136
137 association *create_association(IOCHAN channel, COMSTACK link,
138                                 const char *apdufile);
139 void destroy_association(association *h);
140 void ir_session(IOCHAN h, int event);
141
142 void request_enq(request_q *q, request *r);
143 request *request_head(request_q *q);
144 request *request_deq(request_q *q);
145 request *request_deq_x(request_q *q, request *r);
146 void request_initq(request_q *q);
147 void request_delq(request_q *q);
148 request *request_get(request_q *q);
149 void request_release(request *r);
150
151 int statserv_must_terminate(void);
152
153 int control_association(association *assoc, const char *host, int force);
154
155 int ir_read(IOCHAN h, int event);
156
157 #endif
158 /*
159  * Local variables:
160  * c-basic-offset: 4
161  * c-file-style: "Stroustrup"
162  * indent-tabs-mode: nil
163  * End:
164  * vim: shiftwidth=4 tabstop=8 expandtab
165  */
166