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