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