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