Connections now wait for hosts to be resolved. Delayed connect attempt
[pazpar2-moved-to-github.git] / src / pazpar2.h
1 /* $Id: pazpar2.h,v 1.33 2007-04-22 16:41:42 adam Exp $
2    Copyright (c) 2006-2007, Index Data.
3
4 This file is part of Pazpar2.
5
6 Pazpar2 is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
9 version.
10
11 Pazpar2 is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with Pazpar2; see the file LICENSE.  If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19 02111-1307, USA.
20  */
21
22 #ifndef PAZPAR2_H
23 #define PAZPAR2_H
24
25
26 #include <netdb.h>
27
28 #include <libxslt/xsltutils.h>
29 #include <libxslt/transform.h>
30
31 #include <yaz/comstack.h>
32 #include <yaz/pquery.h>
33 #include <yaz/ccl.h>
34 #include <yaz/yaz-ccl.h>
35
36 #include "termlists.h"
37 #include "relevance.h"
38 #include "reclists.h"
39 #include "eventl.h"
40 #include "config.h"
41
42 struct record;
43 struct client;
44
45 struct connection;
46
47 // Represents a host (irrespective of databases)
48 struct host {
49     char *hostport;
50     char *ipport;
51     struct connection *connections; // All connections to this
52     struct host *next;
53 };
54
55 // Represents a (virtual) database on a host
56 struct database {
57     struct host *host;
58     char *url;
59     char **databases;
60     int errors;
61     struct zr_explain *explain;
62     struct setting **settings;
63     struct database *next;
64 };
65
66 struct database_criterion_value {
67     char *value;
68     struct database_criterion_value *next;
69 };
70
71 struct database_criterion {
72     char *name;
73     struct database_criterion_value *values;
74     struct database_criterion *next;
75 };
76
77 // Represents a physical, reusable  connection to a remote Z39.50 host
78 struct connection {
79     IOCHAN iochan;
80     COMSTACK link;
81     struct host *host;
82     struct client *client;
83     char *ibuf;
84     int ibufsize;
85     enum {
86         Conn_Resolving,
87         Conn_Connecting,
88         Conn_Open,
89         Conn_Waiting,
90     } state;
91     struct connection *next;
92 };
93
94 // Represents client state for a connection to one search target
95 struct client {
96     struct session_database *database;
97     struct connection *connection;
98     struct session *session;
99     char *pquery; // Current search
100     int hits;
101     int records;
102     int setno;
103     int requestid;                              // ID of current outstanding request
104     int diagnostic;
105     enum client_state
106     {
107         Client_Connecting,
108         Client_Connected,
109         Client_Idle,
110         Client_Initializing,
111         Client_Searching,
112         Client_Presenting,
113         Client_Error,
114         Client_Failed,
115         Client_Disconnected,
116         Client_Stopped
117     } state;
118     struct client *next;
119 };
120
121 // Normalization filter. Turns incoming record into internal representation
122 // Simple sequence of stylesheets run in series.
123 struct database_retrievalmap {
124     xsltStylesheet *stylesheet;
125     struct database_retrievalmap *next;
126 };
127
128 // Represents a database as viewed from one session, possibly with settings overriden
129 // for that session
130 struct session_database
131 {
132     struct database *database;
133     struct setting **settings;
134     yaz_marc_t yaz_marc;
135     struct database_retrievalmap *map;
136     struct session_database *next;
137 };
138
139 #define SESSION_WATCH_RECORDS   0
140 #define SESSION_WATCH_MAX       0
141
142 #define SESSION_MAX_TERMLISTS 10
143
144 typedef void (*session_watchfun)(void *data);
145
146 struct named_termlist
147 {
148     char *name;
149     struct termlist *termlist;
150 };
151
152 // End-user session
153 struct session {
154     struct session_database *databases;  // All databases, settings overriden
155     struct client *clients;              // Clients connected for current search
156     int requestid; 
157     NMEM session_nmem;  // Nmem for session-permanent storage
158     NMEM nmem;          // Nmem for each operation (i.e. search, result set, etc)
159     WRBUF wrbuf;        // Wrbuf for scratch(i.e. search)
160     int num_termlists;
161     struct named_termlist termlists[SESSION_MAX_TERMLISTS];
162     struct relevance *relevance;
163     struct reclist *reclist;
164     struct {
165         void *data;
166         session_watchfun fun;
167     } watchlist[SESSION_WATCH_MAX + 1];
168     int expected_maxrecs;
169     int total_hits;
170     int total_records;
171     int total_merged;
172 };
173
174 struct statistics {
175     int num_clients;
176     int num_no_connection;
177     int num_connecting;
178     int num_initializing;
179     int num_searching;
180     int num_presenting;
181     int num_idle;
182     int num_failed;
183     int num_error;
184     int num_hits;
185     int num_records;
186 };
187
188 struct hitsbytarget {
189     char *id;
190     char *name;
191     int hits;
192     int diagnostic;
193     int records;
194     char* state;
195     int connected;
196 };
197
198 struct parameters {
199     char proxy_override[128];
200     char listener_override[128];
201     char zproxy_override[128];
202     char settings_path_override[128];
203     struct conf_server *server;
204     int dump_records;
205     int timeout;                /* operations timeout, in seconds */
206     char implementationId[128];
207     char implementationName[128];
208     char implementationVersion[128];
209     int target_timeout; // seconds
210     int session_timeout;
211     int toget;
212     int chunk;
213     ODR odr_out;
214     ODR odr_in;
215 };
216
217 struct hitsbytarget *hitsbytarget(struct session *s, int *count);
218 int select_targets(struct session *se, struct database_criterion *crit);
219 struct session *new_session(NMEM nmem);
220 void destroy_session(struct session *s);
221 int load_targets(struct session *s, const char *fn);
222 void statistics(struct session *s, struct statistics *stat);
223 char *search(struct session *s, char *query, char *filter);
224 struct record_cluster **show(struct session *s, struct reclist_sortparms *sp, int start,
225         int *num, int *total, int *sumhits, NMEM nmem_show);
226 struct record_cluster *show_single(struct session *s, int id);
227 struct termlist_score **termlist(struct session *s, const char *name, int *num);
228 void session_set_watch(struct session *s, int what, session_watchfun fun, void *data);
229 int session_active_clients(struct session *s);
230 void session_apply_setting(struct session *se, char *dbname, char *setting, char *value);
231 char *session_setting_oneval(struct session_database *db, int offset);
232
233 void start_http_listener(void);
234 void start_proxy(void);
235 void start_zproxy(void);
236
237 extern struct parameters global_parameters;
238 void pazpar2_add_channel(IOCHAN c);
239 void pazpar2_event_loop(void);
240
241 int host_getaddrinfo(struct host *host);
242 void connect_resolver_host(struct host *host);
243
244 #endif
245
246 /*
247  * Local variables:
248  * c-basic-offset: 4
249  * indent-tabs-mode: nil
250  * End:
251  * vim: shiftwidth=4 tabstop=8 expandtab
252  */