Implement max-connect facility which makes the server sleep if a
[yazproxy-moved-to-github.git] / include / yazproxy / proxy.h
1 /* $Id: proxy.h,v 1.29 2006-03-30 10:35:15 adam Exp $
2    Copyright (c) 1998-2006, Index Data.
3
4 This file is part of the yazproxy.
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 <yazpp/socket-observer.h>
26 #include <yazpp/z-assoc.h>
27 #include <yazpp/z-query.h>
28 #include <yazpp/z-databases.h>
29 #include <yazpp/cql2rpn.h>
30 #include <yaz/cql.h>
31 #include <yazpp/gdu.h>
32 #include <yazpp/gduqueue.h>
33 #include <yazproxy/bw.h>
34 #include <yazproxy/limit-connect.h>
35
36 class Yaz_Proxy;
37
38 #define MAX_ZURL_PLEX 10
39
40 #define PROXY_LOG_APDU_CLIENT 1
41 #define PROXY_LOG_APDU_SERVER 2
42 #define PROXY_LOG_REQ_CLIENT 4
43 #define PROXY_LOG_REQ_SERVER 8
44
45 class Yaz_usemarcon;
46 class Yaz_ProxyConfig;
47 class Yaz_ProxyClient;
48 class Yaz_CharsetConverter;
49
50 enum YAZ_Proxy_MARCXML_mode {
51     none,
52     marcxml,
53 };
54
55 class Msg_Thread;
56
57 /// Information Retrieval Proxy Server.
58 class YAZ_EXPORT Yaz_Proxy : public yazpp_1::Z_Assoc {
59     friend class Proxy_Msg;
60  private:
61     char *m_peername;
62     int m_ref_count;
63     bool m_main_ptr_dec;
64     char *get_cookie(Z_OtherInformation **otherInfo);
65     char *get_proxy(Z_OtherInformation **otherInfo);
66     void get_charset_and_lang_negotiation(Z_OtherInformation **otherInfo,
67         char **charstes, char **langs, int *selected);
68     Yaz_ProxyClient *get_client(Z_APDU *apdu, const char *cookie,
69                                 const char *proxy_host);
70     void srw_get_client(const char *db, const char **backend_db);
71     Z_APDU *result_set_optimize(Z_APDU *apdu);
72     void releaseClient();
73     Yaz_ProxyClient *m_client;
74     yazpp_1::IPDU_Observable *m_PDU_Observable;
75     yazpp_1::ISocketObservable *m_socket_observable;
76     Yaz_ProxyClient *m_clientPool;
77     Yaz_Proxy *m_parent;
78     int m_seqno;
79     int m_max_clients;
80     int m_log_mask;
81     int m_keepalive_limit_bw;
82     int m_keepalive_limit_pdu;
83     int m_client_idletime;
84     int m_target_idletime;
85     int m_debug_mode;
86     char *m_proxyTarget;
87     char *m_default_target;
88     char *m_proxy_negotiation_charset;
89     char *m_proxy_negotiation_lang;
90     char *m_proxy_negotiation_default_charset;
91     long m_seed;
92     char *m_optimize;
93     int m_session_no;         // sequence for each client session
94     char m_session_str[30];  // session string (time:session_no)
95     Yaz_ProxyConfig *m_config;
96     char *m_config_fname;
97     int m_bytes_sent;
98     int m_bytes_recv;
99     int m_bw_max;
100
101     yazpp_1::GDU *m_timeout_gdu;
102     enum timeout_mode {
103         timeout_busy,
104         timeout_normal,
105         timeout_reduce,
106         timeout_xsl
107     } m_timeout_mode;
108
109     int m_connect_max;
110     int m_search_max;
111     Yaz_bw m_bw_stat;
112     int m_pdu_max;
113     Yaz_bw m_pdu_stat;
114     int m_max_record_retrieve;
115     void handle_max_record_retrieve(Z_APDU *apdu);
116     void display_diagrecs(Z_DiagRec **pp, int num);
117     Z_Records *create_nonSurrogateDiagnostics(ODR o, int error,
118                                               const char *addinfo);
119
120     Z_APDU *handle_query_validation(Z_APDU *apdu);
121     Z_APDU *handle_query_transformation(Z_APDU *apdu);
122     Z_APDU *handle_target_charset_conversion(Z_APDU *apdu);
123
124     Z_APDU *handle_syntax_validation(Z_APDU *apdu);
125
126     void handle_charset_lang_negotiation(Z_APDU *apdu);
127
128     const char *load_balance(const char **url);
129     int m_reconfig_flag;
130     Yaz_ProxyConfig *check_reconfigure();
131     int m_request_no;
132     int m_flag_invalid_session;
133     YAZ_Proxy_MARCXML_mode m_marcxml_mode;
134     void *m_stylesheet_xsp;  // Really libxslt's xsltStylesheetPtr
135     int m_stylesheet_offset;
136     Z_APDU *m_stylesheet_apdu;
137     Z_NamePlusRecordList *m_stylesheet_nprl;
138     char *m_schema;
139     char *m_backend_type;
140     char *m_backend_charset;
141     int m_frontend_type;
142     void convert_to_frontend_type(Z_NamePlusRecordList *p);
143     void convert_to_marcxml(Z_NamePlusRecordList *p, const char *charset);
144     void convert_records_charset(Z_NamePlusRecordList *p, const char *charset);
145     int convert_xsl(Z_NamePlusRecordList *p, Z_APDU *apdu);
146     void convert_xsl_delay();
147     Z_APDU *m_initRequest_apdu;
148     int m_initRequest_preferredMessageSize;
149     int m_initRequest_maximumRecordSize;
150     Z_Options *m_initRequest_options;
151     Z_ProtocolVersion *m_initRequest_version;
152     char **m_initRequest_oi_negotiation_charsets;
153     int m_initRequest_oi_negotiation_num_charsets;
154     char **m_initRequest_oi_negotiation_langs;
155     int m_initRequest_oi_negotiation_num_langs;
156     int m_initRequest_oi_negotiation_selected;
157     NMEM m_initRequest_mem;
158     Z_APDU *m_apdu_invalid_session;
159     NMEM m_mem_invalid_session;
160     int send_PDU_convert(Z_APDU *apdu);
161     ODR m_s2z_odr_init;
162     ODR m_s2z_odr_search;
163     int m_s2z_hit_count;
164     int m_s2z_packing;
165     char *m_s2z_database;
166     Z_APDU *m_s2z_init_apdu;
167     Z_APDU *m_s2z_search_apdu;
168     Z_APDU *m_s2z_present_apdu;
169     char *m_s2z_stylesheet;
170     char *m_soap_ns;
171     int file_access(Z_HTTP_Request *hreq);
172     int send_to_srw_client_error(int error, const char *add);
173     int send_to_srw_client_ok(int hits, Z_Records *records, int start);
174     int send_http_response(int code);
175     int send_srw_response(Z_SRW_PDU *srw_pdu, int http_code = 200);
176     int send_srw_search_response(Z_SRW_diagnostic *diagnostics,
177                                  int num_diagnostics,
178                                  int http_code = 200);
179     int send_srw_explain_response(Z_SRW_diagnostic *diagnostics,
180                                   int num_diagnostics);
181     int z_to_srw_diag(ODR o, Z_SRW_searchRetrieveResponse *srw_res,
182                       Z_DefaultDiagFormat *ddf);
183     int m_http_keepalive;
184     const char *m_http_version;
185     yazpp_1::Yaz_cql2rpn m_cql2rpn;
186     void *m_time_tv;
187     void logtime();
188     Z_ElementSetNames *mk_esn_from_schema(ODR o, const char *schema);
189     Z_ReferenceId *m_referenceId;
190     NMEM m_referenceId_mem;
191
192 #define NO_SPARE_SOLARIS_FD 10
193     int m_lo_fd[NO_SPARE_SOLARIS_FD];
194     void low_socket_open();
195     void low_socket_close();
196     char *m_usemarcon_ini_stage1;
197     char *m_usemarcon_ini_stage2;
198     Yaz_usemarcon *m_usemarcon;
199     Yaz_CharsetConverter *m_charset_converter;
200     yazpp_1::GDUQueue m_in_queue;
201     LimitConnect m_connect;
202  public:
203     Yaz_Proxy(yazpp_1::IPDU_Observable *the_PDU_Observable,
204               yazpp_1::ISocketObservable *the_socket_observable,
205               Yaz_Proxy *parent = 0);
206     ~Yaz_Proxy();
207
208
209     void inc_ref();
210     bool dec_ref(bool main_ptr);
211
212
213     int handle_authentication(Z_APDU *apdu);
214     void result_authentication(Z_APDU *apdu, int ret);
215     void handle_init(Z_APDU *apdu);
216     void inc_request_no();
217     void recv_GDU(Z_GDU *apdu, int len);
218     void recv_GDU_reduce(yazpp_1::GDU *gdu);
219     void recv_GDU_normal(yazpp_1::GDU *gdu);
220     void recv_GDU_more(bool normal);
221     void handle_incoming_HTTP(Z_HTTP_Request *req);
222     void handle_incoming_Z_PDU(Z_APDU *apdu);
223     void handle_incoming_Z_PDU_2(Z_APDU *apdu);
224     IPDU_Observer *sessionNotify(yazpp_1::IPDU_Observable *the_PDU_Observable,
225                                  int fd);
226     void failNotify();
227     void timeoutNotify();
228     void connectNotify();
229     void markInvalid();
230     const char *option(const char *name, const char *value);
231     void set_default_target(const char *target);
232     void set_proxy_negotiation(const char *charset, const char *lang,
233                                const char *default_charset);
234     void set_target_charset(const char *charset);
235     char *get_proxy_target() { return m_proxyTarget; };
236     char *get_session_str() { return m_session_str; };
237     void set_max_clients(int m) { m_max_clients = m; };
238     void set_client_idletime (int t) { m_client_idletime = (t > 1) ? t : 600; };
239     void set_target_idletime (int t) { m_target_idletime = (t > 1) ? t : 600; };
240     int get_target_idletime () { return m_target_idletime; }
241     int set_config(const char *name);
242     void reconfig() { m_reconfig_flag = 1; }
243     int send_to_client(Z_APDU *apdu);
244     int server(const char *addr);
245     void pre_init();
246     int get_log_mask() { return m_log_mask; };
247     int handle_init_response_for_invalid_session(Z_APDU *apdu);
248     void set_debug_mode(int mode);
249     void send_response_fail_client(const char *addr);
250     Msg_Thread *m_my_thread;
251     void base64_decode(const char *base64, char *buf, int buf_len);
252 };
253
254 #endif
255 /*
256  * Local variables:
257  * c-basic-offset: 4
258  * indent-tabs-mode: nil
259  * End:
260  * vim: shiftwidth=4 tabstop=8 expandtab
261  */
262