1 /* This file is part of Pazpar2.
2 Copyright (C) 2006-2013 Index Data
4 Pazpar2 is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
9 Pazpar2 is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 #include <sys/types.h>
29 #include <yaz/matchstr.h>
35 #include <sys/types.h>
37 struct database_hosts {
42 #if YAZ_VERSIONL > 0x4023e
43 #define strcmp_null(x, y) yaz_strcmp_null(x, y)
45 #define strcmp_null(x, y) local_strcmp_null(x, y)
47 static int local_strcmp_null(const char *v1, const char *v2)
52 return strcmp(v1, v2);
62 // Create a new host structure for hostport
63 static struct host *create_host(const char *proxy,
65 iochan_man_t iochan_man)
69 host = xmalloc(sizeof(struct host));
73 host->proxy = xstrdup(proxy);
77 host->tproxy = xstrdup(tproxy);
79 host->connections = 0;
83 if (host_getaddrinfo(host, iochan_man))
91 pazpar2_mutex_create(&host->mutex, "host");
93 yaz_cond_create(&host->cond_ready);
98 struct host *find_host(database_hosts_t hosts, const char *url,
99 const char *proxy, int port,
100 iochan_man_t iochan_man)
105 if (!proxy || !*proxy)
109 tproxy = xmalloc (strlen(url) + 10); /* so we can add :port */
111 for (cp = tproxy; *cp; cp++)
112 if (strchr("/?#~", *cp))
117 if (!strchr(tproxy, ':'))
118 sprintf(cp, ":%d", port); /* no port given, add it */
120 yaz_mutex_enter(hosts->mutex);
121 for (p = hosts->hosts; p; p = p->next)
123 if (!strcmp_null(p->tproxy, tproxy) &&
124 !strcmp_null(p->proxy, proxy))
131 p = create_host(proxy, tproxy, iochan_man);
134 p->next = hosts->hosts;
138 yaz_mutex_leave(hosts->mutex);
143 database_hosts_t database_hosts_create(void)
145 database_hosts_t p = xmalloc(sizeof(*p));
148 pazpar2_mutex_create(&p->mutex, "database");
152 void database_hosts_destroy(database_hosts_t *pp)
156 struct host *p = (*pp)->hosts;
159 struct host *p_next = p->next;
160 yaz_mutex_destroy(&p->mutex);
161 yaz_cond_destroy(&p->cond_ready);
166 yaz_mutex_destroy(&(*pp)->mutex);
174 * c-file-style: "Stroustrup"
175 * indent-tabs-mode: nil
177 * vim: shiftwidth=4 tabstop=8 expandtab