Handle multiple input PDU packages using GDU class of YAZ++ 1.0.
[yazproxy-moved-to-github.git] / include / yazproxy / proxy.h
1 /* $Id: proxy.h,v 1.21 2005-06-21 18:46:04 adam Exp $
2    Copyright (c) 1998-2005, Index Data.
3
4 This file is part of the yaz-proxy.
5
6 YAZ proxy is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
9 version.
10
11 YAZ proxy is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with YAZ proxy; see the file LICENSE.  If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19 02111-1307, USA.
20  */
21
22 #ifndef YAZ_PROXY_H_INCLUDED
23 #define YAZ_PROXY_H_INCLUDED
24
25 #include <yaz++/socket-observer.h>
26 #include <yaz++/z-assoc.h>
27 #include <yaz++/z-query.h>
28 #include <yaz++/z-databases.h>
29 #include <yaz++/cql2rpn.h>
30 #include <yaz/cql.h>
31 #include <yaz++/gdu.h>
32 #include <yazproxy/bw.h>
33
34 class Yaz_Proxy;
35
36 #define MAX_ZURL_PLEX 10
37
38 #define PROXY_LOG_APDU_CLIENT 1
39 #define PROXY_LOG_APDU_SERVER 2
40 #define PROXY_LOG_REQ_CLIENT 4
41 #define PROXY_LOG_REQ_SERVER 8
42
43 class Yaz_usemarcon;
44 class Yaz_ProxyConfig;
45 class Yaz_ProxyClient;
46 class Yaz_CharsetConverter;
47
48 enum YAZ_Proxy_MARCXML_mode {
49     none,
50     marcxml,
51 };
52
53 class Msg_Thread;
54
55 /// Information Retrieval Proxy Server.
56 class YAZ_EXPORT Yaz_Proxy : public yazpp_1::Z_Assoc {
57     friend class Proxy_Msg;
58  private:
59     char *m_peername;
60     int m_ref_count;
61     char *get_cookie(Z_OtherInformation **otherInfo);
62     char *get_proxy(Z_OtherInformation **otherInfo);
63     void get_charset_and_lang_negotiation(Z_OtherInformation **otherInfo,
64         char **charstes, char **langs, int *selected);
65     Yaz_ProxyClient *get_client(Z_APDU *apdu, const char *cookie,
66                                 const char *proxy_host);
67     void srw_get_client(const char *db, const char **backend_db);
68     Z_APDU *result_set_optimize(Z_APDU *apdu);
69     void releaseClient();    
70     Yaz_ProxyClient *m_client;
71     yazpp_1::IPDU_Observable *m_PDU_Observable;
72     yazpp_1::ISocketObservable *m_socket_observable;
73     Yaz_ProxyClient *m_clientPool;
74     Yaz_Proxy *m_parent;
75     int m_seqno;
76     int m_max_clients;
77     int m_log_mask;
78     int m_keepalive_limit_bw;
79     int m_keepalive_limit_pdu;
80     int m_client_idletime;
81     int m_target_idletime;
82     int m_debug_mode;
83     char *m_proxyTarget;
84     char *m_default_target;
85     char *m_proxy_negotiation_charset;
86     char *m_proxy_negotiation_lang;
87     long m_seed;
88     char *m_optimize;
89     int m_session_no;         // sequence for each client session
90     char m_session_str[30];  // session string (time:session_no)
91     Yaz_ProxyConfig *m_config;
92     char *m_config_fname;
93     int m_bytes_sent;
94     int m_bytes_recv;
95     int m_bw_max;
96
97     yazpp_1::GDU *m_timeout_gdu;
98     enum timeout_mode {
99         timeout_normal,
100         timeout_reduce,
101         timeout_xsl
102     } m_timeout_mode;
103
104     Yaz_bw m_bw_stat;
105     int m_pdu_max;
106     Yaz_bw m_pdu_stat;
107     Z_GDU *m_bw_hold_PDU;
108     int m_max_record_retrieve;
109     void handle_max_record_retrieve(Z_APDU *apdu);
110     void display_diagrecs(Z_DiagRec **pp, int num);
111     Z_Records *create_nonSurrogateDiagnostics(ODR o, int error,
112                                               const char *addinfo);
113
114     Z_APDU *handle_query_validation(Z_APDU *apdu);
115     Z_APDU *handle_query_transformation(Z_APDU *apdu);
116     Z_APDU *handle_target_charset_conversion(Z_APDU *apdu);
117
118     Z_APDU *handle_syntax_validation(Z_APDU *apdu);
119
120     void handle_charset_lang_negotiation(Z_APDU *apdu);
121
122     const char *load_balance(const char **url);
123     int m_reconfig_flag;
124     Yaz_ProxyConfig *check_reconfigure();
125     int m_request_no;
126     int m_flag_invalid_session;
127     YAZ_Proxy_MARCXML_mode m_marcxml_mode;
128     void *m_stylesheet_xsp;  // Really libxslt's xsltStylesheetPtr 
129     int m_stylesheet_offset;
130     Z_APDU *m_stylesheet_apdu;
131     Z_NamePlusRecordList *m_stylesheet_nprl;
132     char *m_schema;
133     char *m_backend_type;
134     char *m_backend_charset;
135     int m_frontend_type;
136     void convert_to_frontend_type(Z_NamePlusRecordList *p);
137     void convert_to_marcxml(Z_NamePlusRecordList *p, const char *charset);
138     void convert_records_charset(Z_NamePlusRecordList *p, const char *charset);
139     int convert_xsl(Z_NamePlusRecordList *p, Z_APDU *apdu);
140     void convert_xsl_delay();
141     Z_APDU *m_initRequest_apdu;
142     int m_initRequest_preferredMessageSize;
143     int m_initRequest_maximumRecordSize;
144     Z_Options *m_initRequest_options;
145     Z_ProtocolVersion *m_initRequest_version;
146     char **m_initRequest_oi_negotiation_charsets;
147     int m_initRequest_oi_negotiation_num_charsets;
148     char **m_initRequest_oi_negotiation_langs;
149     int m_initRequest_oi_negotiation_num_langs;
150     int m_initRequest_oi_negotiation_selected;
151     NMEM m_initRequest_mem;
152     Z_APDU *m_apdu_invalid_session;
153     NMEM m_mem_invalid_session;
154     int send_PDU_convert(Z_APDU *apdu);
155     ODR m_s2z_odr_init;
156     ODR m_s2z_odr_search;
157     int m_s2z_hit_count;
158     int m_s2z_packing;
159     char *m_s2z_database;
160     Z_APDU *m_s2z_init_apdu;
161     Z_APDU *m_s2z_search_apdu;
162     Z_APDU *m_s2z_present_apdu;
163     char *m_s2z_stylesheet;
164     char *m_soap_ns;
165     int file_access(Z_HTTP_Request *hreq);
166     int send_to_srw_client_error(int error, const char *add);
167     int send_to_srw_client_ok(int hits, Z_Records *records, int start);
168     int send_http_response(int code);
169     int send_srw_response(Z_SRW_PDU *srw_pdu);
170     int send_srw_explain_response(Z_SRW_diagnostic *diagnostics,
171                                   int num_diagnostics);
172     int z_to_srw_diag(ODR o, Z_SRW_searchRetrieveResponse *srw_res,
173                       Z_DefaultDiagFormat *ddf);
174     int m_http_keepalive;
175     const char *m_http_version;
176     yazpp_1::Yaz_cql2rpn m_cql2rpn;
177     void *m_time_tv;
178     void logtime();
179     Z_ElementSetNames *mk_esn_from_schema(ODR o, const char *schema);
180     Z_ReferenceId *m_referenceId;
181     NMEM m_referenceId_mem;
182     
183 #define NO_SPARE_SOLARIS_FD 10
184     int m_lo_fd[NO_SPARE_SOLARIS_FD];
185     void low_socket_open();
186     void low_socket_close();
187     char *m_usemarcon_ini_stage1;
188     char *m_usemarcon_ini_stage2;
189     Yaz_usemarcon *m_usemarcon;
190     Yaz_CharsetConverter *m_charset_converter;
191     yazpp_1::GDUQueue m_in_queue;
192  public:
193     Yaz_Proxy(yazpp_1::IPDU_Observable *the_PDU_Observable,
194               yazpp_1::ISocketObservable *the_socket_observable,
195               Yaz_Proxy *parent = 0);
196     ~Yaz_Proxy();
197
198
199     void inc_ref();
200     bool dec_ref();
201
202
203     int handle_authentication(Z_APDU *apdu);
204     void result_authentication(Z_APDU *apdu, int ret);
205     void handle_init(Z_APDU *apdu);
206     void inc_request_no();
207     void recv_GDU(Z_GDU *apdu, int len);
208     void recv_GDU_reduce(yazpp_1::GDU *gdu);
209     void recv_GDU_normal(yazpp_1::GDU *gdu);
210     void recv_GDU_more();
211     void handle_incoming_HTTP(Z_HTTP_Request *req);
212     void handle_incoming_Z_PDU(Z_APDU *apdu);
213     void handle_incoming_Z_PDU_2(Z_APDU *apdu);
214     IPDU_Observer *sessionNotify(yazpp_1::IPDU_Observable *the_PDU_Observable,
215                                  int fd);
216     void failNotify();
217     void timeoutNotify();
218     void connectNotify();
219     void markInvalid();
220     const char *option(const char *name, const char *value);
221     void set_default_target(const char *target);
222     void set_proxy_negotiation (const char *charset, const char *lang);
223     void set_target_charset(const char *charset);
224     char *get_proxy_target() { return m_proxyTarget; };
225     char *get_session_str() { return m_session_str; };
226     void set_max_clients(int m) { m_max_clients = m; };
227     void set_client_idletime (int t) { m_client_idletime = (t > 1) ? t : 600; };
228     void set_target_idletime (int t) { m_target_idletime = (t > 1) ? t : 600; };
229     int get_target_idletime () { return m_target_idletime; }
230     int set_config(const char *name);
231     void reconfig() { m_reconfig_flag = 1; }
232     int send_to_client(Z_APDU *apdu);
233     int server(const char *addr);
234     void pre_init();
235     int get_log_mask() { return m_log_mask; };
236     int handle_init_response_for_invalid_session(Z_APDU *apdu);
237     void set_debug_mode(int mode);
238     Msg_Thread *m_my_thread;
239 };
240
241 #endif