Fix C++ compiles on newer GCC
[yazpp-moved-to-github.git] / include / yaz++ / proxy.h
1 /*
2  * Copyright (c) 1998-2003, Index Data.
3  * See the file LICENSE for details.
4  * 
5  * $Id: proxy.h,v 1.17 2003-10-16 08:28:10 adam Exp $
6  */
7
8 #include <yaz++/z-assoc.h>
9 #include <yaz++/z-query.h>
10 #include <yaz++/z-databases.h>
11
12 #if HAVE_XML2
13 #include <libxml/parser.h>
14 #include <libxml/tree.h>
15 #endif
16
17 class Yaz_Proxy;
18
19 #define MAX_ZURL_PLEX 10
20
21 struct Yaz_RecordCache_Entry;
22
23 class YAZ_EXPORT Yaz_ProxyConfig {
24 public:
25     Yaz_ProxyConfig();
26     ~Yaz_ProxyConfig();
27     int read_xml(const char *fname);
28
29     int get_target_no(int no,
30                       const char **name,
31                       const char **url,
32                       int *limit_bw,
33                       int *limit_pdu,
34                       int *limit_req,
35                       int *target_idletime,
36                       int *client_idletime,
37                       int *max_clients,
38                       int *keepalive_limit_bw,
39                       int *keepalive_limit_pdu,
40                       int *pre_init);
41
42     void get_target_info(const char *name, const char **url,
43                          int *limit_bw, int *limit_pdu, int *limit_req,
44                          int *target_idletime, int *client_idletime,
45                          int *max_clients,
46                          int *keepalive_limit_bw, int *keepalive_limit_pdu,
47                          int *pre_init);
48
49     int check_query(ODR odr, const char *name, Z_Query *query, char **addinfo);
50     int check_syntax(ODR odr, const char *name,
51                      Odr_oid *syntax, char **addinfo);
52 private:
53     void operator=(const Yaz_ProxyConfig &conf);
54 #if HAVE_XML2
55     xmlDocPtr m_docPtr;
56     xmlNodePtr m_proxyPtr;
57     void return_target_info(xmlNodePtr ptr, const char **url,
58                             int *limit_bw, int *limit_pdu, int *limit_req,
59                             int *target_idletime, int *client_idletime,
60                             int *keepalive_limit_bw, int *keepalive_limit_pdu,
61                             int *pre_init);
62     void return_limit(xmlNodePtr ptr,
63                       int *limit_bw, int *limit_pdu, int *limit_req);
64     int check_type_1(ODR odr, xmlNodePtr ptr, Z_RPNQuery *query,
65                      char **addinfo);
66     xmlNodePtr find_target_node(const char *name);
67     const char *get_text(xmlNodePtr ptr);
68     int check_type_1_attributes(ODR odr, xmlNodePtr ptr,
69                                 Z_AttributeList *attrs,
70                                 char **addinfo);
71     int check_type_1_structure(ODR odr, xmlNodePtr ptr, Z_RPNStructure *q,
72                                char **addinfo);
73 #endif
74     int m_copy;
75     int match_list(int v, const char *m);
76     int atoi_l(const char **cp);
77
78 };
79
80 class YAZ_EXPORT Yaz_RecordCache {
81  public:
82     Yaz_RecordCache ();
83     ~Yaz_RecordCache ();
84     void add (ODR o, Z_NamePlusRecordList *npr, int start, int hits);
85     
86     int lookup (ODR o, Z_NamePlusRecordList **npr, int start, int num,
87                 Odr_oid *syntax, Z_RecordComposition *comp);
88     void clear();
89
90     void copy_searchRequest(Z_SearchRequest *sr);
91     void copy_presentRequest(Z_PresentRequest *pr);
92     void set_max_size(int sz);
93  private:
94     NMEM m_mem;
95     Yaz_RecordCache_Entry *m_entries;
96     Z_SearchRequest *m_searchRequest;
97     Z_PresentRequest *m_presentRequest;
98     int match (Yaz_RecordCache_Entry *entry,
99                Odr_oid *syntax, int offset,
100                Z_RecordComposition *comp);
101     int m_max_size;
102 };
103
104 class YAZ_EXPORT Yaz_bw {
105  public:
106     Yaz_bw(int sz);
107     ~Yaz_bw();
108     void add_bytes(int m);
109     int get_total();
110  private:
111     long m_sec;   // time of most recent bucket
112     int *m_bucket;
113     int m_ptr;
114     int m_size;
115 };
116
117 /// Private class
118 class YAZ_EXPORT Yaz_ProxyClient : public Yaz_Z_Assoc {
119     friend class Yaz_Proxy;
120     Yaz_ProxyClient(IYaz_PDU_Observable *the_PDU_Observable,
121                     Yaz_Proxy *parent);
122     ~Yaz_ProxyClient();
123     void recv_Z_PDU(Z_APDU *apdu, int len);
124     IYaz_PDU_Observer* sessionNotify
125         (IYaz_PDU_Observable *the_PDU_Observable, int fd);
126     void shutdown();
127     Yaz_Proxy *m_server;
128     void failNotify();
129     void timeoutNotify();
130     void connectNotify();
131     int send_to_target(Z_APDU *apdu);
132     const char *get_session_str();
133     char *m_cookie;
134     Yaz_ProxyClient *m_next;
135     Yaz_ProxyClient **m_prev;
136     int m_init_flag;
137     Yaz_Z_Query *m_last_query;
138     Yaz_Z_Databases m_last_databases;
139     char *m_last_resultSetId;
140     int m_last_ok;
141     int m_last_resultCount;
142     int m_sr_transform;
143     int m_seqno;
144     int m_waiting;
145     int m_resultSetStartPoint;
146     int m_bytes_sent;
147     int m_bytes_recv;
148     int m_pdu_recv;
149     ODR m_init_odr;
150     Z_APDU *m_initResponse;
151     Yaz_RecordCache m_cache;
152     void pre_init_client();
153     int m_target_idletime;
154     Yaz_Proxy *m_root;
155 };
156
157 /// Information Retrieval Proxy Server.
158 class YAZ_EXPORT Yaz_Proxy : public Yaz_Z_Assoc {
159  private:
160     char *get_cookie(Z_OtherInformation **otherInfo);
161     char *get_proxy(Z_OtherInformation **otherInfo);
162     Yaz_ProxyClient *get_client(Z_APDU *apdu);
163     Z_APDU *result_set_optimize(Z_APDU *apdu);
164     void shutdown();
165     
166     Yaz_ProxyClient *m_client;
167     IYaz_PDU_Observable *m_PDU_Observable;
168     Yaz_ProxyClient *m_clientPool;
169     Yaz_Proxy *m_parent;
170     int m_seqno;
171     int m_max_clients;
172     int m_keepalive_limit_bw;
173     int m_keepalive_limit_pdu;
174     int m_client_idletime;
175     int m_target_idletime;
176     char *m_proxyTarget;
177     char *m_default_target;
178     char *m_proxy_authentication;
179     long m_seed;
180     char *m_optimize;
181     int m_session_no;         // sequence for each client session
182     char m_session_str[30];  // session string (time:session_no)
183     Yaz_ProxyConfig *m_config;
184     char *m_config_fname;
185     int m_bytes_sent;
186     int m_bytes_recv;
187     int m_bw_max;
188     Yaz_bw m_bw_stat;
189     int m_pdu_max;
190     Yaz_bw m_pdu_stat;
191     Z_APDU *m_bw_hold_PDU;
192     int m_max_record_retrieve;
193     void handle_max_record_retrieve(Z_APDU *apdu);
194     void display_diagrecs(Z_DiagRec **pp, int num);
195     Z_Records *create_nonSurrogateDiagnostics(ODR o, int error,
196                                               const char *addinfo);
197
198     Z_APDU *handle_query_validation(Z_APDU *apdu);
199     Z_APDU *handle_syntax_validation(Z_APDU *apdu);
200     const char *load_balance(const char **url);
201     int m_reconfig_flag;
202     Yaz_ProxyConfig *check_reconfigure();
203     int m_request_no;
204     int m_invalid_session;
205     int m_marcxml_flag;
206     void convert_to_marcxml(Z_NamePlusRecordList *p);
207  public:
208     Yaz_Proxy(IYaz_PDU_Observable *the_PDU_Observable,
209               Yaz_Proxy *parent = 0);
210     ~Yaz_Proxy();
211     void recv_Z_PDU(Z_APDU *apdu, int len);
212     void recv_Z_PDU_0(Z_APDU *apdu);
213     IYaz_PDU_Observer* sessionNotify
214         (IYaz_PDU_Observable *the_PDU_Observable, int fd);
215     void failNotify();
216     void timeoutNotify();
217     void connectNotify();
218     const char *option(const char *name, const char *value);
219     void set_default_target(const char *target);
220     void set_proxy_authentication (const char *auth);
221     char *get_proxy_target() { return m_proxyTarget; };
222     char *get_session_str() { return m_session_str; };
223     void set_max_clients(int m) { m_max_clients = m; };
224     void set_client_idletime (int t) { m_client_idletime = (t > 1) ? t : 600; };
225     void set_target_idletime (int t) { m_target_idletime = (t > 1) ? t : 600; };
226     int get_target_idletime () { return m_target_idletime; }
227     int set_config(const char *name);
228     void reconfig() { m_reconfig_flag = 1; }
229     int send_to_client(Z_APDU *apdu);
230     void server(const char *addr);
231     void pre_init();
232 };
233