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