From de634461a0500801cd71d6a42aff032fa083a127 Mon Sep 17 00:00:00 2001 From: Adam Dickmeiss Date: Mon, 7 Sep 2009 15:58:14 +0200 Subject: [PATCH] Refactor / reduce globals. --- src/client.c | 3 +- src/database.c | 4 +++ src/http.c | 15 ++++++--- src/http.h | 3 +- src/http_command.c | 3 +- src/logic.c | 83 +++++++++++++++++++++++++------------------------- src/parameters.h | 3 -- src/pazpar2.c | 19 ++++++------ src/pazpar2.h | 4 ++- src/pazpar2_config.c | 33 ++++++++++---------- src/pazpar2_config.h | 22 +++++++------ src/record.c | 3 -- 12 files changed, 104 insertions(+), 91 deletions(-) diff --git a/src/client.c b/src/client.c index 31c2543..6a29bac 100644 --- a/src/client.c +++ b/src/client.c @@ -67,6 +67,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA /** \brief Represents client state for a connection to one search target */ struct client { struct session_database *database; + struct conf_server *server; struct connection *connection; struct session *session; char *pquery; // Current search @@ -706,7 +707,7 @@ int client_parse_query(struct client *cl, const char *query) char *p[512]; extract_terms(se->nmem, cn, p); se->relevance = relevance_create( - global_parameters.server->relevance_pct, + se->service->relevance_pct, se->nmem, (const char **) p, se->expected_maxrecs); } diff --git a/src/database.c b/src/database.c index 1834b78..204b3ae 100644 --- a/src/database.c +++ b/src/database.c @@ -47,6 +47,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA static struct host *hosts = 0; // The hosts we know about +#if 0 static xmlDoc *get_explain_xml(const char *id) { struct stat st; @@ -71,6 +72,7 @@ static xmlDoc *get_explain_xml(const char *id) else return 0; } +#endif // Create a new host structure for hostport static struct host *create_host(const char *hostport) @@ -115,6 +117,7 @@ static struct database *load_database(const char *id, yaz_log(YLOG_LOG, "New database: %s", id); +#if 0 if (config && config->targetprofiles && (doc = get_explain_xml(id))) { @@ -122,6 +125,7 @@ static struct database *load_database(const char *id, if (!explain) return 0; } +#endif if (strlen(id) > 255) return 0; diff --git a/src/http.c b/src/http.c index 62233b2..69533c6 100644 --- a/src/http.c +++ b/src/http.c @@ -71,7 +71,8 @@ typedef int socklen_t; #define MAX_HTTP_HEADER 4096 static void proxy_io(IOCHAN i, int event); -static struct http_channel *http_create(const char *addr); +static struct http_channel *http_create(const char *addr, + struct conf_server *server); static void http_destroy(IOCHAN i); // If this is set, we proxy normal HTTP requests @@ -735,7 +736,7 @@ static int http_proxy(struct http_request *rq) struct http_buf *requestbuf; char server_via[128] = ""; char server_port[16] = ""; - struct conf_server *ser = global_parameters.server; + struct conf_server *ser = c->server; if (!p) // This is a new connection. Create a proxy channel { @@ -1064,7 +1065,8 @@ static void http_destroy(IOCHAN i) iochan_destroy(i); } -static struct http_channel *http_create(const char *addr) +static struct http_channel *http_create(const char *addr, + struct conf_server *server) { struct http_channel *r = http_channel_freelist; @@ -1080,6 +1082,7 @@ static struct http_channel *http_create(const char *addr) r->nmem = nmem_create(); r->wrbuf = wrbuf_alloc(); } + r->server = server; r->proxy = 0; r->iochan = 0; r->iqueue = r->oqueue = 0; @@ -1107,6 +1110,7 @@ static void http_accept(IOCHAN i, int event) int s; IOCHAN c; struct http_channel *ch; + struct conf_server *server = iochan_getdata(i); len = sizeof addr; if ((s = accept(fd, (struct sockaddr *) &addr, &len)) < 0) @@ -1119,7 +1123,7 @@ static void http_accept(IOCHAN i, int event) yaz_log(YLOG_DEBUG, "New command connection"); c = iochan_create(s, http_io, EVENT_INPUT | EVENT_EXCEPT); - ch = http_create(inet_ntoa(addr.sin_addr)); + ch = http_create(inet_ntoa(addr.sin_addr), server); ch->iochan = c; iochan_setdata(c, ch); @@ -1129,7 +1133,7 @@ static void http_accept(IOCHAN i, int event) static int listener_socket = 0; /* Create a http-channel listener, syntax [host:]port */ -int http_init(const char *addr) +int http_init(const char *addr, struct conf_server *server) { IOCHAN c; int l; @@ -1191,6 +1195,7 @@ int http_init(const char *addr) listener_socket = l; c = iochan_create(l, http_accept, EVENT_INPUT | EVENT_EXCEPT); + iochan_setdata(c, server); pazpar2_add_channel(c); return 0; } diff --git a/src/http.h b/src/http.h index cacabb0..df2c3c2 100644 --- a/src/http.h +++ b/src/http.h @@ -52,6 +52,7 @@ struct http_channel struct http_channel *next; // for freelist char addr[20]; // forwarded address http_channel_observer_t observers; + struct conf_server *server; }; struct http_proxy // attached to iochan for proxy connection @@ -100,7 +101,7 @@ struct http_response }; void http_set_proxyaddr(char *url, char *baseurl); -int http_init(const char *addr); +int http_init(const char *addr, struct conf_server *ser); void http_close_server(void); void http_addheader(struct http_response *r, const char *name, const char *value); diff --git a/src/http_command.c b/src/http_command.c index 762a0fd..da9771e 100644 --- a/src/http_command.c +++ b/src/http_command.c @@ -246,7 +246,8 @@ static void cmd_init(struct http_channel *c) char buf[1024]; const char *clear = http_argbyname(c->request, "clear"); const char *service_name = http_argbyname(c->request, "service"); - struct conf_service *service = locate_service(service_name); + struct conf_service *service = locate_service(c->server, + service_name); struct http_session *s = http_session_create(service); struct http_response *rs = c->response; diff --git a/src/logic.c b/src/logic.c index 0a0c59b..7241594 100644 --- a/src/logic.c +++ b/src/logic.c @@ -76,9 +76,6 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA // Note: Some things in this structure will eventually move to configuration struct parameters global_parameters = { - "", - "", - 0, 0, // dump_records 0, // debug_mode 30, // operations timeout @@ -876,50 +873,52 @@ void statistics(struct session *se, struct statistics *stat) stat->num_clients = count; } -int start_http_listener(void) +int start_http_listener(struct conf_config *conf, + const char *listener_override, + const char *proxy_override) { - char hp[128] = ""; - struct conf_server *ser = global_parameters.server; - - if (*global_parameters.listener_override) - strcpy(hp, global_parameters.listener_override); - else + struct conf_server *ser; + for (ser = conf->servers; ser; ser = ser->next) { - strcpy(hp, ser->host ? ser->host : ""); - if (ser->port) + char hp[128]; + *hp = '\0'; + if (listener_override) { - if (*hp) - strcat(hp, ":"); - sprintf(hp + strlen(hp), "%d", ser->port); + strcpy(hp, listener_override); + listener_override = 0; /* only first server is overriden */ } - } - return http_init(hp); -} - -void start_proxy(void) -{ - char hp[128] = ""; - struct conf_server *ser = global_parameters.server; + else + { + strcpy(hp, ser->host ? ser->host : ""); + if (ser->port) + { + if (*hp) + strcat(hp, ":"); + sprintf(hp + strlen(hp), "%d", ser->port); + } + } + if (http_init(hp, ser)) + return -1; - if (*global_parameters.proxy_override) - strcpy(hp, global_parameters.proxy_override); - else if (ser->proxy_host || ser->proxy_port) - { - strcpy(hp, ser->proxy_host ? ser->proxy_host : ""); - if (ser->proxy_port) + *hp = '\0'; + if (proxy_override) + strcpy(hp, proxy_override); + else if (ser->proxy_host || ser->proxy_port) { - if (*hp) - strcat(hp, ":"); - sprintf(hp + strlen(hp), "%d", ser->proxy_port); + strcpy(hp, ser->proxy_host ? ser->proxy_host : ""); + if (ser->proxy_port) + { + if (*hp) + strcat(hp, ":"); + sprintf(hp + strlen(hp), "%d", ser->proxy_port); + } } + if (*hp) + http_set_proxyaddr(hp, ser->myurl ? ser->myurl : ""); } - else - return; - - http_set_proxyaddr(hp, ser->myurl ? ser->myurl : ""); + return 0; } - // Master list of connections we're handling events to static IOCHAN channel_list = 0; void pazpar2_add_channel(IOCHAN chan) @@ -963,8 +962,8 @@ static struct record_metadata *record_metadata_init( return rec_md; } -const char *get_mergekey(xmlDoc *doc, struct client *cl, int record_no, - struct conf_service *service, NMEM nmem) +static const char *get_mergekey(xmlDoc *doc, struct client *cl, int record_no, + struct conf_service *service, NMEM nmem) { char *mergekey_norm = 0; xmlNode *root = xmlDocGetRootElement(doc); @@ -978,7 +977,7 @@ const char *get_mergekey(xmlDoc *doc, struct client *cl, int record_no, const char *norm_str; pp2_relevance_token_t prt = pp2_relevance_tokenize( - global_parameters.server->mergekey_pct, + service->mergekey_pct, (const char *) mergekey); while ((norm_str = pp2_relevance_token_next(prt))) @@ -1022,7 +1021,7 @@ const char *get_mergekey(xmlDoc *doc, struct client *cl, int record_no, const char *norm_str; pp2_relevance_token_t prt = pp2_relevance_tokenize( - global_parameters.server->mergekey_pct, + service->mergekey_pct, (const char *) value); while ((norm_str = pp2_relevance_token_next(prt))) @@ -1208,7 +1207,7 @@ struct record *ingest_record(struct client *cl, const char *rec, sizeof(union data_types)); prt = pp2_relevance_tokenize( - global_parameters.server->sort_pct, + service->sort_pct, rec_md->data.text.disp); pp2_relevance_token_next(prt); diff --git a/src/parameters.h b/src/parameters.h index 2b15221..3905c92 100644 --- a/src/parameters.h +++ b/src/parameters.h @@ -24,9 +24,6 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA /** \brief global parameters */ struct parameters { - char proxy_override[128]; - char listener_override[128]; - struct conf_server *server; int dump_records; int debug_mode; int timeout; /* operations timeout, in seconds */ diff --git a/src/pazpar2.c b/src/pazpar2.c index 7ab0d8d..f982a03 100644 --- a/src/pazpar2.c +++ b/src/pazpar2.c @@ -38,9 +38,8 @@ static char *path_override = 0; void child_handler(void *data) { - start_proxy(); - - config_read_settings(path_override); + struct conf_config *config = (struct conf_config *) data; + config_read_settings(config, path_override); global_parameters.odr_in = odr_createmem(ODR_DECODE); global_parameters.odr_out = odr_createmem(ODR_ENCODE); @@ -94,6 +93,9 @@ static int sc_main( const char *pidfile = 0; const char *uid = 0; int session_timeout = 60; + const char *listener_override = 0; + const char *proxy_override = 0; + struct conf_config *config; #ifndef WIN32 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) @@ -116,11 +118,12 @@ static int sc_main( daemon = 1; break; case 'f': - if (!read_config(arg)) + config = read_config(arg); + if (!config) exit(1); break; case 'h': - strcpy(global_parameters.listener_override, arg); + listener_override = arg; break; case 'l': yaz_log_init_file(arg); @@ -184,9 +187,7 @@ static int sc_main( yaz_log(YLOG_FATAL, "Load config with -f"); return 1; } - global_parameters.server = config->servers; - - ret = start_http_listener(); + ret = start_http_listener(config, listener_override, proxy_override); if (ret) return ret; /* error starting http listener */ @@ -195,7 +196,7 @@ static int sc_main( yaz_daemon("pazpar2", (global_parameters.debug_mode ? YAZ_DAEMON_DEBUG : 0) + (daemon ? YAZ_DAEMON_FORK : 0) + YAZ_DAEMON_KEEPALIVE, - child_handler, 0 /* child_data */, + child_handler, config /* child_data */, pidfile, uid); return 0; } diff --git a/src/pazpar2.h b/src/pazpar2.h index 5580046..7bb067f 100644 --- a/src/pazpar2.h +++ b/src/pazpar2.h @@ -182,7 +182,9 @@ int session_active_clients(struct session *s); void session_apply_setting(struct session *se, char *dbname, char *setting, char *value); const char *session_setting_oneval(struct session_database *db, int offset); -int start_http_listener(void); +int start_http_listener(struct conf_config *conf, + const char *listener_override, + const char *proxy_override); void start_proxy(void); void pazpar2_add_channel(IOCHAN c); diff --git a/src/pazpar2_config.c b/src/pazpar2_config.c index fb793dd..f58820c 100644 --- a/src/pazpar2_config.c +++ b/src/pazpar2_config.c @@ -34,13 +34,12 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA #include #include -#define CONFIG_NOEXTERNS #include "pazpar2_config.h" #include "settings.h" static char confdir[256] = "."; -struct conf_config *config = 0; +struct conf_config *config1 = 0; static char *parse_settings(NMEM nmem, xmlNode *node); @@ -554,7 +553,15 @@ static struct conf_server *parse_server(NMEM nmem, xmlNode *node) { struct conf_service *s = parse_service(n, service_id); if (s) + { + s->relevance_pct = server->relevance_pct ? + server->relevance_pct : pp2_charset_create(0); + s->sort_pct = server->sort_pct ? + server->sort_pct : pp2_charset_create(0); + s->mergekey_pct = server->mergekey_pct ? + server->mergekey_pct : pp2_charset_create(0); *sp = s; + } } } else @@ -563,12 +570,6 @@ static struct conf_server *parse_server(NMEM nmem, xmlNode *node) return 0; } } - if (!server->relevance_pct) - server->relevance_pct = pp2_charset_create(0); - if (!server->sort_pct) - server->sort_pct = pp2_charset_create(0); - if (!server->mergekey_pct) - server->mergekey_pct = pp2_charset_create(0); return server; } @@ -619,9 +620,10 @@ static struct conf_targetprofiles *parse_targetprofiles(NMEM nmem, return r; } -struct conf_service *locate_service(const char *service_id) +struct conf_service *locate_service(struct conf_server *server, + const char *service_id) { - struct conf_service *s = config->servers->service; + struct conf_service *s = server->service; for (; s; s = s->next) if (s->id && service_id && 0 == strcmp(s->id, service_id)) return s; @@ -673,10 +675,11 @@ static struct conf_config *parse_config(xmlNode *root) return r; } -int read_config(const char *fname) +struct conf_config *read_config(const char *fname) { xmlDoc *doc = xmlParseFile(fname); const char *p; + struct conf_config *config; xmlSubstituteEntitiesDefault(1); xmlLoadExtDtdDefaultValue = 1; @@ -702,13 +705,11 @@ int read_config(const char *fname) config = parse_config(xmlDocGetRootElement(doc)); xmlFreeDoc(doc); - if (config) - return 1; - else - return 0; + return config; } -void config_read_settings(const char *path_override) +void config_read_settings(struct conf_config *config, + const char *path_override) { struct conf_service *s = config->servers->service; for (;s ; s = s->next) diff --git a/src/pazpar2_config.h b/src/pazpar2_config.h index e3637f8..3118631 100644 --- a/src/pazpar2_config.h +++ b/src/pazpar2_config.h @@ -105,6 +105,13 @@ struct conf_service char *id; char *settings; NMEM nmem; + + /* duplicated from conf_server */ + pp2_charset_t relevance_pct; + pp2_charset_t sort_pct; + pp2_charset_t mergekey_pct; + + struct database *databases; }; @@ -134,10 +141,6 @@ int conf_service_metadata_field_id(struct conf_service *service, const char * na int conf_service_sortkey_field_id(struct conf_service *service, const char * name); -void config_read_settings(const char *path_override); - -struct conf_service *locate_service(const char *service_id); - struct conf_server { char *host; @@ -170,14 +173,15 @@ struct conf_config struct conf_targetprofiles *targetprofiles; }; -#ifndef CONFIG_NOEXTERNS +struct conf_config *read_config(const char *fname); +xsltStylesheet *conf_load_stylesheet(const char *fname); -extern struct conf_config *config; +void config_read_settings(struct conf_config *config, + const char *path_override); -#endif +struct conf_service *locate_service(struct conf_server *server, + const char *service_id); -int read_config(const char *fname); -xsltStylesheet *conf_load_stylesheet(const char *fname); #endif diff --git a/src/record.c b/src/record.c index 08b73f5..080822f 100644 --- a/src/record.c +++ b/src/record.c @@ -26,12 +26,9 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA #include #endif -//#define CONFIG_NOEXTERNS #include "pazpar2_config.h" #include "record.h" - - union data_types * data_types_assign(NMEM nmem, union data_types ** data1, union data_types data2) -- 1.7.10.4