Limit record cache size
[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.12 2003-10-08 08:53:08 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 struct Yaz_RecordCache_Entry;
20
21 class YAZ_EXPORT Yaz_ProxyConfig {
22 public:
23     Yaz_ProxyConfig();
24     ~Yaz_ProxyConfig();
25     int read_xml(const char *fname);
26     void get_target_info(const char *name, const char **url, int *keepalive,
27                          int *limit_bw, int *limit_pdu, int *limit_req,
28                          int *target_idletime, int *client_idletime,
29                          int *max_clients);
30     void operator=(const Yaz_ProxyConfig &conf);
31     int check_query(ODR odr, const char *name, Z_Query *query, char **addinfo);
32     int check_syntax(ODR odr, const char *name,
33                      Odr_oid *syntax, char **addinfo);
34 private:
35 #if HAVE_XML2
36     xmlDocPtr m_docPtr;
37     xmlNodePtr m_proxyPtr;
38     void return_target_info(xmlNodePtr ptr, const char **url, int *keepalive,
39                             int *limit_bw, int *limit_pdu, int *limit_req,
40                             int *target_idletime, int *client_idletime);
41     void return_limit(xmlNodePtr ptr,
42                       int *limit_bw, int *limit_pdu, int *limit_req);
43     int check_type_1(ODR odr, xmlNodePtr ptr, Z_RPNQuery *query,
44                      char **addinfo);
45     xmlNodePtr find_target_node(const char *name);
46     const char *get_text(xmlNodePtr ptr);
47     int check_type_1_attributes(ODR odr, xmlNodePtr ptr,
48                                 Z_AttributeList *attrs,
49                                 char **addinfo);
50     int check_type_1_structure(ODR odr, xmlNodePtr ptr, Z_RPNStructure *q,
51                                char **addinfo);
52 #endif
53     int m_copy;
54     int match_list(int v, const char *m);
55     int atoi_l(const char **cp);
56
57 };
58
59 class YAZ_EXPORT Yaz_RecordCache {
60  public:
61     Yaz_RecordCache ();
62     ~Yaz_RecordCache ();
63     void add (ODR o, Z_NamePlusRecordList *npr, int start, int hits);
64     
65     int lookup (ODR o, Z_NamePlusRecordList **npr, int start, int num,
66                 Odr_oid *syntax, Z_RecordComposition *comp);
67     void clear();
68
69     void copy_searchRequest(Z_SearchRequest *sr);
70     void copy_presentRequest(Z_PresentRequest *pr);
71     void set_max_size(int sz);
72  private:
73     NMEM m_mem;
74     Yaz_RecordCache_Entry *m_entries;
75     Z_SearchRequest *m_searchRequest;
76     Z_PresentRequest *m_presentRequest;
77     int match (Yaz_RecordCache_Entry *entry,
78                Odr_oid *syntax, int offset,
79                Z_RecordComposition *comp);
80     int m_max_size;
81 };
82
83 class YAZ_EXPORT Yaz_bw {
84  public:
85     Yaz_bw(int sz);
86     ~Yaz_bw();
87     void add_bytes(int m);
88     int get_total();
89  private:
90     long m_sec;   // time of most recent bucket
91     int *m_bucket;
92     int m_ptr;
93     int m_size;
94 };
95
96 /// Private class
97 class YAZ_EXPORT Yaz_ProxyClient : public Yaz_Z_Assoc {
98     friend class Yaz_Proxy;
99     Yaz_ProxyClient(IYaz_PDU_Observable *the_PDU_Observable);
100     ~Yaz_ProxyClient();
101     void recv_Z_PDU(Z_APDU *apdu, int len);
102     IYaz_PDU_Observer* sessionNotify
103         (IYaz_PDU_Observable *the_PDU_Observable, int fd);
104     void shutdown();
105     Yaz_Proxy *m_server;
106     void failNotify();
107     void timeoutNotify();
108     void connectNotify();
109     int send_to_target(Z_APDU *apdu);
110     const char *get_session_str();
111     char *m_cookie;
112     Yaz_ProxyClient *m_next;
113     Yaz_ProxyClient **m_prev;
114     int m_init_flag;
115     Yaz_Z_Query *m_last_query;
116     Yaz_Z_Databases m_last_databases;
117     char *m_last_resultSetId;
118     int m_last_ok;
119     int m_last_resultCount;
120     int m_sr_transform;
121     int m_seqno;
122     int m_waiting;
123     int m_resultSetStartPoint;
124     int m_bytes_sent;
125     int m_bytes_recv;
126     ODR m_init_odr;
127     Z_APDU *m_initResponse;
128     Yaz_RecordCache m_cache;
129 };
130
131 /// Information Retrieval Proxy Server.
132 class YAZ_EXPORT Yaz_Proxy : public Yaz_Z_Assoc {
133  private:
134     char *get_cookie(Z_OtherInformation **otherInfo);
135     char *get_proxy(Z_OtherInformation **otherInfo);
136     Yaz_ProxyClient *get_client(Z_APDU *apdu);
137     Z_APDU *result_set_optimize(Z_APDU *apdu);
138     void shutdown();
139     
140     Yaz_ProxyClient *m_client;
141     IYaz_PDU_Observable *m_PDU_Observable;
142     Yaz_ProxyClient *m_clientPool;
143     Yaz_Proxy *m_parent;
144     int m_seqno;
145     int m_max_clients;
146     int m_keepalive;
147     int m_client_idletime;
148     int m_target_idletime;
149     char *m_proxyTarget;
150     char *m_default_target;
151     char *m_proxy_authentication;
152     long m_seed;
153     char *m_optimize;
154     int m_session_no;         // sequence for each client session
155     char m_session_str[20];  // session string (time:session_no)
156     Yaz_ProxyConfig m_config;
157     int m_bytes_sent;
158     int m_bytes_recv;
159     int m_bw_max;
160     Yaz_bw m_bw_stat;
161     int m_pdu_max;
162     Yaz_bw m_pdu_stat;
163     Z_APDU *m_bw_hold_PDU;
164     int m_max_record_retrieve;
165     void handle_max_record_retrieve(Z_APDU *apdu);
166     void display_diagrecs(Z_DiagRec **pp, int num);
167     Z_Records *create_nonSurrogateDiagnostics(ODR o, int error,
168                                               const char *addinfo);
169
170     Z_APDU *handle_query_validation(Z_APDU *apdu);
171     Z_APDU *handle_syntax_validation(Z_APDU *apdu);
172  public:
173     Yaz_Proxy(IYaz_PDU_Observable *the_PDU_Observable);
174     ~Yaz_Proxy();
175     void recv_Z_PDU(Z_APDU *apdu, int len);
176     void recv_Z_PDU_0(Z_APDU *apdu);
177     IYaz_PDU_Observer* sessionNotify
178         (IYaz_PDU_Observable *the_PDU_Observable, int fd);
179     void failNotify();
180     void timeoutNotify();
181     void connectNotify();
182     const char *option(const char *name, const char *value);
183     void set_default_target(const char *target);
184     void set_proxy_authentication (const char *auth);
185     char *get_proxy_target() { return m_proxyTarget; };
186     char *get_session_str() { return m_session_str; };
187     void set_max_clients(int m) { m_max_clients = m; };
188     void set_client_idletime (int t) { m_client_idletime = (t > 1) ? t : 600; };
189     void set_target_idletime (int t) { m_target_idletime = (t > 1) ? t : 600; };
190     int get_target_idletime () { return m_target_idletime; }
191     int set_config(const char *name);
192     int send_to_client(Z_APDU *apdu);
193 };
194